Registered Member
|
Hello,
I am replacing a no-longer-supported Linear Algebra library (RogueWave Math.h++) with Eigen in a large simulation. There are some matrix and vector functions that I require that aren't in Eigen, such as abs() and sort(). These functions are called with varying argument types (e.g. VectorXd, VectorXi, MatrixXd, etc.), so I need a templatized function to handle all cases. An example with the method I attempted is:
The above templatized abs() works if I explicitly state the type. But it does not handle "x1-x2" and "A*x" as arguments when the type isn't explicitly stated. I'd be happy to use the above and explicitly state the type (e.g. abs<VectorXf>(x1-x2) ), if abs(x1-x2) yielded a compile-time or run-time error. The problem is, it doesn't. Instead, the program just stops. (I believe it gets a stack overflow due to infinite recursion, as the CwiseBinarOp<> doesn't support calling vabs(i) in the function abs().) Eventually, one of our developers will write "abs(x1-x2)", and the simulation will just stop in a manner that will be very difficult to debug. Any suggestions this community has will be very appreciated! |
Registered Member
|
As you realized, the expression A*x1 has type Product<...>. Your function should return an object of type VectorXd. This type is available as the member PlainMatrixType. Here is a version of your abs function with minimal changes that should work.
Incidentally, the absolute value exists in Eigen 2.0 as Cwise::abs(); see http://eigen.tuxfamily.org/dox/classEigen_1_1Cwise.html#abf73276fc968045bd7d6b9dbb1fe3b72 So you can write
You may need to "#include <Eigen\Array>". |
Registered Member
|
Sweet! PlainMatrixType worked perfectly and is exactly what I needed to know. And also good to know about the additional cwise() functionality that I had overlooked. Many, many thanks!
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]