Reply to topic

Create a matrix directly from an array of doubles?

hughperkins
Registered Member
Posts
6
Karma
0
Hi,

I have an array of doubles, and I want to create a MatrixXd from this. Is there any way of doing this without having to loop over the array and assign each value to the matrix?

How does eigen store the matrix internally? A single array of doubles, or a 2d array?
Dee33
Registered Member
Posts
54
Karma
0
OS
A matrix is stored internally as single array, one column after another (for Column-major)
So you can use col(j) to get read-write access to column j
http://eigen.tuxfamily.org/dox-devel/QuickRefPage.html
hughperkins
Registered Member
Posts
6
Karma
0
Ok, that gets me a ColXpr object? Then, that has a public pointer to the underlying array, so I can memcpy onto that?

Can I get a pointer to the very first element of the matrix, for example by doing &mat(0,0) and memcpy the entire array, all columns in one go, onto that?

Is there going to be any speed advantage of doing this, or is using a column-major loop to copy the elements one by one from the array via the mat(r,c) operator going to be about the same speed anyway?
manuels
Registered Member
Posts
47
Karma
0
hughperkins
Registered Member
Posts
6
Karma
0
Super! That was what I was looking for.

Jeigen wrapping overhead for 1000x1000 matrices is now down from 5.4% to 4.1%. https://github.com/hughperkins/jeigen/b ... jeigen.cpp

Anything similar for sparse matrices?
Dee33
Registered Member
Posts
54
Karma
0
OS
For sparse matrices, you have the class MappedSparseMatrix http://eigen.tuxfamily.org/dox-devel/cl ... atrix.html
Note that the Map classes use the same memory as the arrays you supply. So you should not free those arrays before you finish with the Map classes.

 
Reply to topic

Bookmarks



Who is online

Registered users: 16777216, alake, Baidu [Spider], Bing [Bot], Exabot [Bot], garthecho, Google [Bot], GordieGii, Hans, koriun, Majestic-12 [Bot], mmistretta, nezumi, onesandzeros, Yahoo [Bot]