diff options
author | Hauke Heibel <hauke.heibel@gmail.com> | 2009-09-07 17:22:01 +0200 |
---|---|---|
committer | Hauke Heibel <hauke.heibel@gmail.com> | 2009-09-07 17:22:01 +0200 |
commit | 64095b66108d358ebd31ab2cc596421b2bddfff3 (patch) | |
tree | d1c321dfc0f6d1d7196b7d2fec85187794883281 /Eigen/src/Core | |
parent | 8f4bf4ed7ffa4f39957118a5b4162613b21f190a (diff) |
Changed the AnyMatrixBase / ei_special_scalar_op inheritance order as proposed by Gael.
Added conservativeResizeLike as discussed on the mailing list.
Diffstat (limited to 'Eigen/src/Core')
-rw-r--r-- | Eigen/src/Core/Matrix.h | 71 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 5 | ||||
-rw-r--r-- | Eigen/src/Core/util/XprHelper.h | 4 |
3 files changed, 52 insertions, 28 deletions
diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index 2774e0d78..6bbd3a019 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -308,7 +308,7 @@ class Matrix */ template<typename OtherDerived> EIGEN_STRONG_INLINE void resizeLike(const MatrixBase<OtherDerived>& other) - { + { if(RowsAtCompileTime == 1) { ei_assert(other.isVector()); @@ -324,16 +324,14 @@ class Matrix /** Resizes \c *this to a \a rows x \a cols matrix while leaving old values of *this untouched. * - * This method is intended for dynamic-size matrices, although it is legal to call it on any - * matrix as long as fixed dimensions are left unchanged. If you only want to change the number + * This method is intended for dynamic-size matrices. If you only want to change the number * of rows and/or of columns, you can use conservativeResize(NoChange_t, int), * conservativeResize(int, NoChange_t). * * The top-left part of the resized matrix will be the same as the overlapping top-left corner - * of *this. In case values need to be appended to the matrix they will be uninitialized per - * default and set to zero when init_with_zero is set to true. + * of *this. In case values need to be appended to the matrix they will be uninitialized. */ - inline void conservativeResize(int rows, int cols, bool init_with_zero = false) + inline void conservativeResize(int rows, int cols) { // Note: Here is space for improvement. Basically, for conservativeResize(int,int), // neither RowsAtCompileTime or ColsAtCompileTime must be Dynamic. If only one of the @@ -341,23 +339,23 @@ class Matrix // conservativeResize(NoChange_t, int cols). For these methods new static asserts like // EIGEN_STATIC_ASSERT_DYNAMIC_ROWS and EIGEN_STATIC_ASSERT_DYNAMIC_COLS would be good. EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Matrix) - PlainMatrixType tmp = init_with_zero ? PlainMatrixType::Zero(rows, cols) : PlainMatrixType(rows,cols); + PlainMatrixType tmp(rows, cols); const int common_rows = std::min(rows, this->rows()); const int common_cols = std::min(cols, this->cols()); tmp.block(0,0,common_rows,common_cols) = this->block(0,0,common_rows,common_cols); this->derived().swap(tmp); } - EIGEN_STRONG_INLINE void conservativeResize(int rows, NoChange_t, bool init_with_zero = false) + EIGEN_STRONG_INLINE void conservativeResize(int rows, NoChange_t) { - // Note: see the comment in conservativeResize(int,int,bool) - conservativeResize(rows, cols(), init_with_zero); + // Note: see the comment in conservativeResize(int,int) + conservativeResize(rows, cols()); } - EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, int cols, bool init_with_zero = false) + EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, int cols) { - // Note: see the comment in conservativeResize(int,int,bool) - conservativeResize(rows(), cols, init_with_zero); + // Note: see the comment in conservativeResize(int,int) + conservativeResize(rows(), cols); } /** Resizes \c *this to a vector of length \a size while retaining old values of *this. @@ -366,21 +364,23 @@ class Matrix * partially dynamic matrices when the static dimension is anything other * than 1. For example it will not work with Matrix<double, 2, Dynamic>. * - * When values are appended, they will be uninitialized per default and set - * to zero when init_with_zero is set to true. + * When values are appended, they will be uninitialized. */ - inline void conservativeResize(int size, bool init_with_zero = false) + inline void conservativeResize(int size) { EIGEN_STATIC_ASSERT_VECTOR_ONLY(Matrix) EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Matrix) - if (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) - { - PlainMatrixType tmp = init_with_zero ? PlainMatrixType::Zero(size) : PlainMatrixType(size); - const int common_size = std::min<int>(this->size(),size); - tmp.segment(0,common_size) = this->segment(0,common_size); - this->derived().swap(tmp); - } + PlainMatrixType tmp(size); + const int common_size = std::min<int>(this->size(),size); + tmp.segment(0,common_size) = this->segment(0,common_size); + this->derived().swap(tmp); + } + + template<typename OtherDerived> + inline void conservativeResizeLike(const MatrixBase<OtherDerived>& other) + { + ei_conservative_resize_like_impl<Matrix, OtherDerived>::run(*this, other); } /** Copies the value of the expression \a other into \c *this with automatic resizing. @@ -717,6 +717,31 @@ class Matrix friend struct ei_matrix_swap_impl; }; +template <typename Derived, typename OtherDerived, bool IsVector = static_cast<bool>(Derived::IsVectorAtCompileTime)> +struct ei_conservative_resize_like_impl +{ + static void run(MatrixBase<Derived>& _this, const MatrixBase<OtherDerived>& other) + { + MatrixBase<Derived>::PlainMatrixType tmp(other); + const int common_rows = std::min(tmp.rows(), _this.rows()); + const int common_cols = std::min(tmp.cols(), _this.cols()); + tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols); + _this.derived().swap(tmp); + } +}; + +template <typename Derived, typename OtherDerived> +struct ei_conservative_resize_like_impl<Derived,OtherDerived,true> +{ + static void run(MatrixBase<Derived>& _this, const MatrixBase<OtherDerived>& other) + { + MatrixBase<Derived>::PlainMatrixType tmp(other); + const int common_size = std::min<int>(_this.size(),tmp.size()); + tmp.segment(0,common_size) = _this.segment(0,common_size); + _this.derived().swap(tmp); + } +}; + template<typename MatrixType, typename OtherDerived, bool SwapPointers> struct ei_matrix_swap_impl { diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index ad5fde562..25a0545c6 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -36,8 +36,6 @@ * Notice that this class is trivial, it is only used to disambiguate overloaded functions. */ template<typename Derived> struct AnyMatrixBase - : public ei_special_scalar_op_base<Derived,typename ei_traits<Derived>::Scalar, - typename NumTraits<typename ei_traits<Derived>::Scalar>::Real> { typedef typename ei_plain_matrix_type<Derived>::type PlainMatrixType; @@ -93,7 +91,8 @@ template<typename Derived> struct AnyMatrixBase */ template<typename Derived> class MatrixBase #ifndef EIGEN_PARSED_BY_DOXYGEN - : public AnyMatrixBase<Derived> + : public ei_special_scalar_op_base<Derived,typename ei_traits<Derived>::Scalar, + typename NumTraits<typename ei_traits<Derived>::Scalar>::Real> #endif // not EIGEN_PARSED_BY_DOXYGEN { public: diff --git a/Eigen/src/Core/util/XprHelper.h b/Eigen/src/Core/util/XprHelper.h index 2f8d35d05..cea2faaa8 100644 --- a/Eigen/src/Core/util/XprHelper.h +++ b/Eigen/src/Core/util/XprHelper.h @@ -217,7 +217,7 @@ template<unsigned int Flags> struct ei_are_flags_consistent * overloads for complex types */ template<typename Derived,typename Scalar,typename OtherScalar, bool EnableIt = !ei_is_same_type<Scalar,OtherScalar>::ret > -struct ei_special_scalar_op_base +struct ei_special_scalar_op_base : public AnyMatrixBase<Derived> { // dummy operator* so that the // "using ei_special_scalar_op_base::operator*" compiles @@ -225,7 +225,7 @@ struct ei_special_scalar_op_base }; template<typename Derived,typename Scalar,typename OtherScalar> -struct ei_special_scalar_op_base<Derived,Scalar,OtherScalar,true> +struct ei_special_scalar_op_base<Derived,Scalar,OtherScalar,true> : public AnyMatrixBase<Derived> { const CwiseUnaryOp<ei_scalar_multiple2_op<Scalar,OtherScalar>, Derived> operator*(const OtherScalar& scalar) const |