MatrixVectorProductComp

MatrixVectorProductComp performs a matrix-vector product. It may be vectorized to provide the result at one or more points simultaneously.

\[ \bar{b}_i = \left[ A_i \right] \bar{x}_i \]

MatrixVectorProductComp Options

The default vec_size is 1, providing the matrix vector product of \(a\) and \(x\) at a single point.

Other options for MatrixVectorProductComp allow the user to rename the input variables \(a\) and \(x\) and the output \(b\), as well as specifying their units.

Option Default Acceptable Values Acceptable Types Description Deprecation
A_name A N/A ['str'] The variable name for the matrix. N/A
A_shape (3, 3) N/A ['tuple'] The shape of the input matrix at a single point. N/A
A_units N/A N/A ['str'] The units of the input matrix. N/A
b_name b N/A ['str'] The variable name of the output vector. N/A
b_units N/A N/A ['str'] The units of the output vector. N/A
distributed False [True, False] ['bool'] True if ALL variables in this component are distributed across multiple processes. The 'distributed' option has been deprecated. Individual inputs and outputs should be set as distributed instead, using calls to add_input() or add_output().
run_root_onlyFalse [True, False] ['bool'] If True, call compute/compute_partials/linearize/apply_linear/apply_nonlinear/compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.N/A
vec_size 1 N/A ['int'] The number of points at which the matrix vector product is to be computed N/A
x_name x N/A ['str'] The name of the input vector. N/A
x_units N/A N/A ['str'] The units of the input vector. N/A

MatrixVectorProductComp Constructor

The call signature for the MatrixVectorProductComp constructor is:

MatrixVectorProductComp.__init__(**kwargs)[source]

Initialize the Matrix Vector Product component.

MatrixVectorProductComp Usage

There are often situations when numerous products need to be computed, essentially in parallel. You can reduce the number of components required by having one MatrixVectorProductComp perform multiple operations. This is also convenient when the different operations have common inputs.

The add_product method is used to create additional products after instantiation.

MatrixVectorProductComp.add_product(b_name, A_name='A', x_name='x', A_units=None, x_units=None, b_units=None, vec_size=1, A_shape=(3, 3))[source]

Add a new output product to the matrix vector product component.

Parameters
b_namestr

The name of the vector product output.

A_namestr

The name of the matrix input.

x_namestr

The name of the vector input.

A_unitsstr or None

The units of the input matrix.

x_unitsstr or None

The units of the input vector.

b_unitsstr or None

The units of the output matrix.

vec_sizeint

The number of points at which the matrix vector product should be computed simultaneously.

A_shapetuple of (int, int)

The shape of the matrix at each point. The first element also specifies the size of the output at each point. The second element specifies the size of the input vector at each point. For example, if vec_size=10 and shape is (5, 3), then the input matrix will have a shape of (10, 5, 3), the input vector will have a shape of (10, 3), and the output vector will have shape of (10, 5).

MatrixVectorProductComp Example

The following code demonstrates the use of the MatrixVectorProductComp, finding the product of a random 3x3 matrix Mat and a 3-vector x at 100 points simultaneously.

import numpy as np
import openmdao.api as om

nn = 2

p = om.Problem()

p.model.add_subsystem(name='mat_vec_product_comp',
                      subsys=om.MatrixVectorProductComp(A_name='Mat', vec_size=nn,
                                                        b_name='y', b_units='m',
                                                        x_units='m'),
                      promotes_inputs=['Mat', 'x'])

p.setup()

p.set_val('Mat', np.random.rand(nn, 3, 3))
p.set_val('x', np.random.rand(nn, 3))

p.run_model()

print(p.get_val('mat_vec_product_comp.y', units='ft')[0, :])
[2.35203823 3.19703732 2.93981367]
print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])
[2.62092213 1.61682219 1.04399052]

MatrixVectorProductComp Example with Multiple Products

When defining multiple products:

  • An input name in one call to add_product may not be an output name in another call, and vice-versa.

  • The units and shape of variables used across multiple products must be the same in each one.

import numpy as np

nn = 2

p = om.Problem()

mvp = om.MatrixVectorProductComp(A_name='Mat', vec_size=nn,
                                 b_name='y', b_units='m',
                                 x_units='m')

mvp.add_product(A_name='Mat', vec_size=nn,
                b_name='z', b_units='m',
                x_name='w', x_units='m')

p.model.add_subsystem(name='mat_vec_product_comp',
                      subsys=mvp,
                      promotes_inputs=['Mat', 'x', 'w'])

p.setup()

p.set_val('Mat', np.random.rand(nn, 3, 3))
p.set_val('x', np.random.rand(nn, 3))
p.set_val('w', np.random.rand(nn, 3))

p.run_model()

print(p.get_val('mat_vec_product_comp.y', units='ft')[0, :])
[1.08620818 3.7811815  0.67726221]
print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])
[0.67238087 0.46260593 0.72127568]
print(p.get_val('mat_vec_product_comp.z', units='ft')[0, :])
[1.94400669 4.60425448 2.02518598]
print(p.get_val('mat_vec_product_comp.z', units='ft')[1, :])
[0.86368533 2.9319253  1.97844269]