[英]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我一直在尝试使用for循环用符号表达式填充它,但编译后出现以下错误
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
.这是因为sym.Array
实际上是ImmutableDenseNDimArray
的缩写。 This information is available in the docs .此信息可在文档中找到。 What you need to do is use the mutable version class called MutableDenseNDimArray
like this:您需要做的是使用名为MutableDenseNDimArray
的可变版本 class,如下所示:
import sympy as sym
import numpy as np
ChristSymb = sym.MutableDenseNDimArray(np.zeros((d, d, d), dtype=int))
Array
is an abbrevation for ImmutableDenseNDimArray
. Array
是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).这将创建一个可变的零数组(请注意,您不需要NumPy ,因为SymPy也具有zeros(...)函数)。
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]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.