为什么当一个numpy数组里面有一个“ sympy.core.symbol.Symbol”时,它仍然仍然是<class'numpy.ndarray'>?

如何解决为什么当一个numpy数组里面有一个“ sympy.core.symbol.Symbol”时,它仍然仍然是<class'numpy.ndarray'>?

当我声明一个符号和一个像这样的数组

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中对其进行更改?

解决方法

sympynumpy未集成。 sympy对象在数组中的工作范围可以视为“对象”,并具有适当的方法。否则,numpysympy对象不会做任何特殊的事情。

isympy会话中:

In [200]: t                                                                                          
Out[200]: t

In [201]: tau                                                                                        
Out[201]: τ

带有符号的数组将为object dtype。这就像一个列表,其中引用了这些数字和对象。它不是特殊的数组dtype:

In [202]: arr = np.array([1,2,tau,t])                                                                

In [203]: arr                                                                                        
Out[203]: array([1,t],dtype=object)

np.dot之所以有效,是因为可以添加和乘以符号:

In [204]: np.dot(arr,arr)                                                                            
Out[204]: 
 2    2    
t  + τ  + 5

In [205]: type(_)                                                                                    
Out[205]: sympy.core.add.Add

dot具有两个匹配的1d数组,将返回一个“标量”,在这种情况下为sympy对象。

与具有匹配的数字大小列表的点相同:

In [206]: np.dot([1,3,4],arr)                                                                      
Out[206]: 4⋅t + 3⋅τ + 5

In [207]: type(_)                                                                                    
Out[207]: sympy.core.add.Add

但是标量为tau的3元素列表的点会产生一个数组:

In [208]: np.array([1,3]).dot(tau)                                                                 
Out[208]: array([tau,2*tau,3*tau],dtype=object)

(3,4)与(4,)的点产生一个(3,)数组:

In [210]: np.ones((3,4),int).dot(arr)                                                                
Out[210]: array([t + tau + 3,t + tau + 3,t + tau + 3],dtype=object)

sympy也不“了解” numpy,因此integrate(ndarray,...)不起作用也就不足为奇了。

错误# AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_poly'表示该数组已转换为ImmutableDenseNDimArray sympy对象。无需详细说明,显然这是为此目的错误的sympy对象类型。

使用您的v2

In [220]: v2                                                                                         
Out[220]: array([[t,2*t,3*t,4*t,5*t]],dtype=object)
    
In [222]: Matrix(v2)                                                                                 
Out[222]: [t  2⋅t  3⋅t  4⋅t  5⋅t]

In [225]: type(_222)                                                                                 
Out[225]: sympy.matrices.dense.MutableDenseMatrix

In [227]: ImmutableDenseNDimArray(v2)                                                                
Out[227]: [[t  2⋅t  3⋅t  4⋅t  5⋅t]]

In [228]: type(_)                                                                                    
Out[228]: sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray

这两个方法都不具有as_poly方法,但是integrate可能正在做一些进一步的处理。 numpy有一个主类ndarray,它具有一组众所周知的方法和属性。 sympy定义了更多的类,因此我们(I)需要更加关注文档。这两个类(type(...).__mro__)的类层次结构完全不同。


使用Matrix,集成程序运行并产生:

In [235]: integrate(Matrix(v2),t)                                                                    
Out[235]: 
⎡ 2         2           2⎤
⎢t    2  3⋅t      2  5⋅t ⎥
⎢──  t   ────  2⋅t   ────⎥
⎣2        2           2  ⎦

如果提供了此输出,那就太好了(礼貌!)。

对于无法正常工作的情况,FULL回溯可能具有启发性:

In [236]: integrate(ImmutableDenseNDimArray(v2),t)                                                   
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-236-fbba47abf5a1> in <module>
----> 1 integrate(ImmutableDenseNDimArray(v2),t)

/usr/local/lib/python3.6/dist-packages/sympy/integrals/integrals.py in integrate(*args,**kwargs)
   1543 
   1544     if isinstance(integral,Integral):
-> 1545         return integral.doit(**doit_flags)
   1546     else:
   1547         new_args = [a.doit(**doit_flags) if isinstance(a,Integral) else a

/usr/local/lib/python3.6/dist-packages/sympy/integrals/integrals.py in doit(self,**hints)
    592                 else:
    593                     antideriv = self._eval_integral(
--> 594                         function,xab[0],**eval_kwargs)
    595                     if antideriv is None and meijerg is True:
    596                         ret = try_meijerg(function,xab)

/usr/local/lib/python3.6/dist-packages/sympy/integrals/integrals.py in _eval_integral(self,f,x,meijerg,risch,manual,heurisch,conds)
    921 
    922         # try to convert to poly(x) and then integrate if successful (fast)
--> 923         poly = f.as_poly(x)
    924         if poly is not None and not (manual or meijerg or risch):
    925             return poly.integrate().as_expr()

AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_poly'

我们需要深入研究这段代码(922行)以找出其为什么尝试f.as_poly的原因。是否也使用Matrix尝试了此操作,还是采用了其他方法?像这样的sympy代码中有很多事情!


首先不需要使用np.array。坚持使用sympy

In [249]: integrate(Matrix([[1,4,5]])*t,t)                                                       
Out[249]: 
⎡ 2         2           2⎤
⎢t    2  3⋅t      2  5⋅t ⎥
⎢──  t   ────  2⋅t   ────⎥
⎣2        2           2  ⎦

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-