Is it advisable, while working with arrays of symbolic expresions, to use numpy arrays?
在使用符号表达式数组时,使用numpy数组是否明智?
Something like
就像是
u0=numpy.array([Number(1.0), Number(1.0), Number(1.0)])
u0 = numpy.array([Number(1.0),Number(1.0),Number(1.0)])
I mean, is it faster to use numpy arrays instead of python lists?
我的意思是,使用numpy数组而不是python列表会更快吗?
If so, certain operations with numpy arrays seem to convert automatically to float symbolic expresions, for example:
如果是这样,具有numpy数组的某些操作似乎会自动转换为浮动符号表达式,例如:
u0=np.array([Number(1.0), Number(1.0), Number(1.0)]) u = np.zeros((10, 3)) u[0] = u0
u0 = np.array([Number(1.0),Number(1.0),Number(1.0)])u = np.zeros((10,3))u [0] = u0
Now while type(u0[0])
>> sympy.core.numbers.Float ,
现在输入类型(u0 [0])>> sympy.core.numbers.Float,
type(u[0][0])
>> numpy.float64
type(u [0] [0])>> numpy.float64
How can I avoid numpy to convert the symbolic expresions copied to float64?
如何避免numpy转换复制到float64的符号表达式?
2 个解决方案
#1
2
I think it is ok to work with numpy arrays, if necessary. You should bear in mind that arrays are fundamentally different from lists. Most importantly, all array elements have to be of the same type and you cannot change the type.
我认为如果需要,可以使用numpy数组。您应该记住,数组与列表根本不同。最重要的是,所有数组元素必须属于同一类型,并且您无法更改类型。
In particular, you define the array u0
which is per default an array of floats. That is why you cannot assign any sympy objects to it.
特别是,您定义了数组u0,默认情况下是浮点数组。这就是为什么你不能给它分配任何sympy对象的原因。
I myself use numpy arrays to accommodate sympy expressions. Most notably, in cases where I need more than 2 dimensions and therefore cannot use Sympy matrices.
我自己使用numpy数组来容纳sympy表达式。最值得注意的是,在我需要超过2维的情况下,因此不能使用Sympy矩阵。
If the only reason to use arrays instead of lists is speed, it might not be advisable. Especially, since you have to be a bit careful with types (as you find out) and there should be less surprises when using lists or rather sympy.Matrix.
如果使用数组而不是列表的唯一原因是速度,则可能不建议。特别是,因为你必须对类型有点小心(正如你所知)并且在使用列表或者更喜欢sympy.Matrix时应该会有更少的惊喜。
In your example, you can fix the problem by defining a proper data type:
在您的示例中,您可以通过定义正确的数据类型来解决问题:
u = np.zeros((10, 3), dtype=sp.Symbol)
#2
5
I doubt there's much speed difference vs. a list, since using any non-NumPy data type (i.e., any SymPy data type) in a NumPy array results in dtype=object
, meaning the array is just an array of pointers (which a list
is too).
我怀疑与列表有很大的速度差异,因为在NumPy数组中使用任何非NumPy数据类型(即任何SymPy数据类型)会导致dtype = object,这意味着数组只是一个指针数组(列表中)也是)。
It's really unclear why you want to use a NumPy array?
目前还不清楚为什么要使用NumPy阵列?
The first question is, why don't you want to use float64
? Assumedly you are using
第一个问题是,为什么不想使用float64?假设你正在使用
- Symbolic expressions (such as
x**2
orpi
), - 符号表达式(例如x ** 2或pi),
- Rational numbers, or
- 理性数字,或
-
sympy.Float
objects with higher precision - sympy.Float对象具有更高的精度
Those are the only reasons I can think of that you would want to prefer a SymPy type over a NumPy one.
这些是我能想到的唯一原因,你想要更喜欢SymPy类型而不是NumPy类型。
The main advantage of using a NumPy array would be if you want to take advantage of NumPy's superior indexing syntax. As Stelios pointed out, you can get much of this by using SymPy's tensor module. This is really the only reason to use them, and you have to be careful and be aware of which NumPy methods/functions will work and which won't.
使用NumPy数组的主要优点是,如果您想利用NumPy的卓越索引语法。正如Stelios指出的那样,你可以通过使用SymPy的张量模块来获得更多。这实际上是使用它们的唯一原因,你必须要小心并注意哪些NumPy方法/功能可以工作,哪些不会。
The reason is that any NumPy mathematical function will not work (or at best, will convert the array to float64
first). The reason is that NumPy functions are designed to work on NumPy data types. They don't know about the above data types. To get exact values (symbolic expressions or rational numbers), or higher precision floating point values (for the case of sympy.Float
), you need to use SymPy functions, which do not work on NumPy arrays.
原因是任何NumPy数学函数都不起作用(或者最好将数组首先转换为float64)。原因是NumPy函数被设计用于NumPy数据类型。他们不了解上述数据类型。要获得精确值(符号表达式或有理数)或更高精度的浮点值(对于sympy.Float),您需要使用SymPy函数,这些函数不适用于NumPy数组。
If on the other hand (again, it's not clear what exactly you are trying to do), you want to do calculations in SymPy and then use NumPy functions to numerically evaluate the expressions, you should use SymPy to create your expressions, and then lambdify
(or ufuncify
if performance becomes an issue) to convert the expressions to equivalent NumPy functions, which can operate on NumPy arrays of NumPy dtypes.
如果另一方面(再次,不清楚你究竟想要做什么),你想在SymPy中进行计算,然后使用NumPy函数来数值计算表达式,你应该使用SymPy来创建表达式,然后lambdify (或者,如果性能成为问题,则ufuncify)将表达式转换为等效的NumPy函数,这些函数可以在NumPy dtypes的NumPy数组上运行。
#1
2
I think it is ok to work with numpy arrays, if necessary. You should bear in mind that arrays are fundamentally different from lists. Most importantly, all array elements have to be of the same type and you cannot change the type.
我认为如果需要,可以使用numpy数组。您应该记住,数组与列表根本不同。最重要的是,所有数组元素必须属于同一类型,并且您无法更改类型。
In particular, you define the array u0
which is per default an array of floats. That is why you cannot assign any sympy objects to it.
特别是,您定义了数组u0,默认情况下是浮点数组。这就是为什么你不能给它分配任何sympy对象的原因。
I myself use numpy arrays to accommodate sympy expressions. Most notably, in cases where I need more than 2 dimensions and therefore cannot use Sympy matrices.
我自己使用numpy数组来容纳sympy表达式。最值得注意的是,在我需要超过2维的情况下,因此不能使用Sympy矩阵。
If the only reason to use arrays instead of lists is speed, it might not be advisable. Especially, since you have to be a bit careful with types (as you find out) and there should be less surprises when using lists or rather sympy.Matrix.
如果使用数组而不是列表的唯一原因是速度,则可能不建议。特别是,因为你必须对类型有点小心(正如你所知)并且在使用列表或者更喜欢sympy.Matrix时应该会有更少的惊喜。
In your example, you can fix the problem by defining a proper data type:
在您的示例中,您可以通过定义正确的数据类型来解决问题:
u = np.zeros((10, 3), dtype=sp.Symbol)
#2
5
I doubt there's much speed difference vs. a list, since using any non-NumPy data type (i.e., any SymPy data type) in a NumPy array results in dtype=object
, meaning the array is just an array of pointers (which a list
is too).
我怀疑与列表有很大的速度差异,因为在NumPy数组中使用任何非NumPy数据类型(即任何SymPy数据类型)会导致dtype = object,这意味着数组只是一个指针数组(列表中)也是)。
It's really unclear why you want to use a NumPy array?
目前还不清楚为什么要使用NumPy阵列?
The first question is, why don't you want to use float64
? Assumedly you are using
第一个问题是,为什么不想使用float64?假设你正在使用
- Symbolic expressions (such as
x**2
orpi
), - 符号表达式(例如x ** 2或pi),
- Rational numbers, or
- 理性数字,或
-
sympy.Float
objects with higher precision - sympy.Float对象具有更高的精度
Those are the only reasons I can think of that you would want to prefer a SymPy type over a NumPy one.
这些是我能想到的唯一原因,你想要更喜欢SymPy类型而不是NumPy类型。
The main advantage of using a NumPy array would be if you want to take advantage of NumPy's superior indexing syntax. As Stelios pointed out, you can get much of this by using SymPy's tensor module. This is really the only reason to use them, and you have to be careful and be aware of which NumPy methods/functions will work and which won't.
使用NumPy数组的主要优点是,如果您想利用NumPy的卓越索引语法。正如Stelios指出的那样,你可以通过使用SymPy的张量模块来获得更多。这实际上是使用它们的唯一原因,你必须要小心并注意哪些NumPy方法/功能可以工作,哪些不会。
The reason is that any NumPy mathematical function will not work (or at best, will convert the array to float64
first). The reason is that NumPy functions are designed to work on NumPy data types. They don't know about the above data types. To get exact values (symbolic expressions or rational numbers), or higher precision floating point values (for the case of sympy.Float
), you need to use SymPy functions, which do not work on NumPy arrays.
原因是任何NumPy数学函数都不起作用(或者最好将数组首先转换为float64)。原因是NumPy函数被设计用于NumPy数据类型。他们不了解上述数据类型。要获得精确值(符号表达式或有理数)或更高精度的浮点值(对于sympy.Float),您需要使用SymPy函数,这些函数不适用于NumPy数组。
If on the other hand (again, it's not clear what exactly you are trying to do), you want to do calculations in SymPy and then use NumPy functions to numerically evaluate the expressions, you should use SymPy to create your expressions, and then lambdify
(or ufuncify
if performance becomes an issue) to convert the expressions to equivalent NumPy functions, which can operate on NumPy arrays of NumPy dtypes.
如果另一方面(再次,不清楚你究竟想要做什么),你想在SymPy中进行计算,然后使用NumPy函数来数值计算表达式,你应该使用SymPy来创建表达式,然后lambdify (或者,如果性能成为问题,则ufuncify)将表达式转换为等效的NumPy函数,这些函数可以在NumPy dtypes的NumPy数组上运行。