Matrix-Matrix Multiplication¶
This function allows you to compute matrix-matrix multiplication, as well as, matrix-vector multiplication.
An example of how to use the operation is provided below.
- omtools.std.matmat.matmat(mat1, mat2)¶
This function can compute a matrix-matrix multiplication, similar to the numpy counterpart.
- Parameters
- mat1: Variable
The first input for the matrix-matrix multiplication
- mat2: Variable
The second input for the matrix-matrix multiplication
from openmdao.api import Problem
from omtools.api import Group
import omtools.api as ot
import numpy as np
class ExampleMatMatProduct(Group):
def setup(self):
m = 3
n = 2
p = 4
# Shape of the first matrix (3,2)
shape1 = (m, n)
# Shape of the second matrix (2,4)
shape2 = (n, p)
# Creating the values of both matrices
val1 = np.arange(m * n).reshape(shape1)
val2 = np.arange(n * p).reshape(shape2)
# Declaring the two input matrices as mat1 and mat2
mat1 = self.declare_input('mat1', val=val1)
mat2 = self.declare_input('mat2', val=val2)
# Creating the output for matrix multiplication
self.register_output('MatMat', ot.matmat(mat1, mat2))
prob = Problem()
prob.model = ExampleMatMatProduct()
prob.setup(force_alloc_complex=True)
prob.run_model()
print('mat1', prob['mat1'].shape)
print(prob['mat1'])
print('mat2', prob['mat2'].shape)
print(prob['mat2'])
print('MatMat', prob['MatMat'].shape)
print(prob['MatMat'])
mat1 (3, 2)
[[0. 1.]
[2. 3.]
[4. 5.]]
mat2 (2, 4)
[[0. 1. 2. 3.]
[4. 5. 6. 7.]]
MatMat (3, 4)
[[ 4. 5. 6. 7.]
[12. 17. 22. 27.]
[20. 29. 38. 47.]]