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

From double* into Eigen float vector

Tags: None
(comma "," separated)
mehrdadh
Registered Member
Posts
26
Karma
0
Hi,

I have a function where the inputs are double* (to the first element of an array, which is coming from Excel VBA).

I have lots of calculations in C++ using Eigen library which are based on floats. I would like to copy these input doubles into my Eigen::VectorXf (which are floats).

What is the best way to do this ? Do I have to loop through the array one-by-one and put them into my Eigen float vector? or is there another way to do this ? a command ?


Please note that I don't know whether the double* inputs from VBA are aligned or not, but I need my VectorXf to be aligned in memory.

Also I would like to have my VectorXf to be const. Do I have to cast to const afterwards ? and how ?

ps: my array sizes are not that big, most of them of size 4, or 16, and some of size 440 doubles.

Thanks in advance.
jitseniesen
Registered Member
Posts
204
Karma
2
mehrdadh wrote:I have a function where the inputs are double* (to the first element of an array, which is coming from Excel VBA).

I have lots of calculations in C++ using Eigen library which are based on floats. I would like to copy these input doubles into my Eigen::VectorXf (which are floats).

What is the best way to do this ? Do I have to loop through the array one-by-one and put them into my Eigen float vector? or is there another way to do this ? a command ?


You can use Map<VectorXd> to represent the array of doubles and then use the member function cast() to transform them into floats. The resulting code would look something like this:

Code: Select all
const VectorXf v = Map<VectorXd>(buf, n).cast<float>();

Here, buf is the array you get from Excel VBA and n is the number of elements in the array. By default, Map does not assume the data to be aligned.
mehrdadh
Registered Member
Posts
26
Karma
0
Oh thanks so much.

This is awesome :)

So in this code, my VectorXf, which is called v, is going to be aligned and const.

I think the more correct way is this (having const in Mapping):

Code: Select all
const VectorXf v = Map<const VectorXd>(buf, n).cast<float>();


Perfect! I am going to try it now.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]