Python: Select all
def Array_to_nparray(M):
Arrayshape=M.shape
Mlegs=M.legs
flat_array = []
block_shapes = []
slices_list = []
for block, blockslices, charges, qindices in M:
elements = block.flatten()
flat_array.extend(elements)
block_shapes.append(block.shape)
slices_list.append(blockslices)
flat_array = np.array(flat_array)
return flat_array,block_shapes,slices_list,Arrayshape,Mlegs
def nparray_to_Array(flat_array,block_shapes,slices_list,Arrayshape,Mlegs):
M_array=np.zeros(Arrayshape)
current_index = 0
for shape, slices in zip(block_shapes, slices_list):
num_elements = np.prod(shape)
block_elements = flat_array[current_index:current_index + num_elements]
current_index += len(block_elements)
M_array[slices]=block_elements.reshape(shape)
M=npc.Array.from_ndarray(M_array,legcharges=Mlegs,cutoff=1e-9)
return M