python - How simplifying fractions in matrices with sympy? -


i'm using sympy find matrix's inverse. i've next problem. when compute inverse of matrix a , want prove it, got matrix fractions; mean

>> import sympy >> sympy import pprint >> sympy.abc import * >> import sys >> sys.displayhook = pprint >> sympy.matrices import * >> = matrix([[a, b],[c, d]]) >> b = a.inv() >> b >> [1       b*c           -b     ] >> [- + ------------  -----------] >> [a    2 /    b*c\    /    b*c\] >> [    *|d - ---|  a*|d - ---|] >> [       \     /    \     /] >> [                             ] >> [      -c               1     ] >> [  -----------       -------  ] >> [    /    b*c\           b*c  ] >> [  a*|d - ---|       d - ---  ] >> [    \     /              ] >> b*a >> [  /1       b*c     \       b*c        /1       b*c     \       b*d    ] >> [a*|- + ------------| - -----------  b*|- + ------------| - -----------] >> [  |a    2 /    b*c\|     /    b*c\    |a    2 /    b*c\|     /    b*c\] >> [  |    *|d - ---||   a*|d - ---|    |    *|d - ---||   a*|d - ---|] >> [  \       \     //     \     /    \       \     //     \     /] >> [                                                                      ] >> [                                             d          b*c           ] >> [                0                         ------- - -----------       ] >> [                                              b*c     /    b*c\       ] >> [                                          d - ---   a*|d - ---|       ] >> [                                                    \     /       ] 

and wanna next matrix

>> = matrix([ >> [1, 0], >> [0, 1]]) 

my problem matrix a*b or b*a. want simplify matrix a*b i. tried simplify() doesn't work.

forget python , sympy minute. focus on finding inverse of matrix paper , pen.

for a = [[a, b], [c,d]] matrix, calculate inverse a^-1 as,

(1/d)*[[d, -b],[-c, a]]. here d determinant of a matrix (1/ad-bc)

this (a^-1) equal [[d/d, -b/d][-c/d, a/d]]

let's take first element first row , follow operations i've made. me make no sense, way how sympy :) apply procedure other matrix elements.

=> d/d  d/(a*d-b*c)  a*d/(d*a^2 - a*b*c) (a*d-b*c+b*c)/a^2*(d-b*c/a)  (a*d - a*b*c/a + b*c)/a^2*(d-b*c/a) (a*(d-b*c/a) + b*c)/a^2*(d-b*c/a) a*(d-b*c/a)/a^2*(d-b*c/a) + b*c/a^2*(d-b*c/a) 1/a + b*c/a^2*(d-b*c/a) [this how sympy outputs]   >>> = matrix([[a,b],[c,d]]) >>> b = a**-1 #same b = a.inv() >>> b[0] 1/a + b*c/(a**2*(d - b*c/a)) 

now, let's have sympy a*b output.

>>> n = a*b >>> n matrix([ [a*(1/a + b*c/(a**2*(d - b*c/a))) - b*c/(a*(d - b*c/a)),                                   0], [c*(1/a + b*c/(a**2*(d - b*c/a))) - c*d/(a*(d - b*c/a)), d/(d - b*c/a) - b*c/(a*(d - b*c/a))]]) >>> pprint(n) ⎡  ⎛1       b⋅c     ⎞       b⋅c                           ⎤ ⎢a⋅⎜─ + ────────────⎟ - ───────────            0          ⎥ ⎢  ⎜a    2 ⎛    b⋅c⎞⎟     ⎛    b⋅c⎞                       ⎥ ⎢  ⎜    ⋅⎜d - ───⎟⎟   a⋅⎜d - ───⎟                       ⎥ ⎢  ⎝       ⎝     ⎠⎠     ⎝     ⎠                       ⎥ ⎢                                                         ⎥ ⎢  ⎛1       b⋅c     ⎞       c⋅d         d          b⋅c    ⎥ ⎢c⋅⎜─ + ────────────⎟ - ───────────  ─────── - ───────────⎥ ⎢  ⎜a    2 ⎛    b⋅c⎞⎟     ⎛    b⋅c⎞      b⋅c     ⎛    b⋅c⎞⎥ ⎢  ⎜    ⋅⎜d - ───⎟⎟   a⋅⎜d - ───⎟  d - ───   a⋅⎜d - ───⎟⎥ ⎣  ⎝       ⎝     ⎠⎠     ⎝     ⎠            ⎝     ⎠⎦ 

it doesn't evaluate direct eye(2) if take elements, , simplify them, you'll see messy matrix , 2x2 identity matrix.

a pythonic way check (knowing given):

>>> n[0] a*(1/a + b*c/(a**2*(d - b*c/a))) - b*c/(a*(d - b*c/a)) >>> n[1] 0 >>> n[3] d/(d - b*c/a) - b*c/(a*(d - b*c/a)) >>> n[2] c*(1/a + b*c/(a**2*(d - b*c/a))) - c*d/(a*(d - b*c/a))  >>> def will_evaluate_one(a,b,c,d): ...    return a*(1/a + b*c/(a**2*(d - b*c/a))) - b*c/(a*(d - b*c/a)) #n[0]      ... >>> will_evaluate_one(1,2,3,9) 1 >>> will_evaluate_one(1,2,3,19) 1 >>> will_evaluate_one(1,2,23,19) 1 >>> will_evaluate_one(1,12,23,19) 1  >>> def will_also_evaluate_one(a,b,c,d): ...     return d/(d - b*c/a) - b*c/(a*(d - b*c/a)) #n[1]  ...  >>> will_also_evaluate_one(2,4,5,6) 1 >>> will_also_evaluate_one(2,4,15,6) 1 >>> will_also_evaluate_one(2,14,15,6) 1 >>> will_also_evaluate_one(12,14,15,6) 1 

note: i've realised that, sympy uses anlaytic inversion formula. see here: https://en.wikipedia.org/wiki/helmert%e2%80%93wolf_blocking


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -