3

every matrix can be written in upper or lower triangular form simply just by rotating the basis. Is there a simple routine in python (numpy) to do it? I was unable to find it and I cant believe that there is no such thing. To ilustrate it:

matrix = numpy.array([[a,b,c],
                      [d,e,f],
                      [g,h,i]])

to

matrix2 = numpy.array([[z,0,0],
                       [y,x,0],
                       [v,u,t]])

letters are floats. So how to make this change, but not simply just by zeroing numbers b, c and f, but by correct rotation of basis in the most simple way.

Thank you!

Addman
  • 341
  • 1
  • 5
  • 13

1 Answers1

6

You are looking for Schur decomposition. Schur decomposition decomposes a matrix A as A = Q U Q^H, where U is an upper triangular matrix, Q is a unitary matrix (which effects the basis rotation) and Q^H is the Hermitian adjoint of Q.

import numpy as np
from scipy.linalg import schur

a = np.array([[ 1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
u, q = schur(a) # q is the unitary matrix, u is upper triangular

repr(u)
# array([[  1.61168440e+01,   4.89897949e+00,   1.58820582e-15],
#        [  0.00000000e+00,  -1.11684397e+00,  -1.11643184e-15],
#        [  0.00000000e+00,   0.00000000e+00,  -1.30367773e-15]])
Pascal Bugnion
  • 4,878
  • 1
  • 24
  • 29
  • It worked, but in my case, decomposition was not very fine. One number that should be zero was small non zero. But for my case it was sufficient precision. – Addman Feb 03 '15 at 11:04
  • Could this be a [floating point issue](http://stackoverflow.com/questions/6569528/python-float-to-int-conversion/6569657#6569657)? Matrix rotation normally involves a loss of precision. See the answer to this [question](http://stackoverflow.com/questions/14419290/set-very-low-values-to-zero-in-numpy) for a possible solution. – Pascal Bugnion Feb 03 '15 at 11:08
  • Yes, I think thats the case. There was needed very small rotation, so elements of unitary matrix are small or close to one. But how I said it is suffiecient now. thanks. – Addman Feb 03 '15 at 11:11