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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...