Tuesday 15 February 2011

python - numpy fill an array with arrays -



python - numpy fill an array with arrays -

i want combine unspecified (finite) number of matrices under kroneckerproduct. in order want save matrices in array don't know how this. @ moment have:

for in range(lnew-2): j in range(lnew-2): mass = np.empty(shape=(lnew-1,lnew-1)) if == j: bulk[i,j]=h2 else: bulk[i,j]=idm

here h2 , idm both matrices, want combine under kronecker product. since mass ndarray object suppose wont take arraylike objects within it.

edit:

this function in want utilize idea. using build hamiltonian matrix quantum spin chain. h2 hamiltonian 2 particle chain, h2 4x4 matrix , idm 2x2 identity matrix.

and 3 particle chain np.kron(h2,idm)+np.kron(idm,h2)

and 4 particles np.kron(np.kron(h2,idm),idm)+np.kron(idm,np.kron(h2,idm))+np.kron(idm,np.kron(idm,h2)) , on.

def expandhn(lnew): idm = np.identity(2) h2 = geth(2,'n') hnew = h2 in range(lnew-2): j in range(lnew-2): mass = np.empty(shape=(lnew-1,lnew-1)) if == j: bulk[i,j]=h2 else: bulk[i,j]=idm = 0 in range(lnew-2): j in range(lnew-3): hnew += np.kron(bulk[i,j],bulk[i,j+1]) #something homecoming hnew

as can see sec set of loops hasn't been worked out.

that beingness said, if has totaly different working solution happy too.

if understand correctly, question boils downwards how create arrays of arrays numpy. suggest utilize standard python object dict:

bulk = dict() in range(lnew-2): j in range(lnew-2): if == j: bulk[(i,j)]=h2 else: bulk[(i,j)]=idm

the usage of tuples keys allows maintain array-like indexing of matrices. note, should define mass outside of 2 loops (in case).

hth

python arrays numpy

No comments:

Post a Comment