MyraMath
DiagonalMatrixRange.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_DIAGONALMATRIXRANGE_H
7 #define MYRAMATH_DENSE_DIAGONALMATRIXRANGE_H
8 
15 #include <myramath/utility/detail/LIBPUBLIC.h>
16 
17 #include <iosfwd>
18 
19 namespace myra {
20 
21 // Forward declarations.
22 template<int Arity, class Number> class Expression;
23 template<class Number> class Matrix;
24 template<class Number> class MatrixRange;
25 template<class Number> class CMatrixRange;
26 template<class Number> class Vector;
27 template<class Number> class VectorRange;
28 template<class Number> class CVectorRange;
29 template<class Number> class DiagonalMatrixRange;
30 template<class Number> class CDiagonalMatrixRange;
31 class OutputStream;
32 
34 template<class Number> class LIBPUBLIC DiagonalMatrixRange
35  {
36  public:
37 
39  Number* begin;
41  int N;
43 
44  // Useful typedef.
45  typedef std::pair<DiagonalMatrixRange<Number>, DiagonalMatrixRange<Number> > Pair;
46 
47  // ------------------------------------ Construction and range semantics.
48 
51 
53  DiagonalMatrixRange(Number* in_begin, int in_N);
54 
56  void write(OutputStream& out) const;
57 
58  // ------------------------------------ Size queries.
59 
61  int size() const;
62 
64  int n_words() const;
65 
66  // ------------------------------------ General slicing.
67 
69  const CDiagonalMatrixRange<Number> add_const() const;
70 
72  operator const CDiagonalMatrixRange<Number>() const;
73 
75  const DiagonalMatrixRange<Number> window(int n0, int n1) const;
76 
78  const VectorRange<Number> vector() const;
79 
81  Number& operator() (int n) const;
82 
84  Number& at(int n) const;
85 
87  Matrix<Number> make_Matrix() const;
89  void make_Matrix(const MatrixRange<Number>& A) const;
91 
93  Vector<Number> make_Vector() const;
95  void make_Vector(const VectorRange<Number>& A) const;
97 
98  // ------------------------------------ Diagonal slicing.
99 
101  const DiagonalMatrixRange<Number> first(int n) const;
102 
104  const DiagonalMatrixRange<Number> cut_first(int n) const;
105 
107  const Pair split_first(int n) const;
108 
110  const DiagonalMatrixRange<Number> last(int n) const;
111 
113  const DiagonalMatrixRange<Number> cut_last(int n) const;
114 
116  const Pair split_last(int n) const;
117 
118  // ------------------------------------ Mutating underlying contents.
119 
121  // Functor f should be a have an operator(), that takes a Number and returns a Number.
122  template<class Functor> void transform(const Functor& f) const
123  {
124  for (int n = 0; n < N; ++n)
125  *pointer(n) = f( *pointer(n) );
126  }
127 
129  void assign(const CDiagonalMatrixRange<Number>& that) const;
131  void assign(const Expression<1,Number>& that) const;
133 
135  void operator += (const CDiagonalMatrixRange<Number>& that) const;
137  void operator += (const Expression<1,Number>& that) const;
139 
141  void operator -= (const CDiagonalMatrixRange<Number>& that) const;
143  void operator -= (const Expression<1,Number>& that) const;
145 
147  void operator *= (Number alpha) const;
148 
150  void operator /= (Number alpha) const;
151 
153  void zero() const;
154 
155  private:
156 
157  // Returns pointer to n'th entry.
158  Number* pointer(int n) const;
159 
160  // Throws if that is not the same size as *this.
161  void check_size(const CDiagonalMatrixRange<Number>& that) const;
162 
163  // Throws if S is not the same size as *this.
164  void check_size(int S) const;
165 
166  };
167 
169 template<class Number> class LIBPUBLIC CDiagonalMatrixRange
170  {
171  public:
172 
174  const Number* begin;
176  int N;
178 
179  // Useful typedef.
180  typedef std::pair<CDiagonalMatrixRange<Number>, CDiagonalMatrixRange<Number> > Pair;
181 
182  // ------------------------------------ Construction and range semantics.
183 
185  CDiagonalMatrixRange();
186 
188  CDiagonalMatrixRange(const Number* in_begin, int in_N);
189 
191  void write(OutputStream& out) const;
192 
193  // ------------------------------------ Size queries.
194 
196  int size() const;
197 
199  int n_words() const;
200 
201  // ------------------------------------ General slicing.
202 
204  const DiagonalMatrixRange<Number> remove_const() const;
205 
207  const CDiagonalMatrixRange<Number> window(int n0, int n1) const;
208 
210  const CVectorRange<Number> vector() const;
211 
213  const Number& operator() (int n) const;
214 
216  const Number& at(int n) const;
217 
219  Matrix<Number> make_Matrix() const;
221  void make_Matrix(const MatrixRange<Number>& A) const;
223 
225  Vector<Number> make_Vector() const;
227  void make_Vector(const VectorRange<Number>& A) const;
229 
230  // ------------------------------------ Diagonal slicing.
231 
233  const CDiagonalMatrixRange<Number> first(int n) const;
234 
236  const CDiagonalMatrixRange<Number> cut_first(int n) const;
237 
239  const Pair split_first(int n) const;
240 
242  const CDiagonalMatrixRange<Number> last(int n) const;
243 
245  const CDiagonalMatrixRange<Number> cut_last(int n) const;
246 
248  const Pair split_last(int n) const;
249 
250  private:
251 
252  // Returns pointer to n'th entry.
253  const Number* pointer(int n) const;
254 
255  };
256 
258 template<class Number> class ReflectNumber< DiagonalMatrixRange<Number> >
259  { public: typedef Number type; };
260 
262 template<class Number> class ReflectNumber< CDiagonalMatrixRange<Number> >
263  { public: typedef Number type; };
264 
266 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CDiagonalMatrixRange<NumberS>& a);
268 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CDiagonalMatrixRange<NumberD>& a);
269 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CDiagonalMatrixRange<NumberC>& a);
270 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CDiagonalMatrixRange<NumberZ>& a);
272 
273 } // namespace myra
274 
275 #endif
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
Represents a const DiagonalMatrixRange.
Definition: conjugate.h:35
int N
---------— Data members, all public ----------------—
Definition: DiagonalMatrixRange.h:41
int N
---------— Data members, all public ----------------—
Definition: DiagonalMatrixRange.h:176
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
Definition: syntax.dox:1
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
void transform(const Functor &f) const
Overwrites every D(n) in this DiagonalMatrixRange with f(D(n)).
Definition: DiagonalMatrixRange.h:122
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
Represents a const VectorRange.
Definition: axpy.h:20
Given an index (i,j,etc), returns a value.
Definition: arithmetic.h:19
Represents a mutable DiagonalMatrixRange.
Definition: conjugate.h:29