From 34c95029cad15201efb161a59d72f36cbf997f16 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 17 Dec 2009 10:00:35 +0100 Subject: [PATCH] a couple of fixes and cleaning --- Eigen/src/Core/AnyMatrixBase.h | 6 +-- Eigen/src/Core/DenseBase.h | 2 +- Eigen/src/Core/MatrixBase.h | 86 +++++++++---------------------- Eigen/src/Core/ProductBase.h | 22 +------- Eigen/src/Core/ReturnByValue.h | 2 +- Eigen/src/Core/SolveTriangular.h | 2 +- Eigen/src/Eigen2Support/Flagged.h | 23 ++++++++- test/linearstructure.cpp | 46 ++++++++--------- test/nomalloc.cpp | 2 +- 9 files changed, 79 insertions(+), 112 deletions(-) diff --git a/Eigen/src/Core/AnyMatrixBase.h b/Eigen/src/Core/AnyMatrixBase.h index 7c0268aac..6035585e8 100644 --- a/Eigen/src/Core/AnyMatrixBase.h +++ b/Eigen/src/Core/AnyMatrixBase.h @@ -104,7 +104,7 @@ template struct AnyMatrixBase * special matrix without having to modify MatrixBase */ template template -Derived& MatrixBase::operator=(const AnyMatrixBase &other) +Derived& DenseBase::operator=(const AnyMatrixBase &other) { other.derived().evalTo(derived()); return derived(); @@ -112,7 +112,7 @@ Derived& MatrixBase::operator=(const AnyMatrixBase &other template template -Derived& MatrixBase::operator+=(const AnyMatrixBase &other) +Derived& DenseBase::operator+=(const AnyMatrixBase &other) { other.derived().addToDense(derived()); return derived(); @@ -120,7 +120,7 @@ Derived& MatrixBase::operator+=(const AnyMatrixBase &othe template template -Derived& MatrixBase::operator-=(const AnyMatrixBase &other) +Derived& DenseBase::operator-=(const AnyMatrixBase &other) { other.derived().subToDense(derived()); return derived(); diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index 4735cca50..ea6383998 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -434,7 +434,7 @@ template class DenseBase * Combined with coeffRef() and the \ref flags flags, it allows a direct access to the data * of the underlying matrix. */ - inline int stride(void) const { return derived().stride(); } + inline int stride() const { return derived().stride(); } inline const NestByValue nestByValue() const; inline const ForceAlignedAccess forceAlignedAccess() const; diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 14be84314..4c7113602 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -118,25 +118,6 @@ template class MatrixBase * \sa rows(), cols(), SizeAtCompileTime. */ inline int diagonalSize() const { return std::min(rows(),cols()); } - /** Only plain matrices, not expressions may be resized; therefore the only useful resize method is - * Matrix::resize(). The present method only asserts that the new size equals the old size, and does - * nothing else. - */ - void resize(int size) - { - ei_assert(size == this->size() - && "MatrixBase::resize() does not actually allow to resize."); - } - /** Only plain matrices, not expressions may be resized; therefore the only useful resize method is - * Matrix::resize(). The present method only asserts that the new size equals the old size, and does - * nothing else. - */ - void resize(int rows, int cols) - { - ei_assert(rows == this->rows() && cols == this->cols() - && "MatrixBase::resize() does not actually allow to resize."); - } - #ifndef EIGEN_PARSED_BY_DOXYGEN /** \internal the plain matrix type corresponding to this expression. Note that is not necessarily * exactly the return type of eval(): in the case of plain matrices, the return type of eval() is a const @@ -180,41 +161,24 @@ template class MatrixBase ei_traits::ColsAtCompileTime> BasisReturnType; #endif // not EIGEN_PARSED_BY_DOXYGEN - #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase - #include "../plugins/CommonCwiseUnaryOps.h" - #include "../plugins/CommonCwiseBinaryOps.h" - #include "../plugins/MatrixCwiseUnaryOps.h" - #include "../plugins/MatrixCwiseBinaryOps.h" - #undef EIGEN_CURRENT_STORAGE_BASE_CLASS +#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase +# include "../plugins/CommonCwiseUnaryOps.h" +# include "../plugins/CommonCwiseBinaryOps.h" +# include "../plugins/MatrixCwiseUnaryOps.h" +# include "../plugins/MatrixCwiseBinaryOps.h" +# ifdef EIGEN_MATRIXBASE_PLUGIN +# include EIGEN_MATRIXBASE_PLUGIN +# endif +#undef EIGEN_CURRENT_STORAGE_BASE_CLASS /** Special case of the template operator=, in order to prevent the compiler * from generating a default operator= (issue hit with g++ 4.1) */ Derived& operator=(const MatrixBase& other); - template - Derived& operator=(const AnyMatrixBase &other); - - template - Derived& operator+=(const AnyMatrixBase &other); - - template - Derived& operator-=(const AnyMatrixBase &other); - - template - Derived& operator=(const ReturnByValue& func); - #ifndef EIGEN_PARSED_BY_DOXYGEN template Derived& lazyAssign(const ProductBase& other); - - template - Derived& operator+=(const Flagged, 0, - EvalBeforeAssigningBit>& other); - - template - Derived& operator-=(const Flagged, 0, - EvalBeforeAssigningBit>& other); #endif // not EIGEN_PARSED_BY_DOXYGEN const CoeffReturnType x() const; @@ -306,10 +270,18 @@ template class MatrixBase RealScalar prec = precision()) const; bool isUnitary(RealScalar prec = precision()) const; + /** \returns true if each coefficients of \c *this and \a other are all exactly equal. + * \warning When using floating point scalar values you probably should rather use a + * fuzzy comparison such as isApprox() + * \sa isApprox(), operator!= */ template inline bool operator==(const MatrixBase& other) const { return cwiseEqual(other).all(); } + /** \returns true if at least one pair of coefficients of \c *this and \a other are not exactly equal to each other. + * \warning When using floating point scalar values you probably should rather use a + * fuzzy comparison such as isApprox() + * \sa isApprox(), operator== */ template inline bool operator!=(const MatrixBase& other) const { return cwiseNotEqual(other).all(); } @@ -328,13 +300,6 @@ template class MatrixBase NoAlias noalias(); - /** \returns number of elements to skip to pass from one row (resp. column) to another - * for a row-major (resp. column-major) matrix. - * Combined with coeffRef() and the \ref flags flags, it allows a direct access to the data - * of the underlying matrix. - */ - inline int stride(void) const { return derived().stride(); } - inline const NestByValue nestByValue() const; inline const ForceAlignedAccess forceAlignedAccess() const; inline ForceAlignedAccess forceAlignedAccess(); @@ -439,11 +404,15 @@ template class MatrixBase template void applyOnTheRight(int p, int q, const PlanarRotation& j); - #ifdef EIGEN_MATRIXBASE_PLUGIN - #include EIGEN_MATRIXBASE_PLUGIN - #endif - #ifdef EIGEN2_SUPPORT + template + Derived& operator+=(const Flagged, 0, + EvalBeforeAssigningBit>& other); + + template + Derived& operator-=(const Flagged, 0, + EvalBeforeAssigningBit>& other); + /** \deprecated because .lazy() is deprecated * Overloaded for cache friendly product evaluation */ template @@ -457,11 +426,6 @@ template class MatrixBase inline const Cwise cwise() const; inline Cwise cwise(); - // a workaround waiting the Array class -// inline const Cwise array() const { return cwise(); } - // a workaround waiting the Array class -// inline Cwise array() { return cwise(); } - template typename ei_plain_matrix_type_column_major::type solveTriangular(const MatrixBase& other) const; diff --git a/Eigen/src/Core/ProductBase.h b/Eigen/src/Core/ProductBase.h index 2cac09230..2df864458 100644 --- a/Eigen/src/Core/ProductBase.h +++ b/Eigen/src/Core/ProductBase.h @@ -223,27 +223,9 @@ template template Derived& MatrixBase::lazyAssign(const ProductBase& other) { - other.derived().evalTo(derived()); return derived(); + other.derived().evalTo(derived()); + return derived(); } -/** \internal - * Overloaded to perform an efficient C += (A*B).lazy() */ -template -template -Derived& MatrixBase::operator+=(const Flagged, 0, - EvalBeforeAssigningBit>& other) -{ - other._expression().derived().addTo(derived()); return derived(); -} - -/** \internal - * Overloaded to perform an efficient C -= (A*B).lazy() */ -template -template -Derived& MatrixBase::operator-=(const Flagged, 0, - EvalBeforeAssigningBit>& other) -{ - other._expression().derived().subTo(derived()); return derived(); -} #endif // EIGEN_PRODUCTBASE_H diff --git a/Eigen/src/Core/ReturnByValue.h b/Eigen/src/Core/ReturnByValue.h index 1d977ace2..e35493236 100644 --- a/Eigen/src/Core/ReturnByValue.h +++ b/Eigen/src/Core/ReturnByValue.h @@ -72,7 +72,7 @@ template template template -Derived& MatrixBase::operator=(const ReturnByValue& other) +Derived& DenseBase::operator=(const ReturnByValue& other) { other.evalTo(derived()); return derived(); diff --git a/Eigen/src/Core/SolveTriangular.h b/Eigen/src/Core/SolveTriangular.h index 6e9dd30ec..618e29828 100644 --- a/Eigen/src/Core/SolveTriangular.h +++ b/Eigen/src/Core/SolveTriangular.h @@ -203,7 +203,7 @@ struct ei_triangular_solver_selector::marked() const } /** \deprecated use MatrixBase::noalias() - * + * * \returns an expression of *this with the EvalBeforeAssigningBit flag removed. * * Example: \include MatrixBase_lazy.cpp @@ -146,4 +146,25 @@ MatrixBase::lazy() const return derived(); } + +/** \internal + * Overloaded to perform an efficient C += (A*B).lazy() */ +template +template +Derived& MatrixBase::operator+=(const Flagged, 0, + EvalBeforeAssigningBit>& other) +{ + other._expression().derived().addTo(derived()); return derived(); +} + +/** \internal + * Overloaded to perform an efficient C -= (A*B).lazy() */ +template +template +Derived& MatrixBase::operator-=(const Flagged, 0, + EvalBeforeAssigningBit>& other) +{ + other._expression().derived().subTo(derived()); return derived(); +} + #endif // EIGEN_FLAGGED_H diff --git a/test/linearstructure.cpp b/test/linearstructure.cpp index ac61d108f..3e570f2a0 100644 --- a/test/linearstructure.cpp +++ b/test/linearstructure.cpp @@ -58,30 +58,30 @@ template void linearStructure(const MatrixType& m) VERIFY_IS_APPROX((-m1+m2)*s1, -s1*m1+s1*m2); m3 = m2; m3 += m1; VERIFY_IS_APPROX(m3, m1+m2); -// m3 = m2; m3 -= m1; -// VERIFY_IS_APPROX(m3, m2-m1); -// m3 = m2; m3 *= s1; -// VERIFY_IS_APPROX(m3, s1*m2); -// if(NumTraits::HasFloatingPoint) -// { -// m3 = m2; m3 /= s1; -// VERIFY_IS_APPROX(m3, m2/s1); -// } + m3 = m2; m3 -= m1; + VERIFY_IS_APPROX(m3, m2-m1); + m3 = m2; m3 *= s1; + VERIFY_IS_APPROX(m3, s1*m2); + if(NumTraits::HasFloatingPoint) + { + m3 = m2; m3 /= s1; + VERIFY_IS_APPROX(m3, m2/s1); + } -// // again, test operator() to check const-qualification -// VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c))); -// VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c))); -// VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); -// VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c))); -// VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1); -// if(NumTraits::HasFloatingPoint) -// VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1); -// -// // use .block to disable vectorization and compare to the vectorized version -// VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1); -// VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1)); -// VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1); -// VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1); + // again, test operator() to check const-qualification + VERIFY_IS_APPROX((-m1)(r,c), -(m1(r,c))); + VERIFY_IS_APPROX((m1-m2)(r,c), (m1(r,c))-(m2(r,c))); + VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); + VERIFY_IS_APPROX((s1*m1)(r,c), s1*(m1(r,c))); + VERIFY_IS_APPROX((m1*s1)(r,c), (m1(r,c))*s1); + if(NumTraits::HasFloatingPoint) + VERIFY_IS_APPROX((m1/s1)(r,c), (m1(r,c))/s1); + + // use .block to disable vectorization and compare to the vectorized version + VERIFY_IS_APPROX(m1+m1.block(0,0,rows,cols), m1+m1); + VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), m1.cwiseProduct(m1)); + VERIFY_IS_APPROX(m1 - m1.block(0,0,rows,cols), m1 - m1); + VERIFY_IS_APPROX(m1.block(0,0,rows,cols) * s1, m1 * s1); } void test_linearstructure() diff --git a/test/nomalloc.cpp b/test/nomalloc.cpp index f228519bf..0452de78d 100644 --- a/test/nomalloc.cpp +++ b/test/nomalloc.cpp @@ -64,7 +64,7 @@ template void nomalloc(const MatrixType& m) VERIFY_IS_APPROX((m1+m2)*s1, s1*m1+s1*m2); VERIFY_IS_APPROX((m1+m2)(r,c), (m1(r,c))+(m2(r,c))); - VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array())); + VERIFY_IS_APPROX(m1.cwiseProduct(m1.block(0,0,rows,cols)), (m1.array()*m1.array()).asMatrix()); if (MatrixType::RowsAtCompileTime