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?
CodePudding user response:
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))
CodePudding user response:
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]]]