简体   繁体   中英

How do I fill a symbolic N-dim zero array in python?

I've initialized the following array:

ChristSymb=sym.Array(np.zeros((d,d,d),dtype=int))

and I've been trying to fill it up with symbolic expressions using a for loop, but after compiling appear the following error

TypeError: immutable N-dim array

What's wrong with the array definition that cannot be modified? How can I solve it?

This is because sym.Array is actually an abbreviation for ImmutableDenseNDimArray . This information is available in the docs . What you need to do is use the mutable version class called MutableDenseNDimArray like this:

import sympy as sym
import numpy as np

ChristSymb = sym.MutableDenseNDimArray(np.zeros((d, d, d), dtype=int))

Array is an abbrevation for ImmutableDenseNDimArray . Immutable means that its component elements cannot be changed once the array is defined.

You can create a mutable array of zeros with one of these ways:

In [2]: MutableDenseNDimArray.zeros(3,3,3)
Out[2]: [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

This creates a mutable array of zeros (note that you don't need NumPy , as SymPy also has the zeros(...) function).

Otherwise, convert the immutable array to a mutable array:

In [3]: Array.zeros(3,3,3).as_mutable()
Out[3]: [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM