Sympy-numpy集成存在-在哪里记录? [英] Sympy-numpy integration exists - where is it documented?
问题描述
我只是偶然发现我可以将sympy表达式与numpy数组混合使用:
I just accidentally discovered that I can mix sympy expressions up with numpy arrays:
>>> import numpy as np
>>> import sympy as sym
>>> x, y, z = sym.symbols('x y z')
>>> np.ones(5)*x
array([1.0*x, 1.0*x, 1.0*x, 1.0*x, 1.0*x], dtype=object)
# I was expecting this to throw an error!
# sum works and collects terms etc. as I would expect
>>> np.sum(np.array([x+0.1,y,z+y]))
x + 2*y + z + 0.1
# dot works too
>>> np.dot(np.array([x,y,z]),np.array([z,y,x]))
2*x*z + y**2
>>> np.dot(np.array([x,y,z]),np.array([1,2,3]))
x + 2*y + 3*z
这对我来说非常有用,因为我在同一程序中同时进行了数值和符号计算.但是,我很好奇这种方法的缺陷和局限性,例如,似乎在包含Sympy对象的Numpy数组上都不支持np.sin
和sym.sin
,因为它们都给出了错误.
This is quite useful for me, because I'm doing both numerical and symbolic calculations in the same program. However, I'm curious about the pitfalls and limitations of this approach --- it seems for example that neither np.sin
nor sym.sin
are supported on Numpy arrays containing Sympy objects, since both give an error.
但是,这种numpy-sympy集成似乎在任何地方都没有记录.这些库是如何实现的只是偶然的情况,还是故意的功能?如果是后者,则什么时候设计使用它?什么时候使用sympy.Matrix
或其他解决方案更好?使用这种类型的数组时,我是否可以期望保持numpy的速度,或者一旦涉及sympy符号,它是否会立即回到Python循环?
However, this numpy-sympy integration doesn't appear to be documented anywhere. Is it just an accident of how these libraries are implemented, or is it a deliberate feature? If the latter, when is it designed to be used, and when would it be better to use sympy.Matrix
or other solutions? Can I expect to keep some of numpy's speed when working with arrays of this kind, or will it just drop back to Python loops as soon as a sympy symbol is involved?
简而言之,我很高兴发现此功能存在,但是我想了解更多!
In short I'm pleased to find this feature exists, but I would like to know more about it!
推荐答案
这只是NumPy对对象数组的支持.它不特定于SymPy. NumPy检查操作数,发现不是所有的操作数都是标量.其中涉及一些对象.因此,它将调用该对象的__mul__
或__rmul__
,并将结果放入对象数组中.例如:mpmath对象,
This is just NumPy's support for arrays of objects. It is not specific to SymPy. NumPy examines the operands and finds not all of them are scalars; there are some objects involved. So it calls that object's __mul__
or __rmul__
, and puts the result into an array of objects. For example: mpmath objects,
>>> import mpmath as mp
>>> np.ones(5) * mp.mpf('1.23')
array([mpf('1.23'), mpf('1.23'), mpf('1.23'), mpf('1.23'), mpf('1.23')],
dtype=object)
或列表:
>>> np.array([[2], 3])*5
array([list([2, 2, 2, 2, 2]), 15], dtype=object)
>>> np.array([2, 3])*[[1, 1], [2]]
array([list([1, 1, 1, 1]), list([2, 2, 2])], dtype=object)
使用这种数组时,我能否期望保持numpy的速度?
Can I expect to keep some of numpy's speed when working with arrays of this kind,
不.与Python列表相比,NumPy对象数组没有性能优势;访问元素的开销可能比列表中要大. 在Python中存储Python对象列表与固定长度的Numpy数组
No. NumPy object arrays have no performance benefits over Python lists; there is probably more overhead in accessing elements than would be in a list. Storing Python objects in a Python list vs. a fixed-length Numpy array
如果有更具体的数据结构,则没有理由使用此类数组.
There is no reason to use such arrays if a more specific data structure is available.
这篇关于Sympy-numpy集成存在-在哪里记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!