MyraMath
VectorRange.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_VECTORRANGE_H
7 #define MYRAMATH_DENSE_VECTORRANGE_H
8 
15 #include <myramath/utility/detail/LIBPUBLIC.h>
16 
17 #include <iosfwd>
18 #include <utility>
19 #include <vector>
20 
21 namespace myra {
22 
23 // Forward declarations.
24 template<int Arity, class Number> class Expression;
25 template<class Number> class MatrixRange;
26 template<class Number> class CMatrixRange;
27 template<class Number> class VectorRange;
28 template<class Number> class CVectorRange;
29 template<class T> class Array1;
30 class OutputStream;
31 class intCRange;
32 
34 template<class Number> class LIBPUBLIC VectorRange
35  {
36  public:
37 
39  Number* begin;
41  int N;
43 
44  // Useful typedef.
45  typedef std::pair<VectorRange<Number>, VectorRange<Number> > Pair;
46 
47  // ------------------------------------ Construction and range semantics.
48 
50  VectorRange();
51 
53  VectorRange(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  CVectorRange<Number> add_const() const;
70 
72  operator CVectorRange<Number>() const;
73 
75  VectorRange<Number> window(int n0, int n1) const;
76 
78  Array1<VectorRange<Number> > windows(const intCRange& n) const;
79 
81  MatrixRange<Number> column() const;
82 
84  MatrixRange<Number> row() const;
85 
87  Number& operator() (int n) const;
88  Number& operator[] (int n) const;
89 
91  Number& at(int n) const;
92 
93  // ------------------------------------ Vector slicing.
94 
96  VectorRange<Number> first(int n) const;
97 
99  VectorRange<Number> cut_first(int n) const;
100 
102  Pair split_first(int n) const;
103 
105  VectorRange<Number> last(int n) const;
106 
108  VectorRange<Number> cut_last(int n) const;
109 
111  Pair split_last(int n) const;
112 
113  // ------------------------------------ Mutating underlying contents.
114 
116  // Functor f should be a have an operator(), that takes a Number and returns a Number.
117  template<class Functor> void transform(const Functor& f) const
118  {
119  for (int n = 0; n < N; ++n)
120  *pointer(n) = f( *pointer(n) );
121  }
122 
124  void assign(const CVectorRange<Number>& that) const;
126  void assign(const Expression<1,Number>& that) const;
128 
130  void operator += (const CVectorRange<Number>& that) const;
132  void operator += (const Expression<1,Number>& that) const;
134 
136  void operator -= (const CVectorRange<Number>& that) const;
138  void operator -= (const Expression<1,Number>& that) const;
140 
142  void operator *= (Number alpha) const;
143 
145  void operator /= (Number alpha) const;
146 
148  void zero() const;
149 
150  private:
151 
152  // Returns pointer to n'th entry.
153  Number* pointer(int n) const;
154 
155  // Throws if that is not the same size as *this.
156  void check_size(const CVectorRange<Number>& that) const;
157 
158  // Throws if S is not the same size as *this.
159  void check_size(int S) const;
160 
161  };
162 
164 template<class Number> class LIBPUBLIC CVectorRange
165  {
166  public:
167 
169  const Number* begin;
171  int N;
173 
174  // Useful typedef.
175  typedef std::pair<CVectorRange<Number>, CVectorRange<Number> > Pair;
176 
177  // ------------------------------------ Construction and range semantics.
178 
180  CVectorRange();
181 
183  CVectorRange(const Number* in_begin, int in_N);
184 
186  void write(OutputStream& out) const;
187 
188  // ------------------------------------ Size queries.
189 
191  int size() const;
192 
194  int n_words() const;
195 
196  // ------------------------------------ General slicing.
197 
199  VectorRange<Number> remove_const() const;
200 
202  CVectorRange<Number> window(int n0, int n1) const;
203 
205  Array1<CVectorRange<Number> > windows(const intCRange& n) const;
206 
208  CMatrixRange<Number> column() const;
209 
211  CMatrixRange<Number> row() const;
212 
214  const Number& operator() (int n) const;
215  const Number& operator[] (int n) const;
216 
218  const Number& at(int n) const;
219 
220  // ------------------------------------ Vector slicing.
221 
223  CVectorRange<Number> first(int n) const;
224 
226  CVectorRange<Number> cut_first(int n) const;
227 
229  Pair split_first(int n) const;
230 
232  CVectorRange<Number> last(int n) const;
233 
235  CVectorRange<Number> cut_last(int n) const;
236 
238  Pair split_last(int n) const;
239 
240  private:
241 
242  // Returns pointer to n'th entry.
243  const Number* pointer(int n) const;
244 
245  };
246 
248 template<class Number> class ReflectNumber< VectorRange<Number> >
249  { public: typedef Number type; };
250 
252 template<class Number> class ReflectNumber< CVectorRange<Number> >
253  { public: typedef Number type; };
254 
256 LIBPUBLIC Array1<CVectorRange<NumberS> > add_const(const Array1<VectorRange<NumberS> >& ranges);
258 LIBPUBLIC Array1<CVectorRange<NumberD> > add_const(const Array1<VectorRange<NumberD> >& ranges);
259 LIBPUBLIC Array1<CVectorRange<NumberC> > add_const(const Array1<VectorRange<NumberC> >& ranges);
260 LIBPUBLIC Array1<CVectorRange<NumberZ> > add_const(const Array1<VectorRange<NumberZ> >& ranges);
262 
264 LIBPUBLIC Array1<VectorRange<NumberS> > remove_const(const Array1<CVectorRange<NumberS> >& ranges);
266 LIBPUBLIC Array1<VectorRange<NumberD> > remove_const(const Array1<CVectorRange<NumberD> >& ranges);
267 LIBPUBLIC Array1<VectorRange<NumberC> > remove_const(const Array1<CVectorRange<NumberC> >& ranges);
268 LIBPUBLIC Array1<VectorRange<NumberZ> > remove_const(const Array1<CVectorRange<NumberZ> >& ranges);
270 
272 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CVectorRange<NumberS>& v);
274 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CVectorRange<NumberD>& v);
275 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CVectorRange<NumberC>& v);
276 LIBPUBLIC std::ostream& operator << (std::ostream& out, const CVectorRange<NumberZ>& v);
278 
279 } // namespace myra
280 
281 #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 mutable VectorRange.
Definition: axpy.h:21
void transform(const Functor &f) const
Overwrites every x(n) in this VectorRange with f(x(n)).
Definition: VectorRange.h:117
int N
---------— Data members, all public ----------------—
Definition: VectorRange.h:41
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.
int N
---------— Data members, all public ----------------—
Definition: VectorRange.h:171
Represents a mutable MatrixRange.
Definition: conjugate.h:26
Represents a const VectorRange.
Definition: axpy.h:20
Container of values, allows random (i) access.
Definition: Array1.h:32
Given an index (i,j,etc), returns a value.
Definition: arithmetic.h:19
Represents a const intRange.
Definition: intRange.h:142