Reputation: 111
I have the following code
import numpy as np
import sympy as sp
def bra(i,d):
arr = np.zeros((1,d))
if i <= (d-1):
arr[:,i] = 1
else:
print("Index Out of bounds")
return arr
def density(i,j,d):
return bra(i,d).T*bra(j,d)
SIGMA = (1/3)*(np.kron(np.kron(bra(0,3),bra(1,3)).T,np.kron(bra(0,3),bra(1,3)))+np.kron(np.kron(bra(1,3),bra(2,3)).T,np.kron(bra(1,3),bra(2,3)))+np.kron(np.kron(bra(2,3),bra(0,3)).T,np.kron(bra(2,3),bra(0,3))))
DELTA = (1/3)*(np.kron(np.kron(bra(1,3),bra(0,3)).T,np.kron(bra(1,3),bra(0,3)))+np.kron(np.kron(bra(2,3),bra(1,3)).T,np.kron(bra(2,3),bra(1,3)))+np.kron(np.kron(bra(0,3),bra(2,3)).T,np.kron(bra(0,3),bra(2,3))))
Essentially bra(i,d) gives a (1,d) array with (1,i)th element as one and rest as zero. For example,
bra(0,3)=[[1,0,0]]
bra(1,3)=[[0,1,0]]
bra(0,3)=[[0,0,1]]
density(i,j,d) returns (d,d) matrix with (i,j)th element as one rest as zero, for example,
density(0,1,3) = array([[0., 1., 0.], [0., 0., 0.],[0., 0., 0.]]).
Using the above codes, I have produced a (9,9) Matrix object by using np.kron
a = sp.symbols('a', positive = True)
HORO = (2/21)*(np.kron(density(0,0,3),density(0,0,3))+np.kron(density(1,0,3),density(0,1,3))+np.kron(density(0,0,3),density(2,2,3))
+np.kron(density(0,1,3),density(1,0,3))+np.kron(density(1,1,3),density(1,1,3))+np.kron(density(2,1,3),density(1,2,3))
+np.kron(density(0,2,3),density(2,0,3))+np.kron(density(1,2,3),density(2,1,3))+np.kron(density(2,2,3),density(2,2,3)))+(a/7)*SIGMA+((5-a)/7)*DELTA
M = np.asmatrix(HORO)
It can be shown that M matrix is invertible and it is the following (9,9) matrix
matrix([[0.0952380952380952, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0.0476190476190476*a, 0, 0.0952380952380952, 0, 0, 0, 0, 0],
[0, 0, 0.333333333333333 - 0.0476190476190476*a, 0, 0, 0,
0.0952380952380952, 0, 0],
[0, 0.0952380952380952, 0,
0.238095238095238 - 0.0476190476190476*a, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0.0952380952380952, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0476190476190476*a, 0, 0.0952380952380952, 0],
[0, 0, 0, 0, 0, 0, 0.0476190476190476*a, 0, 0],
[0, 0, 0, 0, 0, 0.0952380952380952, 0,
0.238095238095238 - 0.0476190476190476*a, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0.0952380952380952]], dtype=object)
For this symbolic matrix I want to calculate the eigenvalues as
M.eigenvals()
However, it is returning the following error
AttributeError: 'matrix' object has no attribute 'eigenvals'
I can't figure out what went wrong.
Upvotes: 1
Views: 498
Reputation: 80439
Usually, numpy and sympy don't mix well. Numpy can't work with sympy functions and symbols. Also, sympy tries to calculate exact symbolic solutions, which doesn't work well together with floats (sympy strongly prefers integers, rationals and symbolic expressions such as sp.sqrt()
).
In this case, provided you don't change too much, numpy managed to calculate the matrix elements well (sums and productions are executed via the underlying standard Python).
Now, if you just use that matrix in sympy, you get:
M = sp.Matrix(HORO)
print(M.eigenvals())
You get:
{0.0952380952380952: 3,
0.119047619047619 - 0.152455338986496 * sqrt(0.0975609756097561 * a ** 2 - 0.48780487804878 * a + 1.0): 2,
0.152455338986496 * sqrt(0.0975609756097561 * a ** 2 - 0.48780487804878 * a + 1.0) + 0.119047619047619: 2,
0.0476190476190476 * a: 1,
0.333333333333333 - 0.0476190476190476 * a: 1}
A better approach uses nsimplify
to convert floats to rationals:
M = sp.Matrix(sp.nsimplify(HORO))
print(M.eigenvals())
This gives a nicer symbolic solution:
{2 / 21: 3,
5 / 42 - sqrt(4 * a ** 2 - 20 * a + 41) / 42: 2,
sqrt(4 * a ** 2 - 20 * a + 41) / 42 + 5 / 42: 2,
a / 21: 1,
1 / 3 - a / 21: 1}
Upvotes: 2