I have a linear system in which all the matrices are block diagonal. They have N blocks identical in shape.
Matrices are stored in compressed format as numpy arrays with shape (N, n, m), while the shape of the vectors is (N, m).
I currently implemented the matrix-vector product as
import numpy as np
def mvdot(m, v):
return (m * np.expand_dims(v, -2)).sum(-1)
Thanks to broadcasting rules, if all the blocks of a matrix are the same I have to store it only once in an array with shape (1, n, m): the product with a vector (N, m) still gives the correct (N, n) vector.
My questions are:
- how to implement an efficient matrix-matrix product that yields the matrix with shape
(N, n, m)from two matrices with shapes(N, n, p)and(N, p, m)? - is there a way to perform these operations with a
numpybuilt-in (possibly faster) function? Functions likenp.linalg.invmake me think thatnumpywas designed to support this compressed format for block diagonal matrices.
dotmethod? - dPol