MyraMath
SparseMatrixRange.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_SPARSE_SPARSEMATRIXRANGE_H
7 #define MYRAMATH_SPARSE_SPARSEMATRIXRANGE_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
16 
17 #include <vector>
18 #include <utility>
19 #include <iosfwd>
20 
21 namespace myra {
22 
23 // Forward declarations.
24 template<int Arity, class Number> class Expression;
25 class intCRange;
26 template<class Number> class Matrix;
27 template<class Number> class MatrixRange;
28 class PatternRange;
29 template<class Number> class SparseMatrix;
30 template<class Number> class CSparseMatrixRange;
31 template<class Number> class SparseMatrixRange;
32 template<class Number> class CSparseMatrixIterator;
33 template<class Number> class SparseMatrixIterator;
34 template<class T> class Array1;
35 template<class T> class Array2;
36 class OutputStream;
37 
39 template<class Number> class LIBPUBLIC SparseMatrixRange
40  {
41  public:
42 
44  // Data pointers for underlying SparseMatrix
46  const int* Ao; // Offsets for each j into Ai[] and Av[] data.
47  const int* Ai; // Row indices (i) in compressed-sparse-column format.
48  Number* Av; // Numeric values (v) in compressed-sparse-column format.
49 
50  // Extents of this Range within underlying SparseMatrix.
51  int I0,I1; // I-extents of Range
52  int J0,J1; // J-extents of Range
53  bool sliced; // If true, this SparseMatrixRange has been sliced.
55 
56  // Useful typedefs.
57  typedef std::pair<SparseMatrixRange<Number>, SparseMatrixRange<Number> > Pair;
58 
59  // ------------------------------------ Construction and range semantics.
60 
63 
65  SparseMatrixRange(const int* in_Ao, const int* in_Ai, Number* in_Av, int I, int J);
66 
68  void write(OutputStream& out) const;
69 
70  // ------------------------------------ Size and pattern queries.
71 
73  std::pair<int,int> size() const;
74 
76  int n_nonzeros() const;
77 
79  int n_nonzeros(int j) const;
80 
82  bool test(int i, int j) const;
83 
85  PatternRange pattern() const;
86 
88  std::vector<int> pattern(int j) const;
89 
90  // ------------------------------------ Iterating.
92 
94  SparseMatrixIterator<Number> begin() const;
96  SparseMatrixIterator<Number> end() const;
98 
100  SparseMatrixIterator<Number> begin(int j) const;
102  SparseMatrixIterator<Number> end(int j) const;
104 
105  // ------------------------------------ General slicing.
106 
108  CSparseMatrixRange<Number> add_const() const;
109 
111  operator CSparseMatrixRange<Number> () const;
112 
114  SparseMatrixRange<Number> window(int i0, int i1, int j0, int j1) const ;
115 
117  Array2<SparseMatrixRange<Number> > windows(const intCRange& i, const intCRange& j) const;
118 
120  Number* pointer(int i, int j) const;
121 
123  Number& operator() (int i, int j) const;
124 
126  Number& at(int i, int j) const;
127 
129  Matrix<Number> make_Matrix() const;
131  void make_Matrix(const MatrixRange<Number>& A) const;
133 
134  // ------------------------------------ Row slicing.
135 
137  SparseMatrixRange<Number> row(int i) const;
138 
140  SparseMatrixRange<Number> rows(int i0, int i1) const;
141 
143  Array1<SparseMatrixRange<Number> > rows(const intCRange& i) const;
144 
146  SparseMatrixRange<Number> top(int i) const;
147 
149  SparseMatrixRange<Number> cut_top(int i) const;
150 
152  Pair split_top(int i) const;
153 
155  SparseMatrixRange<Number> bottom(int i) const;
156 
158  SparseMatrixRange<Number> cut_bottom(int i) const;
159 
161  Pair split_bottom(int i) const;
162 
163  // ------------------------------------ Column slicing.
164 
166  SparseMatrixRange<Number> column(int j) const;
167 
169  SparseMatrixRange<Number> columns(int j0, int j1) const;
170 
172  Array1<SparseMatrixRange<Number> > columns(const intCRange& j) const;
173 
175  SparseMatrixRange<Number> left(int j) const;
176 
178  SparseMatrixRange<Number> cut_left(int j) const;
179 
181  Pair split_left(int j) const;
182 
184  SparseMatrixRange<Number> right(int j) const;
185 
187  SparseMatrixRange<Number> cut_right(int j) const;
188 
190  Pair split_right(int j) const;
191 
192  // ------------------------------------ Mutating underlying contents.
193 
195  // Functor f should be a have an operator(), that takes a Number and returns a Number.
196  template<class Functor> void transform(const Functor& f) const
197  {
198  typedef SparseMatrixIterator<Number> Iterator;
199  Iterator b = this->begin();
200  Iterator e = this->end();
201  for (Iterator aij = b; aij != e; ++aij)
202  aij.value() = f(aij.value());
203  }
204 
206  // Functor f should be a have an operator(), that takes a Number and returns a Number.
207  template<class Functor> void transform_triangle(const Functor& f, char uplo) const
208  {
209  typedef SparseMatrixIterator<Number> Iterator;
210  Iterator b = this->begin();
211  Iterator e = this->end();
212  if (uplo == 'U')
213  {
214  for (Iterator aij = b; aij != e; ++aij)
215  if (aij.i() <= aij.j())
216  aij.value() = f(aij.value());
217  }
218  else if (uplo == 'L')
219  {
220  for (Iterator aij = b; aij != e; ++aij)
221  if (aij.i() >= aij.j())
222  aij.value() = f(aij.value());
223  }
224  }
225 
227  // Functor f should be a have an operator(), that takes a Number and returns a Number.
228  template<class Functor> void transform_diagonal(const Functor& f) const
229  {
230  int II = this->I();
231  int JJ = this->J();
232  int N = II < JJ ? II : JJ;
233  for (int n = 0; n < N; ++n)
234  {
235  Number* p = this->pointer(n,n);
236  if (p)
237  *p = f(*p);
238  }
239  }
240 
242  void operator *= (Number alpha) const;
243 
245  void operator /= (Number alpha) const;
246 
247  private:
248 
249 
250  // Implementation details, helps make subranges.
251  int I() const;
252  int J() const;
253 
254  // Implementation detail for iterator compare, verifies both iterator's are over same range.
255  bool same (const SparseMatrixRange<Number>& that) const;
256 
257  // Implementation detail for iterator compare, verifies both iterator's are over same range.
258  bool notsame (const SparseMatrixRange<Number>& that) const;
259 
260  // A special memory location so that a default-constructed 0x0 SparseMatrixRange can point to valid Ai[] data
261  static int Ai_zero;
262 
263  friend class SparseMatrixIterator<Number>;
264  friend class CSparseMatrixRange<Number>;
265  friend class SparseMatrix<Number>;
266 
267  };
268 
269 // Initializes SparseMatrixRange<Number>::Ai_zero.
270 template<class Number> int SparseMatrixRange<Number>::Ai_zero = 0;
271 
273 template<class Number> class ReflectNumber< SparseMatrixRange<Number> >
274  { public: typedef Number type; };
275 
277 template<class Number> class LIBPUBLIC SparseMatrixIterator
278  {
279  public:
280 
282  void operator ++ ();
284  bool operator == (const SparseMatrixIterator<Number>& that) const;
285  bool operator != (const SparseMatrixIterator<Number>& that) const;
287 
289  int i() const;
291  int j() const;
292  Number& value() const;
294 
295  private:
296 
297  // Implementation detail of increment()
298  void seek();
299 
300  // Should call this when jay is modified, sets up traversal A(I0:I1,jay)
301  void j_initialize(int jj);
302 
303  // Implementation detail of initialize(), returns offset to A(ii,jay)
304  int i_initialize(int ii);
305 
306  // Copy of underlying range data.
308 
309  // State members of iterator.
310  int jay; // Column iterator jay
311  int b; // Offset to A(I0,jay), the begin of column jay
312  int e; // Offset to A(I1,jay), the end of column jay
313  int o; // Offset to current nonzero.
314 
315  friend class SparseMatrixRange<Number>;
316 
317  };
318 
320 template<class Number> class LIBPUBLIC CSparseMatrixRange
321  {
322  public:
323 
325  // Data pointers for underlying SparseMatrix
327  const int* Ao; // Offsets for each j into Ai[] and Av[] data.
328  const int* Ai; // Row indices (i) in compressed-sparse-column format.
329  const Number* Av; // Numeric values (v) in compressed-sparse-column format.
330 
331  // Extents of this Range within underlying SparseMatrix.
332  int I0,I1; // I-extents of Range
333  int J0,J1; // J-extents of Range
334  bool sliced; // If true, this SparseMatrixRange has been sliced.
336 
337  // Useful typedefs.
338  typedef std::pair<CSparseMatrixRange<Number>, CSparseMatrixRange<Number> > Pair;
339 
341  CSparseMatrixRange();
342 
344  CSparseMatrixRange(const int* in_Ao, const int* in_Ai, const Number* in_Av, int I, int J);
345 
347  void write(OutputStream& out) const;
348 
349  // ------------------------------------ Size and pattern queries.
350 
352  std::pair<int,int> size() const;
353 
355  int n_nonzeros() const;
356 
358  int n_nonzeros(int j) const;
359 
361  bool test(int i, int j) const;
362 
364  PatternRange pattern() const;
365 
367  std::vector<int> pattern(int j) const;
368 
369  // ------------------------------------ Iterating.
371 
373  CSparseMatrixIterator<Number> begin() const;
375  CSparseMatrixIterator<Number> end() const;
377 
379  CSparseMatrixIterator<Number> begin(int j) const;
381  CSparseMatrixIterator<Number> end(int j) const;
383 
384  // ------------------------------------ General slicing.
385 
387  SparseMatrixRange<Number> remove_const() const;
388 
390  CSparseMatrixRange<Number> window(int i0, int i1, int j0, int j1) const;
391 
393  Array2<CSparseMatrixRange<Number> > windows(const intCRange& i, const intCRange& j) const;
394 
396  const Number* pointer(int i, int j) const;
397 
399  const Number& operator() (int i, int j) const;
400 
402  const Number& at(int i, int j) const;
403 
405  Matrix<Number> make_Matrix() const;
407  void make_Matrix(const MatrixRange<Number>& A) const;
409 
410  // ------------------------------------ Row slicing.
411 
413  CSparseMatrixRange<Number> row(int i) const;
414 
416  CSparseMatrixRange<Number> rows(int i0, int i1) const;
417 
419  Array1<CSparseMatrixRange<Number> > rows(const intCRange& i) const;
420 
422  CSparseMatrixRange<Number> top(int i) const;
423 
425  CSparseMatrixRange<Number> cut_top(int i) const;
426 
428  Pair split_top(int i) const;
429 
431  CSparseMatrixRange<Number> bottom(int i) const;
432 
434  CSparseMatrixRange<Number> cut_bottom(int i) const;
435 
437  Pair split_bottom(int i) const;
438 
439  // ------------------------------------ Column slicing.
440 
442  CSparseMatrixRange<Number> column(int j) const;
443 
445  CSparseMatrixRange<Number> columns(int j0, int j1) const;
446 
448  Array1<CSparseMatrixRange<Number> > columns(const intCRange& j) const;
449 
451  CSparseMatrixRange<Number> left(int j) const;
452 
454  CSparseMatrixRange<Number> cut_left(int j) const;
455 
457  Pair split_left(int j) const;
458 
460  CSparseMatrixRange<Number> right(int j) const;
461 
463  CSparseMatrixRange<Number> cut_right(int j) const;
464 
466  Pair split_right(int j) const;
467 
468  private:
469 
470  // Implementation details, helps make subranges.
471  int I() const;
472  int J() const;
473 
474  // Implementation detail for iterator compare, verifies both iterators are over same range.
475  bool operator == (const CSparseMatrixRange<Number>& that) const;
476 
477  // Implementation detail for iterator compare, verifies both iterators are over same range.
478  bool operator != (const CSparseMatrixRange<Number>& that) const;
479 
480  // A special memory location so that a default-constructed 0x0 CSparseMatrixRange can point to valid Ai[] data
481  static int Ai_zero;
482 
483  friend class CSparseMatrixIterator<Number>;
484  friend class SparseMatrixRange<Number>;
485  friend class SparseMatrix<Number>;
486 
487  };
488 
489 // Initializes CSparseMatrixRange<Number>::Ai_zero.
490 template<class Number> int CSparseMatrixRange<Number>::Ai_zero = 0;
491 
493 template<class Number> class ReflectNumber< CSparseMatrixRange<Number> >
494  { public: typedef Number type; };
495 
497 template<class Number> class LIBPUBLIC CSparseMatrixIterator
498  {
499  public:
500 
502  void operator ++ ();
504  bool operator == (const CSparseMatrixIterator<Number>& that) const;
505  bool operator != (const CSparseMatrixIterator<Number>& that) const;
507 
509  int i() const;
511  int j() const;
512  const Number& value() const;
514 
515  private:
516 
517  // Implementation detail of increment()
518  void seek();
519 
520  // Should call this when jay is modified, sets up traversal A(I0:I1,jay)
521  void j_initialize(int jj);
522 
523  // Implementation detail of initialize(), returns offset to A(ii,jay)
524  int i_initialize(int ii);
525 
526  // Copy of underlying range data.
528 
529  // State members of iterator.
530  int jay; // Column iterator jay
531  int b; // Offset to A(I0,jay), the begin of column jay
532  int e; // Offset to A(I1,jay), the end of column jay
533  int o; // Offset to current nonzero.
534 
535  friend class CSparseMatrixRange<Number>;
536 
537  };
538 
540 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CSparseMatrixRange<NumberS>& A);
542 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CSparseMatrixRange<NumberD>& A);
543 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CSparseMatrixRange<NumberC>& A);
544 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CSparseMatrixRange<NumberZ>& A);
546 
547 } // namespace
548 
549 #endif
Container of values, allows random (i,j) access.
Definition: Array2.h:30
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
Represents a mutable SparseMatrixRange.
Definition: conjugate.h:21
const int * Ao
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:46
const int * Ai
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:47
void transform_triangle(const Functor &f, char uplo) const
Overwrites every A(i,j) in the &#39;U&#39;pper or &#39;L&#39;ower triangle of this SparseMatrixRange with f(A(i...
Definition: SparseMatrixRange.h:207
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Number * Av
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:48
Definition: syntax.dox:1
An iterator over a SparseMatrixRange.
Definition: SparseMatrixRange.h:33
void transform(const Functor &f) const
Overwrites every A(i,j) in this SparseMatrixRange with f(A(i,j)).
Definition: SparseMatrixRange.h:196
Represents an immutable view of a Pattern.
Definition: PatternRange.h:31
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Various utility functions/classes related to scalar Number types.
Represents a mutable MatrixRange.
Definition: conjugate.h:26
const int * Ai
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:328
Represents a const SparseMatrixRange.
Definition: bothcat.h:24
const int * Ao
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:327
int I1
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:332
int J1
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:52
bool sliced
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:53
const Number * Av
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:329
Container of values, allows random (i) access.
Definition: Array1.h:32
bool sliced
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:334
An iterator over a CSparseMatrixRange.
Definition: SparseMatrixRange.h:32
void transform_diagonal(const Functor &f) const
Overwrites every A(n,n) on the diagonal of this SparseMatrixRange with f(A(n,n)). ...
Definition: SparseMatrixRange.h:228
Stores an IxJ matrix A in compressed sparse column format.
Definition: bothcat.h:23
int I1
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:51
int J1
---------— Data members, all public ----------------—
Definition: SparseMatrixRange.h:333
Represents a const intRange.
Definition: intRange.h:142