MyraMath
PatternBuilder.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_PATTERNBUILDER_H
7 #define MYRAMATH_SPARSE_PATTERNBUILDER_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
15 
16 #include <vector>
17 #include <set>
18 #include <unordered_set>
19 #include <iosfwd>
20 
21 namespace myra {
22 
23 // Forward declarations.
24 class InputStream;
25 class OutputStream;
26 class intCRange;
27 class Pattern;
28 class PatternRange;
29 
31 class LIBPUBLIC PatternBuilder
32  {
33 
34  public:
35 
37  typedef std::set<int> Column;
38  typedef std::vector<Column> Columns;
39  typedef Column::const_iterator Iterator;
40 
41  // ------------------------------------ Construction, serialization, value semantics.
42 
45 
47  explicit PatternBuilder (std::pair<int,int> IJ);
48  explicit PatternBuilder (int in_I, int in_J);
49 
51  PatternBuilder(const PatternBuilder& that);
52 
54  void swap(PatternBuilder& that);
55 
56 #ifdef MYRAMATH_ENABLE_CPP11
59 #endif
60 
62  PatternBuilder& operator = (PatternBuilder that);
63 
65  PatternBuilder& operator = (const PatternRange& that);
66 
68  explicit PatternBuilder(const PatternRange& that);
69 
71  explicit PatternBuilder(InputStream& in);
72 
74  void write(OutputStream& out) const;
75 
76  // ------------------------------------ Manipulating symbolic contents.
77 
79  void insert(int i, int j);
80 
82  // Prefer insert_column() over this, due to internal column-based layout.
83  void insert_row(int i, const intCRange& j_pattern);
84 
86  // Prefer this over insert_row(), due to internal column-based layout.
87  void insert_column(const intCRange& i_pattern, int j);
88 
90  void insert_tensor(const intCRange& i_pattern, const intCRange& j_pattern);
91 
93  void insert_tensor(const intCRange& ij_pattern);
94 
96  void insert(const PatternBuilder& that, int i_offset = 0, int j_offset = 0);
97  void insert(const PatternRange& that, int i_offset = 0, int j_offset = 0);
98 
100  void erase(int i, int j);
101 
103  void erase(const PatternBuilder& that, int i_offset = 0, int j_offset = 0);
104  void erase(const PatternRange& that, int i_offset = 0, int j_offset = 0);
105 
106  // ------------------------------------ Iterators.
107 
109  Iterator begin(int j) const;
110  Iterator end (int j) const;
111 
112  // ------------------------------------ Querying symbolic contents.
113 
115  bool test(int i, int j) const;
116 
118  std::vector<int> pattern(int j) const;
119 
121  const Column& column(int j) const;
122 
124  std::pair<int,int> size() const;
125 
127  int n_nonzeros(int j) const;
128  int n_nonzeros() const;
129 
130  // ------------------------------------ Static generators.
131 
133  static PatternBuilder identity(int IJ);
134 
136  static PatternBuilder random(std::pair<int,int> IJ, int N);
137  static PatternBuilder random(int I, int J, int N);
138 
140  Pattern make_Pattern() const;
141 
142  private:
143 
144  // Implementation detail of constructors.
145  void constructor_detail(int in_I, int in_J);
146 
147  // Holds size.
148  int I;
149  int J;
150 
151  // Numeric contents.
152  Columns columns;
153  };
154 
156 LIBPUBLIC PatternBuilder operator+ (const PatternBuilder& P1, const PatternBuilder& P2);
157 
159 LIBPUBLIC PatternBuilder operator- (const PatternBuilder& P1, const PatternBuilder& P2);
160 
162 LIBPUBLIC PatternBuilder operator* (const PatternBuilder& P1, const PatternBuilder& P2);
163 
165 LIBPUBLIC std::ostream& operator << (std::ostream& out, const PatternBuilder& p);
166 
167 } // namespace
168 
169 #endif
Number random()
Generate random real/complex Numbers, uniformly distributed over [-1,1].
std::set< int > Column
Useful typedefs.
Definition: PatternBuilder.h:37
Definition: syntax.dox:1
Represents an immutable view of a Pattern.
Definition: PatternRange.h:31
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
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
Represents a const intRange.
Definition: intRange.h:142