首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >np.linalg.solve()不是为AutoDiff工作吗?

np.linalg.solve()不是为AutoDiff工作吗?
EN

Stack Overflow用户
提问于 2020-10-29 15:04:10
回答 1查看 83关注 0票数 1

np.linalg.solve()不适用于AutoDiff吗?我用它来求解机械手方程。错误消息如下所示。我尝试了类似的“双”版本代码,这是没有问题的。请告诉我怎么修理,谢谢!

代码语言:javascript
复制
### here is the error message                        
vdot_ad = np.linalg.solve(M_,ggg_ad) 
    File "<__array_function__ internals>", line 5, in solve
    File "/usr/local/lib/python3.8/site-packages/numpy/linalg/linalg.py", line 394, in solve
    r = gufunc(a, b, signature=signature, extobj=extobj)
    TypeError: No loop matching the specified signature and casting was found for ufunc solve1

####. here is the code 
plant = MultibodyPlant(time_step= 0.01)
parser = Parser(plant)
parser.AddModelFromFile("double_pendulum.sdf")
plant.Finalize()
plant_autodiff = plant.ToAutoDiffXd()

####### <AutoDiff> get the error message
xu = np.hstack((x, u))
xu_ad = initializeAutoDiff(xu)[:,0]
x_ad = xu_ad[:4]
q_ad = x_ad[:2]
v_ad = x_ad[2:4]
u_ad = xu_ad[4:]
(M_, Cv_, tauG_, B_, tauExt_) = ManipulatorDynamics(plant_autodiff, q_ad, v_ad)
vdot_ad = np.linalg.solve(M_,tauG_ + np.dot(B_,u_ad) - np.dot(Cv_,v_ad)) 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-29 21:01:23

注意,在pydrake中,AutoDiffXd标量使用dtype=object向NumPy公开。

这种方法有一些缺点,比如您现在遇到的问题。

这不一定是Drake的问题,但是考虑到在18.04版(超级老版本)上实现的ufunc,这是对NumPy本身的限制。

为了说明,下面是我在Ubuntu18.04,CPython 3.6.9,NumPy 1.13.3上看到的内容:

代码语言:javascript
复制
>>> import numpy as np
>>> A = np.eye(2)
>>> b = np.array([1, 2])
>>> np.linalg.solve(A, b)
array([ 1.,  2.])
>>> A = A.astype(object)
>>> b = b.astype(object)
>>> np.linalg.solve(A, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/linalg/linalg.py", line 375, in solve
    r = gufunc(a, b, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting
was found for ufunc solve1

最直接的解决方案是在pydrake中公开一个类似的例程,让用户利用它。

这也是我们必须为np.linalg.inv所做的:

https://github.com/RobotLocomotion/drake/pull/11173/files

这不是最好的解决方案:(然而,这很简单!)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64593813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档