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