Registered Member
|
Hi,
I have a MatrixXd and I want to pass it to a function that expects a Matrix3Xd&. The original MatrixXd has already been resized to 3 rows and n cols. I can do that by constructing a new object Matrix3Xd from the MatrixXd and passing this to the function, but that's slower than passing the original MatrixXd. Alternatively, I could do the function to expect a MatrixBase&. However, I chose to use a Matrix3Xd because I thought that that's more efficient later on when I have to multiply this Matrix3Xd by a Matrix3d within the function. So my question is, is it more efficient to use Matrix3Xd (as oposed to MatrixXd) when multiplying with a Matrix3d? If so, how can I convert the MatrixXd into Matrix3Xd without having to make a copy. Any other design suggestions are also appreciated. Martin. |
Moderator
|
If you are multiplying a 3x3 matrix by a KxN one then we know at compile time that K must actually be equal to 3 even if K==Dynamic. So here, using a 3xDynamic or Dynamic x Dynamic matrix boils down to the same for this precise operation.
|
Registered Member
|
Ok, if it is the same then I'll use MatrixXd everywhere, then I won't need any conversions between MatrixXd and Matrix3Xd.
Just as a curiosity, it is possible to cast a Matrix3Xd into a MatrixXd and the other way round? The only methods I found so far is copying the data or using a Map object, and I would preffer to do neither. Thanks, Martin |
Moderator
|
Note that that boils down to same here only because you are multiplying it by a static 3x3 matrix and so the number of rows of your MatrixXd matrix can be inferred at compile time.
There is no easy way to view a MatrixXd as a Matrix3Xd. But if you know that it is a Matrix3Xd why do you use a MatrixXd? Nevertheless the following works too: MatrixXd m; Block<MatrixXd,3,Dynamic> m1(m,0,0,3,m.cols); but that's not easier than using Map. Another option could be: MatrixXd m(3,n) = ....; Matrix3Xd m1; m1.swap(m); // use m1 as an optimized 3xN object, m is an empty object. // restore m: m1.swap(m); Currently, this still perform a copy of the data because we swap the data pointers if and only if the two types are exactly the same, but this mechanism could easily be extended to swap the pointers anytime the matrix holds heap allocated memory. |
Registered Member
|
Hi ggael,
Thanks for your suggestions. Now that I turned all my Matrix3Xd to MatrixXd I remembered why did I use Matrix3Xd in the first instance. At some point in the code I do: Eigen::Transform<double, 3, Eigen::Affine> transform; transform.fromPositionOrientationScale(...,...); pointsDst.noalias() = transform * pointsSrc; if pointsSrc is Matrix3Xd it works well, if it is MatrixXd (with 3 rows and N cols) is produces an assertion saying YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES. Any more suggestions for this? Thanks, Martin |
Registered Member
|
Oh, about your question if I know that it is a Matrix3Xd why do I use a MatrixXd. I use a MatrixXd because these points come out of PCA model in a column vector, then I reshape the vector to have 3 rows N cols.
|
Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient