Medial Code Documentation
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15enum {
16 StandardCompressedFormat = 2
17};
18
19namespace internal {
20
21template<typename Derived> class SparseRefBase;
22
23template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
25 : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
26{
28 enum {
29 Options = _Options,
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 };
39 };
40
41};
42
43template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
45 : public traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
46{
47 enum {
49 };
50};
51
52template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
54 : public traits<SparseVector<MatScalar,MatOptions,MatIndex> >
55{
57 enum {
58 Options = _Options,
60 };
61
62 template<typename Derived> struct match {
63 enum {
64 MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && Derived::IsVectorAtCompileTime
65 };
67 };
68
69};
70
71template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
73 : public traits<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
74{
75 enum {
77 };
78};
79
80template<typename Derived>
81struct traits<SparseRefBase<Derived> > : public traits<Derived> {};
82
83template<typename Derived> class SparseRefBase
84 : public SparseMapBase<Derived>
85{
86public:
87
89 EIGEN_SPARSE_PUBLIC_INTERFACE(SparseRefBase)
90
92 : Base(RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime, 0, 0, 0, 0, 0)
93 {}
94
95protected:
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
122template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
123class Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType >
124 : public internal::SparseRefBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
125{
128 template<int OtherOptions>
130 template<int OtherOptions>
132 public:
133
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>
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
170template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
171class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
172 : public internal::SparseRefBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
173{
176 public:
177
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
238template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
239class Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType >
240 : public internal::SparseRefBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
241{
244 template<int OtherOptions>
246 public:
247
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>
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
273template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
274class Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType>
275 : public internal::SparseRefBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
276{
279 public:
280
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
319namespace 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
323template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
324struct evaluator<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
325 : evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
326{
328 typedef Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
329 evaluator() : Base() {}
330 explicit evaluator(const XprType &mat) : Base(mat) {}
331};
332
333template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
335 : evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
336{
339 evaluator() : Base() {}
340 explicit evaluator(const XprType &mat) : Base(mat) {}
341};
342
343template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
344struct evaluator<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
345 : evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
346{
348 typedef Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
349 evaluator() : Base() {}
350 explicit evaluator(const XprType &mat) : Base(mat) {}
351};
352
353template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
355 : evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
356{
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
A sparse matrix expression referencing an existing sparse expression.
Definition SparseRef.h:125
A sparse vector expression referencing an existing sparse vector expression.
Definition SparseRef.h:241
A matrix or vector expression mapping an existing expression.
Definition Ref.h:188
Pseudo expression representing a solving operation.
Definition Solve.h:63
Definition SparseMap.h:43
A versatible sparse matrix representation.
Definition SparseMatrix.h:94
a sparse vector class
Definition SparseVector.h:66
Definition SparseRef.h:85
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition Constants.h:138
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
const unsigned int CompressedAccessBit
Means that the underlying coefficients can be accessed through pointers to the sparse (un)compressed ...
Definition Constants.h:185
Definition CoreEvaluators.h:82
Definition Meta.h:31
Definition XprHelper.h:628
Definition ForwardDeclarations.h:17
Definition Meta.h:30