Re: [eigen] Diagonal |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] Diagonal*From*: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>*Date*: Thu, 19 Aug 2010 20:26:56 -0400*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type; bh=lB4bbuBO8TwnUhqzd0+3+i3OQlm2BqeqJNcnqXm0Z1Q=; b=K/2X6nJeYyiq4ax5ObvET9t7rLOZ/goDlOL4cMlzsd0BHDsnhrT446ws6lLknDB29Y Ry3cZjRCkG6EAc8s256Congh8z1Yg70A+z1u6DJ5X33VaRAETX+h16GUWoCx6QLCIQEf Qpqc2iGOWD+HdJ12r2JwX5PEVp5Z5H0745O84=*Domainkey-signature*: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=PinDoTXy6Ve0s5m4YnsffDqJyCGi1jRJM4GQCmhl9mckzx/ntDrNSvjImfyJwSaUPp jio5kRx5G8e9kbMWpQuKq26lXUPU5hcck2wwP6HEYIS2+TYt5yU2+V7FADEQ+4ZpcyJ/ GV0rXmmHcdJYDCbCQ9mhz8dqobtJqmzm5WAr4=

2010/8/19 Will S <billy.baloop@xxxxxxxxx>

First of all, .diagonal() doesn't return a diagonal matrix at all, it only returns the column-vector consisting of all diagonal coefficients.

We do have a DiagonalMatrix class, and a method vector.asDiagonal() turning a vector into a diagonal matrix, however, it is one of those "special matrix classes" that support only a rather small subset of operations. General arithmetic is not supported on them, essentially because it would be slow. So,

someMatrix - someDiagonalMatrix

won't work with Eigen, because there really is no way that the resulting _expression_ could be fast to evaluate as a dense _expression_. Indeed, if you try to compute the (i,j)-th coefficient of that matrix, you have to do a runtime branching:

if (i == j)

return someMatrix(i,i) - someDiagonalMatrix(i,i);

else

return someMatrix(i,j);

Since that couldn't be made efficient, we didn't implement it and restricted DiagonalMatrix to support only the operations that are actually efficient on it (for example, products).

Coming back to your use case, if you need to compute

R = A - diag;

your best bet is to do:

R = A;

R.diagonal() -= diag;

Cheers,

Benoit

So, I've retrieved the diagonal of a square nxn MatrixXd A with:

Diagonal diag = A.diagonal() ;

however oddly diag is a column vector and I cannot use it in

operations (like R = A - diag).

Can I use a diagonal matrix in regular MatrixXd operations?

First of all, .diagonal() doesn't return a diagonal matrix at all, it only returns the column-vector consisting of all diagonal coefficients.

We do have a DiagonalMatrix class, and a method vector.asDiagonal() turning a vector into a diagonal matrix, however, it is one of those "special matrix classes" that support only a rather small subset of operations. General arithmetic is not supported on them, essentially because it would be slow. So,

someMatrix - someDiagonalMatrix

won't work with Eigen, because there really is no way that the resulting _expression_ could be fast to evaluate as a dense _expression_. Indeed, if you try to compute the (i,j)-th coefficient of that matrix, you have to do a runtime branching:

if (i == j)

return someMatrix(i,i) - someDiagonalMatrix(i,i);

else

return someMatrix(i,j);

Since that couldn't be made efficient, we didn't implement it and restricted DiagonalMatrix to support only the operations that are actually efficient on it (for example, products).

Coming back to your use case, if you need to compute

R = A - diag;

your best bet is to do:

R = A;

R.diagonal() -= diag;

Cheers,

Benoit

**References**:**[eigen] Diagonal***From:*Will S

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] Diagonal** - Next by Date:
**[eigen] Optimization Algorithms** - Previous by thread:
**[eigen] Diagonal** - Next by thread:
**[eigen] Optimization Algorithms**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |