XprHelper.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_XPRHELPER_H
00027 #define EIGEN_XPRHELPER_H
00028 
00029 // just a workaround because GCC seems to not really like empty structs
00030 // FIXME: gcc 4.3 generates bad code when strict-aliasing is enabled
00031 // so currently we simply disable this optimization for gcc 4.3
00032 #if (defined __GNUG__) && !((__GNUC__==4) && (__GNUC_MINOR__==3))
00033   #define EIGEN_EMPTY_STRUCT_CTOR(X) \
00034     EIGEN_STRONG_INLINE X() {} \
00035     EIGEN_STRONG_INLINE X(const X& ) {}
00036 #else
00037   #define EIGEN_EMPTY_STRUCT_CTOR(X)
00038 #endif
00039 
00040 namespace Eigen {
00041 
00042 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex;
00043 
00044 namespace internal {
00045 
00046 //classes inheriting no_assignment_operator don't generate a default operator=.
00047 class no_assignment_operator
00048 {
00049   private:
00050     no_assignment_operator& operator=(const no_assignment_operator&);
00051 };
00052 
00054 template<typename I1, typename I2>
00055 struct promote_index_type
00056 {
00057   typedef typename conditional<(sizeof(I1)<sizeof(I2)), I2, I1>::type type;
00058 };
00059 
00064 template<typename T, int Value> class variable_if_dynamic
00065 {
00066   public:
00067     EIGEN_EMPTY_STRUCT_CTOR(variable_if_dynamic)
00068     explicit variable_if_dynamic(T v) { EIGEN_ONLY_USED_FOR_DEBUG(v); assert(v == T(Value)); }
00069     static T value() { return T(Value); }
00070     void setValue(T) {}
00071 };
00072 
00073 template<typename T> class variable_if_dynamic<T, Dynamic>
00074 {
00075     T m_value;
00076     variable_if_dynamic() { assert(false); }
00077   public:
00078     explicit variable_if_dynamic(T value) : m_value(value) {}
00079     T value() const { return m_value; }
00080     void setValue(T value) { m_value = value; }
00081 };
00082 
00083 template<typename T> struct functor_traits
00084 {
00085   enum
00086   {
00087     Cost = 10,
00088     PacketAccess = false
00089   };
00090 };
00091 
00092 template<typename T> struct packet_traits;
00093 
00094 template<typename T> struct unpacket_traits
00095 {
00096   typedef T type;
00097   enum {size=1};
00098 };
00099 
00100 template<typename _Scalar, int _Rows, int _Cols,
00101          int _Options = AutoAlign |
00102                           ( (_Rows==1 && _Cols!=1) ? RowMajor
00103                           : (_Cols==1 && _Rows!=1) ? ColMajor
00104                           : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ),
00105          int _MaxRows = _Rows,
00106          int _MaxCols = _Cols
00107 > class make_proper_matrix_type
00108 {
00109     enum {
00110       IsColVector = _Cols==1 && _Rows!=1,
00111       IsRowVector = _Rows==1 && _Cols!=1,
00112       Options = IsColVector ? (_Options | ColMajor) & ~RowMajor
00113               : IsRowVector ? (_Options | RowMajor) & ~ColMajor
00114               : _Options
00115     };
00116   public:
00117     typedef Matrix<_Scalar, _Rows, _Cols, Options, _MaxRows, _MaxCols> type;
00118 };
00119 
00120 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00121 class compute_matrix_flags
00122 {
00123     enum {
00124       row_major_bit = Options&RowMajor ? RowMajorBit : 0,
00125       is_dynamic_size_storage = MaxRows==Dynamic || MaxCols==Dynamic,
00126 
00127       aligned_bit =
00128       (
00129             ((Options&DontAlign)==0)
00130         && (
00131 #if EIGEN_ALIGN_STATICALLY
00132              ((!is_dynamic_size_storage) && (((MaxCols*MaxRows*int(sizeof(Scalar))) % 16) == 0))
00133 #else
00134              0
00135 #endif
00136 
00137           ||
00138 
00139 #if EIGEN_ALIGN
00140              is_dynamic_size_storage
00141 #else
00142              0
00143 #endif
00144 
00145           )
00146       ) ? AlignedBit : 0,
00147       packet_access_bit = packet_traits<Scalar>::Vectorizable && aligned_bit ? PacketAccessBit : 0
00148     };
00149 
00150   public:
00151     enum { ret = LinearAccessBit | LvalueBit | DirectAccessBit | NestByRefBit | packet_access_bit | row_major_bit | aligned_bit };
00152 };
00153 
00154 template<int _Rows, int _Cols> struct size_at_compile_time
00155 {
00156   enum { ret = (_Rows==Dynamic || _Cols==Dynamic) ? Dynamic : _Rows * _Cols };
00157 };
00158 
00159 /* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,
00160  * whereas eval is a const reference in the case of a matrix
00161  */
00162 
00163 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct plain_matrix_type;
00164 template<typename T, typename BaseClassType> struct plain_matrix_type_dense;
00165 template<typename T> struct plain_matrix_type<T,Dense>
00166 {
00167   typedef typename plain_matrix_type_dense<T,typename traits<T>::XprKind>::type type;
00168 };
00169 
00170 template<typename T> struct plain_matrix_type_dense<T,MatrixXpr>
00171 {
00172   typedef Matrix<typename traits<T>::Scalar,
00173                 traits<T>::RowsAtCompileTime,
00174                 traits<T>::ColsAtCompileTime,
00175                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00176                 traits<T>::MaxRowsAtCompileTime,
00177                 traits<T>::MaxColsAtCompileTime
00178           > type;
00179 };
00180 
00181 template<typename T> struct plain_matrix_type_dense<T,ArrayXpr>
00182 {
00183   typedef Array<typename traits<T>::Scalar,
00184                 traits<T>::RowsAtCompileTime,
00185                 traits<T>::ColsAtCompileTime,
00186                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00187                 traits<T>::MaxRowsAtCompileTime,
00188                 traits<T>::MaxColsAtCompileTime
00189           > type;
00190 };
00191 
00192 /* eval : the return type of eval(). For matrices, this is just a const reference
00193  * in order to avoid a useless copy
00194  */
00195 
00196 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct eval;
00197 
00198 template<typename T> struct eval<T,Dense>
00199 {
00200   typedef typename plain_matrix_type<T>::type type;
00201 //   typedef typename T::PlainObject type;
00202 //   typedef T::Matrix<typename traits<T>::Scalar,
00203 //                 traits<T>::RowsAtCompileTime,
00204 //                 traits<T>::ColsAtCompileTime,
00205 //                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00206 //                 traits<T>::MaxRowsAtCompileTime,
00207 //                 traits<T>::MaxColsAtCompileTime
00208 //           > type;
00209 };
00210 
00211 // for matrices, no need to evaluate, just use a const reference to avoid a useless copy
00212 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00213 struct eval<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
00214 {
00215   typedef const Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
00216 };
00217 
00218 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00219 struct eval<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
00220 {
00221   typedef const Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
00222 };
00223 
00224 
00225 
00226 /* plain_matrix_type_column_major : same as plain_matrix_type but guaranteed to be column-major
00227  */
00228 template<typename T> struct plain_matrix_type_column_major
00229 {
00230   enum { Rows = traits<T>::RowsAtCompileTime,
00231          Cols = traits<T>::ColsAtCompileTime,
00232          MaxRows = traits<T>::MaxRowsAtCompileTime,
00233          MaxCols = traits<T>::MaxColsAtCompileTime
00234   };
00235   typedef Matrix<typename traits<T>::Scalar,
00236                 Rows,
00237                 Cols,
00238                 (MaxRows==1&&MaxCols!=1) ? RowMajor : ColMajor,
00239                 MaxRows,
00240                 MaxCols
00241           > type;
00242 };
00243 
00244 /* plain_matrix_type_row_major : same as plain_matrix_type but guaranteed to be row-major
00245  */
00246 template<typename T> struct plain_matrix_type_row_major
00247 {
00248   enum { Rows = traits<T>::RowsAtCompileTime,
00249          Cols = traits<T>::ColsAtCompileTime,
00250          MaxRows = traits<T>::MaxRowsAtCompileTime,
00251          MaxCols = traits<T>::MaxColsAtCompileTime
00252   };
00253   typedef Matrix<typename traits<T>::Scalar,
00254                 Rows,
00255                 Cols,
00256                 (MaxCols==1&&MaxRows!=1) ? RowMajor : ColMajor,
00257                 MaxRows,
00258                 MaxCols
00259           > type;
00260 };
00261 
00262 // we should be able to get rid of this one too
00263 template<typename T> struct must_nest_by_value { enum { ret = false }; };
00264 
00268 template <typename T>
00269 struct ref_selector
00270 {
00271   typedef typename conditional<
00272     bool(traits<T>::Flags & NestByRefBit),
00273     T const&,
00274     const T
00275   >::type type;
00276 };
00277 
00279 template<typename T1, typename T2>
00280 struct transfer_constness
00281 {
00282   typedef typename conditional<
00283     bool(internal::is_const<T1>::value),
00284     typename internal::add_const_on_value_type<T2>::type,
00285     T2
00286   >::type type;
00287 };
00288 
00309 template<typename T, int n=1, typename PlainObject = typename eval<T>::type> struct nested
00310 {
00311   enum {
00312     // for the purpose of this test, to keep it reasonably simple, we arbitrarily choose a value of Dynamic values.
00313     // the choice of 10000 makes it larger than any practical fixed value and even most dynamic values.
00314     // in extreme cases where these assumptions would be wrong, we would still at worst suffer performance issues
00315     // (poor choice of temporaries).
00316     // it's important that this value can still be squared without integer overflowing.
00317     DynamicAsInteger = 10000,
00318     ScalarReadCost = NumTraits<typename traits<T>::Scalar>::ReadCost,
00319     ScalarReadCostAsInteger = ScalarReadCost == Dynamic ? DynamicAsInteger : ScalarReadCost,
00320     CoeffReadCost = traits<T>::CoeffReadCost,
00321     CoeffReadCostAsInteger = CoeffReadCost == Dynamic ? DynamicAsInteger : CoeffReadCost,
00322     NAsInteger = n == Dynamic ? int(DynamicAsInteger) : n,
00323     CostEvalAsInteger   = (NAsInteger+1) * ScalarReadCostAsInteger + CoeffReadCostAsInteger,
00324     CostNoEvalAsInteger = NAsInteger * CoeffReadCostAsInteger
00325   };
00326 
00327   typedef typename conditional<
00328       ( (int(traits<T>::Flags) & EvalBeforeNestingBit) ||
00329         int(CostEvalAsInteger) < int(CostNoEvalAsInteger)
00330       ),
00331       PlainObject,
00332       typename ref_selector<T>::type
00333   >::type type;
00334 };
00335 
00336 template<typename T>
00337 T* const_cast_ptr(const T* ptr)
00338 {
00339   return const_cast<T*>(ptr);
00340 }
00341 
00342 template<typename Derived, typename XprKind = typename traits<Derived>::XprKind>
00343 struct dense_xpr_base
00344 {
00345   /* dense_xpr_base should only ever be used on dense expressions, thus falling either into the MatrixXpr or into the ArrayXpr cases */
00346 };
00347 
00348 template<typename Derived>
00349 struct dense_xpr_base<Derived, MatrixXpr>
00350 {
00351   typedef MatrixBase<Derived> type;
00352 };
00353 
00354 template<typename Derived>
00355 struct dense_xpr_base<Derived, ArrayXpr>
00356 {
00357   typedef ArrayBase<Derived> type;
00358 };
00359 
00362 template<typename Derived,typename Scalar,typename OtherScalar,
00363          bool EnableIt = !is_same<Scalar,OtherScalar>::value >
00364 struct special_scalar_op_base : public DenseCoeffsBase<Derived>
00365 {
00366   // dummy operator* so that the
00367   // "using special_scalar_op_base::operator*" compiles
00368   void operator*() const;
00369 };
00370 
00371 template<typename Derived,typename Scalar,typename OtherScalar>
00372 struct special_scalar_op_base<Derived,Scalar,OtherScalar,true>  : public DenseCoeffsBase<Derived>
00373 {
00374   const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
00375   operator*(const OtherScalar& scalar) const
00376   {
00377     return CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
00378       (*static_cast<const Derived*>(this), scalar_multiple2_op<Scalar,OtherScalar>(scalar));
00379   }
00380 
00381   inline friend const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
00382   operator*(const OtherScalar& scalar, const Derived& matrix)
00383   { return static_cast<const special_scalar_op_base&>(matrix).operator*(scalar); }
00384 };
00385 
00386 template<typename XprType, typename CastType> struct cast_return_type
00387 {
00388   typedef typename XprType::Scalar CurrentScalarType;
00389   typedef typename remove_all<CastType>::type _CastType;
00390   typedef typename _CastType::Scalar NewScalarType;
00391   typedef typename conditional<is_same<CurrentScalarType,NewScalarType>::value,
00392                               const XprType&,CastType>::type type;
00393 };
00394 
00395 template <typename A, typename B> struct promote_storage_type;
00396 
00397 template <typename A> struct promote_storage_type<A,A>
00398 {
00399   typedef A ret;
00400 };
00401 
00405 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
00406 struct plain_row_type
00407 {
00408   typedef Matrix<Scalar, 1, ExpressionType::ColsAtCompileTime,
00409                  ExpressionType::PlainObject::Options | RowMajor, 1, ExpressionType::MaxColsAtCompileTime> MatrixRowType;
00410   typedef Array<Scalar, 1, ExpressionType::ColsAtCompileTime,
00411                  ExpressionType::PlainObject::Options | RowMajor, 1, ExpressionType::MaxColsAtCompileTime> ArrayRowType;
00412 
00413   typedef typename conditional<
00414     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
00415     MatrixRowType,
00416     ArrayRowType 
00417   >::type type;
00418 };
00419 
00420 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
00421 struct plain_col_type
00422 {
00423   typedef Matrix<Scalar, ExpressionType::RowsAtCompileTime, 1,
00424                  ExpressionType::PlainObject::Options & ~RowMajor, ExpressionType::MaxRowsAtCompileTime, 1> MatrixColType;
00425   typedef Array<Scalar, ExpressionType::RowsAtCompileTime, 1,
00426                  ExpressionType::PlainObject::Options & ~RowMajor, ExpressionType::MaxRowsAtCompileTime, 1> ArrayColType;
00427 
00428   typedef typename conditional<
00429     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
00430     MatrixColType,
00431     ArrayColType 
00432   >::type type;
00433 };
00434 
00435 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
00436 struct plain_diag_type
00437 {
00438   enum { diag_size = EIGEN_SIZE_MIN_PREFER_DYNAMIC(ExpressionType::RowsAtCompileTime, ExpressionType::ColsAtCompileTime),
00439          max_diag_size = EIGEN_SIZE_MIN_PREFER_FIXED(ExpressionType::MaxRowsAtCompileTime, ExpressionType::MaxColsAtCompileTime)
00440   };
00441   typedef Matrix<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1> MatrixDiagType;
00442   typedef Array<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1> ArrayDiagType;
00443 
00444   typedef typename conditional<
00445     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
00446     MatrixDiagType,
00447     ArrayDiagType 
00448   >::type type;
00449 };
00450 
00451 template<typename ExpressionType>
00452 struct is_lvalue
00453 {
00454   enum { value = !bool(is_const<ExpressionType>::value) &&
00455                  bool(traits<ExpressionType>::Flags & LvalueBit) };
00456 };
00457 
00458 } // end namespace internal
00459 
00460 } // end namespace Eigen
00461 
00462 #endif // EIGEN_XPRHELPER_H