Home | History | Annotate | Download | only in util
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #ifndef EIGEN_XPRHELPER_H
     12 #define EIGEN_XPRHELPER_H
     13 
     14 // just a workaround because GCC seems to not really like empty structs
     15 // FIXME: gcc 4.3 generates bad code when strict-aliasing is enabled
     16 // so currently we simply disable this optimization for gcc 4.3
     17 #if (defined __GNUG__) && !((__GNUC__==4) && (__GNUC_MINOR__==3))
     18   #define EIGEN_EMPTY_STRUCT_CTOR(X) \
     19     EIGEN_STRONG_INLINE X() {} \
     20     EIGEN_STRONG_INLINE X(const X& ) {}
     21 #else
     22   #define EIGEN_EMPTY_STRUCT_CTOR(X)
     23 #endif
     24 
     25 namespace Eigen {
     26 
     27 typedef EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex;
     28 
     29 namespace internal {
     30 
     31 //classes inheriting no_assignment_operator don't generate a default operator=.
     32 class no_assignment_operator
     33 {
     34   private:
     35     no_assignment_operator& operator=(const no_assignment_operator&);
     36 };
     37 
     38 /** \internal return the index type with the largest number of bits */
     39 template<typename I1, typename I2>
     40 struct promote_index_type
     41 {
     42   typedef typename conditional<(sizeof(I1)<sizeof(I2)), I2, I1>::type type;
     43 };
     44 
     45 /** \internal If the template parameter Value is Dynamic, this class is just a wrapper around a T variable that
     46   * can be accessed using value() and setValue().
     47   * Otherwise, this class is an empty structure and value() just returns the template parameter Value.
     48   */
     49 template<typename T, int Value> class variable_if_dynamic
     50 {
     51   public:
     52     EIGEN_EMPTY_STRUCT_CTOR(variable_if_dynamic)
     53     explicit variable_if_dynamic(T v) { EIGEN_ONLY_USED_FOR_DEBUG(v); assert(v == T(Value)); }
     54     static T value() { return T(Value); }
     55     void setValue(T) {}
     56 };
     57 
     58 template<typename T> class variable_if_dynamic<T, Dynamic>
     59 {
     60     T m_value;
     61     variable_if_dynamic() { assert(false); }
     62   public:
     63     explicit variable_if_dynamic(T value) : m_value(value) {}
     64     T value() const { return m_value; }
     65     void setValue(T value) { m_value = value; }
     66 };
     67 
     68 /** \internal like variable_if_dynamic but for DynamicIndex
     69   */
     70 template<typename T, int Value> class variable_if_dynamicindex
     71 {
     72   public:
     73     EIGEN_EMPTY_STRUCT_CTOR(variable_if_dynamicindex)
     74     explicit variable_if_dynamicindex(T v) { EIGEN_ONLY_USED_FOR_DEBUG(v); assert(v == T(Value)); }
     75     static T value() { return T(Value); }
     76     void setValue(T) {}
     77 };
     78 
     79 template<typename T> class variable_if_dynamicindex<T, DynamicIndex>
     80 {
     81     T m_value;
     82     variable_if_dynamicindex() { assert(false); }
     83   public:
     84     explicit variable_if_dynamicindex(T value) : m_value(value) {}
     85     T value() const { return m_value; }
     86     void setValue(T value) { m_value = value; }
     87 };
     88 
     89 template<typename T> struct functor_traits
     90 {
     91   enum
     92   {
     93     Cost = 10,
     94     PacketAccess = false,
     95     IsRepeatable = false
     96   };
     97 };
     98 
     99 template<typename T> struct packet_traits;
    100 
    101 template<typename T> struct unpacket_traits
    102 {
    103   typedef T type;
    104   enum {size=1};
    105 };
    106 
    107 template<typename _Scalar, int _Rows, int _Cols,
    108          int _Options = AutoAlign |
    109                           ( (_Rows==1 && _Cols!=1) ? RowMajor
    110                           : (_Cols==1 && _Rows!=1) ? ColMajor
    111                           : EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION ),
    112          int _MaxRows = _Rows,
    113          int _MaxCols = _Cols
    114 > class make_proper_matrix_type
    115 {
    116     enum {
    117       IsColVector = _Cols==1 && _Rows!=1,
    118       IsRowVector = _Rows==1 && _Cols!=1,
    119       Options = IsColVector ? (_Options | ColMajor) & ~RowMajor
    120               : IsRowVector ? (_Options | RowMajor) & ~ColMajor
    121               : _Options
    122     };
    123   public:
    124     typedef Matrix<_Scalar, _Rows, _Cols, Options, _MaxRows, _MaxCols> type;
    125 };
    126 
    127 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
    128 class compute_matrix_flags
    129 {
    130     enum {
    131       row_major_bit = Options&RowMajor ? RowMajorBit : 0,
    132       is_dynamic_size_storage = MaxRows==Dynamic || MaxCols==Dynamic,
    133 
    134       aligned_bit =
    135       (
    136             ((Options&DontAlign)==0)
    137         && (
    138 #if EIGEN_ALIGN_STATICALLY
    139              ((!is_dynamic_size_storage) && (((MaxCols*MaxRows*int(sizeof(Scalar))) % 16) == 0))
    140 #else
    141              0
    142 #endif
    143 
    144           ||
    145 
    146 #if EIGEN_ALIGN
    147              is_dynamic_size_storage
    148 #else
    149              0
    150 #endif
    151 
    152           )
    153       ) ? AlignedBit : 0,
    154       packet_access_bit = packet_traits<Scalar>::Vectorizable && aligned_bit ? PacketAccessBit : 0
    155     };
    156 
    157   public:
    158     enum { ret = LinearAccessBit | LvalueBit | DirectAccessBit | NestByRefBit | packet_access_bit | row_major_bit | aligned_bit };
    159 };
    160 
    161 template<int _Rows, int _Cols> struct size_at_compile_time
    162 {
    163   enum { ret = (_Rows==Dynamic || _Cols==Dynamic) ? Dynamic : _Rows * _Cols };
    164 };
    165 
    166 /* plain_matrix_type : the difference from eval is that plain_matrix_type is always a plain matrix type,
    167  * whereas eval is a const reference in the case of a matrix
    168  */
    169 
    170 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct plain_matrix_type;
    171 template<typename T, typename BaseClassType> struct plain_matrix_type_dense;
    172 template<typename T> struct plain_matrix_type<T,Dense>
    173 {
    174   typedef typename plain_matrix_type_dense<T,typename traits<T>::XprKind>::type type;
    175 };
    176 
    177 template<typename T> struct plain_matrix_type_dense<T,MatrixXpr>
    178 {
    179   typedef Matrix<typename traits<T>::Scalar,
    180                 traits<T>::RowsAtCompileTime,
    181                 traits<T>::ColsAtCompileTime,
    182                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
    183                 traits<T>::MaxRowsAtCompileTime,
    184                 traits<T>::MaxColsAtCompileTime
    185           > type;
    186 };
    187 
    188 template<typename T> struct plain_matrix_type_dense<T,ArrayXpr>
    189 {
    190   typedef Array<typename traits<T>::Scalar,
    191                 traits<T>::RowsAtCompileTime,
    192                 traits<T>::ColsAtCompileTime,
    193                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
    194                 traits<T>::MaxRowsAtCompileTime,
    195                 traits<T>::MaxColsAtCompileTime
    196           > type;
    197 };
    198 
    199 /* eval : the return type of eval(). For matrices, this is just a const reference
    200  * in order to avoid a useless copy
    201  */
    202 
    203 template<typename T, typename StorageKind = typename traits<T>::StorageKind> struct eval;
    204 
    205 template<typename T> struct eval<T,Dense>
    206 {
    207   typedef typename plain_matrix_type<T>::type type;
    208 //   typedef typename T::PlainObject type;
    209 //   typedef T::Matrix<typename traits<T>::Scalar,
    210 //                 traits<T>::RowsAtCompileTime,
    211 //                 traits<T>::ColsAtCompileTime,
    212 //                 AutoAlign | (traits<T>::Flags&RowMajorBit ? RowMajor : ColMajor),
    213 //                 traits<T>::MaxRowsAtCompileTime,
    214 //                 traits<T>::MaxColsAtCompileTime
    215 //           > type;
    216 };
    217 
    218 // for matrices, no need to evaluate, just use a const reference to avoid a useless copy
    219 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
    220 struct eval<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
    221 {
    222   typedef const Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
    223 };
    224 
    225 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
    226 struct eval<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
    227 {
    228   typedef const Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& type;
    229 };
    230 
    231 
    232 
    233 /* plain_matrix_type_column_major : same as plain_matrix_type but guaranteed to be column-major
    234  */
    235 template<typename T> struct plain_matrix_type_column_major
    236 {
    237   enum { Rows = traits<T>::RowsAtCompileTime,
    238          Cols = traits<T>::ColsAtCompileTime,
    239          MaxRows = traits<T>::MaxRowsAtCompileTime,
    240          MaxCols = traits<T>::MaxColsAtCompileTime
    241   };
    242   typedef Matrix<typename traits<T>::Scalar,
    243                 Rows,
    244                 Cols,
    245                 (MaxRows==1&&MaxCols!=1) ? RowMajor : ColMajor,
    246                 MaxRows,
    247                 MaxCols
    248           > type;
    249 };
    250 
    251 /* plain_matrix_type_row_major : same as plain_matrix_type but guaranteed to be row-major
    252  */
    253 template<typename T> struct plain_matrix_type_row_major
    254 {
    255   enum { Rows = traits<T>::RowsAtCompileTime,
    256          Cols = traits<T>::ColsAtCompileTime,
    257          MaxRows = traits<T>::MaxRowsAtCompileTime,
    258          MaxCols = traits<T>::MaxColsAtCompileTime
    259   };
    260   typedef Matrix<typename traits<T>::Scalar,
    261                 Rows,
    262                 Cols,
    263                 (MaxCols==1&&MaxRows!=1) ? RowMajor : ColMajor,
    264                 MaxRows,
    265                 MaxCols
    266           > type;
    267 };
    268 
    269 // we should be able to get rid of this one too
    270 template<typename T> struct must_nest_by_value { enum { ret = false }; };
    271 
    272 /** \internal The reference selector for template expressions. The idea is that we don't
    273   * need to use references for expressions since they are light weight proxy
    274   * objects which should generate no copying overhead. */
    275 template <typename T>
    276 struct ref_selector
    277 {
    278   typedef typename conditional<
    279     bool(traits<T>::Flags & NestByRefBit),
    280     T const&,
    281     const T
    282   >::type type;
    283 };
    284 
    285 /** \internal Adds the const qualifier on the value-type of T2 if and only if T1 is a const type */
    286 template<typename T1, typename T2>
    287 struct transfer_constness
    288 {
    289   typedef typename conditional<
    290     bool(internal::is_const<T1>::value),
    291     typename internal::add_const_on_value_type<T2>::type,
    292     T2
    293   >::type type;
    294 };
    295 
    296 /** \internal Determines how a given expression should be nested into another one.
    297   * For example, when you do a * (b+c), Eigen will determine how the expression b+c should be
    298   * nested into the bigger product expression. The choice is between nesting the expression b+c as-is, or
    299   * evaluating that expression b+c into a temporary variable d, and nest d so that the resulting expression is
    300   * a*d. Evaluating can be beneficial for example if every coefficient access in the resulting expression causes
    301   * many coefficient accesses in the nested expressions -- as is the case with matrix product for example.
    302   *
    303   * \param T the type of the expression being nested
    304   * \param n the number of coefficient accesses in the nested expression for each coefficient access in the bigger expression.
    305   *
    306   * Note that if no evaluation occur, then the constness of T is preserved.
    307   *
    308   * Example. Suppose that a, b, and c are of type Matrix3d. The user forms the expression a*(b+c).
    309   * b+c is an expression "sum of matrices", which we will denote by S. In order to determine how to nest it,
    310   * the Product expression uses: nested<S, 3>::ret, which turns out to be Matrix3d because the internal logic of
    311   * nested determined that in this case it was better to evaluate the expression b+c into a temporary. On the other hand,
    312   * since a is of type Matrix3d, the Product expression nests it as nested<Matrix3d, 3>::ret, which turns out to be
    313   * const Matrix3d&, because the internal logic of nested determined that since a was already a matrix, there was no point
    314   * in copying it into another matrix.
    315   */
    316 template<typename T, int n=1, typename PlainObject = typename eval<T>::type> struct nested
    317 {
    318   enum {
    319     // for the purpose of this test, to keep it reasonably simple, we arbitrarily choose a value of Dynamic values.
    320     // the choice of 10000 makes it larger than any practical fixed value and even most dynamic values.
    321     // in extreme cases where these assumptions would be wrong, we would still at worst suffer performance issues
    322     // (poor choice of temporaries).
    323     // it's important that this value can still be squared without integer overflowing.
    324     DynamicAsInteger = 10000,
    325     ScalarReadCost = NumTraits<typename traits<T>::Scalar>::ReadCost,
    326     ScalarReadCostAsInteger = ScalarReadCost == Dynamic ? int(DynamicAsInteger) : int(ScalarReadCost),
    327     CoeffReadCost = traits<T>::CoeffReadCost,
    328     CoeffReadCostAsInteger = CoeffReadCost == Dynamic ? int(DynamicAsInteger) : int(CoeffReadCost),
    329     NAsInteger = n == Dynamic ? int(DynamicAsInteger) : n,
    330     CostEvalAsInteger   = (NAsInteger+1) * ScalarReadCostAsInteger + CoeffReadCostAsInteger,
    331     CostNoEvalAsInteger = NAsInteger * CoeffReadCostAsInteger
    332   };
    333 
    334   typedef typename conditional<
    335       ( (int(traits<T>::Flags) & EvalBeforeNestingBit) ||
    336         int(CostEvalAsInteger) < int(CostNoEvalAsInteger)
    337       ),
    338       PlainObject,
    339       typename ref_selector<T>::type
    340   >::type type;
    341 };
    342 
    343 template<typename T>
    344 T* const_cast_ptr(const T* ptr)
    345 {
    346   return const_cast<T*>(ptr);
    347 }
    348 
    349 template<typename Derived, typename XprKind = typename traits<Derived>::XprKind>
    350 struct dense_xpr_base
    351 {
    352   /* dense_xpr_base should only ever be used on dense expressions, thus falling either into the MatrixXpr or into the ArrayXpr cases */
    353 };
    354 
    355 template<typename Derived>
    356 struct dense_xpr_base<Derived, MatrixXpr>
    357 {
    358   typedef MatrixBase<Derived> type;
    359 };
    360 
    361 template<typename Derived>
    362 struct dense_xpr_base<Derived, ArrayXpr>
    363 {
    364   typedef ArrayBase<Derived> type;
    365 };
    366 
    367 /** \internal Helper base class to add a scalar multiple operator
    368   * overloads for complex types */
    369 template<typename Derived,typename Scalar,typename OtherScalar,
    370          bool EnableIt = !is_same<Scalar,OtherScalar>::value >
    371 struct special_scalar_op_base : public DenseCoeffsBase<Derived>
    372 {
    373   // dummy operator* so that the
    374   // "using special_scalar_op_base::operator*" compiles
    375   void operator*() const;
    376 };
    377 
    378 template<typename Derived,typename Scalar,typename OtherScalar>
    379 struct special_scalar_op_base<Derived,Scalar,OtherScalar,true>  : public DenseCoeffsBase<Derived>
    380 {
    381   const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
    382   operator*(const OtherScalar& scalar) const
    383   {
    384     return CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
    385       (*static_cast<const Derived*>(this), scalar_multiple2_op<Scalar,OtherScalar>(scalar));
    386   }
    387 
    388   inline friend const CwiseUnaryOp<scalar_multiple2_op<Scalar,OtherScalar>, Derived>
    389   operator*(const OtherScalar& scalar, const Derived& matrix)
    390   { return static_cast<const special_scalar_op_base&>(matrix).operator*(scalar); }
    391 };
    392 
    393 template<typename XprType, typename CastType> struct cast_return_type
    394 {
    395   typedef typename XprType::Scalar CurrentScalarType;
    396   typedef typename remove_all<CastType>::type _CastType;
    397   typedef typename _CastType::Scalar NewScalarType;
    398   typedef typename conditional<is_same<CurrentScalarType,NewScalarType>::value,
    399                               const XprType&,CastType>::type type;
    400 };
    401 
    402 template <typename A, typename B> struct promote_storage_type;
    403 
    404 template <typename A> struct promote_storage_type<A,A>
    405 {
    406   typedef A ret;
    407 };
    408 
    409 /** \internal gives the plain matrix or array type to store a row/column/diagonal of a matrix type.
    410   * \param Scalar optional parameter allowing to pass a different scalar type than the one of the MatrixType.
    411   */
    412 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
    413 struct plain_row_type
    414 {
    415   typedef Matrix<Scalar, 1, ExpressionType::ColsAtCompileTime,
    416                  ExpressionType::PlainObject::Options | RowMajor, 1, ExpressionType::MaxColsAtCompileTime> MatrixRowType;
    417   typedef Array<Scalar, 1, ExpressionType::ColsAtCompileTime,
    418                  ExpressionType::PlainObject::Options | RowMajor, 1, ExpressionType::MaxColsAtCompileTime> ArrayRowType;
    419 
    420   typedef typename conditional<
    421     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
    422     MatrixRowType,
    423     ArrayRowType
    424   >::type type;
    425 };
    426 
    427 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
    428 struct plain_col_type
    429 {
    430   typedef Matrix<Scalar, ExpressionType::RowsAtCompileTime, 1,
    431                  ExpressionType::PlainObject::Options & ~RowMajor, ExpressionType::MaxRowsAtCompileTime, 1> MatrixColType;
    432   typedef Array<Scalar, ExpressionType::RowsAtCompileTime, 1,
    433                  ExpressionType::PlainObject::Options & ~RowMajor, ExpressionType::MaxRowsAtCompileTime, 1> ArrayColType;
    434 
    435   typedef typename conditional<
    436     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
    437     MatrixColType,
    438     ArrayColType
    439   >::type type;
    440 };
    441 
    442 template<typename ExpressionType, typename Scalar = typename ExpressionType::Scalar>
    443 struct plain_diag_type
    444 {
    445   enum { diag_size = EIGEN_SIZE_MIN_PREFER_DYNAMIC(ExpressionType::RowsAtCompileTime, ExpressionType::ColsAtCompileTime),
    446          max_diag_size = EIGEN_SIZE_MIN_PREFER_FIXED(ExpressionType::MaxRowsAtCompileTime, ExpressionType::MaxColsAtCompileTime)
    447   };
    448   typedef Matrix<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1> MatrixDiagType;
    449   typedef Array<Scalar, diag_size, 1, ExpressionType::PlainObject::Options & ~RowMajor, max_diag_size, 1> ArrayDiagType;
    450 
    451   typedef typename conditional<
    452     is_same< typename traits<ExpressionType>::XprKind, MatrixXpr >::value,
    453     MatrixDiagType,
    454     ArrayDiagType
    455   >::type type;
    456 };
    457 
    458 template<typename ExpressionType>
    459 struct is_lvalue
    460 {
    461   enum { value = !bool(is_const<ExpressionType>::value) &&
    462                  bool(traits<ExpressionType>::Flags & LvalueBit) };
    463 };
    464 
    465 } // end namespace internal
    466 
    467 } // end namespace Eigen
    468 
    469 #endif // EIGEN_XPRHELPER_H
    470