Trying to replace the first row of a matrix with some expression, similar to my MATLAB code:
%Matrix A defined and Ny
A(1,:) = (-1).^(1:Ny 1).*(0:Ny).^2;
A(Ny 1,:) = (0:Ny).^2;
The C code I wrote is:
static const int ny = 10;
Eigen::VectorXd i = VectorXd(ny 1);
std::iota(i.begin(), i.end(), 0);
//matrix A is also defined correctly here
A.row(0) = pow(-1,(i))*(pow(i,2)); //error here
std::cout << A.row(0) << "\n";
can I get this done without having to rewrite it in a for loop, I am guessing that would require me changing the initialization of i
The full error message:
Test.cpp:329:25: error: no matching function for call to ‘pow(int, Eigen::VectorXd&)’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
| ^~~
/usr/include/c /9/complex:1885:5: note: template argument deduction/substitution failed:
Test.cpp:329:25: note: ‘Eigen::VectorXd’ {aka ‘Eigen::Matrix<double, -1, 1>’} is not derived from ‘const std::complex<_Up>’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
/mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/src/Core/GlobalFunctions.h:175:3: note: template argument deduction/substitution failed:
Test.cpp:329:35: note: mismatched types ‘const Eigen::ArrayBase<ExponentDerived>’ and ‘int’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
And it keeps repeating similar errors. I tried initializing i with Eigen::Matrix< double, 1, ny 1>
instead but I get the same error.
%%%%%%%%%%%%%%%%%%%%%
Edit:
Currently I have this C code working, but I would like to make use of the operators .row()
and .col()
:
for (int i = 0; i < ny 1; i ){
for (int j = 0; j < ny 1; j ){
A(0,j) = -1. * pow(-1,(j))*(pow(j,2));
A((ny),j) = 1. * pow(j,2);
}
}
std::cout << A << "\n";
This replaces the first and last rows of the matrix A
CodePudding user response:
This is how I fixed the problem with help from @Sedenion in the comments:
Eigen::ArrayXi exponents((ny 1));
exponents = Eigen::ArrayXi::LinSpaced((ny 1), 0, (ny 1));
A.row(0) = -1. * (Eigen::pow(-1., exponents.cast<double>())) * (Eigen::pow(exponents.cast<double>(),2)) ; //first row
A.row((ny)) = 1. * (Eigen::pow(exponents.cast<double>(),2)) ; //last row