Inverse of a matrix in SymPy?

17,137

Solution 1

If your question was: How to compute the inverse of a matrix M in sympy then:

M_inverse = M.inv()

As for how to create a matrix:

M = Matrix(2,3, [1,2,3,4,5,6])

will give you the following 2X3 matrix:

1 2 3

4 5 6

See: http://docs.sympy.org/0.7.2/modules/matrices/matrices.html

Solution 2

Here is example of how we can compute inverse for a symbolic matrix (taking the one from the question):

import sympy as sym


# Not necessary but gives nice-looking latex output
# More info at: http://docs.sympy.org/latest/tutorial/printing.html
sym.init_printing()

sx, sy, rho = sym.symbols('sigma_x sigma_y rho')
matrix = sym.Matrix([[sx ** 2, rho * sx * sy], 
                     [rho * sx * sy, sy ** 2]])

Now printing the inverse matrix.inv() will give:
                                      enter image description here

which can be further simplified like sym.simplify(matrix.inv()):
                                                enter image description here

Share:
17,137
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on June 05, 2022

Comments

  • Tim
    Tim almost 2 years

    I was wondering how to create a matrix and compute its inverse using SymPy in Python?

    For example, for this symbolic matrix:

  • Karlo
    Karlo almost 7 years
    What is the difference between M.inv() and Inverse(M)?
  • yokke
    yokke almost 6 years
    @Karlo I'm not sure, if related, but have a look: stackoverflow.com/questions/50854803/…