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

Typecasting std::vector<double> to Eigen::VectorXd

Tags: None
(comma "," separated)
a1re
Registered Member
Posts
16
Karma
0
Good day,

I currently have code that uses std::vectors and am new to Eigen. Could you please inform me on how to typecast these std::vectors to Eigen::VectorXd? I don't want a copy of the data in the std::vector to be made. So far, I've only seen the 'Map' function but that maps arrays and not vectors.

Thank You
User avatar
bjacob
Registered Member
Posts
658
Karma
3
You can use Map for any kind of dense objects, like vectors.

VectorXf::Map(pointer, size)


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
a1re
Registered Member
Posts
16
Karma
0
Thanks. I figured it out. I was supposed to create a pointer to the vector as follows.


std::vector<double> v(4, 100.0);
double* ptr = &v[0];
Eigen::Map<Eigen::VectorXd> my_vect(ptr, 4);
msinvent
Registered Member
Posts
1
Karma
0
Is their a way to get to Eigen::VectorXd to std::vector<double> ? I searched the internet but all in vain till now. May be its something very basic.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I guess this is a cross-post, so for the record: http://stackoverflow.com/questions/2609 ... -stdvector
krisrose
Registered Member
Posts
6
Karma
0
Hi,

Sorry to dredge up an old post but my issue is related.

I am trying to convert a std::vector of complex doubles to a VectorXcd but am getting the error: "YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY"

As far as I can tell the scalar type should both be complex doubles? Here is my code:

VectorXcd v3 = VectorXcd::Map(&v2[10], v2.size());

I also tried Map<VectorXcd>v3(&v2[0], v2.size());

Thanks,

Kris
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your problem is probably somewhere else. Here is a self-contained working example:
Code: Select all
#include <Eigen/Dense>
#include <vector>
using namespace Eigen;
int main() {
  std::vector<std::complex<double> > v2(10);
  VectorXcd v3 = VectorXcd::Map(v2.data(), v2.size());
}
krisrose
Registered Member
Posts
6
Karma
0
ggael wrote:Your problem is probably somewhere else. Here is a self-contained working example:
Code: Select all
#include <Eigen/Dense>
#include <vector>
using namespace Eigen;
int main() {
  std::vector<std::complex<double> > v2(10);
  VectorXcd v3 = VectorXcd::Map(v2.data(), v2.size());
}



Thanks for this! I got it sorted, you were right it was related to an issue elsewhere in my code. :)


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], Google [Bot], lockheed, Sogou [Bot], Yahoo [Bot]