Reputation: 4745
I am working with an example in pydrake that has a constraint with polar coordinates that includes evaluating the following function:
def f(x):
r = np.sqrt(x[0]**2 + x[1]**2)
theta = np.arctan(x[1] / x[0])
return np.array([r, theta])
However, it chokes on the np.arctan
call with this error:
TypeError: loop of ufunc does not support argument 0 of type pydrake.autodiffutils.AutoD iffXd which has no callable arctan method
Is there an atan
function in pydrake somewhere suitable for autodiff?
I have seen this error in other places and have just had to re-implement things myself (e.g. I tried np.linalg.cholesky
and got a similar error so I implemented my own cholesky
function using more basic operations). Are there better ways to work around this such that I can better utilize things in numpy with the pydrake autodiff?
It turns out you can use np.arctan2(x[1], x[0])
and it works with pydrake autodiff, so I suppose that solves my specific problem. But I'm still curious to know how to work around these errors as they come up aside from implementing my own versions of them.
Upvotes: 1
Views: 108
Reputation: 106
While I'm not familiar with Drake/PyDrake, any autodiffing program requires functions be implemented in a way that their derivatives are known. It seems that PyDrake is inspecting your code, identifying functions it knows autodiff versions of (e.g., np.arctan2
) and replacing them with those versions. It looks like this is the list of functions PyDrake has implemented, so you may want to refer to this list rather than use trial and error. Oddly enough, arctan
is there as well as arctan2
. I think there may be an additional problem here, specifically that arctan(y/x)
is not differentiable everywhere, whereas arctan2(x, y)
is designed to fix that. See these plots of arctan(y/x) and arctan2(x, y) as examples.
Regardless, for mathematical reasons you probably want to be using arctan2
to find that angle, unless you know it's restricted to a certain range.
Upvotes: 3