当我声明一个符号和一个像这样的数组时
t = symbols('t')
v1 = np.array([[1, 2, 3, 4, 5, ]])
print( type(v1) )
我在数组中放了一个符号
v2 = v1.dot(t)
print( type(v2) )
问题1:如果现在v2里面有一个符号,为什么它仍然是numpy.ndarray?
当我尝试集成此数组时,我无法做到
# I1 = integrate( v2 , t ) # this cannot be integrate because...
# AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_poly'
那我就要做
v3 = smp.Matrix( v2 )
I2 = integrate( v3 , t )
问题2:
是否有另一种集成v2的方法,而无需在sympy Matrix中对其进行更改?
sympy
andnumpy
are not integrated.sympy
objects work in arrays to the extent that they can be treated as 'objects', and have the appropriate methods. Otherwisenumpy
does not do anything special withsympy
objects.In an
isympy
session:An array with symbols will be
object
dtype. That's like a list, with references to those numbers and objects. It's not a special array dtype:np.dot
works because the symbols can be added and multiplied:dot
with two matching 1d arrays returns a "scalar", in this case asympy
object.与具有匹配的数字大小列表的点相同:
But dot of the 3 element list with scalar
tau
produces an array:(3,4)与(4,)的点产生一个(3,)数组:
sympy
also does not "know" aboutnumpy
, so it shouldn't be surprising thatintegrate(ndarray, ...)
does not work.The error
# AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_poly'
indicates that the array has been converted into anImmutableDenseNDimArray
sympy object. Without getting into details, that apparently is the wrong type of sympy object for this purpose.With your
v2
:Neither of these has an
as_poly
method, butintegrate
might be doing some further processing.numpy
has one main class,ndarray
, with a well known set of methods and attributes.sympy
defines many more classes, so we (I) need to pay a lot more attention to the docs.