Eigen  3.2.92
SparseRef.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 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_SPARSE_REF_H
11 #define EIGEN_SPARSE_REF_H
12 
13 namespace Eigen {
14 
15 enum {
16  StandardCompressedFormat = 2
17 };
18 
19 namespace internal {
20 
21 template<typename Derived> class SparseRefBase;
22 
23 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
24 struct traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
25  : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
26 {
27  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
28  enum {
29  Options = _Options,
30  Flags = traits<PlainObjectType>::Flags | CompressedAccessBit | NestByRefBit
31  };
32 
33  template<typename Derived> struct match {
34  enum {
35  StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
36  MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && StorageOrderMatch
37  };
38  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
39  };
40 
41 };
42 
43 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
44 struct traits<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
45  : public traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
46 {
47  enum {
48  Flags = (traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit) & ~LvalueBit
49  };
50 };
51 
52 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
53 struct traits<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
54  : public traits<SparseVector<MatScalar,MatOptions,MatIndex> >
55 {
56  typedef SparseVector<MatScalar,MatOptions,MatIndex> PlainObjectType;
57  enum {
58  Options = _Options,
59  Flags = traits<PlainObjectType>::Flags | CompressedAccessBit | NestByRefBit
60  };
61 
62  template<typename Derived> struct match {
63  enum {
64  MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && Derived::IsVectorAtCompileTime
65  };
66  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
67  };
68 
69 };
70 
71 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
72 struct traits<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
73  : public traits<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
74 {
75  enum {
76  Flags = (traits<SparseVector<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit) & ~LvalueBit
77  };
78 };
79 
80 template<typename Derived>
81 struct traits<SparseRefBase<Derived> > : public traits<Derived> {};
82 
83 template<typename Derived> class SparseRefBase
84  : public SparseMapBase<Derived>
85 {
86 public:
87 
88  typedef SparseMapBase<Derived> Base;
89  EIGEN_SPARSE_PUBLIC_INTERFACE(SparseRefBase)
90 
91  SparseRefBase()
92  : Base(RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime, 0, 0, 0, 0, 0)
93  {}
94 
95 protected:
96 
97  template<typename Expression>
98  void construct(Expression& expr)
99  {
100  if(expr.outerIndexPtr()==0)
101  ::new (static_cast<Base*>(this)) Base(expr.size(), expr.nonZeros(), expr.innerIndexPtr(), expr.valuePtr());
102  else
103  ::new (static_cast<Base*>(this)) Base(expr.rows(), expr.cols(), expr.nonZeros(), expr.outerIndexPtr(), expr.innerIndexPtr(), expr.valuePtr(), expr.innerNonZeroPtr());
104  }
105 };
106 
107 } // namespace internal
108 
109 
122 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
123 class Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType >
124  : public internal::SparseRefBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
125 {
126  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
127  typedef internal::traits<Ref> Traits;
128  template<int OtherOptions>
130  template<int OtherOptions>
132  public:
133 
134  typedef internal::SparseRefBase<Ref> Base;
135  EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
136 
137 
138  #ifndef EIGEN_PARSED_BY_DOXYGEN
139  template<int OtherOptions>
141  {
142  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
143  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
144  Base::construct(expr.derived());
145  }
146 
147  template<int OtherOptions>
149  {
150  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
151  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
152  Base::construct(expr.derived());
153  }
154 
155  template<typename Derived>
156  inline Ref(const SparseCompressedBase<Derived>& expr)
157  #else
158  template<typename Derived>
159  inline Ref(SparseCompressedBase<Derived>& expr)
160  #endif
161  {
162  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
163  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
164  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
165  Base::construct(expr.const_cast_derived());
166  }
167 };
168 
169 // this is the const ref version
170 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
171 class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
172  : public internal::SparseRefBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
173 {
174  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> TPlainObjectType;
175  typedef internal::traits<Ref> Traits;
176  public:
177 
178  typedef internal::SparseRefBase<Ref> Base;
179  EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
180 
181  template<typename Derived>
182  inline Ref(const SparseMatrixBase<Derived>& expr)
183  {
184  construct(expr.derived(), typename Traits::template match<Derived>::type());
185  }
186 
187  inline Ref(const Ref& other) : Base(other) {
188  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
189  }
190 
191  template<typename OtherRef>
192  inline Ref(const RefBase<OtherRef>& other) {
193  construct(other.derived(), typename Traits::template match<OtherRef>::type());
194  }
195 
196  protected:
197 
198  template<typename Expression>
199  void construct(const Expression& expr,internal::true_type)
200  {
201  if((Options & int(StandardCompressedFormat)) && (!expr.isCompressed()))
202  {
203  TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes);
204  ::new (obj) TPlainObjectType(expr);
205  Base::construct(*obj);
206  }
207  else
208  {
209  Base::construct(expr);
210  }
211  }
212 
213  template<typename Expression>
214  void construct(const Expression& expr, internal::false_type)
215  {
216  TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes);
217  ::new (obj) TPlainObjectType(expr);
218  Base::construct(*obj);
219  }
220 
221  protected:
222  char m_object_bytes[sizeof(TPlainObjectType)];
223 };
224 
225 
226 
238 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
239 class Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType >
240  : public internal::SparseRefBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
241 {
242  typedef SparseVector<MatScalar,MatOptions,MatIndex> PlainObjectType;
243  typedef internal::traits<Ref> Traits;
244  template<int OtherOptions>
246  public:
247 
248  typedef internal::SparseRefBase<Ref> Base;
249  EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
250 
251  #ifndef EIGEN_PARSED_BY_DOXYGEN
252  template<int OtherOptions>
254  {
255  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseVector<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
256  Base::construct(expr.derived());
257  }
258 
259  template<typename Derived>
260  inline Ref(const SparseCompressedBase<Derived>& expr)
261  #else
262  template<typename Derived>
263  inline Ref(SparseCompressedBase<Derived>& expr)
264  #endif
265  {
266  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
267  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
268  Base::construct(expr.const_cast_derived());
269  }
270 };
271 
272 // this is the const ref version
273 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
274 class Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType>
275  : public internal::SparseRefBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
276 {
277  typedef SparseVector<MatScalar,MatOptions,MatIndex> TPlainObjectType;
278  typedef internal::traits<Ref> Traits;
279  public:
280 
281  typedef internal::SparseRefBase<Ref> Base;
282  EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
283 
284  template<typename Derived>
285  inline Ref(const SparseMatrixBase<Derived>& expr)
286  {
287  construct(expr.derived(), typename Traits::template match<Derived>::type());
288  }
289 
290  inline Ref(const Ref& other) : Base(other) {
291  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
292  }
293 
294  template<typename OtherRef>
295  inline Ref(const RefBase<OtherRef>& other) {
296  construct(other.derived(), typename Traits::template match<OtherRef>::type());
297  }
298 
299  protected:
300 
301  template<typename Expression>
302  void construct(const Expression& expr,internal::true_type)
303  {
304  Base::construct(expr);
305  }
306 
307  template<typename Expression>
308  void construct(const Expression& expr, internal::false_type)
309  {
310  TPlainObjectType* obj = reinterpret_cast<TPlainObjectType*>(m_object_bytes);
311  ::new (obj) TPlainObjectType(expr);
312  Base::construct(*obj);
313  }
314 
315  protected:
316  char m_object_bytes[sizeof(TPlainObjectType)];
317 };
318 
319 namespace internal {
320 
321 // FIXME shall we introduce a general evaluatior_ref that we can specialize for any sparse object once, and thus remove this copy-pasta thing...
322 
323 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
324 struct evaluator<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
325  : evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
326 {
327  typedef evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
328  typedef Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
329  evaluator() : Base() {}
330  explicit evaluator(const XprType &mat) : Base(mat) {}
331 };
332 
333 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
334 struct evaluator<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
335  : evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
336 {
337  typedef evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
338  typedef Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
339  evaluator() : Base() {}
340  explicit evaluator(const XprType &mat) : Base(mat) {}
341 };
342 
343 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
344 struct evaluator<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
345  : evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
346 {
347  typedef evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
348  typedef Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
349  evaluator() : Base() {}
350  explicit evaluator(const XprType &mat) : Base(mat) {}
351 };
352 
353 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
354 struct evaluator<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
355  : evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
356 {
357  typedef evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
358  typedef Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
359  evaluator() : Base() {}
360  explicit evaluator(const XprType &mat) : Base(mat) {}
361 };
362 
363 }
364 
365 } // end namespace Eigen
366 
367 #endif // EIGEN_SPARSE_REF_H
const unsigned int CompressedAccessBit
Definition: Constants.h:185
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
const unsigned int LvalueBit
Definition: Constants.h:138
Definition: LDLT.h:16
Derived & derived()
Definition: EigenBase.h:44
const unsigned int RowMajorBit
Definition: Constants.h:61
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:278
a sparse vector class
Definition: SparseUtil.h:54
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:186
Definition: Eigen_Colamd.h:54
Sparse matrix.
Definition: MappedSparseMatrix.h:32