# 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.
b_namebN/A['str']The variable name of the output vector.
b_unitsN/AN/A['str']The units of the output vector.
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.
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()

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, :])

[1.92252253 2.73397729 1.95995091]

print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])

[2.89358547 4.55465395 4.0100558 ]


## 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')

b_name='z', b_units='m',
x_name='w', x_units='m')

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, :])

[3.69620519 3.55776518 4.31708061]

print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])

[4.55177985 1.47105996 4.61037553]

print(p.get_val('mat_vec_product_comp.z', units='ft')[0, :])

[2.45630447 2.59583279 1.44598805]

print(p.get_val('mat_vec_product_comp.z', units='ft')[1, :])

[4.87781233 1.9278168  4.53887351]