![]() Registered Member ![]()
|
Hi,
I have the following problem where I use in many parts of my code a multiplication of three matrices A * B * C. The thing is that A and/or C can (this is known only at execution time) be the Identity matrix (ideally it would be alpha * Id) or a dense matrix. For the moment, what I do is to use by convention an empty matrix (size 0 x 0) whenever the matrix is to be considered as identity, but this is far from ideal: it makes the code harder to read, I might introduce more errors by duplicating parts of the code. I am currently writing a new class deriving from EigenBase, but before starting to really code this, would there be a simpler solution? Thanks Benjamin |
![]() Moderator ![]()
|
the Eigen way would be to have a templates function that you would call with either a Matrix or a DiagonalMatrix, or an Identity object depending on the context. On the other hand if that cannot work for you and that the identity status has to be stored by a Matrix then then best is indeed to write a small wrapper on top of Matrix adding a flag (+ a scale?) with a simple operator* branching at runtime.
|
![]() Registered Member ![]()
|
Hi,
thanks for your answer. I cannot go for the template function way since some of these matrices are returned by functions that switch between one or the other at evaluation time. I started to implement (copying bits from SparseMatrix), but is there any documentation of how to extend Eigen with custom matrix types? One thing I could not figure out is: Is there any evaluation time processing of the parse tree (say, to multiply in an order that will minimize the cost). If yes, how can this cost is determined? Thanks Benjamin |
![]() Moderator ![]()
|
perhaps it would be simpler to derived from Matrix and only add the missing feature. An alternative would be to extend Matrix using EIGEN_MATRIX_PLUGIN (http://eigen.tuxfamily.org/dox-devel/To ... Eigen.html)
this will be for 3.2 once we get the evaluators finalized. |
![]() Registered Member ![]()
|
Hi,
just to let you know what I did, in case this can be of use of somebody else. I defined a new template class template<typename T1, typename T2> class AltMatrix : public AltMatrixBase< AltMatrix<T1, T2> > where T1 and T2 are two Eigen types (in my case, identity or dense). When building the expression, all the possible cases are considered and are generated through various templates instantiations. The result is that it acts as a switch (a few boolean checks), before using the expression that would have been generated by Eigen. The current implementation is not complete (I just implemented the operators I needed), and can be seen at: https://github.com/bpiwowar/kqp/blob/ma ... matrix.hpp Benjamin |
Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]