MyraMath
Matrix.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_DENSE_MATRIX_H
7 #define MYRAMATH_DENSE_MATRIX_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
16 
19 
20 #include <utility>
21 #include <vector>
22 #include <iosfwd>
23 #ifdef MYRAMATH_ENABLE_CPP11
24 #include <initializer_list>
25 #endif
26 
27 namespace myra {
28 
29 // Forward declarations.
30 class intCRange;
31 class InputStream;
32 class OutputStream;
33 template<int Arity, class Number> class Expression;
34 template<class Number> class Matrix;
35 template<class T> class Array1;
36 template<class T> class Array2;
37 
39 template<class Number> class LIBPUBLIC Matrix
40  {
41  public:
42 
44  typedef std::pair< MatrixRange<Number>, MatrixRange<Number> > Pair;
45  typedef std::pair<CMatrixRange<Number>, CMatrixRange<Number> > CPair;
46 
47  // ------------------------------------ Construction, serialization, value semantics.
48 
50  Matrix();
51 
53  explicit Matrix(int I, int J);
55  explicit Matrix(std::pair<int,int> IJ);
57 
59  explicit Matrix(const Expression<2,Number>& e);
60 
62  Matrix(const Matrix& that);
63 
65  void swap(Matrix& that);
66 
67 #ifdef MYRAMATH_ENABLE_CPP11
68  Matrix(Matrix&& that);
70 #endif
71 
73  Matrix<Number>& operator = (Matrix that);
74 
76  explicit Matrix(const CMatrixRange<Number>& that);
77 
79  explicit Matrix(InputStream& in);
80 
82  void write(OutputStream& out) const;
83 
85  const Matrix<Number>& add_const() const;
86 
88  ~Matrix();
89 
90  // ------------------------------------ Size queries.
91 
93  std::pair<int,int> size() const;
94 
96  uint64_t n_words() const;
97 
98  // ------------------------------------ General slicing.
99 
101  CMatrixRange<Number> range() const;
103  MatrixRange<Number> range() ;
105 
107  operator const CMatrixRange<Number> () const;
109  operator const MatrixRange<Number> () ;
111 
113  CMatrixRange<Number> window(int i0, int i1, int j0, int j1) const;
115  MatrixRange<Number> window(int i0, int i1, int j0, int j1) ;
117 
119  Array2<CMatrixRange<Number> > windows(const intCRange& i, const intCRange& j) const;
121  Array2< MatrixRange<Number> > windows(const intCRange& i, const intCRange& j) ;
123 
125  CVectorRange<Number> vector(int j) const;
127  VectorRange<Number> vector(int j) ;
129 
131  const Number& operator () (int i, int j) const;
133  Number& operator () (int i, int j) ;
135 
137  const Number& at(int i, int j) const;
139  Number& at(int i, int j) ;
141 
142  // ------------------------------------ Row slicing.
143 
145  CMatrixRange<Number> row(int i) const;
147  MatrixRange<Number> row(int i) ;
149 
151  CMatrixRange<Number> rows(int i0, int i1) const;
153  MatrixRange<Number> rows(int i0, int i1) ;
155 
157  Array1<CMatrixRange<Number> > rows(const intCRange& i) const;
159  Array1< MatrixRange<Number> > rows(const intCRange& i) ;
161 
163  CMatrixRange<Number> top(int i) const;
165  MatrixRange<Number> top(int i) ;
167 
169  CMatrixRange<Number> cut_top(int i) const;
171  MatrixRange<Number> cut_top(int i) ;
173 
175  CPair split_top(int i) const;
177  Pair split_top(int i) ;
179 
181  CMatrixRange<Number> bottom(int i) const;
183  MatrixRange<Number> bottom(int i) ;
185 
187  CMatrixRange<Number> cut_bottom(int i) const;
189  MatrixRange<Number> cut_bottom(int i) ;
191 
193  CPair split_bottom(int i) const;
195  Pair split_bottom(int i) ;
197 
198  // ------------------------------------ Column slicing.
199 
201  CMatrixRange<Number> column(int j) const;
203  MatrixRange<Number> column(int j) ;
205 
207  CMatrixRange<Number> columns(int j0, int j1) const;
209  MatrixRange<Number> columns(int j0, int j1) ;
211 
213  Array1<CMatrixRange<Number> > columns(const intCRange& j) const;
215  Array1< MatrixRange<Number> > columns(const intCRange& j) ;
217 
219  CMatrixRange<Number> left(int j) const;
221  MatrixRange<Number> left(int j) ;
223 
225  CMatrixRange<Number> cut_left(int j) const;
227  MatrixRange<Number> cut_left(int j) ;
229 
231  CPair split_left(int j) const;
233  Pair split_left(int j) ;
235 
237  CMatrixRange<Number> right(int j) const;
239  MatrixRange<Number> right(int j) ;
241 
243  CMatrixRange<Number> cut_right(int j) const;
245  MatrixRange<Number> cut_right(int j) ;
247 
249  CPair split_right(int j) const;
251  Pair split_right(int j) ;
253 
254  // ------------------------------------ Manipulating numeric contents.
255 
257  // Functor f should have an operator(), that takes a Number and returns a Number.
258  template<class Functor> void transform(const Functor& f)
259  { this->range().transform(f); }
260 
262  // Functor f should have an operator(), that takes a Number and returns a Number.
263  template<class Functor> void transform_triangle(const Functor& f, char uplo)
264  { this->range().transform_triangle(f,uplo); }
265 
267  // Functor f should have an operator(), that takes a Number and returns a Number.
268  template<class Functor> void transform_diagonal(const Functor& f)
269  { this->range().transform_diagonal(f); }
270 
272  Matrix<Number>& operator = (const CMatrixRange<Number>& that);
274  Matrix<Number>& operator = (const Expression<2,Number>& that);
276 
278  void assign(const CMatrixRange<Number>& that);
280  void assign(const CMatrixRange<Number>& that, char op);
281  void assign(const Expression<2,Number>& that);
283 
285  void operator += (const CMatrixRange<Number>& that);
287  void operator += (const Expression<2,Number>& that);
289 
291  void operator -= (const CMatrixRange<Number>& that);
293  void operator -= (const Expression<2,Number>& that);
295 
297  void operator *= (Number alpha);
298 
300  void operator /= (Number alpha);
301 
303  Matrix<Number> operator -();
304 
305  // ------------------------------------ Static generators.
306 
308  static Matrix<Number> identity(int IJ);
309 
311  static Matrix<Number> random(int I, int J);
312 
314  static Matrix<Number> zeros(int I, int J);
315 
317  static Matrix<Number> ones(int I, int J);
318 
320  static Matrix<Number> fill(int I, int J, Number c);
321 
322 #ifdef MYRAMATH_ENABLE_CPP11
323  static Matrix<Number> fill_cmajor(int I, int J, std::initializer_list<Number> list); // list given in column-major order
325  static Matrix<Number> fill_rmajor(int I, int J, std::initializer_list<Number> list); // list given in row-major order (more legible)
326 #endif
327 
329  static Matrix<Number> evaluate(const Expression<2,Number>& e);
330 
331  private:
332 
333  // Implementation detail of constructors/assignment operators.
334  void initialize(int in_I, int in_J);
335 
336  // Returns the offset to A(i,j) inside contents[]
337  size_t offset(int i, int j) const;
338 
339  // Size members.
340  int I;
341  int J;
342 
343  // Numeric contents.
344  typedef std::vector<Number> Contents;
345  Contents contents;
346 
347  };
348 
350 template<class Number> class ReflectNumber< Matrix<Number> >
351  { public: typedef Number type; };
352 
354 LIBPUBLIC Matrix<NumberS> operator + (const CMatrixRange<NumberS>& A, const CMatrixRange<NumberS>& B);
356 LIBPUBLIC Matrix<NumberD> operator + (const CMatrixRange<NumberD>& A, const CMatrixRange<NumberD>& B);
357 LIBPUBLIC Matrix<NumberC> operator + (const CMatrixRange<NumberC>& A, const CMatrixRange<NumberC>& B);
358 LIBPUBLIC Matrix<NumberZ> operator + (const CMatrixRange<NumberZ>& A, const CMatrixRange<NumberZ>& B);
360 
362 LIBPUBLIC Matrix<NumberS> operator - (const CMatrixRange<NumberS>& A, const CMatrixRange<NumberS>& B);
364 LIBPUBLIC Matrix<NumberD> operator - (const CMatrixRange<NumberD>& A, const CMatrixRange<NumberD>& B);
365 LIBPUBLIC Matrix<NumberC> operator - (const CMatrixRange<NumberC>& A, const CMatrixRange<NumberC>& B);
366 LIBPUBLIC Matrix<NumberZ> operator - (const CMatrixRange<NumberZ>& A, const CMatrixRange<NumberZ>& B);
368 
370 LIBPUBLIC Matrix<NumberS> operator * (NumberS alpha, const CMatrixRange<NumberS>& A);
372 LIBPUBLIC Matrix<NumberD> operator * (NumberD alpha, const CMatrixRange<NumberD>& A);
373 LIBPUBLIC Matrix<NumberC> operator * (NumberC alpha, const CMatrixRange<NumberC>& A);
374 LIBPUBLIC Matrix<NumberZ> operator * (NumberZ alpha, const CMatrixRange<NumberZ>& A);
376 
378 LIBPUBLIC Matrix<NumberS> operator * (const CMatrixRange<NumberS>& A, NumberS alpha);
380 LIBPUBLIC Matrix<NumberD> operator * (const CMatrixRange<NumberD>& A, NumberD alpha);
381 LIBPUBLIC Matrix<NumberC> operator * (const CMatrixRange<NumberC>& A, NumberC alpha);
382 LIBPUBLIC Matrix<NumberZ> operator * (const CMatrixRange<NumberZ>& A, NumberZ alpha);
384 
388 LIBPUBLIC Matrix<NumberZ> make_complex(const CMatrixRange<NumberD>& A);
390 
392 LIBPUBLIC Matrix<NumberC> make_complex(const CMatrixRange<NumberS>& R, const CMatrixRange<NumberS>& I);
394 LIBPUBLIC Matrix<NumberZ> make_complex(const CMatrixRange<NumberD>& R, const CMatrixRange<NumberD>& I);
396 
398 LIBPUBLIC Matrix<NumberS> realpart(const CMatrixRange<NumberS>& A);
400 LIBPUBLIC Matrix<NumberD> realpart(const CMatrixRange<NumberD>& A);
401 LIBPUBLIC Matrix<NumberS> realpart(const CMatrixRange<NumberC>& A);
402 LIBPUBLIC Matrix<NumberD> realpart(const CMatrixRange<NumberZ>& A);
404 
406 LIBPUBLIC Matrix<NumberS> imagpart(const CMatrixRange<NumberC>& A);
408 LIBPUBLIC Matrix<NumberD> imagpart(const CMatrixRange<NumberZ>& A);
410 
411 } // namespace myra
412 
413 #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].
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
void transform(const Functor &f)
Overwrites every A(i,j) in this Matrix with f(A(i,j)).
Definition: Matrix.h:258
Interface class for representing subranges of dense Matrix&#39;s.
Interface class for representing subranges of dense Vector&#39;s.
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Represents a mutable VectorRange.
Definition: axpy.h:21
void transform_triangle(const Functor &f, char uplo)
Overwrites every A(i,j) in the &#39;U&#39;pper or &#39;L&#39;ower triangle of this Matrix with f(A(i,j)).
Definition: Matrix.h:263
Definition: syntax.dox:1
Represents a const MatrixRange.
Definition: bothcat.h:22
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Various utility functions/classes related to scalar Number types.
std::pair< MatrixRange< Number >, MatrixRange< Number > > Pair
Useful typedefs.
Definition: Matrix.h:44
Represents a mutable MatrixRange.
Definition: conjugate.h:26
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
void transform_diagonal(const Functor &f)
Overwrites every A(ij,ij) on the diagonal of this Matrix with f(A(ij,ij)).
Definition: Matrix.h:268
Represents a const VectorRange.
Definition: axpy.h:20
Container of values, allows random (i) access.
Definition: Array1.h:32
Expression< 1, NumberC > make_complex(const Expression< 1, NumberS > &A)
Promotes a real Expression into a complex one.
Definition: functions_complex.cpp:122
Given an index (i,j,etc), returns a value.
Definition: arithmetic.h:19
Represents a const intRange.
Definition: intRange.h:142
float NumberS
Useful typedefs.
Definition: Number.h:21