In Drake, I have NumPy ndarray
's (some multidimensional) that are of dtype=float
, and I want to convert them to AutoDiffXd
, Expression
, etc.
In C++, I know I can do things like this:
MatrixXd X(2, 2);
X <<
1.0, 2.0,
3.0, 4.0;
MatrixX<AutoDiffXd> X_ad = X.cast<AutoDiffXd>();
However, in Python, I've found myself writing loops like this:
import numpy as np
from pydrake.autodiffutils import AutoDiffXd
X = np.array([
[1.0, 2.0],
[3.0, 4.0],
])
X_ad = np.zeros(X.shape, dtype=object)
for i in X.shape[0]:
for j in X.shape[1]:
X_ad[i, j] = AutoDiffXd(X[i, j])
Is there a better way to do this?