MyraMath
Vector.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_VECTOR_H
7 #define MYRAMATH_DENSE_VECTOR_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
16 
19 
20 #include <vector>
21 #include <iosfwd>
22 #ifdef MYRAMATH_ENABLE_CPP11
23 #include <initializer_list>
24 #endif
25 
26 namespace myra {
27 
28 // Forward declarations.
29 class intCRange;
30 class InputStream;
31 class OutputStream;
32 template<int Arity, class Number> class Expression;
33 template<class Number> class Vector;
34 template<class T> class Array1;
35 
37 template<class Number> class MYRAMATH_EXPORT Vector
38  {
39  public:
40 
42  typedef std::pair<CVectorRange<Number>, CVectorRange<Number> > CPair;
43  typedef std::pair< VectorRange<Number>, VectorRange<Number> > Pair;
44  typedef typename ReflectPrecision<Number>::type Precision;
45 
46  // ------------------------------------ Construction, serialization, value semantics.
47 
49  Vector();
50 
52  explicit Vector(int N);
53 
55  explicit Vector(const Expression<1,Number>& e);
56 
58  Vector(const Vector& that);
59 
61  void swap (Vector& that);
62 
63 #ifdef MYRAMATH_ENABLE_CPP11
64  Vector(Vector&& that);
66 #endif
67 
69  Vector& operator = (Vector that);
70 
72  explicit Vector(const CVectorRange<Number>& that);
73 
75  explicit Vector(const CMatrixRange<Number>& that);
76 
78  explicit Vector(InputStream& in);
79 
81  void write(OutputStream& out) const;
82 
84  const Vector<Number>& add_const() const;
85 
87  ~Vector();
88 
89  // ------------------------------------ Size queries.
90 
92  int size() const;
93 
95  int n_words() const;
96 
97  // ------------------------------------ General slicing.
98 
100  CVectorRange<Number> range() const;
102  VectorRange<Number> range() ;
104 
106  operator CVectorRange<Number> () const;
108  operator VectorRange<Number> () ;
110 
112  CVectorRange<Number> window(int n0, int n1) const;
114  VectorRange<Number> window(int n0, int n1) ;
116 
118  Array1<CVectorRange<Number> > windows(const intCRange& n) const;
120  Array1< VectorRange<Number> > windows(const intCRange& n) ;
122 
124  CMatrixRange<Number> column() const;
126  MatrixRange<Number> column() ;
128 
130  CMatrixRange<Number> row() const;
132  MatrixRange<Number> row() ;
134 
136  const Number& operator () (int n) const;
138  const Number& operator [] (int n) const;
139  Number& operator () (int n) ;
140  Number& operator [] (int n) ;
142 
144  const Number& at(int n) const;
146  Number& at(int n) ;
148 
149  // ------------------------------------ Vector slicing.
150 
152  CVectorRange<Number> first(int n) const;
154  VectorRange<Number> first(int n) ;
156 
158  CVectorRange<Number> cut_first(int n) const;
160  VectorRange<Number> cut_first(int n) ;
162 
164  CPair split_first(int n) const;
166  Pair split_first(int n) ;
168 
170  CVectorRange<Number> last(int n) const;
172  VectorRange<Number> last(int n) ;
174 
176  CVectorRange<Number> cut_last(int n) const;
178  VectorRange<Number> cut_last(int n) ;
180 
182  CPair split_last(int n) const;
184  Pair split_last(int n) ;
186 
187  // ------------------------------------ Manipulating numeric contents.
188 
190  // Functor f should have an operator(), that takes a Number and returns a Number.
191  template<class Functor> void transform(const Functor& f)
192  { this->range().transform(f); }
193 
195  Vector& operator = (const CVectorRange<Number>& that);
196  Vector& operator = (const Expression<1,Number>& that);
197 
199  void assign(const CVectorRange<Number>& that);
200  void assign(const Expression<1,Number>& that);
201 
203  void operator += (const CVectorRange<Number>& that);
204  void operator += (const Expression<1,Number>& that);
205 
207  void operator -= (const CVectorRange<Number>& that);
208  void operator -= (const Expression<1,Number>& that);
209 
211  void operator *= (Number alpha);
212 
214  void operator /= (Number alpha);
215 
217  Vector<Number> operator -();
218 
219  // ------------------------------------ Static generators.
220 
222  static Vector<Number> random(int N);
223 
225  static Vector<Number> zeros(int N);
226 
228  static Vector<Number> ones(int N);
229 
231  static Vector<Number> fill(int N, Number c);
232 
233 #ifdef MYRAMATH_ENABLE_CPP11
234  static Vector<Number> fill(std::initializer_list<Number> list);
236 #endif
237 
239  static Vector<Number> linspace(Number x0, Number x1, int N);
240 
242  static Vector<Number> logspace(Precision x0, Precision x1, int N);
243 
245  static Vector<Number> evaluate(const Expression<1,Number>& e);
246 
247  private:
248 
249  // Numeric contents.
250  typedef std::vector<Number> Contents;
251  Contents contents;
252 
253  };
254 
256 template<class Number> class ReflectNumber< Vector<Number> >
257  { public: typedef Number type; };
258 
260 MYRAMATH_EXPORT Vector<NumberS> operator + (const CVectorRange<NumberS>& a, const CVectorRange<NumberS>& b);
262 MYRAMATH_EXPORT Vector<NumberD> operator + (const CVectorRange<NumberD>& a, const CVectorRange<NumberD>& b);
263 MYRAMATH_EXPORT Vector<NumberC> operator + (const CVectorRange<NumberC>& a, const CVectorRange<NumberC>& b);
264 MYRAMATH_EXPORT Vector<NumberZ> operator + (const CVectorRange<NumberZ>& a, const CVectorRange<NumberZ>& b);
266 
268 MYRAMATH_EXPORT Vector<NumberS> operator - (const CVectorRange<NumberS>& a, const CVectorRange<NumberS>& b);
270 MYRAMATH_EXPORT Vector<NumberD> operator - (const CVectorRange<NumberD>& a, const CVectorRange<NumberD>& b);
271 MYRAMATH_EXPORT Vector<NumberC> operator - (const CVectorRange<NumberC>& a, const CVectorRange<NumberC>& b);
272 MYRAMATH_EXPORT Vector<NumberZ> operator - (const CVectorRange<NumberZ>& a, const CVectorRange<NumberZ>& b);
274 
276 MYRAMATH_EXPORT Vector<NumberS> operator * (NumberS alpha, const CVectorRange<NumberS>& a);
278 MYRAMATH_EXPORT Vector<NumberD> operator * (NumberD alpha, const CVectorRange<NumberD>& a);
279 MYRAMATH_EXPORT Vector<NumberC> operator * (NumberC alpha, const CVectorRange<NumberC>& a);
280 MYRAMATH_EXPORT Vector<NumberZ> operator * (NumberZ alpha, const CVectorRange<NumberZ>& a);
282 
284 MYRAMATH_EXPORT Vector<NumberS> operator * (const CVectorRange<NumberS>& a, NumberS alpha);
286 MYRAMATH_EXPORT Vector<NumberD> operator * (const CVectorRange<NumberD>& a, NumberD alpha);
287 MYRAMATH_EXPORT Vector<NumberC> operator * (const CVectorRange<NumberC>& a, NumberC alpha);
288 MYRAMATH_EXPORT Vector<NumberZ> operator * (const CVectorRange<NumberZ>& a, NumberZ alpha);
290 
292 MYRAMATH_EXPORT Vector<NumberC> make_complex(const CVectorRange<NumberS>& a);
294 MYRAMATH_EXPORT Vector<NumberZ> make_complex(const CVectorRange<NumberD>& a);
296 
298 MYRAMATH_EXPORT Vector<NumberC> make_complex(const CVectorRange<NumberS>& r, const CVectorRange<NumberS>& i);
300 MYRAMATH_EXPORT Vector<NumberZ> make_complex(const CVectorRange<NumberD>& r, const CVectorRange<NumberD>& i);
302 
304 MYRAMATH_EXPORT Vector<NumberS> realpart(const CVectorRange<NumberS>& a);
306 MYRAMATH_EXPORT Vector<NumberD> realpart(const CVectorRange<NumberD>& a);
307 MYRAMATH_EXPORT Vector<NumberS> realpart(const CVectorRange<NumberC>& a);
308 MYRAMATH_EXPORT Vector<NumberD> realpart(const CVectorRange<NumberZ>& a);
310 
312 MYRAMATH_EXPORT Vector<NumberS> imagpart(const CVectorRange<NumberC>& a);
314 MYRAMATH_EXPORT Vector<NumberD> imagpart(const CVectorRange<NumberZ>& a);
316 
317 } // namespace myra
318 
319 #endif
Number random()
Generate random real/complex Numbers, uniformly distributed over [-1,1].
void transform(const Functor &f)
Overwrites every x(n) in this Vector with f(x(n)).
Definition: Vector.h:191
Reflects Number trait for a Container, containers of Numbers (Matrix&#39;s, Vector&#39;s, etc) should special...
Definition: Number.h:55
Interface class for representing subranges of dense Matrix&#39;s.
std::pair< CVectorRange< Number >, CVectorRange< Number > > CPair
Useful typedefs.
Definition: Vector.h:42
Interface class for representing subranges of dense Vector&#39;s.
Represents a mutable VectorRange.
Definition: axpy.h:21
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.
Represents a mutable MatrixRange.
Definition: conjugate.h:26
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
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