I am trying to switch my code and start using Eigen library in C++ since I have heard it is really good with matrices. Now my old C++ code used mostly fftw_malloc to initialize my arrays like the following:
static const int nx = 10;
static const int ny = 10;
double *XX;
XX = (double*) fftw_malloc(nx*ny*sizeof(double));
memset(XX, 42, nx*ny* sizeof(double));
for(int i = 0; i< nx; i++){
for(int j = 0; j< ny+1; j++){
XX[j + ny*i] = (i)*2*M_PI/nx;
}
}
So I changed it to the following,
Matrix <double, nx, ny> eXX;
eXX.setZero();
for(int i = 0; i< nx; i++){
for(int j = 0; j< ny+1; j++){
eXX[j + ny*i] = (i)*2*EIGEN_PI/nx;
}
}
This however returns the error:
In file included from /mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/Core:164,
from /mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/Dense:1,
from Test.cpp:21:
/mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/src/Core/DenseCoeffsBase.h: In instantiation of ‘Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator[](Eigen::Index) [with Derived = Eigen::Matrix<double, 10, 10>; Eigen::DenseCoeffsBase<Derived, 1>::Scalar = double; Eigen::Index = long int]’:
Test.cpp:134:16: required from here
/mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/src/Core/DenseCoeffsBase.h:408:36: error: static assertion failed: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD
408 | EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
| ^~~~~~~~~~~~~~~~~~~~~
/mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/src/Core/util/StaticAssert.h:33:54: note: in definition of macro ‘EIGEN_STATIC_ASSERT’
33 | #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
| ^
I see the error is pointing at line: eXX[j + ny*i] = (i)*2*EIGEN_PI/nx;
I am fairly new to Eigen and still trying to understand my way around so any feedback/suggestions would help. Thanks.