This forum has been archived. All content is frozen. Please use KDE Discuss instead.

2D Pointer to Eigen Matrix

Tags: None
(comma "," separated)
ghernandez
Registered Member
Posts
1
Karma
0

2D Pointer to Eigen Matrix

Tue Sep 09, 2014 4:01 pm
Hi All,
I have this code that creates a 2,000 by 2,000 matrix (2D pointer), xran[i][j]. I want to take the eigenvalues and eigenvectors of xran[i][j] using EIGEN but I don't know how to go from a 2d pointer to an Eigen matrix MatrixXd.

int main()
{

int N=2000;
double alpha=40;
double *x;
x= new double[N];
double *y;
y=new double[N];


//Itilialize Array
double **xran = new double *[N];
for (int i=0; i<N; i++){
xran[i]= new double[N]; }
for (int i=0; i<N; i++) {
for(int j=0;j<N; j++) {
double a=0;
xran[i][j]= a;}
}

for (int i=0;i<N; i++) {
for ( int j=0; j<N; j++) {
xran[i][j]=exp(-alpha*sqrt( (x[i]-x[j]) * (x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) ));
myfile << xran [i][j]<< " "; }
myfile<<endl;
}


for (int i=0; i<N; i++){
delete [] xran[i];}
delete [] xran;
}


Thanks
GH
jitseniesen
Registered Member
Posts
204
Karma
2

Re: 2D Pointer to Eigen Matrix

Wed Sep 10, 2014 8:47 am
There is no easy way to go from a "2D pointer" to an Eigen matrix. Eigen matrices store the matrix entries in one big array. Depending on your situation, you can take these approaches:
  • Copy the data from the 2D pointer to an Eigen matrix. This does waste some time and memory, but that should be very small compared to the eigenvalue computation you are doing otherwise.
  • Change the code for constructing the matrix to construct it directly as an Eigen matrix.
  • Change the code for constructing the matrix to construct it in a 1D array and then use Map.
  • There is a hard way to go from a 2D pointer to an Eigen matrix, and that is by writing an Eigen expression class yourself similar to Map. This does require studying the internals of Eigen, and it will probably break in the next version because we are changing the internals of Eigen.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]