pythonnumpylinear-algebra

Right matrix division in NumPy, any better way than np.linalg.inv()?


What is the fastest way of doing right matrix division, i.e. xA = B in pyhton numpy? A and B are (NxN) square matrices and A is invertible, I thus want to compute equation x = BA^{-1}.

I am asking because for the left matrix division , Ax = B, x = A^{-1}B, using x = np.linalg.solve(A,B) is preferred to x = np.linalg.inv(A) @ B because of speed reasons.

But I have not found a way how to do this for the right matrix division. Right now I use x = B @ np.linalg.inv(A) Is there any better (= faster) way of doing this?


Solution

  • Simply use numpy.linalg.solve to solve the transposed equation A.T x.T = B.T:

    x = np.linalg.solve(A.T, B.T).T