numpy cross does not support Drake's AutoDiffXd?

Viewed 44

The following code

import numpy as np
from pydrake.all import InitializeAutoDiffTuple

x = np.array([1,2,3])
y = np.array([4,5,6])
np.cross(x,y)
(x_ad,y_ad) = InitializeAutoDiffTuple(x, y)
np.cross(x_ad, y_ad)

Leads to the error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-f44901f6fd4e> in <module>
----> 1 np.cross(x_ad, y_ad)

<__array_function__ internals> in cross(*args, **kwargs)

~/.local/lib/python3.8/site-packages/numpy/core/numeric.py in cross(a, b, axisa, axisb, axisc, axis)
   1604            "(dimension must be 2 or 3)")
   1605     if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3):
-> 1606         raise ValueError(msg)
   1607 
   1608     # Create the output array

ValueError: incompatible dimensions for cross product
(dimension must be 2 or 3)

Does Drake AutoDiffXd not support numpy's cross product?

1 Answers

This is not a lack of support from Drake. np.cross just doesn't accept (3,1) vectors, which is the default shape coming out of InitializeAutoDiffTuple. Either of the following can work instead:

np.cross(np.squeeze(x_ad),np.squeeze(y_ad))
np.cross(x_ad.T, y_ad.T)
Related