You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below is a self contained example that shows the issue. Taking the derivative of a vector valued function works, but not when the function returns a matrix.
I can work around the issue by reshaping the matrix as a vector, but I wanted to make sure I am not doing something wrong (or if this is a known limitation).
Thanks for any help you can provide!
Best,
Goeric
#include<iostream>
#include<Eigen/Core>
#include<autodiff/forward/dual.hpp>
#include<autodiff/forward/real/eigen.hpp>// The single-variable vector-valued function
Eigen::Vector<autodiff::real, Eigen::Dynamic> fVector(autodiff::real x) {
Eigen::Vector<autodiff::real, Eigen::Dynamic> v(5);
v.setConstant(1 + x + x * x + 1 / x + log(x));
return v;
}
// The single-variable matrix-valued function
Eigen::Matrix<autodiff::real, Eigen::Dynamic, Eigen::Dynamic> fMatrix(autodiff::real x) {
Eigen::Matrix<autodiff::real, Eigen::Dynamic, Eigen::Dynamic> m(5, 3);
m.setConstant(1 + x + x * x + 1 / x + log(x));
return m;
}
intmain(int argc, char *argv[]) {
{ // THIS COMPILES FINE
autodiff::real x = 2.0;
Eigen::Vector<autodiff::real, Eigen::Dynamic> u = fVector(x);
std::cout << "u = " << u << std::endl;
Eigen::Vector<double, Eigen::Dynamic> dudx = autodiff::derivative(fVector, autodiff::wrt(x), autodiff::at(x));
std::cout << "dudx = " << dudx << std::endl;
}
{ // THIS FAILS TO COMPILE
autodiff::real x = 2.0;
Eigen::Matrix<autodiff::real, Eigen::Dynamic, Eigen::Dynamic> u = fMatrix(x);
std::cout << "u = " << u << std::endl;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> dudx = autodiff::derivative(fMatrix, autodiff::wrt(x), autodiff::at(x));
std::cout << "dudx = " << dudx << std::endl;
}
return0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi!
When trying to take the derivative of a matrix valued function, I get a failed assertion from Eigen:
THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD
Below is a self contained example that shows the issue. Taking the derivative of a vector valued function works, but not when the function returns a matrix.
I can work around the issue by reshaping the matrix as a vector, but I wanted to make sure I am not doing something wrong (or if this is a known limitation).
Thanks for any help you can provide!
Best,
Goeric
Beta Was this translation helpful? Give feedback.
All reactions