MyraMath
SparseMatrixBuilder.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_SPARSEMATRIXBUILDER_H
7 #define MYRAMATH_SPARSE_SPARSEMATRIXBUILDER_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
16 
17 #include <vector>
18 #include <map>
19 #include <iosfwd>
20 
21 namespace myra {
22 
23 // Forward declarations.
24 class PatternBuilder;
25 class InputStream;
26 class OutputStream;
27 template<class Number> class SparseMatrix;
28 template<class Number> class CSparseMatrixRange;
29 template<class Number> class Matrix;
30 
32 template<class Number> class LIBPUBLIC SparseMatrixBuilder
33  {
34  public:
35 
36  // Useful typedefs.
37  typedef std::map<int,Number> Column;
38  typedef std::vector<Column> Columns;
39  typedef typename Column::const_iterator Iterator;
40 
41  // ------------------------------------ Construction, serialization, value semantics.
42 
44  SparseMatrixBuilder();
45 
47  explicit SparseMatrixBuilder(int in_I, int in_J);
48  explicit SparseMatrixBuilder(std::pair<int,int> in_IJ);
49 
51  SparseMatrixBuilder(const SparseMatrixBuilder& that);
52 
54  void swap(SparseMatrixBuilder& that);
55 
56 #ifdef MYRAMATH_ENABLE_CPP11
57  SparseMatrixBuilder(SparseMatrixBuilder&& that);
59 #endif
60 
62  SparseMatrixBuilder& operator = (SparseMatrixBuilder that);
63 
65  SparseMatrixBuilder& operator = (const CSparseMatrixRange<Number>& that);
66 
68  explicit SparseMatrixBuilder(const CSparseMatrixRange<Number>& that);
69 
71  explicit SparseMatrixBuilder(InputStream& in);
72 
74  void write(OutputStream& out) const;
75 
77  ~SparseMatrixBuilder();
78 
79  // ------------------------------------ Iterators.
80 
82  Iterator begin(int j) const;
83  Iterator end(int j) const;
84 
85  // ------------------------------------ Manipulating numeric contents.
86 
88  const Number& operator () (int i, int j) const;
89 
91  Number& operator () (int i, int j);
92 
94  void erase(int i, int j);
95 
97  void operator += (const SparseMatrixBuilder& that);
98  void operator += (const CSparseMatrixRange<Number>& that);
99 
101  void operator -= (const SparseMatrixBuilder& that);
102  void operator -= (const CSparseMatrixRange<Number>& that);
103 
105  void operator *= (Number alpha);
106 
108  void operator /= (Number alpha);
109 
110  // ------------------------------------ Symbolic queries.
111 
113  bool test(int i, int j) const;
114 
116  PatternBuilder pattern() const;
117 
119  std::vector<int> pattern(int j) const;
120 
122  std::pair<int,int> size() const;
123 
125  int n_nonzeros(int j) const;
126  int n_nonzeros() const;
127 
128  // ------------------------------------ Static generators.
129 
131  static SparseMatrixBuilder<Number> identity(int IJ);
132 
134  static SparseMatrixBuilder<Number> random(int I, int J, int N);
135 
137  static SparseMatrixBuilder<Number> random(const PatternBuilder& P);
138 
140  SparseMatrix<Number> make_SparseMatrix() const;
141 
143  Matrix<Number> make_Matrix() const;
144 
145  private:
146 
147  // Throws if IJ is not the same size as *this.
148  void check_size(const std::pair<int,int>& IJ) const;
149 
150  // Holds size.
151  int I;
152  int J;
153 
154  // Numeric contents.
155  Columns columns;
156  };
157 
159 template<class Number> class ReflectNumber
160  { public: typedef Number type; };
161 
163 LIBPUBLIC SparseMatrixBuilder<NumberS> operator + (const SparseMatrixBuilder<NumberS>& A, const SparseMatrixBuilder<NumberS>& B);
164 LIBPUBLIC SparseMatrixBuilder<NumberD> operator + (const SparseMatrixBuilder<NumberD>& A, const SparseMatrixBuilder<NumberD>& B);
165 LIBPUBLIC SparseMatrixBuilder<NumberC> operator + (const SparseMatrixBuilder<NumberC>& A, const SparseMatrixBuilder<NumberC>& B);
166 LIBPUBLIC SparseMatrixBuilder<NumberZ> operator + (const SparseMatrixBuilder<NumberZ>& A, const SparseMatrixBuilder<NumberZ>& B);
167 
169 LIBPUBLIC SparseMatrixBuilder<NumberS> operator - (const SparseMatrixBuilder<NumberS>& A, const SparseMatrixBuilder<NumberS>& B);
170 LIBPUBLIC SparseMatrixBuilder<NumberD> operator - (const SparseMatrixBuilder<NumberD>& A, const SparseMatrixBuilder<NumberD>& B);
171 LIBPUBLIC SparseMatrixBuilder<NumberC> operator - (const SparseMatrixBuilder<NumberC>& A, const SparseMatrixBuilder<NumberC>& B);
172 LIBPUBLIC SparseMatrixBuilder<NumberZ> operator - (const SparseMatrixBuilder<NumberZ>& A, const SparseMatrixBuilder<NumberZ>& B);
173 
175 LIBPUBLIC SparseMatrixBuilder<NumberS> operator * (const SparseMatrixBuilder<NumberS>& A, NumberS alpha);
176 LIBPUBLIC SparseMatrixBuilder<NumberD> operator * (const SparseMatrixBuilder<NumberD>& A, NumberD alpha);
177 LIBPUBLIC SparseMatrixBuilder<NumberC> operator * (const SparseMatrixBuilder<NumberC>& A, NumberC alpha);
178 LIBPUBLIC SparseMatrixBuilder<NumberZ> operator * (const SparseMatrixBuilder<NumberZ>& A, NumberZ alpha);
179 
181 LIBPUBLIC SparseMatrixBuilder<NumberS> operator * (NumberS alpha, const SparseMatrixBuilder<NumberS>& A);
182 LIBPUBLIC SparseMatrixBuilder<NumberD> operator * (NumberD alpha, const SparseMatrixBuilder<NumberD>& A);
183 LIBPUBLIC SparseMatrixBuilder<NumberC> operator * (NumberC alpha, const SparseMatrixBuilder<NumberC>& A);
184 LIBPUBLIC SparseMatrixBuilder<NumberZ> operator * (NumberZ alpha, const SparseMatrixBuilder<NumberZ>& A);
185 
187 LIBPUBLIC std::ostream& operator << (std::ostream& out, const SparseMatrixBuilder<NumberS>& A);
188 LIBPUBLIC std::ostream& operator << (std::ostream& out, const SparseMatrixBuilder<NumberD>& A);
189 LIBPUBLIC std::ostream& operator << (std::ostream& out, const SparseMatrixBuilder<NumberC>& A);
190 LIBPUBLIC std::ostream& operator << (std::ostream& out, const SparseMatrixBuilder<NumberZ>& A);
191 
192 } // namespace
193 
194 #endif
Number random()
Generate random real/complex Numbers, uniformly distributed over [-1,1].
Definition: syntax.dox:1
Various utility functions/classes related to scalar Number types.