gslpp_vector_complex.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 HEPfit Collaboration
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  */
7 
8 #include <stdlib.h>
9 #include <iostream>
10 #ifndef __GSL_BLAS_H__
11 #include <gsl/gsl_blas.h>
12 #endif
13 #ifndef GSLPP_VECTOR_COMPLEX_H
14 #include "gslpp_vector_complex.h"
15 #endif
16 #ifndef GSLPP_VECTOR_DOUBLE_H
17 #include "gslpp_vector_double.h"
18 #endif
19 
20 namespace gslpp {
21 
23  vector<complex>::vector(const size_t& size, const complex& z)
24  {
25  _vector = gsl_vector_complex_alloc(size);
26  gsl_vector_complex_set_all(_vector, z.as_gsl_type());
27  }
28 
29  vector<complex>::vector(const size_t& size, const double& a)
30  {
31  complex z(a);
32  _vector = gsl_vector_complex_alloc(size);
33  gsl_vector_complex_set_all(_vector, z.as_gsl_type());
34  }
35 
38  {
39  _vector = gsl_vector_complex_alloc(v.size());
40  gsl_vector_complex_memcpy(_vector, v.as_gsl_type_ptr());
41  }
42 
44  {
45  size_t i, n;
46  n = v.size();
47  _vector = gsl_vector_complex_alloc(n);
48  for (i = 0; i < n; i++)
49  gsl_vector_complex_set(_vector, i, v(i) + 0. * complex::i());
50  }
51 
52  vector<complex>::vector(const gsl_vector_complex& v)
53  {
54  _vector = gsl_vector_complex_alloc(v.size);
55  gsl_vector_complex_memcpy(_vector, &v);
56  }
57 
58  vector<complex>::vector(const gsl_vector_complex* v)
59  {
60  _vector = gsl_vector_complex_alloc(v->size);
61  gsl_vector_complex_memcpy(_vector, v);
62  }
63 
66  {
67  gsl_vector_complex_free(_vector);
68  }
69 
71  const complex vector<complex>::operator()(const size_t& i) const
72  {
73  const gsl_complex *x = gsl_vector_complex_const_ptr(_vector, i);
74  return complex(x);
75  }
76 
78  // complex& vector<complex>::operator()(const size_t& i)
79  // {
80  // gsl_complex *x = gsl_vector_complex_ptr(_vector, i);
81  // return complex(x);
82  // }
83 
86  {
87  gsl_vector_complex_memcpy(_vector, v.as_gsl_type_ptr());
88  return *this;
89  }
90 
92  {
93  complex z(a);
94  gsl_vector_complex_set_all(_vector, z.as_gsl_type());
95  return *this;
96  }
97 
99  void vector<complex>::assign(const size_t& i, const complex& z)
100  {
101  gsl_complex *x = gsl_vector_complex_ptr(_vector, i);
102  *x = z.as_gsl_type();
103  }
104 
105  void vector<complex>::assign(const size_t& i, const double& a)
106  {
107  gsl_complex *x = gsl_vector_complex_ptr(_vector, i);
108  *x = complex(a).as_gsl_type();
109  }
110 
112  size_t vector<complex>::size() const
113  {
114  return _vector->size;
115  }
116 
118  double vector<complex>::mod() const
119  {
120  return gsl_blas_dznrm2(_vector);
121  }
122 
125  {
126  vector<complex> v1(*this);
127  for (unsigned int i = 0; i < v1.size(); i++)
128  v1.assign(i, v1(i).conjugate());
129 
130  return v1;
131  }
132 
135  {
136  gsl_vector_view r = gsl_vector_complex_real(_vector);
137  vector<double> re((&r.vector));
138  return re;
139  }
140 
143  {
144  gsl_vector_view r = gsl_vector_complex_imag(_vector);
145  vector<double> re((&r.vector));
146  return re;
147  }
148 
150  gsl_vector_complex* vector<complex>::as_gsl_type_ptr() const
151  {
152  return _vector;
153  }
154 
155  gsl_vector_complex& vector<complex>::as_gsl_type()
156  {
157  return const_cast<gsl_vector_complex&> (*_vector);
158  }
159 
160  const gsl_vector_complex& vector<complex>::as_gsl_type() const
161  {
162  return const_cast<gsl_vector_complex&> (*_vector);
163  }
164 
167  {
168  vector<complex> v1(_vector);
169  gsl_blas_zdscal(-1., v1.as_gsl_type_ptr());
170  return v1;
171  }
172 
175  {
176  vector<complex> v1(_vector);
177  gsl_complex z1;
178  GSL_SET_COMPLEX(&z1, 1., 0.);
179  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), v1.as_gsl_type_ptr())) {
180  std::cout << "\n Error in vector<complex> +" << std::endl;
181  exit(EXIT_FAILURE);
182  }
183  return v1;
184  }
185 
188  {
189  vector<complex> v1(_vector);
190  gsl_complex z1;
191  GSL_SET_COMPLEX(&z1, -1., 0.);
192  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), v1.as_gsl_type_ptr())) {
193  std::cout << "\n Error in vector<complex> -" << std::endl;
194  exit(EXIT_FAILURE);
195  }
196  return v1;
197  }
198 
201  {
202  complex z1(0., 0., false);
203  vector<complex> v1(_vector);
204  if (gsl_blas_zdotu(v1.as_gsl_type_ptr(), v.as_gsl_type_ptr(), z1.as_gsl_type_ptr())) {
205  std::cout << "\n Error in vector<complex> *" << std::endl;
206  exit(EXIT_FAILURE);
207  }
208  return z1;
209  }
211  // vector<complex> vector<complex>::operator^(const vector<complex>& v)
212  // {
213  // std::cout << "\n To be implemented" << std::endl;
214  // exit(EXIT_FAILURE);
215  // }
216 
219  {
220  /* gsl_complex z1;
221  GSL_SET_COMPLEX(&z1,1.,0.);
222  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), _vector))
223  {
224  std::cout << "\n Error in vector<complex> +=" << std::endl;
225  exit(EXIT_FAILURE);
226  }
227  return *this;*/
228  *this = *this +v;
229  return *this;
230  }
231 
234  {
235  *this = *this -v;
236  return *this;
237  }
238 
241  {
242  vector<complex> v1(_vector);
243  gsl_complex z1;
244  gsl_vector_complex *v2;
245  GSL_SET_COMPLEX(&z1, 1., 0.);
246  v2 = gsl_vector_complex_alloc(v1.size());
247  gsl_vector_complex_set_all(v2, z1);
248  if (gsl_blas_zaxpy(z, v2, v1.as_gsl_type_ptr())) {
249  std::cout << "\n Error in vector<complex> + (double)" << std::endl;
250  exit(EXIT_FAILURE);
251  }
252  gsl_vector_complex_free(v2);
253  return v1;
254  }
255 
258  {
259  vector<complex> v1(_vector);
260  gsl_complex z1;
261  gsl_vector_complex *v2;
262  GSL_SET_COMPLEX(&z1, -1., 0.);
263  v2 = gsl_vector_complex_alloc(v1.size());
264  gsl_vector_complex_set_all(v2, z1);
265  if (gsl_blas_zaxpy(z, v2, v1.as_gsl_type_ptr())) {
266  std::cout << "\n Error in vector<complex> - (double)" << std::endl;
267  exit(EXIT_FAILURE);
268  }
269  gsl_vector_complex_free(v2);
270  return v1;
271  }
272 
275  {
276  vector<complex> v1(_vector);
277  gsl_blas_zscal(z.as_gsl_type(), v1.as_gsl_type_ptr());
278  return v1;
279  }
280 
283  {
284  vector<complex> v1(_vector);
285  gsl_blas_zscal(z.inverse().as_gsl_type(), v1.as_gsl_type_ptr());
286  return v1;
287  }
288 
291  {
292  *this = *this +z;
293  return *this;
294  }
295 
298  {
299  *this = *this -z;
300  return *this;
301  }
302 
305  {
306  *this = *this * z;
307  return *this;
308  }
309 
312  {
313  *this = *this / z;
314  return *this;
315  }
316 
319  {
320  complex z(a);
321  return *this +z;
322  }
323 
326  {
327  complex z(a);
328  return *this -z;
329  }
330 
333  {
334  complex z(a);
335  return *this * z;
336  }
337 
340  {
341  complex z(a);
342  return *this / z;
343  }
344 
347  {
348  *this = *this +a;
349  return *this;
350  }
351 
354  {
355  *this = *this -a;
356  return *this;
357  }
358 
361  {
362  *this = *this * a;
363  return *this;
364  }
365 
368  {
369  *this = *this / a;
370  return *this;
371  }
372 
374  std::ostream& operator<<(std::ostream& output, const vector<complex>& v)
375  {
376  size_t i;
377  output << "(";
378  for (i = 0; i < v.size() - 1; i++)
379  output << v(i) << ",";
380  output << v(i) << ")";
381  return output;
382  }
383 
385  {
386  return v + z;
387  }
388 
390  {
391  return -v + z;
392  }
393 
395  {
396  return v*z;
397  }
398 
400  {
401  return v + a;
402  }
403 
405  {
406  return -v + a;
407  }
408 
410  {
411  return v*a;
412  }
413 }
complex operator*(const double &x1, const complex &z2)
vector< complex > operator*(const double &a, vector< complex > v)
gsl_complex & as_gsl_type()
complex inverse() const
static const complex & i()
A class for constructing and defining operations on complex vectors.
void assign(const size_t &i, const complex &z)
complex operator/(const double &x1, const complex &z2)
gsl_vector_complex * as_gsl_type_ptr() const
gsl_complex * as_gsl_type_ptr() const
A class for constructing and defining operations on real vectors.
vector< complex > operator+(const double &a, vector< complex > v)
A base class for defining operations on vectors, both real and complex.
complex operator+(const double &x1, const complex &z2)
A class for defining operations on and functions of complex numbers.
Definition: gslpp_complex.h:35
complex operator-(const double &x1, const complex &z2)
Complex number, vector and matrix manipulation using GSL.
vector< complex > operator-(const double &a, vector< complex > v)