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 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex;
00041 
00042 namespace internal {
00043 
00044 //classes inheriting no_assignment_operator don't generate a default operator=.
00045 class no_assignment_operator
00046 {
00047   private:
00048     no_assignment_operator& operator=(const no_assignment_operator&);
00049 };
00050 
00052 template<typename I1, typename I2>
00053 struct promote_index_type
00054 {
00055   typedef typename conditional<(sizeof(I1)<sizeof(I2)), I2, I1>::type type;
00056 };
00057 
00062 template<typename T, int Value> class variable_if_dynamic
00063 {
00064   public:
00065     EIGEN_EMPTY_STRUCT_CTOR(variable_if_dynamic)
00066     explicit variable_if_dynamic(T v) { EIGEN_ONLY_USED_FOR_DEBUG(v); assert(v == T(Value)); }
00067     static T value() { return T(Value); }
00068     void setValue(T) {}
00069 };
00070 
00071 template<typename T> class variable_if_dynamic<T, Dynamic>
00072 {
00073     T m_value;
00074     variable_if_dynamic() { assert(false); }
00075   public:
00076     explicit variable_if_dynamic(T value) : m_value(value) {}
00077     T value() const { return m_value; }
00078     void setValue(T value) { m_value = value; }
00079 };
00080 
00081 template<typename T> struct functor_traits
00082 {
00083   enum
00084   {
00085     Cost = 10,
00086     PacketAccess = false
00087   };
00088 };
00089 
00090 template<typename T> struct packet_traits;
00091 
00092 template<typename T> struct unpacket_traits
00093 {
00094   typedef T type;
00095   enum {size=1};
00096 };
00097 
00098 template<typename _Scalar, int _Rows, int _Cols,
00099          int _Options = AutoAlign |
00100                           ( (_Rows==1 && _Cols!=1) ? RowMajor
00101                           : (_Cols==1 && _Rows!=1) ? ColMajor
00102                           : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ),
00103          int _MaxRows = _Rows,
00104          int _MaxCols = _Cols
00105 > class make_proper_matrix_type
00106 {
00107     enum {
00108       IsColVector = _Cols==1 && _Rows!=1,
00109       IsRowVector = _Rows==1 && _Cols!=1,
00110       Options = IsColVector ? (_Options | ColMajor) & ~RowMajor
00111               : IsRowVector ? (_Options | RowMajor) & ~ColMajor
00112               : _Options
00113     };
00114   public:
00115     typedef Matrix<_Scalar, _Rows, _Cols, Options, _MaxRows, _MaxCols> type;
00116 };
00117 
00118 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00119 class compute_matrix_flags
00120 {
00121     enum {
00122       row_major_bit = Options&RowMajor ? RowMajorBit : 0,
00123       is_dynamic_size_storage = MaxRows==Dynamic || MaxCols==Dynamic,
00124 
00125       aligned_bit =
00126       (
00127             ((Options&DontAlign)==0)
00128         &&  packet_traits<Scalar>::Vectorizable
00129         && (
00130 #if EIGEN_ALIGN_STATICALLY
00131              ((!is_dynamic_size_storage) && (((MaxCols*MaxRows) % packet_traits<Scalar>::size) == 0))
00132 #else
00133              0
00134 #endif
00135 
00136           ||
00137 
00138 #if EIGEN_ALIGN
00139              is_dynamic_size_storage
00140 #else
00141              0
00142 #endif
00143 
00144           )
00145       ) ? AlignedBit : 0,
00146       packet_access_bit = packet_traits<Scalar>::Vectorizable && aligned_bit ? PacketAccessBit : 0
00147     };
00148 
00149   public:
00150     enum { ret = LinearAccessBit | LvalueBit | DirectAccessBit | NestByRefBit | packet_access_bit | row_major_bit | aligned_bit };
00151 };
00152 
00153 template<int _Rows, int _Cols> struct size_at_compile_time
00154 {
00155   enum { ret = (_Rows==Dynamic || _Cols==Dynamic) ? Dynamic : _Rows * _Cols };
00156 };
00157 
00158 /* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,
00159  * whereas eval is a const reference in the case of a matrix
00160  */
00161 
00162 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct plain_matrix_type;
00163 template<typename T, typename BaseClassType> struct plain_matrix_type_dense;
00164 template<typename T> struct plain_matrix_type<T,Dense>
00165 {
00166   typedef typename plain_matrix_type_dense<T,typename traits<T>::XprKind>::type type;
00167 };
00168 
00169 template<typename T> struct plain_matrix_type_dense<T,MatrixXpr>
00170 {
00171   typedef Matrix<typename traits<T>::Scalar,
00172                 traits<T>::RowsAtCompileTime,
00173                 traits<T>::ColsAtCompileTime,
00174                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00175                 traits<T>::MaxRowsAtCompileTime,
00176                 traits<T>::MaxColsAtCompileTime
00177           > type;
00178 };
00179 
00180 template<typename T> struct plain_matrix_type_dense<T,ArrayXpr>
00181 {
00182   typedef Array<typename traits<T>::Scalar,
00183                 traits<T>::RowsAtCompileTime,
00184                 traits<T>::ColsAtCompileTime,
00185                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00186                 traits<T>::MaxRowsAtCompileTime,
00187                 traits<T>::MaxColsAtCompileTime
00188           > type;
00189 };
00190 
00191 /* eval : the return type of eval(). For matrices, this is just a const reference
00192  * in order to avoid a useless copy
00193  */
00194 
00195 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct eval;
00196 
00197 template<typename T> struct eval<T,Dense>
00198 {
00199   typedef typename plain_matrix_type<T>::type type;
00200 //   typedef typename T::PlainObject type;
00201 //   typedef T::Matrix<typename traits<T>::Scalar,
00202 //                 traits<T>::RowsAtCompileTime,
00203 //                 traits<T>::ColsAtCompileTime,
00204 //                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
00205 //                 traits<T>::MaxRowsAtCompileTime,
00206 //                 traits<T>::MaxColsAtCompileTime
00207 //           > type;
00208 };
00209 
00210 // for matrices, no need to evaluate, just use a const reference to avoid a useless copy
00211 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00212 struct eval<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
00213 {
00214   typedef const Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
00215 };
00216 
00217 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00218 struct eval<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
00219 {
00220   typedef const Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
00221 };
00222 
00223 
00224 
00225 /* plain_matrix_type_column_major : same as plain_matrix_type but guaranteed to be column-major
00226  */
00227 template<typename T> struct plain_matrix_type_column_major
00228 {
00229   enum { Rows = traits<T>::RowsAtCompileTime,
00230          Cols = traits<T>::ColsAtCompileTime,
00231          MaxRows = traits<T>::MaxRowsAtCompileTime,
00232          MaxCols = traits<T>::MaxColsAtCompileTime
00233   };
00234   typedef Matrix<typename traits<T>::Scalar,
00235                 Rows,
00236                 Cols,
00237                 (MaxRows==1&&MaxCols!=1) ? RowMajor : ColMajor,
00238                 MaxRows,
00239                 MaxCols
00240           > type;
00241 };
00242 
00243 /* plain_matrix_type_row_major : same as plain_matrix_type but guaranteed to be row-major
00244  */
00245 template<typename T> struct plain_matrix_type_row_major
00246 {
00247   enum { Rows = traits<T>::RowsAtCompileTime,
00248          Cols = traits<T>::ColsAtCompileTime,
00249          MaxRows = traits<T>::MaxRowsAtCompileTime,
00250          MaxCols = traits<T>::MaxColsAtCompileTime
00251   };
00252   typedef Matrix<typename traits<T>::Scalar,
00253                 Rows,
00254                 Cols,
00255                 (MaxCols==1&&MaxRows!=1) ? RowMajor : ColMajor,
00256                 MaxRows,
00257                 MaxCols
00258           > type;
00259 };
00260 
00261 // we should be able to get rid of this one too
00262 template<typename T> struct must_nest_by_value { enum { ret = false }; };
00263 
00264 template<class T>
00265 struct is_reference
00266 {
00267   enum { ret = false };
00268 };
00269 
00270 template<class T>
00271 struct is_reference<T&>
00272 {
00273   enum { ret = true };
00274 };
00275 
00281 template <typename T>
00282 struct ref_selector
00283 {
00284   typedef typename conditional<
00285     bool(traits<T>::Flags & NestByRefBit),
00286     T const&,
00287     T
00288   >::type type;
00289 };
00290 
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 #endif // EIGEN_XPRHELPER_H


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:34:05