Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
SparseMap.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_MAP_H
11#define EIGEN_SPARSE_MAP_H
12
13#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
17namespace internal {
18
19template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
20struct traits<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
21 : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
22{
23 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
24 typedef traits<PlainObjectType> TraitsBase;
25 enum {
26 Flags = TraitsBase::Flags & (~NestByRefBit)
27 };
28};
29
30template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
31struct traits<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
32 : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
33{
34 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
35 typedef traits<PlainObjectType> TraitsBase;
36 enum {
37 Flags = TraitsBase::Flags & (~ (NestByRefBit | LvalueBit))
38 };
39};
40
41} // end namespace internal
42
43template<typename Derived,
44 int Level = internal::accessors_level<Derived>::has_write_access ? WriteAccessors : ReadOnlyAccessors
45> class SparseMapBase;
46
51template<typename Derived>
52class SparseMapBase<Derived,ReadOnlyAccessors>
53 : public SparseCompressedBase<Derived>
54{
55 public:
57 typedef typename Base::Scalar Scalar;
58 typedef typename Base::StorageIndex StorageIndex;
59 enum { IsRowMajor = Base::IsRowMajor };
60 using Base::operator=;
61 protected:
62
63 typedef typename internal::conditional<
64 bool(internal::is_lvalue<Derived>::value),
65 Scalar *, const Scalar *>::type ScalarPointer;
66 typedef typename internal::conditional<
67 bool(internal::is_lvalue<Derived>::value),
68 StorageIndex *, const StorageIndex *>::type IndexPointer;
69
70 Index m_outerSize;
71 Index m_innerSize;
72 Array<StorageIndex,2,1> m_zero_nnz;
73 IndexPointer m_outerIndex;
74 IndexPointer m_innerIndices;
75 ScalarPointer m_values;
76 IndexPointer m_innerNonZeros;
77
78 public:
79
81 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
83 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
85 inline Index innerSize() const { return m_innerSize; }
87 inline Index outerSize() const { return m_outerSize; }
89 inline Index nonZeros() const { return m_zero_nnz[1]; }
90
92 bool isCompressed() const { return m_innerNonZeros==0; }
93
94 //----------------------------------------
95 // direct access interface
97 inline const Scalar* valuePtr() const { return m_values; }
99 inline const StorageIndex* innerIndexPtr() const { return m_innerIndices; }
101 inline const StorageIndex* outerIndexPtr() const { return m_outerIndex; }
103 inline const StorageIndex* innerNonZeroPtr() const { return m_innerNonZeros; }
104 //----------------------------------------
105
107 inline Scalar coeff(Index row, Index col) const
108 {
109 const Index outer = IsRowMajor ? row : col;
110 const Index inner = IsRowMajor ? col : row;
111
112 Index start = m_outerIndex[outer];
113 Index end = isCompressed() ? m_outerIndex[outer+1] : start + m_innerNonZeros[outer];
114 if (start==end)
115 return Scalar(0);
116 else if (end>0 && inner==m_innerIndices[end-1])
117 return m_values[end-1];
118 // ^^ optimization: let's first check if it is the last coefficient
119 // (very common in high level algorithms)
120
121 const StorageIndex* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
122 const Index id = r-&m_innerIndices[0];
123 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
124 }
125
126 inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
127 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
128 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(outerIndexPtr),
129 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(innerNonZerosPtr)
130 {}
131
132 // for vectors
133 inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
134 : m_outerSize(1), m_innerSize(size), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(m_zero_nnz.data()),
135 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(0)
136 {}
137
139 inline ~SparseMapBase() {}
140
141 protected:
142 inline SparseMapBase() {}
143};
144
149template<typename Derived>
150class SparseMapBase<Derived,WriteAccessors>
151 : public SparseMapBase<Derived,ReadOnlyAccessors>
152{
154
155 public:
157 typedef typename Base::Scalar Scalar;
158 typedef typename Base::StorageIndex StorageIndex;
159 enum { IsRowMajor = Base::IsRowMajor };
160
161 using Base::operator=;
162
163 public:
164
165 //----------------------------------------
166 // direct access interface
167 using Base::valuePtr;
168 using Base::innerIndexPtr;
169 using Base::outerIndexPtr;
170 using Base::innerNonZeroPtr;
172 inline Scalar* valuePtr() { return Base::m_values; }
174 inline StorageIndex* innerIndexPtr() { return Base::m_innerIndices; }
176 inline StorageIndex* outerIndexPtr() { return Base::m_outerIndex; }
178 inline StorageIndex* innerNonZeroPtr() { return Base::m_innerNonZeros; }
179 //----------------------------------------
180
182 inline Scalar& coeffRef(Index row, Index col)
183 {
184 const Index outer = IsRowMajor ? row : col;
185 const Index inner = IsRowMajor ? col : row;
186
187 Index start = Base::m_outerIndex[outer];
188 Index end = Base::isCompressed() ? Base::m_outerIndex[outer+1] : start + Base::m_innerNonZeros[outer];
189 eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
190 eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient");
191 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start],&Base::m_innerIndices[end],inner);
192 const Index id = r - &Base::m_innerIndices[0];
193 eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
194 return const_cast<Scalar*>(Base::m_values)[id];
195 }
196
197 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
198 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
199 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
200 {}
201
202 // for vectors
203 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
204 : Base(size, nnz, innerIndexPtr, valuePtr)
205 {}
206
208 inline ~SparseMapBase() {}
209
210 protected:
211 inline SparseMapBase() {}
212};
213
222#ifndef EIGEN_PARSED_BY_DOXYGEN
223template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
224class Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
225 : public SparseMapBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
226#else
227template<typename SparseMatrixType>
228class Map<SparseMatrixType>
229 : public SparseMapBase<Derived,WriteAccessors>
230#endif
231{
232 public:
233 typedef SparseMapBase<Map> Base;
234 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
235 enum { IsRowMajor = Base::IsRowMajor };
236
237 public:
238
247 inline Map(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr,
248 StorageIndex* innerIndexPtr, Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
249 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
250 {}
251#ifndef EIGEN_PARSED_BY_DOXYGEN
253 inline ~Map() {}
254};
255
256template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
257class Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
258 : public SparseMapBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
259{
260 public:
261 typedef SparseMapBase<Map> Base;
262 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
263 enum { IsRowMajor = Base::IsRowMajor };
264
265 public:
266#endif
272 inline Map(Index rows, Index cols, Index nnz, const StorageIndex* outerIndexPtr,
273 const StorageIndex* innerIndexPtr, const Scalar* valuePtr, const StorageIndex* innerNonZerosPtr = 0)
274 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
275 {}
276
278 inline ~Map() {}
279};
280
281namespace internal {
282
283template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
284struct evaluator<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
285 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
286{
287 typedef evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
288 typedef Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
289 evaluator() : Base() {}
290 explicit evaluator(const XprType &mat) : Base(mat) {}
291};
292
293template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
294struct evaluator<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
295 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
296{
297 typedef evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
298 typedef Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
299 evaluator() : Base() {}
300 explicit evaluator(const XprType &mat) : Base(mat) {}
301};
302
303}
304
305} // end namespace Eigen
306
307#endif // EIGEN_SPARSE_MAP_H
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:49
Base class for dense Map and Block expression with direct access.
Definition: MapBase.h:41
Map(Index rows, Index cols, Index nnz, const StorageIndex *outerIndexPtr, const StorageIndex *innerIndexPtr, const Scalar *valuePtr, const StorageIndex *innerNonZerosPtr=0)
Definition: SparseMap.h:272
Map(Index rows, Index cols, Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=0)
Definition: SparseMap.h:247
~Map()
Definition: SparseMap.h:278
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition: Map.h:131
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:40
Common base class for Map and Ref instance of sparse matrix and vector.
Definition: SparseMap.h:54
Index innerSize() const
Definition: SparseMap.h:85
Index cols() const
Definition: SparseMap.h:83
const StorageIndex * innerIndexPtr() const
Definition: SparseMap.h:99
const Scalar * valuePtr() const
Definition: SparseMap.h:97
Scalar coeff(Index row, Index col) const
Definition: SparseMap.h:107
Index rows() const
Definition: SparseMap.h:81
Index outerSize() const
Definition: SparseMap.h:87
Index nonZeros() const
Definition: SparseMap.h:89
const StorageIndex * innerNonZeroPtr() const
Definition: SparseMap.h:103
bool isCompressed() const
Definition: SparseMap.h:92
const StorageIndex * outerIndexPtr() const
Definition: SparseMap.h:101
Scalar & coeffRef(Index row, Index col)
Definition: SparseMap.h:182
Scalar * valuePtr()
Definition: SparseMap.h:172
StorageIndex * innerNonZeroPtr()
Definition: SparseMap.h:178
~SparseMapBase()
Definition: SparseMap.h:208
StorageIndex * outerIndexPtr()
Definition: SparseMap.h:176
StorageIndex * innerIndexPtr()
Definition: SparseMap.h:174
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:45
static const lastp1_t end
Definition: IndexedViewHelper.h:183
@ ReadOnlyAccessors
Definition: Constants.h:378
@ WriteAccessors
Definition: Constants.h:380
const unsigned int LvalueBit
Definition: Constants.h:146
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41