0

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?

  • Does this help? See https://stackoverflow.com/questions/10524989/calculating-inverse-of-a-very-large-matrix. – Eric Jin Apr 28 '22 at 23:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 28 '22 at 23:20
  • 1
    Many different things can be meant by "symbolic". The answer depends on the type of expressions in the elements of the matrix, the sparsity of the matrix etc. There are other methods to compute the inverse of a matrix using sympy or also by using other libraries but you have not given enough information to be able to answer this. It's also possible that the inverse of your matrix is just inherently very complicated and that no library can give you a sensible representation of it (usually there are better approaches for solving your actual problem than computing an inverse matrix explicitly). – Oscar Benjamin Apr 28 '22 at 23:34
  • 1
    `sympy` will try to calculate a symbolic, algebraic answer. Experiment with small matrices, like (2,2) or (3,3) and up to see what I mean. I'm not surprised that a (20,20) takes forever. As for result, was there an error? `numpy/scipy` can calculate a numeric inverse. – hpaulj Apr 28 '22 at 23:48

1 Answers1

2

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]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353