source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen/src/SparseCore/SparseMatrixBase.h@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 18.7 KB
Line 
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2011 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
15/** \ingroup SparseCore_Module
16 *
17 * \class SparseMatrixBase
18 *
19 * \brief Base class of any sparse matrices or sparse expressions
20 *
21 * \tparam Derived
22 *
23 * This class can be extended with the help of the plugin mechanism described on the page
24 * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_SPARSEMATRIXBASE_PLUGIN.
25 */
26template<typename Derived> class SparseMatrixBase
27#ifndef EIGEN_PARSED_BY_DOXYGEN
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 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
39 typedef typename internal::traits<Derived>::StorageKind StorageKind;
40 typedef typename internal::traits<Derived>::Index Index;
41 typedef typename internal::traits<Derived>::Index StorageIndex;
42 typedef typename internal::add_const_on_value_type_if_arithmetic<
43 typename internal::packet_traits<Scalar>::type
44 >::type PacketReturnType;
45
46 typedef SparseMatrixBase StorageBaseType;
47
48 template<typename OtherDerived>
49 Derived& operator=(const EigenBase<OtherDerived> &other)
50 {
51 other.derived().evalTo(derived());
52 return derived();
53 }
54
55 enum {
56
57 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
58 /**< The number of rows at compile-time. This is just a copy of the value provided
59 * by the \a Derived type. If a value is not known at compile-time,
60 * it is set to the \a Dynamic constant.
61 * \sa MatrixBase::rows(), MatrixBase::cols(), ColsAtCompileTime, SizeAtCompileTime */
62
63 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
64 /**< The number of columns at compile-time. This is just a copy of the value provided
65 * by the \a Derived type. If a value is not known at compile-time,
66 * it is set to the \a Dynamic constant.
67 * \sa MatrixBase::rows(), MatrixBase::cols(), RowsAtCompileTime, SizeAtCompileTime */
68
69
70 SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
71 internal::traits<Derived>::ColsAtCompileTime>::ret),
72 /**< This is equal to the number of coefficients, i.e. the number of
73 * rows times the number of columns, or to \a Dynamic if this is not
74 * known at compile-time. \sa RowsAtCompileTime, ColsAtCompileTime */
75
76 MaxRowsAtCompileTime = RowsAtCompileTime,
77 MaxColsAtCompileTime = ColsAtCompileTime,
78
79 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
80 MaxColsAtCompileTime>::ret),
81
82 IsVectorAtCompileTime = RowsAtCompileTime == 1 || ColsAtCompileTime == 1,
83 /**< This is set to true if either the number of rows or the number of
84 * columns is known at compile-time to be equal to 1. Indeed, in that case,
85 * we are dealing with a column-vector (if there is only one column) or with
86 * a row-vector (if there is only one row). */
87
88 Flags = internal::traits<Derived>::Flags,
89 /**< This stores expression \ref flags flags which may or may not be inherited by new expressions
90 * constructed from this one. See the \ref flags "list of flags".
91 */
92
93 CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
94 /**< This is a rough measure of how expensive it is to read one coefficient from
95 * this expression.
96 */
97
98 IsRowMajor = Flags&RowMajorBit ? 1 : 0,
99
100 InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
101 : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
102
103 #ifndef EIGEN_PARSED_BY_DOXYGEN
104 _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
105 #endif
106 };
107
108 /** \internal the return type of MatrixBase::adjoint() */
109 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
110 CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, Eigen::Transpose<const Derived> >,
111 Transpose<const Derived>
112 >::type AdjointReturnType;
113
114
115 typedef SparseMatrix<Scalar, Flags&RowMajorBit ? RowMajor : ColMajor, Index> PlainObject;
116
117
118#ifndef EIGEN_PARSED_BY_DOXYGEN
119 /** This is the "real scalar" type; if the \a Scalar type is already real numbers
120 * (e.g. int, float or double) then \a RealScalar is just the same as \a Scalar. If
121 * \a Scalar is \a std::complex<T> then RealScalar is \a T.
122 *
123 * \sa class NumTraits
124 */
125 typedef typename NumTraits<Scalar>::Real RealScalar;
126
127 /** \internal the return type of coeff()
128 */
129 typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
130
131 /** \internal Represents a matrix with all coefficients equal to one another*/
132 typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Matrix<Scalar,Dynamic,Dynamic> > ConstantReturnType;
133
134 /** type of the equivalent square matrix */
135 typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
136 EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
137
138 inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
139 inline Derived& derived() { return *static_cast<Derived*>(this); }
140 inline Derived& const_cast_derived() const
141 { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
142
143 typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base;
144 using Base::operator*;
145#endif // not EIGEN_PARSED_BY_DOXYGEN
146
147#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
148# include "../plugins/CommonCwiseUnaryOps.h"
149# include "../plugins/CommonCwiseBinaryOps.h"
150# include "../plugins/MatrixCwiseUnaryOps.h"
151# include "../plugins/MatrixCwiseBinaryOps.h"
152# include "../plugins/BlockMethods.h"
153# ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
154# include EIGEN_SPARSEMATRIXBASE_PLUGIN
155# endif
156# undef EIGEN_CURRENT_STORAGE_BASE_CLASS
157#undef EIGEN_CURRENT_STORAGE_BASE_CLASS
158
159 /** \returns the number of rows. \sa cols() */
160 inline Index rows() const { return derived().rows(); }
161 /** \returns the number of columns. \sa rows() */
162 inline Index cols() const { return derived().cols(); }
163 /** \returns the number of coefficients, which is \a rows()*cols().
164 * \sa rows(), cols(). */
165 inline Index size() const { return rows() * cols(); }
166 /** \returns the number of nonzero coefficients which is in practice the number
167 * of stored coefficients. */
168 inline Index nonZeros() const { return derived().nonZeros(); }
169 /** \returns true if either the number of rows or the number of columns is equal to 1.
170 * In other words, this function returns
171 * \code rows()==1 || cols()==1 \endcode
172 * \sa rows(), cols(), IsVectorAtCompileTime. */
173 inline bool isVector() const { return rows()==1 || cols()==1; }
174 /** \returns the size of the storage major dimension,
175 * i.e., the number of columns for a columns major matrix, and the number of rows otherwise */
176 Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
177 /** \returns the size of the inner dimension according to the storage order,
178 * i.e., the number of rows for a columns major matrix, and the number of cols otherwise */
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 other.evalTo(derived());
191 return derived();
192 }
193
194
195 template<typename OtherDerived>
196 inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other)
197 {
198 return assign(other.derived());
199 }
200
201 inline Derived& operator=(const Derived& other)
202 {
203// if (other.isRValue())
204// derived().swap(other.const_cast_derived());
205// else
206 return assign(other.derived());
207 }
208
209 protected:
210
211 template<typename OtherDerived>
212 inline Derived& assign(const OtherDerived& other)
213 {
214 const bool transpose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit);
215 const Index outerSize = (int(OtherDerived::Flags) & RowMajorBit) ? other.rows() : other.cols();
216 if ((!transpose) && other.isRValue())
217 {
218 // eval without temporary
219 derived().resize(other.rows(), other.cols());
220 derived().setZero();
221 derived().reserve((std::max)(this->rows(),this->cols())*2);
222 for (Index j=0; j<outerSize; ++j)
223 {
224 derived().startVec(j);
225 for (typename OtherDerived::InnerIterator it(other, j); it; ++it)
226 {
227 Scalar v = it.value();
228 derived().insertBackByOuterInner(j,it.index()) = v;
229 }
230 }
231 derived().finalize();
232 }
233 else
234 {
235 assignGeneric(other);
236 }
237 return derived();
238 }
239
240 template<typename OtherDerived>
241 inline void assignGeneric(const OtherDerived& other)
242 {
243 //const bool transpose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit);
244 eigen_assert(( ((internal::traits<Derived>::SupportedAccessPatterns&OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
245 (!((Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit)))) &&
246 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
247
248 enum { Flip = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit) };
249
250 const Index outerSize = other.outerSize();
251 //typedef typename internal::conditional<transpose, LinkedVectorMatrix<Scalar,Flags&RowMajorBit>, Derived>::type TempType;
252 // thanks to shallow copies, we always eval to a tempary
253 Derived temp(other.rows(), other.cols());
254
255 temp.reserve((std::max)(this->rows(),this->cols())*2);
256 for (Index j=0; j<outerSize; ++j)
257 {
258 temp.startVec(j);
259 for (typename OtherDerived::InnerIterator it(other.derived(), j); it; ++it)
260 {
261 Scalar v = it.value();
262 temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
263 }
264 }
265 temp.finalize();
266
267 derived() = temp.markAsRValue();
268 }
269
270 public:
271
272 template<typename Lhs, typename Rhs>
273 inline Derived& operator=(const SparseSparseProduct<Lhs,Rhs>& product);
274
275 friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
276 {
277 typedef typename Derived::Nested Nested;
278 typedef typename internal::remove_all<Nested>::type NestedCleaned;
279
280 if (Flags&RowMajorBit)
281 {
282 const Nested nm(m.derived());
283 for (Index row=0; row<nm.outerSize(); ++row)
284 {
285 Index col = 0;
286 for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
287 {
288 for ( ; col<it.index(); ++col)
289 s << "0 ";
290 s << it.value() << " ";
291 ++col;
292 }
293 for ( ; col<m.cols(); ++col)
294 s << "0 ";
295 s << std::endl;
296 }
297 }
298 else
299 {
300 const Nested nm(m.derived());
301 if (m.cols() == 1) {
302 Index row = 0;
303 for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
304 {
305 for ( ; row<it.index(); ++row)
306 s << "0" << std::endl;
307 s << it.value() << std::endl;
308 ++row;
309 }
310 for ( ; row<m.rows(); ++row)
311 s << "0" << std::endl;
312 }
313 else
314 {
315 SparseMatrix<Scalar, RowMajorBit, Index> trans = m;
316 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, Index> >&>(trans);
317 }
318 }
319 return s;
320 }
321
322 template<typename OtherDerived>
323 Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
324 template<typename OtherDerived>
325 Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
326
327 Derived& operator*=(const Scalar& other);
328 Derived& operator/=(const Scalar& other);
329
330 template<typename OtherDerived> struct CwiseProductDenseReturnType {
331 typedef CwiseBinaryOp<internal::scalar_product_op<typename internal::scalar_product_traits<
332 typename internal::traits<Derived>::Scalar,
333 typename internal::traits<OtherDerived>::Scalar
334 >::ReturnType>,
335 const Derived,
336 const OtherDerived
337 > Type;
338 };
339
340 template<typename OtherDerived>
341 EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
342 cwiseProduct(const MatrixBase<OtherDerived> &other) const;
343
344 // sparse * sparse
345 template<typename OtherDerived>
346 const typename SparseSparseProductReturnType<Derived,OtherDerived>::Type
347 operator*(const SparseMatrixBase<OtherDerived> &other) const;
348
349 // sparse * diagonal
350 template<typename OtherDerived>
351 const SparseDiagonalProduct<Derived,OtherDerived>
352 operator*(const DiagonalBase<OtherDerived> &other) const;
353
354 // diagonal * sparse
355 template<typename OtherDerived> friend
356 const SparseDiagonalProduct<OtherDerived,Derived>
357 operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
358 { return SparseDiagonalProduct<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
359
360 /** dense * sparse (return a dense object unless it is an outer product) */
361 template<typename OtherDerived> friend
362 const typename DenseSparseProductReturnType<OtherDerived,Derived>::Type
363 operator*(const MatrixBase<OtherDerived>& lhs, const Derived& rhs)
364 { return typename DenseSparseProductReturnType<OtherDerived,Derived>::Type(lhs.derived(),rhs); }
365
366 /** sparse * dense (returns a dense object unless it is an outer product) */
367 template<typename OtherDerived>
368 const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
369 operator*(const MatrixBase<OtherDerived> &other) const
370 { return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived()); }
371
372 /** \returns an expression of P H P^-1 where H is the matrix represented by \c *this */
373 SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,Index>& perm) const
374 {
375 return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
376 }
377
378 template<typename OtherDerived>
379 Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
380
381 #ifdef EIGEN2_SUPPORT
382 // deprecated
383 template<typename OtherDerived>
384 typename internal::plain_matrix_type_column_major<OtherDerived>::type
385 solveTriangular(const MatrixBase<OtherDerived>& other) const;
386
387 // deprecated
388 template<typename OtherDerived>
389 void solveTriangularInPlace(MatrixBase<OtherDerived>& other) const;
390 #endif // EIGEN2_SUPPORT
391
392 template<int Mode>
393 inline const SparseTriangularView<Derived, Mode> triangularView() const;
394
395 template<unsigned int UpLo> inline const SparseSelfAdjointView<Derived, UpLo> selfadjointView() const;
396 template<unsigned int UpLo> inline SparseSelfAdjointView<Derived, UpLo> selfadjointView();
397
398 template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
399 template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
400 RealScalar squaredNorm() const;
401 RealScalar norm() const;
402 RealScalar blueNorm() const;
403
404 Transpose<Derived> transpose() { return derived(); }
405 const Transpose<const Derived> transpose() const { return derived(); }
406 const AdjointReturnType adjoint() const { return transpose(); }
407
408 // inner-vector
409 typedef Block<Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> InnerVectorReturnType;
410 typedef Block<const Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> ConstInnerVectorReturnType;
411 InnerVectorReturnType innerVector(Index outer);
412 const ConstInnerVectorReturnType innerVector(Index outer) const;
413
414 // set of inner-vectors
415 typedef Block<Derived,Dynamic,Dynamic,true> InnerVectorsReturnType;
416 typedef Block<const Derived,Dynamic,Dynamic,true> ConstInnerVectorsReturnType;
417 InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
418 const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
419
420 /** \internal use operator= */
421 template<typename DenseDerived>
422 void evalTo(MatrixBase<DenseDerived>& dst) const
423 {
424 dst.setZero();
425 for (Index j=0; j<outerSize(); ++j)
426 for (typename Derived::InnerIterator i(derived(),j); i; ++i)
427 dst.coeffRef(i.row(),i.col()) = i.value();
428 }
429
430 Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> toDense() const
431 {
432 return derived();
433 }
434
435 template<typename OtherDerived>
436 bool isApprox(const SparseMatrixBase<OtherDerived>& other,
437 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
438 { return toDense().isApprox(other.toDense(),prec); }
439
440 template<typename OtherDerived>
441 bool isApprox(const MatrixBase<OtherDerived>& other,
442 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
443 { return toDense().isApprox(other,prec); }
444
445 /** \returns the matrix or vector obtained by evaluating this expression.
446 *
447 * Notice that in the case of a plain matrix or vector (not an expression) this function just returns
448 * a const reference, in order to avoid a useless copy.
449 */
450 inline const typename internal::eval<Derived>::type eval() const
451 { return typename internal::eval<Derived>::type(derived()); }
452
453 Scalar sum() const;
454
455 protected:
456
457 bool m_isRValue;
458};
459
460} // end namespace Eigen
461
462#endif // EIGEN_SPARSEMATRIXBASE_H
Note: See TracBrowser for help on using the repository browser.