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

Bug: Inputing asDiagonal to MatrixBase function

Tags: None
(comma "," separated)
User avatar
emilf
Registered Member
Posts
29
Karma
0
OS
Hi, I think I have found a bug.

This simple code will show it:
Code: Select all
#include <Eigen/Dense>

template <typename T>
void test(Eigen::MatrixBase<T> &t)
{
}

int main(int argc, char *argv[])
{
  Eigen::Vector3d t = Eigen::Vector3d::Zero();
  test(t.asDiagonal());
}

Which produces the following error:
main.cpp:47:3: error: no matching function for call to 'test'
test(t.asDiagonal());
^~~~
main.cpp:23:6: note: candidate template ignored: could not match 'MatrixBase' against 'DiagonalWrapper'
void test(Eigen::MatrixBase<T> &t)
^
1 error generated.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This is on purpose, MatrixBase is a base class for *dense* matrices, whereas asDiagonal() returns a diagonal matrix which has a special structure. If your function can properly deal with both dense and diagonal matrices, then use EigenBase (ok EigenBase is even more general...), or you need to explicitly convert the argument to a dense one: test(MatrixXd(t.asDiagonal()))
User avatar
emilf
Registered Member
Posts
29
Karma
0
OS
That seems a bit strange?
How can I then hint to the underlying expression templates that only the diagonal of the dense matrix has values, the rest are zeros?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You can use a DiagonalMatrix instead of a dense one, and if you already have a dense one then:

Code: Select all
MatrixXd mat;
VectorXd a, b, c;
mat = a.asDiagonal();
c = mat.diagonal().asDiagonal() * b;


Bookmarks



Who is online

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