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

Diagonal matrix question

Tags: None
(comma "," separated)
bravegag
Registered Member
Posts
52
Karma
0

Diagonal matrix question

Thu Sep 05, 2013 7:23 am
Hello,

In the context of ridge regression I need to compute:
Code: Select all
(X*X^T + lambda*Identity)

where lambda is a scalar. In order to compute the "lambda * Identity" I did:
Code: Select all
MatrixXd reg_I = lambda * MatrixXd::Identity(m, n);

but this turns out to be slow (and unnecessary) i.e. multiplying by a bunch of zeros. I then tried:
Code: Select all
DiagonalMatrixXd reg_I(m, n);
reg_I.identity();
reg_I *= lambda;

but apparently I can't combine DiagonalMatrix and MatrixXd in the main expression? I get a compilation error for this:
Code: Select all
(X*X^T + reg_I)

Then the question is how can I do this elegantly and efficiently in Eigen and by elegantly I mean simply not compromising on the code readability and end up writing a loop over the diagonal elements of X*X^T and adding lambda.

TIA,
Best regards,
Giovanni

Last edited by bravegag on Thu Sep 05, 2013 8:16 am, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Diagonal matrix question  Topic is solved

Thu Sep 05, 2013 8:00 am
First compute X2 = X*X^T and then do:
Code: Select all
X2.diagonal().array() -= lambda;

This is what X*X^T - reg_I should do for you in the future.
bravegag
Registered Member
Posts
52
Karma
0

Re: Diagonal matrix question

Thu Sep 05, 2013 8:10 am
hi ggael,

Always excellent support, Thank you :)

I will miss having the visible ridge formula but the performance improvement really does justify it.

Cheers,
Giovanni


Bookmarks



Who is online

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