Question

I tried to orthogonalize a 2d-numpy array and failed. I used this approach and translated it to this code:

def sym(w):
    return w.dot((w.T.dot(w))**(-.5))

but

In [1]: a
Out[2]: 
array([[ 1.1,  0.1],
       [ 0.1,  1.1]])
In [3]: a = sym(a)

In [4]: a
Out[5]: 
array([[ 1.20909392,  2.43574363],
       [ 2.43574363,  1.20909392]])

In [6]: a.dot(a.T)
Out[7]: 
array([[ 7.39475513,  5.89008563],
       [ 5.89008563,  7.39475513]])

a.dot(a.T) should output the identity.

Was it helpful?

Solution

The working definition of sym is:

from scipy.linalg import sqrtm, inv

def sym(w):
    return w.dot(inv(sqrtm(w.T.dot(w))))

This is because raising a numpy matrix to the power of -.5 is not equivalent to taking the matrix square root and then inverting it, which is what the formula requires.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top