OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
MVector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Chris Rogers
3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright notice,
9 * this list of conditions and the following disclaimer in the documentation
10 * and/or other materials provided with the distribution.
11 * 3. Neither the name of STFC nor the names of its contributors may be used to
12 * endorse or promote products derived from this software without specific
13 * prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef MVector_h
29#define MVector_h
30
31#include <iostream>
32#include <vector>
33
35#include "Utilities/GSLMatrix.h"
36
38
39namespace interpolation {
40
56
57 typedef gsl_complex
58 m_complex; // typedef because I guess at some point I may want to do something else
59
60 inline m_complex m_complex_build(double r, double i) {
61 m_complex c = {{r, i}};
62 return c;
63 }
64 inline m_complex m_complex_build(double r) {
65 m_complex c = {{r, 0.}};
66 return c;
67 }
68 // Just overload the standard operators
69 inline m_complex operator*(m_complex c, double d) { return gsl_complex_mul_real(c, d); }
70 inline m_complex operator*(double d, m_complex c) { return gsl_complex_mul_real(c, d); }
71 inline m_complex operator*(m_complex c1, m_complex c2) { return gsl_complex_mul(c1, c2); }
72
73 inline m_complex operator/(m_complex c, double d) { return gsl_complex_div_real(c, d); }
74 inline m_complex operator/(m_complex c1, m_complex c2) { return gsl_complex_div(c1, c2); }
75 inline m_complex operator/(double d, m_complex c) {
77 return gsl_complex_div(c1, c);
78 }
79
80 inline m_complex operator+(m_complex c, double d) { return gsl_complex_add_real(c, d); }
81 inline m_complex operator+(double d, m_complex c) { return gsl_complex_add_real(c, d); }
82 inline m_complex operator+(m_complex c1, m_complex c2) { return gsl_complex_add(c1, c2); }
83
85 c.dat[0] = -c.dat[0];
86 c.dat[1] = -c.dat[1];
87 return c;
88 }
89 inline m_complex operator-(m_complex c, double d) { return gsl_complex_sub_real(c, d); }
90 inline m_complex operator-(double d, m_complex c) { return -gsl_complex_sub_real(c, d); }
91 inline m_complex operator-(m_complex c1, m_complex c2) { return gsl_complex_sub(c1, c2); }
92
93 // suboptimal *= ... should do the substitution in place
94 inline m_complex& operator*=(m_complex& c, double d) {
95 c = gsl_complex_mul_real(c, d);
96 return c;
97 }
99 c1 = gsl_complex_mul(c1, c2);
100 return c1;
101 }
102
103 inline m_complex& operator/=(m_complex& c, double d) {
104 c = gsl_complex_div_real(c, d);
105 return c;
106 }
108 c1 = gsl_complex_div(c1, c2);
109 return c1;
110 }
111
112 inline m_complex& operator+=(m_complex& c, double d) {
113 c = gsl_complex_add_real(c, d);
114 return c;
115 }
117 c1 = gsl_complex_add(c1, c2);
118 return c1;
119 }
120
121 inline m_complex& operator-=(m_complex& c, double d) {
122 c = gsl_complex_sub_real(c, d);
123 return c;
124 }
126 c1 = gsl_complex_sub(c1, c2);
127 return c1;
128 }
129 // comparators
130 inline bool operator==(m_complex c1, m_complex c2) {
131 return c1.dat[0] == c2.dat[0] && c1.dat[1] == c2.dat[1];
132 }
133 inline bool operator!=(m_complex c1, m_complex c2) { return !(c1 == c2); }
134
135 // real and imaginary
136 inline double& re(m_complex& c) { return c.dat[0]; }
137 inline double& im(m_complex& c) { return c.dat[1]; }
138 inline const double& re(const m_complex& c) { return c.dat[0]; }
139 inline const double& im(const m_complex& c) { return c.dat[1]; }
140
141 // complex conjugate
142 inline m_complex conj(const m_complex& c) { return m_complex_build(re(c), -im(c)); }
143
144 std::ostream& operator<<(std::ostream& out, m_complex c);
145 std::istream& operator>>(std::istream& in, m_complex& c);
146
148
149 template <class Tmplt>
150 class MMatrix; // forward declaration because MVector needs to see MMatrix for T() method (and
151 // others)
152
154
155 template <class Tmplt>
156 class MVector {
157 public:
158 MVector() : _vector(nullptr) { ; }
159 MVector(const MVector<Tmplt>& mv);
160 MVector(const Tmplt* ta_beg, const Tmplt* ta_end) : _vector(nullptr) {
161 build_vector(ta_beg, ta_end);
162 } // copy from data and put it in the vector
163 MVector(std::vector<Tmplt> tv) : _vector(nullptr) { build_vector(&tv[0], &tv.back() + 1); }
164 MVector(size_t i);
165 MVector(size_t i, Tmplt value);
166 template <class Tmplt2>
168 ~MVector();
169
170 // return number of elements
171 size_t num_row() const;
172 // return reference to ith element; indexing starts from 1, runs to num_row (inclusive); not
173 // bound checked
174 Tmplt& operator()(size_t i);
175 const Tmplt& operator()(size_t i) const;
176
178 size_t n1, size_t n2) const; // return a vector running from n1 to n2 (inclusive)
179 MMatrix<Tmplt> T() const; // return a matrix that is the transpose of the vector
180 MVector<Tmplt>& operator=(const MVector<Tmplt>& mv); // set *this to be equal to mv
181
183 friend MVector<double>& operator*=(MVector<double>& v, double d);
184
187
190
191 friend class MMatrix<Tmplt>;
192 friend class MMatrix<double>;
193
194 private:
195 void build_vector(size_t size); // copy from data and put it in the vector
196 void build_vector(
197 const Tmplt* data_start,
198 const Tmplt* data_end); // copy from data and put it in the vector
199
201
202 static gsl_vector* get_vector(const MVector<double>& m);
204
205 void* _vector;
206 };
207
208 // Operators do what you would expect - i.e. normal mathematical functions
209 template <class Tmplt>
210 bool operator==(const MVector<Tmplt>& v1, const MVector<Tmplt>& v2);
211 template <class Tmplt>
212 bool operator!=(const MVector<Tmplt>& v1, const MVector<Tmplt>& v2);
213
216 template <class Tmplt>
218 template <class Tmplt>
220
221 template <class Tmplt>
223 template <class Tmplt>
225 template <class Tmplt>
227
230 template <class Tmplt>
232
233 template <class Tmplt>
235 template <class Tmplt>
237 template <class Tmplt>
239
240 // format is
241 // num_row
242 // v1
243 // v2
244 //...
245 template <class Tmplt>
246 std::ostream& operator<<(std::ostream& out, MVector<Tmplt> v);
247 template <class Tmplt>
248 std::istream& operator>>(std::istream& out, MVector<Tmplt>& v);
249
250 // Interfaces between MVector<m_complex> and MVector<double>
253 // return vector of doubles filled with either real or imaginary part of mv
256
258
260
261 template <class Tmplt>
263 delete_vector();
264 }
265 template <>
267 if (_vector != nullptr) gsl_vector_free((gsl_vector*)_vector);
268 }
269 template <>
271 if (_vector != nullptr) gsl_vector_complex_free((gsl_vector_complex*)_vector);
272 }
273
280 return v;
281 }
282
283 template <class Tmplt>
285 return v *= d;
286 }
287 template <class Tmplt>
289 return v * d;
290 }
291
292 template <class Tmplt>
294 return v *= 1. / d;
295 }
296 template <class Tmplt>
298 return v /= d;
299 }
300
309 template <class Tmplt>
311 return v1 += v2;
312 }
313
314 template <class Tmplt>
316 MVector<Tmplt> vo(v);
317 for (size_t i = 1; i <= vo.num_row(); i++)
318 vo(i) = -vo(i);
319 return vo;
320 }
321 template <class Tmplt>
323 return v1 += -v2;
324 }
325 template <class Tmplt>
327 return v1 -= v2;
328 }
329
330 template <class Tmplt>
331 bool inline operator!=(const MVector<Tmplt>& c1, const MVector<Tmplt>& c2) {
332 return !(c1 == c2);
333 }
334
335 template <class Tmplt>
337 if (m._vector == nullptr)
339 "MVector::get_vector", "Attempt to access uninitialised matrix"));
340 return (gsl_vector*)m._vector;
341 }
342
343 template <class Tmplt>
345 if (m._vector == nullptr)
347 "MVector::get_vector", "Attempt to access uninitialised vector"));
348 return (gsl_vector_complex*)m._vector;
349 }
350
351} // namespace interpolation
352
353#endif
gsl_complex gsl_complex_add_real(gsl_complex a, double x)
Add real scalar .
Definition GSLComplex.h:116
gsl_complex gsl_complex_mul_real(gsl_complex a, double x)
Multiply by real scalar .
Definition GSLComplex.h:134
gsl_complex gsl_complex_div(gsl_complex a, gsl_complex b)
Quotient .
Definition GSLComplex.h:106
gsl_complex gsl_complex_div_real(gsl_complex a, double x)
Divide by real scalar .
Definition GSLComplex.h:143
gsl_complex gsl_complex_mul(gsl_complex a, gsl_complex b)
Product .
Definition GSLComplex.h:97
gsl_complex gsl_complex_sub_real(gsl_complex a, double x)
Subtract real scalar .
Definition GSLComplex.h:125
gsl_complex gsl_complex_add(gsl_complex a, gsl_complex b)
Sum .
Definition GSLComplex.h:79
gsl_complex gsl_complex_sub(gsl_complex a, gsl_complex b)
Difference .
Definition GSLComplex.h:88
void gsl_vector_complex_add(gsl_vector_complex *a, const gsl_vector_complex *b)
Add another complex vector in-place.
Definition GSLMatrix.h:437
void gsl_vector_free(gsl_vector *v)
Free a vector allocated by gsl_vector_alloc.
Definition GSLMatrix.h:160
void gsl_vector_scale(gsl_vector *v, double x)
Scale a vector in-place.
Definition GSLMatrix.h:401
void gsl_vector_complex_scale(gsl_vector_complex *v, gsl_complex x)
Scale a complex vector in-place.
Definition GSLMatrix.h:412
void gsl_vector_complex_free(gsl_vector_complex *v)
Free a vector allocated by gsl_vector_complex_alloc.
Definition GSLMatrix.h:186
void gsl_vector_add(gsl_vector *a, const gsl_vector *b)
Add another vector in-place.
Definition GSLMatrix.h:423
MMatrix< Tmplt > T() const
Definition MVector.cpp:130
MVector(const Tmplt *ta_beg, const Tmplt *ta_end)
Definition MVector.h:160
MVector< Tmplt > & operator=(const MVector< Tmplt > &mv)
friend MVector< m_complex > operator*(MMatrix< m_complex > m, MVector< m_complex > v)
Definition MMatrix.h:284
MVector< Tmplt > sub(size_t n1, size_t n2) const
Definition MVector.cpp:195
void build_vector(size_t size)
friend MVector< m_complex > & operator*=(MVector< m_complex > &v, m_complex c)
Definition MVector.h:274
MVector(std::vector< Tmplt > tv)
Definition MVector.h:163
friend MVector< m_complex > & operator+=(MVector< m_complex > &v1, MVector< m_complex > v2)
Definition MVector.h:301
Tmplt & operator()(size_t i)
static gsl_vector * get_vector(const MVector< double > &m)
Definition MVector.h:336
const Tmplt & operator()(size_t i) const
MVector(MVector< Tmplt2 >)
size_t num_row() const
Definition MVector.cpp:103
MMatrix< Tmplt > operator/(MMatrix< Tmplt >, Tmplt)
Definition MMatrix.h:339
m_complex m_complex_build(double r, double i)
Definition MVector.h:60
Mesh::Iterator operator+(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Mesh::Iterator operator-(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
MMatrix< m_complex > complex(MMatrix< double > real)
Definition MMatrix.cpp:399
bool operator!=(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
gsl_complex m_complex
Definition MVector.h:58
Mesh::Iterator & operator+=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
MVector< m_complex > operator*(MMatrix< m_complex > m, MVector< m_complex > v)
Definition MMatrix.h:284
template std::istream & operator>>(std::istream &in, MMatrix< double > &mat)
MMatrix< double > re(MMatrix< m_complex > mc)
Definition MMatrix.cpp:383
m_complex conj(const m_complex &c)
Definition MVector.h:142
std::ostream & operator<<(std::ostream &out, const Mesh::Iterator &it)
Definition Mesh.cpp:33
MMatrix< Tmplt > inline & operator/=(MMatrix< Tmplt > &m, Tmplt t)
Definition MMatrix.h:335
MMatrix< double > & operator*=(MMatrix< double > &m1, MMatrix< double > m2)
Definition MMatrix.cpp:337
MMatrix< double > im(MMatrix< m_complex > mc)
Definition MMatrix.cpp:391
Mesh::Iterator & operator-=(Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
Complex number stored as .
Definition GSLComplex.h:27
double dat[2]
Definition GSLComplex.h:28
Dense complex vector with stride.
Definition GSLMatrix.h:76
Dense real vector with stride.
Definition GSLMatrix.h:61