MyraMath
Pattern.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_PATTERN_H
7 #define MYRAMATH_SPARSE_PATTERN_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
15 
17 
18 #include <vector>
19 #include <iosfwd>
20 #include <utility>
21 
22 namespace myra {
23 
24 // Forward declarations.
25 class InputStream;
26 class OutputStream;
27 class intCRange;
28 class Pattern;
29 class PatternRange;
30 class PatternIterator;
31 class PatternBuilder;
32 template<class Number> class SparseMatrix;
33 template<class T> class Array1;
34 template<class T> class Array2;
35 
36 namespace detail {
37 
38 // Some of these functions can be implemented more efficiently with access to internal
39 // data structures, they're forward declared here so they can be granted friendship later.
40 class permute_wrapper;
41 class horzcat_wrapper;
42 class vertcat_wrapper;
43 class bothcat_wrapper;
44 class diagcat_wrapper;
45 class transpose_wrapper;
46 class multiply_wrapper;
47 class flip_wrapper;
48 Pattern stencil1(int I);
49 Pattern stencil2(int I, int J);
50 Pattern stencil3(int I, int J, int K);
51 
52 } // namespace detail
53 
55 class LIBPUBLIC Pattern
56  {
57  public:
58 
60  typedef std::pair<PatternRange, PatternRange> Pair;
61 
62  // ------------------------------------ Construction, serialization, value semantics.
63 
65  Pattern();
66 
68  Pattern(const Pattern& that);
69 
71  void swap(Pattern& that);
72 
73 #ifdef MYRAMATH_ENABLE_CPP11
74  Pattern(Pattern&& that);
76 #endif
77 
79  Pattern& operator = (Pattern that);
80 
82  explicit Pattern(const PatternRange& that);
83 
85  explicit Pattern(InputStream& in);
86 
88  void write(OutputStream& out) const;
89 
91  const Pattern& add_const() const;
92 
94  ~Pattern();
95 
96  // ------------------------------------ Size queries.
97 
99  std::pair<int,int> size() const;
100 
102  int n_nonzeros() const;
103 
105  int n_nonzeros(int j) const;
106 
108  bool test(int i, int j) const;
109 
111  std::vector<int> pattern(int j) const;
112 
113  // ------------------------------------ Iterating.
114 
116  PatternIterator begin() const;
117  PatternIterator end () const;
118 
120  PatternIterator begin(int j) const;
121  PatternIterator end (int j) const;
122 
123  // ------------------------------------ General slicing.
124 
126  PatternRange range() const;
127 
129  operator PatternRange() const;
130 
132  PatternRange window(int i0, int i1, int j0, int j1) const;
133 
135  Array2<PatternRange> windows(const intCRange& i, const intCRange& j) const;
136 
138  Pattern& operator = (const PatternRange& that);
139 
140  // ------------------------------------ Row slicing.
141 
143  PatternRange row(int i) const;
144 
146  PatternRange rows(int i0, int i1) const;
147 
149  Array1<PatternRange> rows(const intCRange& i) const;
150 
152  PatternRange top(int i) const;
153 
155  PatternRange cut_top(int i) const;
156 
158  Pair split_top(int i) const;
159 
161  PatternRange bottom(int i) const;
162 
164  PatternRange cut_bottom(int i) const;
165 
167  Pair split_bottom(int i) const;
168 
169  // ------------------------------------ Column slicing.
170 
172  PatternRange column(int j) const;
173 
175  PatternRange columns(int j0, int j1) const;
176 
178  Array1<PatternRange> columns(const intCRange& j) const;
179 
181  PatternRange left(int j) const;
182 
184  PatternRange cut_left(int j) const;
185 
187  Pair split_left(int j) const;
188 
190  PatternRange right(int j) const;
191 
193  PatternRange cut_right(int j) const;
194 
196  Pair split_right(int j) const;
197 
198  // ------------------------------------ Static generators.
199 
201  static Pattern identity(int IJ);
202 
204  static Pattern random(int I, int J, int N);
205 
207  static Pattern fill(const intCRange& i, const intCRange& j);
208 
210  static Pattern swap(int I, int J, std::vector<int>& Ao, std::vector<int>& Ai);
211 
213  void debug(std::ostream& out) const;
214 
215  private:
216 
217  // Returns offset to *this A(i,j). Returns -1 if (i,j) is a structural zero. Throws if (i,j) are out of (I,J) bounds.
218  int offset(int i, int j) const;
219 
220  // Stores size.
221  int I;
222  int J;
223 
224  // Stores offsets for each column of A.
225  std::vector<int> Ao;
226 
227  // Holds row indices (i). The sorted i-pattern of column A(:,j) is given by Ai[Ao[j]:Ao[j+1]]
228  std::vector<int> Ai;
229 
230  // Nonmember friend functions that are granted access to internal data structures.
231  friend class PatternBuilder;
232  friend class detail::permute_wrapper;
233  friend class detail::horzcat_wrapper;
234  friend class detail::vertcat_wrapper;
235  friend class detail::bothcat_wrapper;
236  friend class detail::diagcat_wrapper;
237  friend class detail::transpose_wrapper;
238  friend class detail::multiply_wrapper;
239  friend class detail::flip_wrapper;
240  friend Pattern detail::stencil1(int I);
241  friend Pattern detail::stencil2(int I, int J);
242  friend Pattern detail::stencil3(int I, int J, int K);
243  template<class Number> friend class SparseMatrix;
244  };
245 
246 } // namespace
247 
248 #endif
Container of values, allows random (i,j) access.
Definition: Array2.h:30
Number random()
Generate random real/complex Numbers, uniformly distributed over [-1,1].
Definition: bothcat.cpp:20
Definition: multiply.cpp:22
An iterator over a Pattern.
Definition: PatternRange.h:178
Definition: syntax.dox:1
Represents an immutable view of a Pattern.
Definition: PatternRange.h:31
Definition: random.cpp:45
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Definition: flip.cpp:18
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
std::pair< PatternRange, PatternRange > Pair
Useful typedefs.
Definition: Pattern.h:60
Definition: diagcat.cpp:20
Range/Iterator types associated with Pattern.
Holds the nonzero pattern of a sparse matrix.
Definition: Pattern.h:55
Like Pattern, but easier to populate via insert()/erase() methods.
Definition: PatternBuilder.h:31
Container of values, allows random (i) access.
Definition: Array1.h:32
Definition: horzcat.cpp:20
Stores an IxJ matrix A in compressed sparse column format.
Definition: bothcat.h:23
Definition: permute.cpp:29
Definition: vertcat.cpp:20
Represents a const intRange.
Definition: intRange.h:142
Definition: transpose.cpp:23