1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
2 | // for linear algebra.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2008-2009 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_BLOCK_H
|
---|
11 | #define EIGEN_SPARSE_BLOCK_H
|
---|
12 |
|
---|
13 | namespace Eigen {
|
---|
14 |
|
---|
15 | template<typename XprType, int BlockRows, int BlockCols>
|
---|
16 | class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
|
---|
17 | : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
|
---|
18 | {
|
---|
19 | typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
|
---|
20 | typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
|
---|
21 | public:
|
---|
22 | enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
|
---|
23 | protected:
|
---|
24 | enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
|
---|
25 | public:
|
---|
26 | EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
|
---|
27 |
|
---|
28 | class InnerIterator: public XprType::InnerIterator
|
---|
29 | {
|
---|
30 | typedef typename BlockImpl::Index Index;
|
---|
31 | public:
|
---|
32 | inline InnerIterator(const BlockType& xpr, Index outer)
|
---|
33 | : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
34 | {}
|
---|
35 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
36 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
37 | protected:
|
---|
38 | Index m_outer;
|
---|
39 | };
|
---|
40 | class ReverseInnerIterator: public XprType::ReverseInnerIterator
|
---|
41 | {
|
---|
42 | typedef typename BlockImpl::Index Index;
|
---|
43 | public:
|
---|
44 | inline ReverseInnerIterator(const BlockType& xpr, Index outer)
|
---|
45 | : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
46 | {}
|
---|
47 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
48 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
49 | protected:
|
---|
50 | Index m_outer;
|
---|
51 | };
|
---|
52 |
|
---|
53 | inline BlockImpl(const XprType& xpr, int i)
|
---|
54 | : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
|
---|
55 | {}
|
---|
56 |
|
---|
57 | inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
|
---|
58 | : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
|
---|
59 | {}
|
---|
60 |
|
---|
61 | inline const Scalar coeff(int row, int col) const
|
---|
62 | {
|
---|
63 | return m_matrix.coeff(row + IsRowMajor ? m_outerStart : 0, col +IsRowMajor ? 0 : m_outerStart);
|
---|
64 | }
|
---|
65 |
|
---|
66 | inline const Scalar coeff(int index) const
|
---|
67 | {
|
---|
68 | return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
|
---|
69 | }
|
---|
70 |
|
---|
71 | EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
|
---|
72 | EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
|
---|
73 |
|
---|
74 | protected:
|
---|
75 |
|
---|
76 | typename XprType::Nested m_matrix;
|
---|
77 | Index m_outerStart;
|
---|
78 | const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
|
---|
79 |
|
---|
80 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
|
---|
81 | private:
|
---|
82 | Index nonZeros() const;
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | /***************************************************************************
|
---|
87 | * specialisation for SparseMatrix
|
---|
88 | ***************************************************************************/
|
---|
89 |
|
---|
90 | template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
|
---|
91 | class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
|
---|
92 | : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
|
---|
93 | {
|
---|
94 | typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
|
---|
95 | typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
|
---|
96 | typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
|
---|
97 | typedef Block<const SparseMatrixType, BlockRows, BlockCols, true> ConstBlockType;
|
---|
98 | public:
|
---|
99 | enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
|
---|
100 | EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
|
---|
101 | protected:
|
---|
102 | enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
|
---|
103 | public:
|
---|
104 |
|
---|
105 | class InnerIterator: public SparseMatrixType::InnerIterator
|
---|
106 | {
|
---|
107 | public:
|
---|
108 | inline InnerIterator(const BlockType& xpr, Index outer)
|
---|
109 | : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
110 | {}
|
---|
111 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
112 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
113 | protected:
|
---|
114 | Index m_outer;
|
---|
115 | };
|
---|
116 | class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
|
---|
117 | {
|
---|
118 | public:
|
---|
119 | inline ReverseInnerIterator(const BlockType& xpr, Index outer)
|
---|
120 | : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
121 | {}
|
---|
122 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
123 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
124 | protected:
|
---|
125 | Index m_outer;
|
---|
126 | };
|
---|
127 |
|
---|
128 | inline BlockImpl(const SparseMatrixType& xpr, int i)
|
---|
129 | : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
|
---|
130 | {}
|
---|
131 |
|
---|
132 | inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
|
---|
133 | : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
|
---|
134 | {}
|
---|
135 |
|
---|
136 | template<typename OtherDerived>
|
---|
137 | inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
|
---|
138 | {
|
---|
139 | typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
|
---|
140 | _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
|
---|
141 | // This assignement is slow if this vector set is not empty
|
---|
142 | // and/or it is not at the end of the nonzeros of the underlying matrix.
|
---|
143 |
|
---|
144 | // 1 - eval to a temporary to avoid transposition and/or aliasing issues
|
---|
145 | SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other);
|
---|
146 |
|
---|
147 | // 2 - let's check whether there is enough allocated memory
|
---|
148 | Index nnz = tmp.nonZeros();
|
---|
149 | Index start = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
|
---|
150 | Index end = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending posiiton of the current block
|
---|
151 | Index block_size = end - start; // available room in the current block
|
---|
152 | Index tail_size = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
|
---|
153 |
|
---|
154 | Index free_size = m_matrix.isCompressed()
|
---|
155 | ? Index(matrix.data().allocatedSize()) + block_size
|
---|
156 | : block_size;
|
---|
157 |
|
---|
158 | if(nnz>free_size)
|
---|
159 | {
|
---|
160 | // realloc manually to reduce copies
|
---|
161 | typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
|
---|
162 |
|
---|
163 | std::memcpy(newdata.valuePtr(), m_matrix.data().valuePtr(), start*sizeof(Scalar));
|
---|
164 | std::memcpy(newdata.indexPtr(), m_matrix.data().indexPtr(), start*sizeof(Index));
|
---|
165 |
|
---|
166 | std::memcpy(newdata.valuePtr() + start, tmp.data().valuePtr(), nnz*sizeof(Scalar));
|
---|
167 | std::memcpy(newdata.indexPtr() + start, tmp.data().indexPtr(), nnz*sizeof(Index));
|
---|
168 |
|
---|
169 | std::memcpy(newdata.valuePtr()+start+nnz, matrix.data().valuePtr()+end, tail_size*sizeof(Scalar));
|
---|
170 | std::memcpy(newdata.indexPtr()+start+nnz, matrix.data().indexPtr()+end, tail_size*sizeof(Index));
|
---|
171 |
|
---|
172 | newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
|
---|
173 |
|
---|
174 | matrix.data().swap(newdata);
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | // no need to realloc, simply copy the tail at its respective position and insert tmp
|
---|
179 | matrix.data().resize(start + nnz + tail_size);
|
---|
180 |
|
---|
181 | std::memmove(matrix.data().valuePtr()+start+nnz, matrix.data().valuePtr()+end, tail_size*sizeof(Scalar));
|
---|
182 | std::memmove(matrix.data().indexPtr()+start+nnz, matrix.data().indexPtr()+end, tail_size*sizeof(Index));
|
---|
183 |
|
---|
184 | std::memcpy(matrix.data().valuePtr()+start, tmp.data().valuePtr(), nnz*sizeof(Scalar));
|
---|
185 | std::memcpy(matrix.data().indexPtr()+start, tmp.data().indexPtr(), nnz*sizeof(Index));
|
---|
186 | }
|
---|
187 |
|
---|
188 | // update innerNonZeros
|
---|
189 | if(!m_matrix.isCompressed())
|
---|
190 | for(Index j=0; j<m_outerSize.value(); ++j)
|
---|
191 | matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros();
|
---|
192 |
|
---|
193 | // update outer index pointers
|
---|
194 | Index p = start;
|
---|
195 | for(Index k=0; k<m_outerSize.value(); ++k)
|
---|
196 | {
|
---|
197 | matrix.outerIndexPtr()[m_outerStart+k] = p;
|
---|
198 | p += tmp.innerVector(k).nonZeros();
|
---|
199 | }
|
---|
200 | std::ptrdiff_t offset = nnz - block_size;
|
---|
201 | for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
|
---|
202 | {
|
---|
203 | matrix.outerIndexPtr()[k] += offset;
|
---|
204 | }
|
---|
205 |
|
---|
206 | return derived();
|
---|
207 | }
|
---|
208 |
|
---|
209 | inline BlockType& operator=(const BlockType& other)
|
---|
210 | {
|
---|
211 | return operator=<BlockType>(other);
|
---|
212 | }
|
---|
213 |
|
---|
214 | inline const Scalar* valuePtr() const
|
---|
215 | { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
216 | inline Scalar* valuePtr()
|
---|
217 | { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
218 |
|
---|
219 | inline const Index* innerIndexPtr() const
|
---|
220 | { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
221 | inline Index* innerIndexPtr()
|
---|
222 | { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
223 |
|
---|
224 | inline const Index* outerIndexPtr() const
|
---|
225 | { return m_matrix.outerIndexPtr() + m_outerStart; }
|
---|
226 | inline Index* outerIndexPtr()
|
---|
227 | { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
|
---|
228 |
|
---|
229 | Index nonZeros() const
|
---|
230 | {
|
---|
231 | if(m_matrix.isCompressed())
|
---|
232 | return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
|
---|
233 | - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
|
---|
234 | else if(m_outerSize.value()==0)
|
---|
235 | return 0;
|
---|
236 | else
|
---|
237 | return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
|
---|
238 | }
|
---|
239 |
|
---|
240 | inline Scalar& coeffRef(int row, int col)
|
---|
241 | {
|
---|
242 | return m_matrix.const_cast_derived().coeffRef(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
|
---|
243 | }
|
---|
244 |
|
---|
245 | inline const Scalar coeff(int row, int col) const
|
---|
246 | {
|
---|
247 | return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
|
---|
248 | }
|
---|
249 |
|
---|
250 | inline const Scalar coeff(int index) const
|
---|
251 | {
|
---|
252 | return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
|
---|
253 | }
|
---|
254 |
|
---|
255 | const Scalar& lastCoeff() const
|
---|
256 | {
|
---|
257 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
|
---|
258 | eigen_assert(nonZeros()>0);
|
---|
259 | if(m_matrix.isCompressed())
|
---|
260 | return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
|
---|
261 | else
|
---|
262 | return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
|
---|
263 | }
|
---|
264 |
|
---|
265 | EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
|
---|
266 | EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
|
---|
267 |
|
---|
268 | protected:
|
---|
269 |
|
---|
270 | typename SparseMatrixType::Nested m_matrix;
|
---|
271 | Index m_outerStart;
|
---|
272 | const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
|
---|
273 |
|
---|
274 | };
|
---|
275 |
|
---|
276 |
|
---|
277 | template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
|
---|
278 | class BlockImpl<const SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
|
---|
279 | : public SparseMatrixBase<Block<const SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
|
---|
280 | {
|
---|
281 | typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
|
---|
282 | typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
|
---|
283 | typedef Block<const SparseMatrixType, BlockRows, BlockCols, true> BlockType;
|
---|
284 | public:
|
---|
285 | enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
|
---|
286 | EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
|
---|
287 | protected:
|
---|
288 | enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
|
---|
289 | public:
|
---|
290 |
|
---|
291 | class InnerIterator: public SparseMatrixType::InnerIterator
|
---|
292 | {
|
---|
293 | public:
|
---|
294 | inline InnerIterator(const BlockType& xpr, Index outer)
|
---|
295 | : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
296 | {}
|
---|
297 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
298 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
299 | protected:
|
---|
300 | Index m_outer;
|
---|
301 | };
|
---|
302 | class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
|
---|
303 | {
|
---|
304 | public:
|
---|
305 | inline ReverseInnerIterator(const BlockType& xpr, Index outer)
|
---|
306 | : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
|
---|
307 | {}
|
---|
308 | inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
|
---|
309 | inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
|
---|
310 | protected:
|
---|
311 | Index m_outer;
|
---|
312 | };
|
---|
313 |
|
---|
314 | inline BlockImpl(const SparseMatrixType& xpr, int i)
|
---|
315 | : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
|
---|
316 | {}
|
---|
317 |
|
---|
318 | inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
|
---|
319 | : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
|
---|
320 | {}
|
---|
321 |
|
---|
322 | inline const Scalar* valuePtr() const
|
---|
323 | { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
324 |
|
---|
325 | inline const Index* innerIndexPtr() const
|
---|
326 | { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
|
---|
327 |
|
---|
328 | inline const Index* outerIndexPtr() const
|
---|
329 | { return m_matrix.outerIndexPtr() + m_outerStart; }
|
---|
330 |
|
---|
331 | Index nonZeros() const
|
---|
332 | {
|
---|
333 | if(m_matrix.isCompressed())
|
---|
334 | return std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
|
---|
335 | - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
|
---|
336 | else if(m_outerSize.value()==0)
|
---|
337 | return 0;
|
---|
338 | else
|
---|
339 | return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
|
---|
340 | }
|
---|
341 |
|
---|
342 | inline const Scalar coeff(int row, int col) const
|
---|
343 | {
|
---|
344 | return m_matrix.coeff(row + (IsRowMajor ? m_outerStart : 0), col + (IsRowMajor ? 0 : m_outerStart));
|
---|
345 | }
|
---|
346 |
|
---|
347 | inline const Scalar coeff(int index) const
|
---|
348 | {
|
---|
349 | return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
|
---|
350 | }
|
---|
351 |
|
---|
352 | const Scalar& lastCoeff() const
|
---|
353 | {
|
---|
354 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
|
---|
355 | eigen_assert(nonZeros()>0);
|
---|
356 | if(m_matrix.isCompressed())
|
---|
357 | return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
|
---|
358 | else
|
---|
359 | return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
|
---|
360 | }
|
---|
361 |
|
---|
362 | EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
|
---|
363 | EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
|
---|
364 |
|
---|
365 | protected:
|
---|
366 |
|
---|
367 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
|
---|
368 |
|
---|
369 | typename SparseMatrixType::Nested m_matrix;
|
---|
370 | Index m_outerStart;
|
---|
371 | const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
|
---|
372 | };
|
---|
373 |
|
---|
374 | //----------
|
---|
375 |
|
---|
376 | /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
|
---|
377 | * is col-major (resp. row-major).
|
---|
378 | */
|
---|
379 | template<typename Derived>
|
---|
380 | typename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer)
|
---|
381 | { return InnerVectorReturnType(derived(), outer); }
|
---|
382 |
|
---|
383 | /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
|
---|
384 | * is col-major (resp. row-major). Read-only.
|
---|
385 | */
|
---|
386 | template<typename Derived>
|
---|
387 | const typename SparseMatrixBase<Derived>::ConstInnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) const
|
---|
388 | { return ConstInnerVectorReturnType(derived(), outer); }
|
---|
389 |
|
---|
390 | /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
|
---|
391 | * is col-major (resp. row-major).
|
---|
392 | */
|
---|
393 | template<typename Derived>
|
---|
394 | typename SparseMatrixBase<Derived>::InnerVectorsReturnType
|
---|
395 | SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize)
|
---|
396 | {
|
---|
397 | return Block<Derived,Dynamic,Dynamic,true>(derived(),
|
---|
398 | IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
|
---|
399 | IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
|
---|
400 |
|
---|
401 | }
|
---|
402 |
|
---|
403 | /** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
|
---|
404 | * is col-major (resp. row-major). Read-only.
|
---|
405 | */
|
---|
406 | template<typename Derived>
|
---|
407 | const typename SparseMatrixBase<Derived>::ConstInnerVectorsReturnType
|
---|
408 | SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const
|
---|
409 | {
|
---|
410 | return Block<const Derived,Dynamic,Dynamic,true>(derived(),
|
---|
411 | IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
|
---|
412 | IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
|
---|
413 |
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** Generic implementation of sparse Block expression.
|
---|
417 | * Real-only.
|
---|
418 | */
|
---|
419 | template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
|
---|
420 | class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
|
---|
421 | : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
|
---|
422 | {
|
---|
423 | typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
|
---|
424 | typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
|
---|
425 | public:
|
---|
426 | enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
|
---|
427 | EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
|
---|
428 |
|
---|
429 | /** Column or Row constructor
|
---|
430 | */
|
---|
431 | inline BlockImpl(const XprType& xpr, int i)
|
---|
432 | : m_matrix(xpr),
|
---|
433 | m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
|
---|
434 | m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
|
---|
435 | m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
|
---|
436 | m_blockCols(BlockCols==1 ? 1 : xpr.cols())
|
---|
437 | {}
|
---|
438 |
|
---|
439 | /** Dynamic-size constructor
|
---|
440 | */
|
---|
441 | inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
|
---|
442 | : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
|
---|
443 | {}
|
---|
444 |
|
---|
445 | inline int rows() const { return m_blockRows.value(); }
|
---|
446 | inline int cols() const { return m_blockCols.value(); }
|
---|
447 |
|
---|
448 | inline Scalar& coeffRef(int row, int col)
|
---|
449 | {
|
---|
450 | return m_matrix.const_cast_derived()
|
---|
451 | .coeffRef(row + m_startRow.value(), col + m_startCol.value());
|
---|
452 | }
|
---|
453 |
|
---|
454 | inline const Scalar coeff(int row, int col) const
|
---|
455 | {
|
---|
456 | return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
|
---|
457 | }
|
---|
458 |
|
---|
459 | inline Scalar& coeffRef(int index)
|
---|
460 | {
|
---|
461 | return m_matrix.const_cast_derived()
|
---|
462 | .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
|
---|
463 | m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
|
---|
464 | }
|
---|
465 |
|
---|
466 | inline const Scalar coeff(int index) const
|
---|
467 | {
|
---|
468 | return m_matrix
|
---|
469 | .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
|
---|
470 | m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
|
---|
471 | }
|
---|
472 |
|
---|
473 | inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
|
---|
474 |
|
---|
475 | class InnerIterator : public _MatrixTypeNested::InnerIterator
|
---|
476 | {
|
---|
477 | typedef typename _MatrixTypeNested::InnerIterator Base;
|
---|
478 | const BlockType& m_block;
|
---|
479 | Index m_end;
|
---|
480 | public:
|
---|
481 |
|
---|
482 | EIGEN_STRONG_INLINE InnerIterator(const BlockType& block, Index outer)
|
---|
483 | : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
|
---|
484 | m_block(block),
|
---|
485 | m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
|
---|
486 | {
|
---|
487 | while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
|
---|
488 | Base::operator++();
|
---|
489 | }
|
---|
490 |
|
---|
491 | inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
|
---|
492 | inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
|
---|
493 | inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
|
---|
494 | inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
|
---|
495 |
|
---|
496 | inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
|
---|
497 | };
|
---|
498 | class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
|
---|
499 | {
|
---|
500 | typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
|
---|
501 | const BlockType& m_block;
|
---|
502 | Index m_begin;
|
---|
503 | public:
|
---|
504 |
|
---|
505 | EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer)
|
---|
506 | : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
|
---|
507 | m_block(block),
|
---|
508 | m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
|
---|
509 | {
|
---|
510 | while( (Base::operator bool()) && (Base::index() >= (IsRowMajor ? m_block.m_startCol.value()+block.m_blockCols.value() : m_block.m_startRow.value()+block.m_blockRows.value())) )
|
---|
511 | Base::operator--();
|
---|
512 | }
|
---|
513 |
|
---|
514 | inline Index index() const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
|
---|
515 | inline Index outer() const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
|
---|
516 | inline Index row() const { return Base::row() - m_block.m_startRow.value(); }
|
---|
517 | inline Index col() const { return Base::col() - m_block.m_startCol.value(); }
|
---|
518 |
|
---|
519 | inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
|
---|
520 | };
|
---|
521 | protected:
|
---|
522 | friend class InnerIterator;
|
---|
523 | friend class ReverseInnerIterator;
|
---|
524 |
|
---|
525 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
|
---|
526 |
|
---|
527 | typename XprType::Nested m_matrix;
|
---|
528 | const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
|
---|
529 | const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
|
---|
530 | const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
|
---|
531 | const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
|
---|
532 | private:
|
---|
533 | Index nonZeros() const;
|
---|
534 | };
|
---|
535 |
|
---|
536 | } // end namespace Eigen
|
---|
537 |
|
---|
538 | #endif // EIGEN_SPARSE_BLOCK_H
|
---|