在python

时间:2017-07-27 11:26:52

标签: python-2.7 matrix out-of-memory sparse-matrix physics

我一直在使用numpy的普通矩阵来存储物理项目的矩阵。矩阵的大小由物理系统决定。 因此,例如,如果系统具有参数:
L = 4且N = 2,则矩阵的尺寸为4C2 = 6,因此矩阵为6×6矩阵。

这是好的,除了现在我需要更大的尺寸,即20C10 = 184,756。所以所需的矩阵现在是184756x184756矩阵,当我尝试创建这个大小的空矩阵时,会给我一个内存错误。 (有16GB的RAM)

结果矩阵大多只是对角线和非对角线项,因此在大尺寸矩阵中存在大量的零。因此,稀疏矩阵似乎是正确的方法。

我试图通过查看其他答案并从python库中自己尝试来使其工作,但无济于事。

以下是我的普通矩阵的代码:

def HamGen(L,N,delta,J):
    """
    Will Generate the hamiltonian matrix, 
    Takes parameters:
        L : Number of sites
        N : Number of spin downs
        delta : anistropy 

    Each term is gotten by getting H(i,j) = <Set(i)|H|Set(j)>
    The term will be a number
    Where H is an operator that acts on elements of the set 
    """
    D = BS.dimension(L,N) # Gets the dimension of the matrix, i.e NxN matrix
    Hamiltonian = np.zeros((D,D)) # Creates empty matrix 

    count1 = 0
    Set = BS.getSet(L,N) # The set of states to construct the hamiltonian

    for alpha in Set: #loop through the set (i)
        count2 = 0
        for beta in Set: # j
            """
            Compute ab = <alpha|Hamiltonian|beta>
            Then let Hamiltonian[a][b] = ab                        
            """
            if (alpha == beta):
                for i in range(L-1):
                    # Sz is just a function
                    Hamiltonian[count1][count2] += (J*delta*Sz(beta,i)*Sz(beta,i+1))
            b = check(alpha,beta)
            if b:
                del b[0]
                for j in b:
                    Hamiltonian[count1][count2] += (J*0.5*(Sp(beta,j)*Sm(beta,j+1) + Sm(beta,j)*Sp(beta,j+1)))
            count2 += 1
        count1 += 1
    return (np.asmatrix(Hamiltonian))

我大多只需要知道如何制作矩阵而不必使用尽可能多的内存,然后如何将我计算的术语放入矩阵中。

这是我尝试将矩阵作为稀疏矩阵。

def SPHamGen(L,N,delta):
"""
Will Generate the hamiltonian matrix, 
Takes parameters:
    L : Number of sites
    N : Number of spin downs
    delta : anistropy 
"""
start = timeit.default_timer()

D = BS.dimension(L,N)
Ham = sp.coo_matrix((D,D))
print Ham
#data = ([0])*D

count1 = 0
Set = BS.getSet(L,N)


data = ([0])*(D*D)
rows = ([0])*(D*D)
cols = ([0])*(D*D)
for alpha in Set:
    count2 = 0
    for beta in Set:
        """
        Compute ab = <alpha|Hamiltonian|beta>
        Then let Hamiltonian[a][b] = ab                        
        """
        if (alpha == beta):
            for i in range(L-1):
                #Hamiltonian[count1][count2] += (J*delta*Sz(beta,i)*Sz(beta,i+1))
                data[count2] += (J*delta*Sz(beta,i)*Sz(beta,i+1)) 
            rows[count2] = count1
            cols[count2] = count2
        b = check(alpha,beta)
        if b:
            del b[0]
            for j in b:
                #Hamiltonian[count1][count2] += (J*0.5*(Sp(beta,j)*Sm(beta,j+1) + Sm(beta,j)*Sp(beta,j+1)))
                data[count2] += (J*0.5*(Sp(beta,j)*Sm(beta,j+1) + Sm(beta,j)*Sp(beta,j+1)))
            rows[count2] = count1
            cols[count2] = count2
        count2 += 1
    count1 += 1
    Ham = Ham + sp.coo_matrix((data,(rows,cols)), shape = (D,D))

time = (timeit.default_timer() - start)
print "\n"+str(time) +"s to calculate H"
#return Ham   
return sparse.csr_matrix(Ham)

谢谢,菲尔。

0 个答案:

没有答案