OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
MVector.cpp
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 
30 
31 namespace interpolation {
33 
34 std::ostream& operator<<(std::ostream& out, m_complex c)
35 {
36  out << re(c) << " r " << im(c) << " i";
37  return out;
38 }
39 
40 std::istream& operator>>(std::istream& in, m_complex& c)
41 {
42  std::string dummy;
43  in >> re(c) >> dummy >> im(c) >> dummy;
44  return in;
45 }
46 
47 
49 
50 template MVector<double> ::MVector(size_t i);
51 template MVector<m_complex>::MVector(size_t i);
52 
53 template MVector<double> ::MVector(const MVector<double>&);
54 template MVector<m_complex>::MVector(const MVector<m_complex>&);
55 template MVector<double> ::MVector(size_t i, double value);
56 template MVector<m_complex>::MVector(size_t i, m_complex value);
57 
58 template void MVector<double>::build_vector( const double* data_begin, const double* data_end );
59 template void MVector<m_complex>::build_vector( const m_complex* data_begin, const m_complex* data_end );
60 
61 template std::ostream& operator<<(std::ostream& out, MVector<double> v);
62 template std::ostream& operator<<(std::ostream& out, MVector<m_complex> v);
63 template std::istream& operator>>(std::istream& out, MVector<double>& v);
64 template std::istream& operator>>(std::istream& out, MVector<m_complex>& v);
65 template MVector<double> MVector<double> ::sub(size_t n1, size_t n2) const;
66 template MVector<m_complex> MVector<m_complex>::sub(size_t n1, size_t n2) const;
67 
68 
69 template <typename Tmplt>
70 MVector<Tmplt>::MVector( size_t i ) : _vector(NULL)
71 {
72  build_vector(i);
73 }
74 
75 
76 template <typename Tmplt>
77 MVector<Tmplt>::MVector( const MVector<Tmplt>& mv) : _vector(NULL)
78 { *this = mv; }
79 
80 
81 template <typename Tmplt>
82 MVector<Tmplt>::MVector( size_t size, Tmplt value ) : _vector(NULL)
83 {
84  build_vector(size);
85  for(size_t i=0; i<size; i++) operator()(i+1) = value;
86 }
87 
88 
89 template <>
90 void MVector<double>::build_vector ( size_t size )
91 {
92  if(_vector != NULL) gsl_vector_free((gsl_vector*)_vector);
93  _vector = gsl_vector_alloc(size);
94 }
95 
96 template <>
98 {
99  if(_vector != NULL) gsl_vector_complex_free((gsl_vector_complex*)_vector);
100  _vector = gsl_vector_complex_alloc(size);
101 }
102 
103 template <class Tmplt>
104 void MVector<Tmplt>::build_vector ( const Tmplt* data_begin, const Tmplt* data_end )
105 {
106  build_vector(data_end - data_begin);
107  for(size_t i=0; i<num_row(); i++) operator()(i+1) = data_begin[i];
108 }
109 
110 
111 template <class Tmplt>
113 { if(_vector != NULL) return ((gsl_vector*)_vector)->size; else return 0;}
114 template size_t MVector<double> ::num_row() const;
115 template size_t MVector<m_complex>::num_row() const;
116 
117 template <>
118 const double& MVector<double>::operator()(const size_t i) const
119 {return *gsl_vector_ptr( (gsl_vector*)_vector, i-1);}
120 template <>
121 const m_complex& MVector<m_complex>::operator()(const size_t i) const
122 {return *gsl_vector_complex_ptr((gsl_vector_complex*)_vector, i-1);}
123 template <>
124 double& MVector<double>::operator()(const size_t i)
125 {return *gsl_vector_ptr( (gsl_vector*)_vector, i-1);}
126 template <>
128 {return *gsl_vector_complex_ptr((gsl_vector_complex*)_vector, i-1);}
129 
130 
131 template <class Tmplt>
133 {
134  MMatrix<Tmplt> mat = MMatrix<Tmplt>(1,num_row());
135  for(size_t i=1; i<=num_row(); i++)
136  mat(1, i) = this->operator()(i);
137  return mat;
138 }
139 template MMatrix<double> MVector<double> ::T() const;
141 
142 template <class Tmplt> bool operator==(const MVector<Tmplt>& c1, const MVector<Tmplt>& c2)
143 {
144  if(c1.num_row() != c2.num_row())
145  return false;
146  for(size_t i=0; i<c1.num_row(); i++)
147  if(c1(i+1) != c2(i+1)) return false;
148  return true;
149 }
150 template bool operator==(const MVector<double>& c1, const MVector<double>& c2);
151 template bool operator==(const MVector<m_complex>& c1, const MVector<m_complex>& c2);
152 
153 template <>
155 {
156  if (&mv == this) return *this;
157  delete_vector();
158  if(!mv._vector) { _vector = NULL; return *this; }
159  _vector = gsl_vector_alloc( mv.num_row() );
160  gsl_vector_memcpy((gsl_vector*)_vector, (const gsl_vector*)mv._vector);
161  return *this;
162 }
163 
164 template <>
166 {
167  if (&mv == this) return *this;
168  delete_vector();
169  if(!mv._vector) { _vector = NULL; return *this; }
170  _vector = gsl_vector_complex_alloc( mv.num_row() );
171  gsl_vector_complex_memcpy((gsl_vector_complex*)_vector, (const gsl_vector_complex*)mv._vector);
172  return *this;
173 }
174 
175 template <class Tmplt> std::ostream& operator<<(std::ostream& out, MVector<Tmplt> v)
176 {
177  out << v.num_row() << "\n";
178  for(size_t i=0; i<v.num_row(); i++) out << " " << v(i+1) << "\n";
179  return out;
180 }
181 
182 template <class Tmplt> std::istream& operator>>(std::istream& in, MVector<Tmplt>& v)
183 {
184  size_t n;
185  in >> n;
186  v = MVector<Tmplt>(n);
187  for(size_t i=1; i<=v.num_row(); i++) in >> v(i);
188  return in;
189 }
190 
191 const gsl_vector* MVector_to_gsl(const MVector<double>& vd)
192 {return vd.get_vector(vd);}
193 const gsl_vector_complex* MVector_to_gsl(const MVector<gsl_complex>& vc)
194 {return vc.get_vector(vc);}
195 
196 template <class Tmplt>
197 MVector<Tmplt> MVector<Tmplt>::sub(size_t n1, size_t n2) const
198 {
199  MVector<Tmplt> temp(n2-n1+1);
200  for(size_t i=n1; i<=n2; i++) temp(i-n1+1) = operator()(i);
201  return temp;
202 }
203 
205 {
206  MVector<m_complex> c(real.num_row());
207  for(size_t i=1; i<=real.num_row(); i++) c(i) = m_complex_build(real(i));
208  return c;
209 }
210 
212 {
213  MVector<m_complex> c(real.num_row());
214  for(size_t i=1; i<=real.num_row(); i++) c(i) = m_complex_build(real(i), im(i));
215  return c;
216 }
217 
219 {
220  MVector<double> d(c.num_row());
221  for(size_t i=1; i<=c.num_row(); i++) d(i) = re( c(i) );
222  return d;
223 }
224 
226 {
227  MVector<double> d(c.num_row());
228  for(size_t i=1; i<=c.num_row(); i++) d(i) = im( c(i) );
229  return d;
230 }
231 
232 }
gsl_complex m_complex
Definition: MVector.h:60
MMatrix< m_complex > complex(MMatrix< double > real)
Definition: MMatrix.cpp:407
Tmplt & operator()(size_t i)
MVector< Tmplt > & operator=(const MVector< Tmplt > &mv)
static gsl_vector * get_vector(const MVector< double > &m)
Definition: MVector.h:255
m_complex m_complex_build(double r, double i)
Definition: MVector.h:62
template std::istream & operator>>(std::istream &in, MMatrix< double > &mat)
MVector< Tmplt > sub(size_t n1, size_t n2) const
Definition: MVector.cpp:197
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
void build_vector(size_t size)
const gsl_vector * MVector_to_gsl(const MVector< double > &vd)
Definition: MVector.cpp:191
MMatrix< double > im(MMatrix< m_complex > mc)
Definition: MMatrix.cpp:398
MMatrix< double > re(MMatrix< m_complex > mc)
Definition: MMatrix.cpp:389
MMatrix< Tmplt > T() const
Definition: MVector.cpp:132
bool operator==(const Mesh::Iterator &lhs, const Mesh::Iterator &rhs)
std::ostream & operator<<(std::ostream &out, const Mesh::Iterator &it)
Definition: Mesh.cpp:33
size_t num_row() const
Definition: MVector.cpp:112