Registered Member
|
Here is my template for both static and dynamic matrix.
I tested my template for both static matrix and dynamic matrix, and it gives right answers. But I don't know why my template is executed rightly for dynamic matrix. For dynamic matrix, Is my template really O.K ? Thanks in advance. template< typename Derived1, typename Derived2 > const Matrix<typename Derived1::Scalar, Derived1::RowsAtCompileTime, Derived1::ColsAtCompileTime, Derived1::Options> MkMatrixType( const MatrixBase<Derived1>& Mat1, const MatrixBase<Derived2>& Mat2 ) { if ( ( Derived1::RowsAtCompileTime == Derived2::RowsAtCompileTime ) && ( Derived1::ColsAtCompileTime == Derived2::ColsAtCompileTime ) ) return Mat1 + Mat2; }; int _tmain(int argc, _TCHAR* argv[]) { MatrixXd AA(3,3); MatrixXd BB(3,3); MatrixXd CC(3,3); AA.setRandom(); BB.setRandom(); cout << "AA=\n" << AA << endl; cout << "BB=\n" << BB << endl; CC = MkMatrixType( AA, BB ); cout << "CC=\n" << CC << endl; cout << "Must be zeros =\n" << CC-(AA+BB) << endl; return 0; } |
Registered Member
|
For dynamic matrices, RowsAtCompileTime and ColsAtCompileTime have the special value Dynamic, so the condition in the if statement evaluates to true.
Your code might not perform as expected in all cases. If you pass two dynamic matrices with unequal size, then the condition in the if statement still evaluates to true (for the same reason as above), but the addition will fail. If you pass a static matrix and a dynamic matrix with the same size, then the condition in the if statement will evaluate to false but the addition is still valid and will succeed. |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]