diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2008-05-28 05:14:16 +0000 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2008-05-28 05:14:16 +0000 |
commit | f54760c889efcfabfdb8be594abee0647ed0c8da (patch) | |
tree | 0e20a6997401343ac1f64139c0b7098f5b237c7d /Eigen/src/Core | |
parent | aebecae510dd29f5573d3f86dfed526e6d8be9a8 (diff) |
hehe, the complicated nesting scheme in Flagged in the previous commit
was a sign that we were doing something wrong. In fact, having
NestByValue as a special case of Flagged was wrong, and the previous
commit, while not buggy, was inefficient because then when the resulting
NestByValue xpr was nested -- hence copied -- the original xpr which was
already nested by value was copied again; hence instead of 1 copy we got
3 copies.
The solution was to ressuscitate the old Temporary.h (renamed
NestByValue.h) as it was the right approach.
Diffstat (limited to 'Eigen/src/Core')
-rw-r--r-- | Eigen/src/Core/Flagged.h | 15 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 7 | ||||
-rw-r--r-- | Eigen/src/Core/NestByValue.h | 93 | ||||
-rw-r--r-- | Eigen/src/Core/Product.h | 4 | ||||
-rw-r--r-- | Eigen/src/Core/Transpose.h | 4 | ||||
-rw-r--r-- | Eigen/src/Core/util/Constants.h | 1 | ||||
-rw-r--r-- | Eigen/src/Core/util/ForwardDeclarations.h | 1 | ||||
-rw-r--r-- | Eigen/src/Core/util/Macros.h | 1 | ||||
-rw-r--r-- | Eigen/src/Core/util/Meta.h | 5 |
9 files changed, 107 insertions, 24 deletions
diff --git a/Eigen/src/Core/Flagged.h b/Eigen/src/Core/Flagged.h index f287abee4..1107e39aa 100644 --- a/Eigen/src/Core/Flagged.h +++ b/Eigen/src/Core/Flagged.h @@ -94,11 +94,7 @@ template<typename ExpressionType, unsigned int Added, unsigned int Removed> clas } protected: - const typename ei_meta_if< - Added & ~Removed & NestByValueBit, - ExpressionType, - typename ExpressionType::Nested - >::ret m_matrix; + typename ExpressionType::Nested m_matrix; }; /** \returns an expression of *this with added flags @@ -121,13 +117,4 @@ MatrixBase<Derived>::lazy() const return derived(); } -/** \returns an expression of *this with the NestByValueBit flag added. - */ -template<typename Derived> -inline const Flagged<Derived, NestByValueBit, 0> -MatrixBase<Derived>::nestByValue() const -{ - return derived(); -} - #endif // EIGEN_FLAGGED_H diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 1a634ce37..8d1b03eb6 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -305,8 +305,8 @@ template<typename Derived> class MatrixBase Transpose<Derived> transpose(); const Transpose<Derived> transpose() const; const Transpose< - Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived> - , NestByValueBit, 0> > + NestByValue<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived> > + > adjoint() const; //@} @@ -452,7 +452,6 @@ template<typename Derived> class MatrixBase template<unsigned int Added> const Flagged<Derived, Added, 0> marked() const; const Flagged<Derived, 0, EvalBeforeNestingBit | EvalBeforeAssigningBit> lazy() const; - const Flagged<Derived, NestByValueBit, 0> nestByValue() const; /** \returns number of elements to skip to pass from one row (resp. column) to another * for a row-major (resp. column-major) matrix. @@ -460,6 +459,8 @@ template<typename Derived> class MatrixBase * of the underlying matrix. */ inline int stride(void) const { return derived()._stride(); } + + inline const NestByValue<Derived> nestByValue() const; //@} /// \name Coefficient-wise operations diff --git a/Eigen/src/Core/NestByValue.h b/Eigen/src/Core/NestByValue.h new file mode 100644 index 000000000..dc2ea950e --- /dev/null +++ b/Eigen/src/Core/NestByValue.h @@ -0,0 +1,93 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_NESTBYVALUE_H +#define EIGEN_NESTBYVALUE_H + +/** \class NestByValue + * + * \brief Expression which must be nested by value + * + * \param ExpressionType the type of the object of which we are requiring nesting-by-value + * + * This class is the return type of MatrixBase::nestByValue() + * and most of the time this is the only way it is used. + * + * \sa MatrixBase::nestByValue() + */ +template<typename ExpressionType> +struct ei_traits<NestByValue<ExpressionType> > +{ + typedef typename ExpressionType::Scalar Scalar; + enum { + RowsAtCompileTime = ExpressionType::RowsAtCompileTime, + ColsAtCompileTime = ExpressionType::ColsAtCompileTime, + MaxRowsAtCompileTime = ExpressionType::MaxRowsAtCompileTime, + MaxColsAtCompileTime = ExpressionType::MaxColsAtCompileTime, + Flags = ExpressionType::Flags, + CoeffReadCost = ExpressionType::CoeffReadCost + }; +}; + +template<typename ExpressionType> class NestByValue + : public MatrixBase<NestByValue<ExpressionType> > +{ + public: + + EIGEN_GENERIC_PUBLIC_INTERFACE(NestByValue) + + inline NestByValue(const ExpressionType& matrix) : m_expression(matrix) {} + + private: + + inline int _rows() const { return m_expression.rows(); } + inline int _cols() const { return m_expression.cols(); } + inline int _stride() const { return m_expression.stride(); } + + inline const Scalar _coeff(int row, int col) const + { + return m_expression.coeff(row, col); + } + + template<int LoadMode> + inline const PacketScalar _packetCoeff(int row, int col) const + { + return m_expression.template packetCoeff<LoadMode>(row, col); + } + + protected: + const ExpressionType m_expression; +}; + +/** \returns an expression of the temporary version of *this. + */ +template<typename Derived> +inline const NestByValue<Derived> +MatrixBase<Derived>::nestByValue() const +{ + return NestByValue<Derived>(derived()); +} + +#endif // EIGEN_NESTBYVALUE_H diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index 15867d704..c65e6ec47 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -165,7 +165,7 @@ template<typename T> class ei_product_eval_to_column_major template<typename T, int n=1> struct ei_product_nested_rhs { typedef typename ei_meta_if< - (ei_traits<T>::Flags & NestByValueBit) && (!(ei_traits<T>::Flags & RowMajorBit)) && (int(ei_traits<T>::Flags) & DirectAccessBit), + ei_must_nest_by_value<T>::ret && (!(ei_traits<T>::Flags & RowMajorBit)) && (int(ei_traits<T>::Flags) & DirectAccessBit), T, typename ei_meta_if< ((ei_traits<T>::Flags & EvalBeforeNestingBit) @@ -181,7 +181,7 @@ template<typename T, int n=1> struct ei_product_nested_rhs template<typename T, int n=1> struct ei_product_nested_lhs { typedef typename ei_meta_if< - ei_traits<T>::Flags & NestByValueBit && (int(ei_traits<T>::Flags) & DirectAccessBit), + ei_must_nest_by_value<T>::ret && (int(ei_traits<T>::Flags) & DirectAccessBit), T, typename ei_meta_if< int(ei_traits<T>::Flags) & EvalBeforeNestingBit diff --git a/Eigen/src/Core/Transpose.h b/Eigen/src/Core/Transpose.h index c536a4608..8c59d2425 100644 --- a/Eigen/src/Core/Transpose.h +++ b/Eigen/src/Core/Transpose.h @@ -126,8 +126,8 @@ MatrixBase<Derived>::transpose() const * \sa transpose(), conjugate(), class Transpose, class ei_scalar_conjugate_op */ template<typename Derived> inline const Transpose< - Flagged<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived > - , NestByValueBit, 0> > + NestByValue<CwiseUnaryOp<ei_scalar_conjugate_op<typename ei_traits<Derived>::Scalar>, Derived > > + > MatrixBase<Derived>::adjoint() const { return conjugate().nestByValue(); diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h index 0e6ed4b21..1a84e64c6 100644 --- a/Eigen/src/Core/util/Constants.h +++ b/Eigen/src/Core/util/Constants.h @@ -45,7 +45,6 @@ const unsigned int SelfAdjointBit = 0x100; ///< means the matrix is selfadjoint const unsigned int UpperTriangularBit = 0x200; ///< means the strictly triangular lower part is 0 const unsigned int LowerTriangularBit = 0x400; ///< means the strictly triangular upper part is 0 const unsigned int DirectAccessBit = 0x800; ///< means the underlying matrix data can be direclty accessed -const unsigned int NestByValueBit = 0x1000; ///< means the expression should be copied by value when nested // list of flags that are inherited by default const unsigned int HereditaryBits = RowMajorBit diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 0ab40c6a4..c229351aa 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -35,6 +35,7 @@ template<typename _Scalar, int _Rows, int _Cols, class Matrix; template<typename ExpressionType, unsigned int Added, unsigned int Removed> class Flagged; +template<typename ExpressionType> class NestByValue; template<typename MatrixType> class Minor; template<typename MatrixType, int BlockRows=Dynamic, int BlockCols=Dynamic> class Block; template<typename MatrixType> class Transpose; diff --git a/Eigen/src/Core/util/Macros.h b/Eigen/src/Core/util/Macros.h index b511da16d..306144218 100644 --- a/Eigen/src/Core/util/Macros.h +++ b/Eigen/src/Core/util/Macros.h @@ -135,7 +135,6 @@ typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \ typedef typename Base::PacketScalar PacketScalar; \ typedef typename Eigen::ei_nested<Derived>::type Nested; \ typedef typename Eigen::ei_eval<Derived>::type Eval; \ -typedef Eigen::Flagged<Derived, NestByValueBit, 0> Temporary; \ enum { RowsAtCompileTime = Base::RowsAtCompileTime, \ ColsAtCompileTime = Base::ColsAtCompileTime, \ MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime, \ diff --git a/Eigen/src/Core/util/Meta.h b/Eigen/src/Core/util/Meta.h index 6c0730531..f1edf7d6f 100644 --- a/Eigen/src/Core/util/Meta.h +++ b/Eigen/src/Core/util/Meta.h @@ -192,11 +192,14 @@ template<typename T> struct ei_unref<T&> { typedef T type; }; template<typename T> struct ei_unconst { typedef T type; }; template<typename T> struct ei_unconst<const T> { typedef T type; }; +template<typename T> struct ei_must_nest_by_value { enum { ret = false }; }; +template<typename T> struct ei_must_nest_by_value<NestByValue<T> > { enum { ret = true }; }; + template<typename T, int n=1> struct ei_nested { typedef typename ei_meta_if< - ei_traits<T>::Flags & NestByValueBit, + ei_must_nest_by_value<T>::ret, T, typename ei_meta_if< int(ei_traits<T>::Flags) & EvalBeforeNestingBit |