1
Ich bin neu bei Python - ich kam aus Matlab. Ich versuche, diesen Code zu kompilieren:rekursiv Add python matrix spärlich
import numpy as np
from scipy import sparse
n=3
dim=2^n
sx = np.array([[0,1],[1,0]])
sy = np.array([[0,-1j],[1j,0]])
sz = np.array([[1,0],[0,-1]])
ssx= sparse.csr_matrix(sx)
ssy= sparse.csr_matrix(sy)
ssz= sparse.csr_matrix(sz)
expon1=np.zeros((n,n))
for i in range(n-1):
expon1[i,i]=1
expon1[i+1,i]=1
expon1[0,n-1]=1
expon1[n-1,n-1]=1
expon2=np.identity(n)
Sigs1=sparse.csr_matrix(0,(dim, dim))
for j in range(n-1):
Sig1=sparse.csr_matrix(1)
for i in range(n-1):
Sig1=sparse.kron(Sig1,ssx.power(expon1[i,j]))
Sigs1= Sigs1+Sig1
Nach dem Ausführen python3 sparse.py [name of file]
, druckt das Terminal den folgenden Text:
Traceback (most recent call last):
File "sparse.py", line 31, in <module>
Sigs1= Sigs1+Sig1
File "/usr/lib/python3/dist-packages/scipy/sparse/compressed.py", line 341, in __add__
raise ValueError("inconsistent shapes")
ValueError: inconsistent shapes
tnx aber entfernen, wenn Sigs1 sparse.csr_matrix = (0, (dim, dim)) für j im Bereich (n-1): Sig1 = sparse.csr_matrix (1) für i im Bereich (n -1): Sig1 = sparse.kron (Sig1, ssx.power (expon1 [i, j])) Sigs1 = Sigs1 + Sig1 der Code funktioniert –
, wenn Sie diesen Teil entfernen Sie den Code nicht beschweren, aber es heißt nicht, dass es funktioniert. Das Problem mit Ihrem Code ist "dim = 2^n" gibt 1 statt 8 zurück. –
thx für die Beratung, ich ändere dim = 8, ist am praktischsten, aber jetzt ist das Problem in der add: Sigs1 = Sigs1 + Sig1 Datei "/usr/lib/python3/dist-packages/scipy/sparse/base.py", Zeile 280, in __add__ –