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.

OptionDefaultAcceptable ValuesAcceptable TypesDescription
A_nameAN/A['str']The variable name for the matrix.
A_shape(3, 3)N/A['tuple']The shape of the input matrix at a single point.
A_unitsN/AN/A['str']The units of the input matrix.
always_optFalse[True, False]['bool']If True, force nonlinear operations on this component to be included in the optimization loop even if this component is not relevant to the design variables and responses.
b_namebN/A['str']The variable name of the output vector.
b_unitsN/AN/A['str']The units of the output vector.
derivs_methodN/A['jax', 'cs', 'fd', None]N/AThe method to use for computing derivatives
distributedFalse[True, False]['bool']If True, set all variables in this component as distributed across multiple processes
run_root_onlyFalse[True, False]['bool']If True, call compute, compute_partials, linearize, apply_linear, apply_nonlinear, and compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.
use_jitTrue[True, False]['bool']If True, attempt to use jit on compute_primal, assuming jax or some other AD package is active.
vec_size1N/A['int']The number of points at which the matrix vector product is to be computed
x_namexN/A['str']The name of the input vector.
x_unitsN/AN/A['str']The units of the input vector.

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, :])
[3.35418885 3.33361082 3.84600624]
print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])
[4.69401617 3.36746107 1.73171621]

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, :])
[2.08169472 2.44276235 2.24401948]
print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])
[0.66521051 2.16648666 0.91371113]
print(p.get_val('mat_vec_product_comp.z', units='ft')[0, :])
[1.17149294 1.29015118 0.75364731]
print(p.get_val('mat_vec_product_comp.z', units='ft')[1, :])
[2.4662562  2.47172471 1.34400422]