微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

np.linalg.solve是否不适用于AutoDiff?

如何解决np.linalg.solve是否不适用于AutoDiff?

np.linalg.solve()是否不适用于autodiff?我使用的是求解机械手方程。错误消息如下所示。 我尝试类似的“ double”版本代码,这没有问题。请告诉我如何解决,谢谢!

### 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)) 

解决方法

请注意,在pydrake中,AutoDiffXd标量使用dtype=object暴露给NumPy。

这种方法有一些缺点,例如您遇到的问题。
这并不是Drake的问题,而是NumPy本身的局限性,因为ufunc是在18.04的(超旧)版本上实现的。

为说明起见,这是我在Ubuntu 18.04,CPython 3.6.9,NumPy 1.13.3上看到的内容:

>>> 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

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

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