Неправильные аргументы шаблона для Eigen :: Spline с Eigen :: AutoDiff

Решение от 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. Компиляция также терпит неудачу с версиями 3.1 и 3.0 Eigen.

Версия компилятора:

gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)

1

Решение

Как пользователь Anycorn Указанный исходный код в разделе «Старый вопрос» компилируется и запускается с самой последней версией Eigen на Bitbucket:

$hg showconfig
bundle.mainreporoot=/usr/local/eigen
paths.default=https://bitbucket.org/eigen/eigen
/usr/local/eigen
$hg identify
fabd880592ac tip
/usr/local/eigen

Я проверил принцип с помощью следующего кода:

/**
Spline interpolation with AD.
*/
#include <iostream>
#include <fstream>
#include <Eigen/Eigen>
#include <unsupported/Eigen/AutoDiff>
#include <unsupported/Eigen/Splines>

constexpr const 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 Eigen::Spline<double,nCols-1> Spline;

constexpr size_t interpolOrder=3;
constexpr double xStart=array[0];
constexpr double xStop=array[nCols*(rowSize-1)];
constexpr double xDelta=xStop-xStart;

const Spline spline=Eigen::SplineFitting<Spline>::Interpolate(Map(
const_cast<double*>(array)+1),
interpolOrder,
(Map(const_cast<double*>(array)).array()-xStart)/xDelta);

typedef Eigen::AutoDiffScalar<Eigen::Matrix<double,1,2> > DerType;

DerType f(const DerType& x){
DerType ret;
auto y = spline.derivatives<1>((x.value()-xStart)/xDelta);
ret.value() = y(0,0);
ret.derivatives() = x.derivatives()*y(0,1)/xDelta;
return ret;
}int main() {
std::ofstream of("/temp/test.dat");

constexpr size_t n=101;
const double xStart=0.0;
const double xStop=6.0;
const double dx=(xStop-xStart)/(n-1);

double x=xStart;

for(size_t i=0; i!=n; i++, x+=dx) {
DerType xAD(x,DerType::DerType(1.0,1.0));
auto yAD = f(xAD);

of << x << ' ' << yAD.value() << ' ' << yAD.derivatives()[0] << '\n';
}

of.close();

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:
*/

Следующий график сгенерированных данных показывает, что код теперь работает нормально с hg-версией Eigen. Красная кривая — это интерполирующий сплайн, а зеленая кривая — его производная.

введите описание изображения здесь

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector