I want to calculate complex eigenvectors (Psi functions for shrodinger equation) for 20k*20k matrices, but always gets smth like this:
[ 2.99009782e-09 -1.12381299e-08 -2.82346868e-08 ..., 6.20967928e-34
-4.80675528e-34 -3.84848719e-35]
[ 4.07337553e-08 -1.45976681e-07 -3.47961439e-07 ..., 7.43558322e-34
-5.74815572e-34 -4.61317607e-35]
[ 5.51921102e-07 -1.88491289e-06 -4.26000711e-06 ..., 9.29535407e-34
-7.15304083e-34 -5.78846547e-35]
As I understand it just replaces part of data with dots, it's strange. May be there is a problem with saving data in file? My code is attached:
import numpy as np
import math
import scipy.linalg as la
from numpy.linalg import eigvalsh
def potential(i):
return -1/(10*(math.cos((i/2-5)*math.pi/10)-1)*(math.cos((i/2-5)*math.pi/10)-1) 1)
def element(i,j):
if i==j:
return 0.1524 potential(i)
if abs(i-j)==1:
return -0.1524/2
return 0
I = 1j
a=[]
for i in range(0,2000):
b=[]
for j in range(0,2000):
b.append(element(i,j))
a.append(b)
arr = np.array(a)
print('matrix created')
eigvals, eigvecs = la.eig(arr)
print('eigenvalues and eigenvectors calculated')
eigvals = eigvals.real
f = open('Energies.txt', 'w')
for element in eigvals:
f.write(str(element) '\n')
print("eigenvalues stored")
f = open('Psi_functions.txt', 'w')
for element in eigvecs:
f.write(str(element) '\n')
CodePudding user response:
Use np.savetxt
np.savetxt('Energies.txt', eigvals)
np.savetxt('Psi_functions.txt', eigvecs)
Use np.loadtxt
to load them:
eigvals2 = np.loadtxt('Energies.txt')
eigvecs2 = np.loadtxt('Psi_functions.txt')
>>> (eigvals == eigvals2).all()
True
>>> (eigvecs == eigvecs2).all()
True
CodePudding user response:
You can use:
for element in eigvecs:
for e in element:
f.write(str(e) ' ')
f.write('\n')
But watch out for the size of the file, you will get about 100 Mbytes.