Diagonalize the symbolic matrix

I need to diagonalize a symbolic matrix with python. In Mathematica, this can be done easily, but problems arise when using the numpy.linalg module.

For concreteness, we consider the matrix

 [[2, x], [x, 3]] 

where x is a symbolic variable. I suppose I'm having problems because the numpy package is provided for numerical computation, not symbolic, but I cannot find how to do this with sympy.

+4
source share
2 answers

You can calculate it from eigenvalues, but actually there is a method that will do this for you, diagonalize

 In [13]: M.diagonalize() Out[13]: βŽ› ⎑ __________ ⎀⎞ ⎜ ⎒ β•± 2 βŽ₯⎟ ⎜⎑ -2β‹…x 2β‹…x ⎀ ⎒ β•²β•± 4β‹…x + 1 5 βŽ₯⎟ βŽœβŽ’β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ ─────────────────βŽ₯, ⎒- ───────────── + ─ 0 βŽ₯⎟ ⎜⎒ __________ __________ βŽ₯ ⎒ 2 2 βŽ₯⎟ ⎜⎒ β•± 2 β•± 2 βŽ₯ ⎒ βŽ₯⎟ βŽœβŽ’β•²β•± 4β‹…x + 1 - 1 β•²β•± 4β‹…x + 1 + 1βŽ₯ ⎒ __________ βŽ₯⎟ ⎜⎒ βŽ₯ ⎒ β•± 2 βŽ₯⎟ ⎜⎣ 1 1 ⎦ ⎒ β•²β•± 4β‹…x + 1 5βŽ₯⎟ ⎜ ⎒ 0 ───────────── + ─βŽ₯⎟ ⎝ ⎣ 2 2⎦⎠ 

M.diagonalize() returns a pair of matrices (P, D) such that M = P*D*P**-1 . If he cannot calculate sufficient eigenvalues, either because the matrix is ​​not diagonalizable, or because solve() cannot find all the roots of the characteristic polynomial, it will raise a MatrixError .

See also this section of the SymPy tutorial.

+6
source

Assuming that the matrix is ​​diagonalizable, you can get the eigenvectors and eigenvalues ​​through

 from sympy import * x = Symbol('x') M = Matrix([[2,x],[x,3]]) print M.eigenvects() print M.eigenvals() 

Donation:

 [(-sqrt(4*x**2 + 1)/2 + 5/2, 1, [[-x/(sqrt(4*x**2 + 1)/2 - 1/2)] [ 1]]), (sqrt(4*x**2 + 1)/2 + 5/2, 1, [[-x/(-sqrt(4*x**2 + 1)/2 - 1/2)] [ 1]])] {sqrt(4*x**2 + 1)/2 + 5/2: 1, -sqrt(4*x**2 + 1)/2 + 5/2: 1} 

You should check the documentation , there are many other expansions.

Note that not every matrix is ​​diagonalizable, but you can put each matrix in the Jordan Normal Form using the sympy .jordan_form .

+2
source

Source: https://habr.com/ru/post/1501325/


All Articles