Ref.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_REF_H
11 #define EIGEN_REF_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename _PlainObjectType, int _Options, typename _StrideType>
18 struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
19  : public traits<Map<_PlainObjectType, _Options, _StrideType> >
20 {
21  typedef _PlainObjectType PlainObjectType;
22  typedef _StrideType StrideType;
23  enum {
24  Options = _Options,
27  };
28 
29  template<typename Derived> struct match {
30  enum {
31  IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
33  StorageOrderMatch = IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
34  InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
35  || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
36  || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
37  OuterStrideMatch = IsVectorAtCompileTime
38  || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
39  // NOTE, this indirection of evaluator<Derived>::Alignment is needed
40  // to workaround a very strange bug in MSVC related to the instantiation
41  // of has_*ary_operator in evaluator<CwiseNullaryOp>.
42  // This line is surprisingly very sensitive. For instance, simply adding parenthesis
43  // as "DerivedAlignment = (int(evaluator<Derived>::Alignment))," will make MSVC fail...
44  DerivedAlignment = int(evaluator<Derived>::Alignment),
45  AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (DerivedAlignment >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
47  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
48  };
50  };
51 
52 };
53 
54 template<typename Derived>
55 struct traits<RefBase<Derived> > : public traits<Derived> {};
56 
57 }
58 
59 template<typename Derived> class RefBase
60  : public MapBase<Derived>
61 {
64 
65 public:
66 
69 
71  {
72  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
73  }
74 
76  {
77  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
78  : IsVectorAtCompileTime ? this->size()
79  : int(Flags)&RowMajorBit ? this->cols()
80  : this->rows();
81  }
82 
84  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
85  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
86  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
87  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
88  {}
89 
91 
92 protected:
93 
95 
96  // Resolves inner stride if default 0.
98  return inner == 0 ? 1 : inner;
99  }
100 
101  // Resolves outer stride if default 0.
102  static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveOuterStride(Index inner, Index outer, Index rows, Index cols, bool isVectorAtCompileTime, bool isRowMajor) {
103  return outer == 0 ? isVectorAtCompileTime ? inner * rows * cols : isRowMajor ? inner * cols : inner * rows : outer;
104  }
105 
106  // Returns true if construction is valid, false if there is a stride mismatch,
107  // and fails if there is a size mismatch.
108  template<typename Expression>
109  EIGEN_DEVICE_FUNC bool construct(Expression& expr)
110  {
111  // Check matrix sizes. If this is a compile-time vector, we do allow
112  // implicitly transposing.
115  // If it is a vector, the transpose sizes might match.
116  || ( PlainObjectType::IsVectorAtCompileTime
117  && ((int(PlainObjectType::RowsAtCompileTime)==Eigen::Dynamic
118  || int(Expression::ColsAtCompileTime)==Eigen::Dynamic
119  || int(PlainObjectType::RowsAtCompileTime)==int(Expression::ColsAtCompileTime))
120  && (int(PlainObjectType::ColsAtCompileTime)==Eigen::Dynamic
121  || int(Expression::RowsAtCompileTime)==Eigen::Dynamic
122  || int(PlainObjectType::ColsAtCompileTime)==int(Expression::RowsAtCompileTime)))),
123  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
124  )
125 
126  // Determine runtime rows and columns.
127  Index rows = expr.rows();
128  Index cols = expr.cols();
129  if(PlainObjectType::RowsAtCompileTime==1)
130  {
131  eigen_assert(expr.rows()==1 || expr.cols()==1);
132  rows = 1;
133  cols = expr.size();
134  }
135  else if(PlainObjectType::ColsAtCompileTime==1)
136  {
137  eigen_assert(expr.rows()==1 || expr.cols()==1);
138  rows = expr.size();
139  cols = 1;
140  }
141  // Verify that the sizes are valid.
142  eigen_assert(
143  (PlainObjectType::RowsAtCompileTime == Dynamic) || (PlainObjectType::RowsAtCompileTime == rows));
144  eigen_assert(
145  (PlainObjectType::ColsAtCompileTime == Dynamic) || (PlainObjectType::ColsAtCompileTime == cols));
146 
147 
148  // If this is a vector, we might be transposing, which means that stride should swap.
149  const bool transpose = PlainObjectType::IsVectorAtCompileTime && (rows != expr.rows());
150  // If the storage format differs, we also need to swap the stride.
151  const bool row_major = ((PlainObjectType::Flags)&RowMajorBit) != 0;
152  const bool expr_row_major = (Expression::Flags&RowMajorBit) != 0;
153  const bool storage_differs = (row_major != expr_row_major);
154 
155  const bool swap_stride = (transpose != storage_differs);
156 
157  // Determine expr's actual strides, resolving any defaults if zero.
158  const Index expr_inner_actual = resolveInnerStride(expr.innerStride());
159  const Index expr_outer_actual = resolveOuterStride(expr_inner_actual,
160  expr.outerStride(),
161  expr.rows(),
162  expr.cols(),
163  Expression::IsVectorAtCompileTime != 0,
164  expr_row_major);
165 
166  // If this is a column-major row vector or row-major column vector, the inner-stride
167  // is arbitrary, so set it to either the compile-time inner stride or 1.
168  const bool row_vector = (rows == 1);
169  const bool col_vector = (cols == 1);
170  const Index inner_stride =
171  ( (!row_major && row_vector) || (row_major && col_vector) ) ?
172  ( StrideType::InnerStrideAtCompileTime > 0 ? Index(StrideType::InnerStrideAtCompileTime) : 1)
173  : swap_stride ? expr_outer_actual : expr_inner_actual;
174 
175  // If this is a column-major column vector or row-major row vector, the outer-stride
176  // is arbitrary, so set it to either the compile-time outer stride or vector size.
177  const Index outer_stride =
178  ( (!row_major && col_vector) || (row_major && row_vector) ) ?
179  ( StrideType::OuterStrideAtCompileTime > 0 ? Index(StrideType::OuterStrideAtCompileTime) : rows * cols * inner_stride)
180  : swap_stride ? expr_inner_actual : expr_outer_actual;
181 
182  // Check if given inner/outer strides are compatible with compile-time strides.
183  const bool inner_valid = (StrideType::InnerStrideAtCompileTime == Dynamic)
184  || (resolveInnerStride(Index(StrideType::InnerStrideAtCompileTime)) == inner_stride);
185  if (!inner_valid) {
186  return false;
187  }
188 
189  const bool outer_valid = (StrideType::OuterStrideAtCompileTime == Dynamic)
190  || (resolveOuterStride(
191  inner_stride,
192  Index(StrideType::OuterStrideAtCompileTime),
193  rows, cols, PlainObjectType::IsVectorAtCompileTime != 0,
194  row_major)
195  == outer_stride);
196  if (!outer_valid) {
197  return false;
198  }
199 
200  ::new (static_cast<Base*>(this)) Base(expr.data(), rows, cols);
201  ::new (&m_stride) StrideBase(
202  (StrideType::OuterStrideAtCompileTime == 0) ? 0 : outer_stride,
203  (StrideType::InnerStrideAtCompileTime == 0) ? 0 : inner_stride );
204  return true;
205  }
206 
208 };
209 
281 template<typename PlainObjectType, int Options, typename StrideType> class Ref
282  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
283 {
284  private:
286  template<typename Derived>
288  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
289  public:
290 
293 
294 
295  #ifndef EIGEN_PARSED_BY_DOXYGEN
296  template<typename Derived>
298  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
299  {
300  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
301  // Construction must pass since we will not create temprary storage in the non-const case.
302  const bool success = Base::construct(expr.derived());
303  EIGEN_UNUSED_VARIABLE(success)
304  eigen_assert(success);
305  }
306  template<typename Derived>
308  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
309  #else
310 
311  template<typename Derived>
312  inline Ref(DenseBase<Derived>& expr)
313  #endif
314  {
315  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
316  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
317  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
318  // Construction must pass since we will not create temporary storage in the non-const case.
319  const bool success = Base::construct(expr.const_cast_derived());
320  EIGEN_UNUSED_VARIABLE(success)
321  eigen_assert(success);
322  }
323 
325 
326 };
327 
328 // this is the const ref version
329 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
330  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
331 {
333  public:
334 
337 
338  template<typename Derived>
340  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
341  {
342 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
343 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
344 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
345  construct(expr.derived(), typename Traits::template match<Derived>::type());
346  }
347 
348  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
349  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
350  }
351 
352  template<typename OtherRef>
354  construct(other.derived(), typename Traits::template match<OtherRef>::type());
355  }
356 
357  protected:
358 
359  template<typename Expression>
360  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
361  {
362  // Check if we can use the underlying expr's storage directly, otherwise call the copy version.
363  if (!Base::construct(expr)) {
365  }
366  }
367 
368  template<typename Expression>
369  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
370  {
372  Base::construct(m_object);
373  }
374 
375  protected:
376  TPlainObjectType m_object;
377 };
378 
379 } // end namespace Eigen
380 
381 #endif // EIGEN_REF_H
Eigen::RefBase::construct
EIGEN_DEVICE_FUNC bool construct(Expression &expr)
Definition: Ref.h:109
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
Eigen::Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime >
Eigen::Ref< const TPlainObjectType, Options, StrideType >::Ref
EIGEN_DEVICE_FUNC Ref(const RefBase< OtherRef > &other)
Definition: Ref.h:353
Eigen::RefBase::resolveInnerStride
static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveInnerStride(Index inner)
Definition: Ref.h:97
Eigen::Stride::inner
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index inner() const
Definition: Stride.h:85
Eigen::Ref< const TPlainObjectType, Options, StrideType >::Traits
internal::traits< Ref > Traits
Definition: Ref.h:332
Eigen::Unaligned
@ Unaligned
Definition: Constants.h:233
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::internal::is_lvalue
Definition: XprHelper.h:659
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::internal::false_type
Definition: Meta.h:97
EIGEN_CONSTEXPR
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
type
Definition: pytypes.h:1525
EIGEN_DENSE_PUBLIC_INTERFACE
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1283
Eigen::internal::call_assignment_no_alias
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
Definition: AssignEvaluator.h:873
EIGEN_PREDICATE_SAME_MATRIX_SIZE
#define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:174
Eigen::RefBase::m_stride
StrideBase m_stride
Definition: Ref.h:207
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::internal::traits< Ref< _PlainObjectType, _Options, _StrideType > >::StrideType
_StrideType StrideType
Definition: Ref.h:22
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::RefBase::innerStride
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const
Definition: Ref.h:70
Eigen::RefBase
Definition: Ref.h:59
EIGEN_UNUSED_VARIABLE
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1076
match
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Definition: indexed_view.cpp:54
Eigen::Ref< const TPlainObjectType, Options, StrideType >::m_object
TPlainObjectType m_object
Definition: Ref.h:376
Eigen::MapBase
Definition: ForwardDeclarations.h:112
Eigen::RefBase::PlainObjectType
internal::traits< Derived >::PlainObjectType PlainObjectType
Definition: Ref.h:62
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::Stride::outer
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outer() const
Definition: Stride.h:82
Eigen::RefBase::Base
MapBase< Derived > Base
Definition: Ref.h:67
Eigen::PlainObjectBase
Definition: PlainObjectBase.h:98
Eigen::Ref< const TPlainObjectType, Options, StrideType >::construct
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::false_type)
Definition: Ref.h:369
Eigen::RefBase::outerStride
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: Ref.h:75
Eigen::Ref::Ref
EIGEN_DEVICE_FUNC Ref(const PlainObjectBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::Ref< const TPlainObjectType, Options, StrideType >::Base
RefBase< Ref > Base
Definition: Ref.h:335
Eigen::internal::assign_op
Definition: AssignmentFunctors.h:21
Eigen::Ref::Traits
internal::traits< Ref > Traits
Definition: Ref.h:285
Eigen::Ref< const TPlainObjectType, Options, StrideType >::construct
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::true_type)
Definition: Ref.h:360
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
Eigen::RefBase::resolveOuterStride
static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveOuterStride(Index inner, Index outer, Index rows, Index cols, bool isVectorAtCompileTime, bool isRowMajor)
Definition: Ref.h:102
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::DenseBase
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Eigen::RefBase::StrideType
internal::traits< Derived >::StrideType StrideType
Definition: Ref.h:63
Eigen::Ref< const TPlainObjectType, Options, StrideType >::Ref
EIGEN_DEVICE_FUNC Ref(const DenseBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::ScalarTypeMatch), Derived >::type *=0)
Definition: Ref.h:339
Eigen::internal::traits< Ref< _PlainObjectType, _Options, _StrideType > >::match::type
internal::conditional< MatchAtCompileTime, internal::true_type, internal::false_type >::type type
Definition: Ref.h:49
Eigen::RefBase::StrideBase
Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime > StrideBase
Definition: Ref.h:94
Eigen::Ref::Ref
EIGEN_DEVICE_FUNC Ref(const DenseBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:307
Eigen::internal::is_same
Definition: Meta.h:148
Eigen::Ref< const TPlainObjectType, Options, StrideType >::Ref
EIGEN_DEVICE_FUNC Ref(const Ref &other)
Definition: Ref.h:348
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::enable_if
Definition: Meta.h:273
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::NestByRefBit
const unsigned int NestByRefBit
Definition: Constants.h:169
EIGEN_INHERIT_ASSIGNMENT_OPERATORS
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1231
Eigen::RefBase::RefBase
EIGEN_DEVICE_FUNC RefBase()
Definition: Ref.h:83
Eigen::internal::traits< Ref< _PlainObjectType, _Options, _StrideType > >::PlainObjectType
_PlainObjectType PlainObjectType
Definition: Ref.h:21
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::Ref::Ref
EIGEN_DEVICE_FUNC Ref(PlainObjectBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:297
Eigen::internal::has_direct_access
Definition: ForwardDeclarations.h:25
Eigen::Ref::Base
RefBase< Ref > Base
Definition: Ref.h:291
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:58