I want to get the inverse of symbolic matrix of size (20x20). I used Sympy .inv() method, but it takes forever and no result is found.
So, is there another method/algorithm to find this inverse?
I want to get the inverse of symbolic matrix of size (20x20). I used Sympy .inv() method, but it takes forever and no result is found.
So, is there another method/algorithm to find this inverse?
sympy
inv for a (3,3) matrix. You can extrapolate to a (20,20) at your risk:
In [61]: M = Matrix([[x,2*y,3*z],[-1*x,z,y],[1,2*z,0]])
In [62]: M.inv()
Out[62]:
⎡ 2
⎢ 2⋅y⋅z -6⋅z
⎢────────────────────────────── ────────────────────────────── ────
⎢ 2 2 2 2 2 2
⎢2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅
⎢
⎢ -y 3⋅z
⎢────────────────────────────── ────────────────────────────── ────
⎢ 2 2 2 2 2 2
⎢2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅
⎢
⎢ 2⋅x⋅z + z 2⋅x⋅z - 2⋅y
⎢────────────────────────────── ────────────────────────────── ────
⎢ 2 2 2 2 2 2
⎣2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z 2⋅x⋅
2 2 ⎤
- 2⋅y + 3⋅z ⎥
──────────────────────────⎥
2 2 2⎥
y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z ⎥
⎥
x⋅y + 3⋅x⋅z ⎥
──────────────────────────⎥
2 2 2⎥
y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z ⎥
⎥
-2⋅x⋅y - x⋅z ⎥
──────────────────────────⎥
2 2 2⎥
y⋅z + 6⋅x⋅z - 2⋅y + 3⋅z ⎦
A purely numeric one is simpler
In [63]: M = Matrix([[1,2,3],[-1,1,1],[1,2,0]])
In [64]: M.inv()
Out[64]:
⎡2/9 -2/3 1/9 ⎤
⎢ ⎥
⎢-1/9 1/3 4/9 ⎥
⎢ ⎥
⎣1/3 0 -1/3⎦
and using numpy
In [72]: arr = np.array(M).astype(int)
In [73]: arr
Out[73]:
array([[ 1, 2, 3],
[-1, 1, 1],
[ 1, 2, 0]])
In [74]: np.linalg.inv(arr)
Out[74]:
array([[ 0.22222222, -0.66666667, 0.11111111],
[-0.11111111, 0.33333333, 0.44444444],
[ 0.33333333, -0. , -0.33333333]])