3

I use Eigen for most of my code but I would like to use Miser or Vegas monte-carlo integration from GSL. I need to convert Eigen's vectors to c arrays of doubles what would be the best way to do it?

Matrix<double,3,1> --> c_array []
kirill_igum
  • 3,953
  • 5
  • 47
  • 73

1 Answers1

5

I have worked with Eigen before...

Usually, for simple access to the internal array data, like mentioned by user janneb in this thread, you just want to invoke data():

Vector3d v;
// Operations to add values to the vector.
double *c_ptr = v.data();

If you wish to iterate the individual values to perform some operation, you want to iterate every line (.row(index)) and column (.col(index)), depending on the matrix order that you want to lay down in the destination vector.

In your specific example, you need only iterate the rows:

Matrix<double,3,1> --> c_array []

You would want to call .col(0). Should similar needs arise, the specific documentation is always helpful!

So you would end up with something like (assuming you wish to use a three-line single-column matrix):

Vector3d v;
// Operations to add values to the vector.
for (int i=0; i<v.rows(); ++i)
  c_array[i] = v(i,0);

Hope that helped.

Community
  • 1
  • 1
Blitzkoder
  • 1,768
  • 3
  • 15
  • 30