I want solve linear equation Ax= b, each A contains in 3d matrix. For-example,
In Ax = B, Suppose A.shape is (2,3,3)
i.e. = [[[1,2,3],[1,2,3],[1,2,3]] [[1,2,3],[1,2,3],[1,2,3]]]
and B.shape is (3,1) i.e. [1,2,3]^T
And I want to know each 3-vector x of Ax = B i.e.(x_1, x_2, x_3).
What comes to mind is multiply B with np.ones(2,3) and use function dot with the inverse of each A element. But It needs loop to do this.(which consumes lots of time when matrix size going up high) (Ex. A[:][:] = [1,2,3]) How can I solve many Ax = B equation without loop?
- I made elements of A and B are same, but as you probably know, it is just example.
For invertible matrices, we could use
np.linalg.inv
on the3D
arrayA
and then use tensor matrix-multiplication withB
so that we lose the last and first axes of those two arrays respectively, like so -Sample run -
Alternatively, the tensor matrix-multiplication could be replaced by
np.matmul
, like so -On Python 3.x, we could use
@
operator for the same functionality -