Medial Code Documentation
Loading...
Searching...
No Matches
SparseMatrixBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 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_SPARSEMATRIXBASE_H
11#define EIGEN_SPARSEMATRIXBASE_H
12
13namespace Eigen {
14
26template<typename Derived> class SparseMatrixBase
28 : public internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
29 typename NumTraits<typename internal::traits<Derived>::Scalar>::Real,
30 EigenBase<Derived> >
31#else
32 : public EigenBase<Derived>
33#endif // not EIGEN_PARSED_BY_DOXYGEN
34{
35 public:
36
37 typedef typename internal::traits<Derived>::Scalar Scalar;
38
42 typedef Scalar value_type;
43
44 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
45 typedef typename internal::traits<Derived>::StorageKind StorageKind;
46 typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
48 typename internal::packet_traits<Scalar>::type
49 >::type PacketReturnType;
50
52
55
56 template<typename OtherDerived>
57 Derived& operator=(const EigenBase<OtherDerived> &other);
58
59 enum {
60
80 MaxRowsAtCompileTime = RowsAtCompileTime,
81 MaxColsAtCompileTime = ColsAtCompileTime,
82
83 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
84 MaxColsAtCompileTime>::ret),
85
97 IsRowMajor = Flags&RowMajorBit ? 1 : 0,
98
99 InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
100 : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
101
103 _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
104 #endif
105 };
106
111 >::type AdjointReturnType;
114
115 // FIXME storage order do not match evaluator storage order
117
118#ifndef EIGEN_PARSED_BY_DOXYGEN
125 typedef typename NumTraits<Scalar>::Real RealScalar;
126
130
133
137 typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
139
140 inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
141 inline Derived& derived() { return *static_cast<Derived*>(this); }
142 inline Derived& const_cast_derived() const
143 { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
144
145 typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base;
146 using Base::operator*;
147 using Base::operator/;
148#endif // not EIGEN_PARSED_BY_DOXYGEN
149
150#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
151# include "../plugins/CommonCwiseUnaryOps.h"
152# include "../plugins/CommonCwiseBinaryOps.h"
153# include "../plugins/MatrixCwiseUnaryOps.h"
154# include "../plugins/MatrixCwiseBinaryOps.h"
155# include "../plugins/BlockMethods.h"
156# ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
157# include EIGEN_SPARSEMATRIXBASE_PLUGIN
158# endif
159# undef EIGEN_CURRENT_STORAGE_BASE_CLASS
160#undef EIGEN_CURRENT_STORAGE_BASE_CLASS
161
163 inline Index rows() const { return derived().rows(); }
165 inline Index cols() const { return derived().cols(); }
168 inline Index size() const { return rows() * cols(); }
173 inline bool isVector() const { return rows()==1 || cols()==1; }
176 Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
179 Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
180
181 bool isRValue() const { return m_isRValue; }
182 Derived& markAsRValue() { m_isRValue = true; return derived(); }
183
184 SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
185
186
187 template<typename OtherDerived>
188 Derived& operator=(const ReturnByValue<OtherDerived>& other);
189
190 template<typename OtherDerived>
191 inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
192
193 inline Derived& operator=(const Derived& other);
194
195 protected:
196
197 template<typename OtherDerived>
198 inline Derived& assign(const OtherDerived& other);
199
200 template<typename OtherDerived>
201 inline void assignGeneric(const OtherDerived& other);
202
203 public:
204
205 friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
206 {
207 typedef typename Derived::Nested Nested;
208 typedef typename internal::remove_all<Nested>::type NestedCleaned;
209
210 if (Flags&RowMajorBit)
211 {
212 const Nested nm(m.derived());
213 for (Index row=0; row<nm.outerSize(); ++row)
214 {
215 Index col = 0;
216 for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
217 {
218 for ( ; col<it.index(); ++col)
219 s << "0 ";
220 s << it.value() << " ";
221 ++col;
222 }
223 for ( ; col<m.cols(); ++col)
224 s << "0 ";
225 s << std::endl;
226 }
227 }
228 else
229 {
230 const Nested nm(m.derived());
231 if (m.cols() == 1) {
232 Index row = 0;
233 for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
234 {
235 for ( ; row<it.index(); ++row)
236 s << "0" << std::endl;
237 s << it.value() << std::endl;
238 ++row;
239 }
240 for ( ; row<m.rows(); ++row)
241 s << "0" << std::endl;
242 }
243 else
244 {
245 SparseMatrix<Scalar, RowMajorBit, StorageIndex> trans = m;
246 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
247 }
248 }
249 return s;
250 }
251
252 template<typename OtherDerived>
253 Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
254 template<typename OtherDerived>
255 Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
256
257 template<typename OtherDerived>
258 Derived& operator+=(const DiagonalBase<OtherDerived>& other);
259 template<typename OtherDerived>
260 Derived& operator-=(const DiagonalBase<OtherDerived>& other);
261
262 Derived& operator*=(const Scalar& other);
263 Derived& operator/=(const Scalar& other);
264
265 template<typename OtherDerived> struct CwiseProductDenseReturnType {
269 >::ReturnType>,
270 const Derived,
271 const OtherDerived
272 > Type;
273 };
274
275 template<typename OtherDerived>
276 EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
277 cwiseProduct(const MatrixBase<OtherDerived> &other) const;
278
279 // sparse * diagonal
280 template<typename OtherDerived>
282 operator*(const DiagonalBase<OtherDerived> &other) const
283 { return Product<Derived,OtherDerived>(derived(), other.derived()); }
284
285 // diagonal * sparse
286 template<typename OtherDerived> friend
288 operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
289 { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
290
291 // sparse * sparse
292 template<typename OtherDerived>
293 const Product<Derived,OtherDerived,AliasFreeProduct>
294 operator*(const SparseMatrixBase<OtherDerived> &other) const;
295
296 // sparse * dense
297 template<typename OtherDerived>
298 const Product<Derived,OtherDerived>
299 operator*(const MatrixBase<OtherDerived> &other) const
300 { return Product<Derived,OtherDerived>(derived(), other.derived()); }
301
302 // dense * sparse
303 template<typename OtherDerived> friend
304 const Product<OtherDerived,Derived>
305 operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
306 { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
307
313
314 template<typename OtherDerived>
315 Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
316
317 template<int Mode>
318 inline const TriangularView<const Derived, Mode> triangularView() const;
319
320 template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
321 template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
322
323 template<unsigned int UpLo> inline
324 typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
325 template<unsigned int UpLo> inline
326 typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
327
328 template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
329 template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
330 RealScalar squaredNorm() const;
331 RealScalar norm() const;
332 RealScalar blueNorm() const;
333
334 TransposeReturnType transpose() { return TransposeReturnType(derived()); }
335 const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
336 const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
337
338 // inner-vector
339 typedef Block<Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> InnerVectorReturnType;
340 typedef Block<const Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> ConstInnerVectorReturnType;
341 InnerVectorReturnType innerVector(Index outer);
342 const ConstInnerVectorReturnType innerVector(Index outer) const;
343
344 // set of inner-vectors
345 typedef Block<Derived,Dynamic,Dynamic,true> InnerVectorsReturnType;
346 typedef Block<const Derived,Dynamic,Dynamic,true> ConstInnerVectorsReturnType;
347 InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
348 const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
349
350 DenseMatrixType toDense() const
351 {
352 return DenseMatrixType(derived());
353 }
354
355 template<typename OtherDerived>
356 bool isApprox(const SparseMatrixBase<OtherDerived>& other,
357 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
358
359 template<typename OtherDerived>
360 bool isApprox(const MatrixBase<OtherDerived>& other,
361 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
362 { return toDense().isApprox(other,prec); }
363
369 inline const typename internal::eval<Derived>::type eval() const
370 { return typename internal::eval<Derived>::type(derived()); }
371
372 Scalar sum() const;
373
374 inline const SparseView<Derived>
375 pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
376
377 protected:
378
379 bool m_isRValue;
380
381 static inline StorageIndex convert_index(const Index idx) {
382 return internal::convert_index<StorageIndex>(idx);
383 }
384 private:
385 template<typename Dest> void evalTo(Dest &) const;
386};
387
388} // end namespace Eigen
389
390#endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:85
Pseudo expression representing a solving operation.
Definition Solve.h:63
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:34
Index size() const
Definition SparseMatrixBase.h:168
Index innerSize() const
Definition SparseMatrixBase.h:179
Index rows() const
Definition SparseMatrixBase.h:163
@ IsVectorAtCompileTime
This is set to true if either the number of rows or the number of columns is known at compile-time to...
Definition SparseMatrixBase.h:86
@ ColsAtCompileTime
The number of columns at compile-time.
Definition SparseMatrixBase.h:67
@ Flags
This stores expression Flags flags which may or may not be inherited by new expressions constructed f...
Definition SparseMatrixBase.h:92
@ RowsAtCompileTime
The number of rows at compile-time.
Definition SparseMatrixBase.h:61
@ SizeAtCompileTime
This is equal to the number of coefficients, i.e.
Definition SparseMatrixBase.h:74
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition SparseBlock.h:337
InnerVectorReturnType innerVector(Index outer)
Definition SparseBlock.h:322
bool isVector() const
Definition SparseMatrixBase.h:173
Matrix< Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime), EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType
type of the equivalent square matrix
Definition SparseMatrixBase.h:138
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition SparseMatrixBase.h:125
Scalar value_type
The numeric type of the expression' coefficients, e.g.
Definition SparseMatrixBase.h:42
Index outerSize() const
Definition SparseMatrixBase.h:176
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition SparseView.h:214
Index cols() const
Definition SparseMatrixBase.h:165
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
type of the equivalent dense matrix
Definition SparseMatrixBase.h:135
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition SparseMatrixBase.h:309
const internal::eval< Derived >::type eval() const
Definition SparseMatrixBase.h:369
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array.
Definition Constants.h:149
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition EigenBase.h:29
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:37
Holds information about the various numeric (i.e.
Definition NumTraits.h:108
Definition Meta.h:88
Definition Meta.h:34
Definition XprHelper.h:272
Definition BinaryFunctors.h:59
Definition XprHelper.h:222
Definition XprHelper.h:458
Definition ForwardDeclarations.h:17
Definition Meta.h:30