1

A linear equation XA = B, we know the 'X = B * inv(A)'. where A, B, X are all matrix.

in the matlab it can be solve:

X = B / A

it avoid doing inverse a matrix which is fast. is there any equal form in python using numpy?

jason
  • 1,998
  • 3
  • 22
  • 42

2 Answers2

2

Use numpy linalg.inv function:

import numpy as np
x = np.matmul(b, np.linalg.inv(a))
Osman Mamun
  • 2,864
  • 1
  • 16
  • 22
  • cool. it has same result as matlab. – jason Oct 16 '18 at 00:15
  • 2
    @jason it may give the same answer, but it will take a lot longer due to inverting `a` if `a` is large. Look into using `np.linalg.solve(a, b)` (or `np.linalg.lstsq(a, b)` if `a` is not linearly independent) instead. These do a similar short circuit to `MATLAB` without doing the full inverse. – Daniel F Oct 16 '18 at 06:29
0
import numpy as np
a = np.array([[1,7],[2,9]])
b = np.array([[2,6],[7,5]])

# XA = B
# X = BA^-1

x = np.matmul(b,np.linalg.inv(a))
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Oct 16 '18 at 03:35