Registered Member
|
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. |
Registered Member
|
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:
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. |
Registered Member
|
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):
Perfect! I am going to try it now. |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]