来自 EDIT 的解决方案现在作为答案发布。
老问题
我想将 的样条插值包含Eigen::Spline
到一个更大的公式中,并希望借助 来确定该公式的导数Eigen::AutoDiff
。
我尝试了以下代码:
#include <iostream>
#include <Eigen/Eigen>
#include <unsupported/Eigen/AutoDiff>
#include <unsupported/Eigen/Splines>
double array[] = {
1.0, 2.0,
3.0, 4.0,
5.0, 1.0,
6.0, 2.0
};
constexpr size_t nCols=2;
constexpr size_t rowSize=sizeof(array)/sizeof(double)/nCols;
constexpr size_t derOrder=1;
typedef Eigen::Map<Eigen::Matrix<double,1,rowSize>,Eigen::Unaligned,Eigen::InnerStride<nCols> > Map;
typedef const Eigen::Spline<double,nCols-1> Spline;
constexpr size_t interpolOrder=3;
constexpr double& xStop=array[nCols*(rowSize-1)];
Spline spline=Eigen::SplineFitting<Spline>::Interpolate(Map(array+1),interpolOrder,Map(static_cast<double*>(array))/xStop);
typedef Eigen::AutoDiffScalar<Eigen::Matrix<double,1,2> > DerType;
DerType f(const DerType& x){
DerType ret;
auto y = spline.derivatives<1>(x.value()/xStop);
//*** Compilation is okay if previous line is substituted with:
// Eigen::Array<double,1,2> y; y << 1.0, 2.0;
ret.value() = y(0,0);
ret.derivatives() = x.derivatives()*y(0,0);
return ret;
}
int main() {
DerType x(1.0,DerType::DerType(1.0,1.0));
auto y = f(x);
std::cout << "\nValue=" << y.value() << "\nDer=" << y.derivatives() << '\n';
return 0;
}
/*
Local Variables:
compile-command: "g++ -g -std=c++11 -I/usr/local/include/eigen3 eigenInterpolAD.cc -o a.exe && (echo \"Running\"; ./a.exe);"
End:
*/
遗憾的是,代码的编译给出了以下错误消息:
g++ -g -std=c++11 -I/usr/local/include/eigen3 eigenInterpolAD.cc -o a.exe && (echo "Running"; ./a.exe);
In file included from /usr/local/include/eigen3/Eigen/Core:254:0,
from /usr/local/include/eigen3/Eigen/Dense:1,
from /usr/local/include/eigen3/Eigen/Eigen:1,
from eigenInterpolAD.cc:5:
/usr/local/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In instantiation of ‘static void Eigen::PlainObjectBase<Derived>::_check_template_params() [with Derived = Eigen::Array<double, 1, -1, 0, 1, 2>]’:
/usr/local/include/eigen3/Eigen/src/Core/Array.h:195:36: required from ‘Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Array(const Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&) [with _Scalar = double; int _Rows = 1; int _Cols = -1; int _Options = 0; int _MaxRows = 1; int _MaxCols = 2]’
eigenInterpolAD.cc:33:48: required from here
/usr/local/include/eigen3/Eigen/src/Core/util/StaticAssert.h:32:40: error: static assertion failed: INVALID_MATRIX_TEMPLATE_PARAMETERS
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
^
/usr/local/include/eigen3/Eigen/src/Core/PlainObjectBase.h:657:7: note: in expansion of macro ‘EIGEN_STATIC_ASSERT’
EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
^
我怎样才能避免错误?在此先感谢您提供任何有用的提示。
特征版本是 3.2.1。对于 Eigen 的 3.1 和 3.0 版本,编译也会失败。
编译器版本为:
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)