Matrix-Vector Multiplication¶
This function allows you to compute matrix-vector multiplication.
An example of how to use the operation is provided below.
- omtools.std.matvec.matvec(mat1, vec1)¶
This function can compute a matrix-vector multiplication, similar to the numpy counterpart.
- Parameters
- mat1: Variable
The matrix needed for the matrix-vector multiplication
- vec1: Variable
The vector needed for the matrix-vector multiplication
from openmdao.api import Problem
from omtools.api import Group
import omtools.api as ot
import numpy as np
class ExampleMatVecProduct(Group):
def setup(self):
m = 3
n = 4
# Shape of the first matrix (3,2)
shape1 = (m, n)
# Shape of the second matrix (2,4)
shape2 = (n, )
# Creating the values of both matrices
val1 = np.arange(m * n).reshape(shape1)
val2 = np.arange(n).reshape(shape2)
# Declaring the input matrix and input vector
mat1 = self.declare_input('mat1', val=val1)
vec1 = self.declare_input('vec1', val=val2)
# Creating the output for matrix-vector multiplication
self.register_output('MatVec', ot.matvec(mat1, vec1))
prob = Problem()
prob.model = ExampleMatVecProduct()
prob.setup(force_alloc_complex=True)
prob.run_model()
print('mat1', prob['mat1'].shape)
print(prob['mat1'])
print('vec1', prob['vec1'].shape)
print(prob['vec1'])
print('MatVec', prob['MatVec'].shape)
print(prob['MatVec'])
mat1 (3, 4)
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
vec1 (4,)
[0. 1. 2. 3.]
MatVec (3,)
[14. 38. 62.]