I want to use Eigen to do some calculation in some C-style code, the function interface has a raw pointer as below,
#include <iostream>
#include <memory>
#include <Eigen/Dense>
using namespace Eigen;
typedef Eigen::Matrix<double, -1, -1, Eigen::RowMajor> Mrow;
void compute_with_Eigen(double * p_data, int row, int col)
{
// Q1: is there any data copy here?
Eigen::MatrixXd Mc = Eigen::Map<Mrow>(p_data, row, col);
// do computations with Mc, for example
auto M_temp = Mc.inverse();
Mc = M_temp;
// Q2: why is this assign-back necessary?
Eigen::Map<Mrow>( p_data, row, col ) = Mc;
}
int main()
{
std::unique_ptr<double[]> p(new double[10]);
for (int i = 0; i < 9; ++i)
{
p[i]=i+1.0;
std::cout<<p[i]<<std::endl;
}
compute_with_Eigen(p.get(),3,3);
std::cout<<"after inverse\n";
for (int i = 0; i < 10; ++i)
std::cout<<p[i]<<std::endl;
}
I have Question1 because the accepted answer in this thread suggests there are some copy, however, in principle a "view" shouldn't copy anything.
I have Question2 because otherwise the result is not as expected, however this is not like a "view"(also see this answer) if I really have to assign back