Ask Your Question

Revision history [back]

Diagonalize matrix numerically over $\mathbb{C}$

Suppose I have a matrix m:

$$ m = \left(\begin{array}{rr} 2 & -3 \\ 1 & 0 \end{array}\right). $$ It is diagonalizable and has complex eigenvalues. I now want to diagonalize it, but get an error:

In [20]: m = matrix([[2, -3], [1, 0]]); m.diagonalization() ... ValueError: matrix entries must be from a field

When I specify the field m = matrix(CDF, [[2, -3], [1, 0]]), I get ValueError: base field must be exact, but Complex Double Field is not. Specifying ComplexLazyField() instead of CDF raises NotImplementedError.

So, apparently, Sage is trying to diagonalize the matrix symbolically, i.e. exactly. But what if I don't care about exactness and just want a straightforward numerical answer?

This is how I would do it with sympy:

In [22]: import sympy as sp ...: m = sp.Matrix([[2, -3], [1, 0]]) ...: m.diagonalize() Out[22]: (Matrix([ [1 - sqrt(2)*I, 1 + sqrt(2)*I], [ 1, 1]]), Matrix([ [1 - sqrt(2)*I, 0], [ 0, 1 + sqrt(2)*I]])) Notice that the output is actually exact. I know I can run this same Python code in Sage, but I assume there's a more native way to do it.

To sum up, how do I get Sage to diagonalize a matrix over $\mathbb{C}$? How do I change the code if I only need the numerical answer?

Diagonalize matrix numerically over $\mathbb{C}$

Suppose I have a matrix m:

$$ m = \left(\begin{array}{rr} 2 & -3 \\ 1 & 0 \end{array}\right). $$ It is diagonalizable and has complex eigenvalues. I now want to diagonalize it, but get an error:

In [20]: m = matrix([[2, -3], [1, 0]]); m.diagonalization()  
...   
ValueError: matrix entries must be from a field

When I specify the field m = matrix(CDF, [[2, -3], [1, 0]]), I get ValueError: base field must be exact, but Complex Double Field is not. Specifying ComplexLazyField() instead of CDF raises NotImplementedError.

So, apparently, Sage is trying to diagonalize the matrix symbolically, i.e. exactly. But what if I don't care about exactness and just want a straightforward numerical answer?

This is how I would do it with sympy:

In [22]: import sympy as sp  
          ...: m = sp.Matrix([[2, -3], [1, 0]])  
          ...: m.diagonalize()  
Out[22]:  
(Matrix([    
 [1 - sqrt(2)*I, 1 + sqrt(2)*I],    
 [            1,             1]]),   
 Matrix([     
 [1 - sqrt(2)*I,             0],    
 [            0, 1 + sqrt(2)*I]]))     
 sqrt(2)*I]]))

Notice that the output is actually exact. I know I can run this same Python code in Sage, but I assume there's a more native way to do it.

To sum up, how do I get Sage to diagonalize a matrix over $\mathbb{C}$? How do I change the code if I only need the numerical answer?