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

[SOLVED] Tripping over laziness and .adjoint()

Tags: None
(comma "," separated)
moritz
Registered Member
Posts
12
Karma
0
Consider this small program:

Code: Select all
#include
#include
#include
#include
using namespace std;

USING_PART_OF_NAMESPACE_EIGEN

typedef double num;
typedef complex cnum;
typedef Eigen::SparseMatrix esm;

int main(int argc, char** argv) {
   MatrixXcd foo(3, 3);
   foo(0, 2) = cnum(2, 3);
   cout << foo << endl;

   foo = foo.adjoint();
   cout << "adjoint: n";

   cout << foo << endl;
}


And the output:

Code: Select all
(0,0) (0,0) (2,3)
(0,0) (0,0) (0,0)
(0,0) (0,0) (0,0)
adjoint:
(0,-0) (0,0) (2,3)
(0,-0) (0,-0) (0,0)
(2,-3) (0,-0) (0,-0)


When I use .adjoint().eval() instead of just .adjoint() the (2,3) in the upper right corner goes away, as I'd expect it to. But shouldn't eigen autodetect that? If not, a warning in the documentation / in the tutorial would be appropriate, IMHO.

Cheers,
Moritz
User avatar
bjacob
Registered Member
Posts
658
Karma
3
You are right that a warning in the documentation is much needed. In fact, we have a transposeInPlace() method but we are lacking adjointInPlace(). I take care of that now, thanks.

There is no reasonable way that Eigen could detect that automatically for you:

* first, we don't want to put an eval-before-assign flag on the adjoint expression, because it is very cheap to evaluate so the relative cost of the redundant copy would be high in "a = b.adjoint();"

* second, we can't test if a != b to determine whether to evaluate because that test by itself would be a constant overhead, and because it would be impossible to do in a completely reliable way (e.g. what if a is a Map on b's data...)

For example, for matrix products "m = m*m;" Eigen always evaluates-before-assign because the product expression is so costly that the potentially useless copy is relatively cheap. Here the difference is that adjoint() is a lightweight expression.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
moritz
Registered Member
Posts
12
Karma
0
[quote='bjacob' pid='57251' dateline='1238500669']
You are right that a warning in the documentation is much needed. In fact, we have a transposeInPlace() method but we are lacking adjointInPlace(). I take care of that now, thanks.

Great, thank you.

For example, for matrix products "m = m*m;" Eigen always evaluates-before-assign because the product expression is so costly that the potentially useless copy is relatively cheap. Here the difference is that adjoint() is a lightweight expression.


Ah, I wondered what kind of magic autodetection it uses to make m = m * m work, but that of course explains it :-)

Thanks,
Moritz


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell