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

Diagonal matrices

Tags: None
(comma "," separated)
stair314
Registered Member
Posts
8
Karma
0

Diagonal matrices

Sat Aug 08, 2009 5:42 am
I've found that in one of my programs, the asDiagonal() method is running in quadratic time. Is this likely to be because I am misunderstanding how to use it, or is it still not supported very well?
For example, should this code run in quadratic time?

void foo(VectorXf & v, MatrixXf & m)
{
m = v.asDiagonal();
}

I would expect it to run in time linear in the size of v, but I could see how if m is fixed as diagonal or not diagonal at compile time this would not actually happen.

Also if there's anything you could point me to that I could read that would help me answer such questions on my own in the future, that would be great.

Thanks in advance!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Diagonal matrices

Sat Aug 08, 2009 8:06 am
asDiagonal() by itself only constructs an abstract expression: as such, it returns in constant time.

But when you do:
Code: Select all
MatrixXf m = v.asDiagonal();

you're asking for the dense (MatrixXf is a dense matrix type) matrix m to be initialized as a diagonal matrix, which requires writing zeros everywhere outside the diagonal. That takes quadratic time.

In Eigen 2.0, there's no way around that.

In the development branch, you can now use a "diagonal matrix" type that stores only the diagonal. You would do:
Code: Select all
DiagonalMatrix<float, Dynamic> m = v.asDiagonal();


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
stair314
Registered Member
Posts
8
Karma
0

Re: Diagonal matrices

Sat Aug 08, 2009 6:10 pm
Is it possible to make a matrix that is agnostic at compile time as to whether it is dense or sparse? I'm writing a class that represents vector-valued functions, and can return the Jacobian at a specific point. Often the Jacobian is a diagonal matrix so there could be considerable savings in both time and memory if the class could return a sparse matrix, but I'd prefer that the client didn't have to worry about which type would be returned.
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Diagonal matrices

Sat Aug 08, 2009 6:54 pm
No, we don't have such a class.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
stair314
Registered Member
Posts
8
Karma
0

Re: Diagonal matrices

Sat Aug 08, 2009 7:08 pm
OK, thanks.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]