Cytnx
Cytnx copied to clipboard
.at() and slice for diagnoal UniTensor
When we do eigen decompostion, the eigenvalues are returned as a diagonal UniTensor. Now if I wan to use the 0-th eigenvalue as a number. One need to use eigvals.at([0]).value or eigvals.get_elem([0]) or eigvals[0].get_elem([0])). Note that eigvals[0] gives you a UniTensor with single element, but you still need to extract the element.
# create a randomly initialized Hermitian matrix
T = cytnx.random.uniform([4,4], low=-1., high=1.)
uT = cytnx.UniTensor(T, labels=["a","b"], name="uT", rowrank=1)
eigvals, V = cytnx.linalg.Eigh(uT)
eigvals.relabels_(["a","b"])
eigvals.print_diagram()
print(eigvals)
print(eigvals.at([0]).value)
print(eigvals[0])
print(eigvals.get_elem([0]))
print(eigvals[0].get_elem([0]))
However, the following ways are not allowed.
eigvals.at([0,0]).value
eigvals.at(["a","b"],[0,0]).value
eigvals[0,0]
I think we should allow this. For off-diagonal elements such as eigvals.at([0,1]).value, we can return 0.0, or we can raise error.