a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
gslpp_vector_complex.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 HEPfit Collaboration
3  *
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 
100  {
101  gsl_vector_complex_set_zero(_vector);
102  }
103 
104  void vector<complex>::assign(const size_t& i, const complex& z)
105  {
106  gsl_complex *x = gsl_vector_complex_ptr(_vector, i);
107  *x = z.as_gsl_type();
108  }
109 
110  void vector<complex>::assign(const size_t& i, const double& a)
111  {
112  gsl_complex *x = gsl_vector_complex_ptr(_vector, i);
113  *x = complex(a).as_gsl_type();
114  }
115 
117  size_t vector<complex>::size() const
118  {
119  return _vector->size;
120  }
121 
123  double vector<complex>::mod() const
124  {
125  return gsl_blas_dznrm2(_vector);
126  }
127 
130  {
131  vector<complex> v1(*this);
132  for (unsigned int i = 0; i < v1.size(); i++)
133  v1.assign(i, v1(i).conjugate());
134 
135  return v1;
136  }
137 
140  {
141  gsl_vector_view r = gsl_vector_complex_real(_vector);
142  vector<double> re((&r.vector));
143  return re;
144  }
145 
148  {
149  gsl_vector_view r = gsl_vector_complex_imag(_vector);
150  vector<double> re((&r.vector));
151  return re;
152  }
153 
155  gsl_vector_complex* vector<complex>::as_gsl_type_ptr() const
156  {
157  return _vector;
158  }
159 
160  gsl_vector_complex& vector<complex>::as_gsl_type()
161  {
162  return const_cast<gsl_vector_complex&> (*_vector);
163  }
164 
165  const gsl_vector_complex& vector<complex>::as_gsl_type() const
166  {
167  return const_cast<gsl_vector_complex&> (*_vector);
168  }
169 
172  {
173  vector<complex> v1(_vector);
174  gsl_blas_zdscal(-1., v1.as_gsl_type_ptr());
175  return v1;
176  }
177 
180  {
181  vector<complex> v1(_vector);
182  gsl_complex z1;
183  GSL_SET_COMPLEX(&z1, 1., 0.);
184  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), v1.as_gsl_type_ptr())) {
185  std::cout << "\n Error in vector<complex> +" << std::endl;
186  exit(EXIT_FAILURE);
187  }
188  return v1;
189  }
190 
193  {
194  vector<complex> v1(_vector);
195  gsl_complex z1;
196  GSL_SET_COMPLEX(&z1, -1., 0.);
197  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), v1.as_gsl_type_ptr())) {
198  std::cout << "\n Error in vector<complex> -" << std::endl;
199  exit(EXIT_FAILURE);
200  }
201  return v1;
202  }
203 
206  {
207  complex z1(0., 0., false);
208  vector<complex> v1(_vector);
209  if (gsl_blas_zdotu(v1.as_gsl_type_ptr(), v.as_gsl_type_ptr(), z1.as_gsl_type_ptr())) {
210  std::cout << "\n Error in vector<complex> *" << std::endl;
211  exit(EXIT_FAILURE);
212  }
213  return z1;
214  }
216  // vector<complex> vector<complex>::operator^(const vector<complex>& v)
217  // {
218  // std::cout << "\n To be implemented" << std::endl;
219  // exit(EXIT_FAILURE);
220  // }
221 
224  {
225  /* gsl_complex z1;
226  GSL_SET_COMPLEX(&z1,1.,0.);
227  if (gsl_blas_zaxpy(z1, v.as_gsl_type_ptr(), _vector))
228  {
229  std::cout << "\n Error in vector<complex> +=" << std::endl;
230  exit(EXIT_FAILURE);
231  }
232  return *this;*/
233  *this = *this +v;
234  return *this;
235  }
236 
239  {
240  *this = *this -v;
241  return *this;
242  }
243 
246  {
247  vector<complex> v1(_vector);
248  gsl_complex z1;
249  gsl_vector_complex *v2;
250  GSL_SET_COMPLEX(&z1, 1., 0.);
251  v2 = gsl_vector_complex_alloc(v1.size());
252  gsl_vector_complex_set_all(v2, z1);
253  if (gsl_blas_zaxpy(z, v2, v1.as_gsl_type_ptr())) {
254  std::cout << "\n Error in vector<complex> + (double)" << std::endl;
255  exit(EXIT_FAILURE);
256  }
257  gsl_vector_complex_free(v2);
258  return v1;
259  }
260 
263  {
264  vector<complex> v1(_vector);
265  gsl_complex z1;
266  gsl_vector_complex *v2;
267  GSL_SET_COMPLEX(&z1, -1., 0.);
268  v2 = gsl_vector_complex_alloc(v1.size());
269  gsl_vector_complex_set_all(v2, z1);
270  if (gsl_blas_zaxpy(z, v2, v1.as_gsl_type_ptr())) {
271  std::cout << "\n Error in vector<complex> - (double)" << std::endl;
272  exit(EXIT_FAILURE);
273  }
274  gsl_vector_complex_free(v2);
275  return v1;
276  }
277 
280  {
281  vector<complex> v1(_vector);
282  gsl_blas_zscal(z.as_gsl_type(), v1.as_gsl_type_ptr());
283  return v1;
284  }
285 
288  {
289  vector<complex> v1(_vector);
290  gsl_blas_zscal(z.inverse().as_gsl_type(), v1.as_gsl_type_ptr());
291  return v1;
292  }
293 
296  {
297  *this = *this +z;
298  return *this;
299  }
300 
303  {
304  *this = *this -z;
305  return *this;
306  }
307 
310  {
311  *this = *this * z;
312  return *this;
313  }
314 
317  {
318  *this = *this / z;
319  return *this;
320  }
321 
324  {
325  complex z(a);
326  return *this +z;
327  }
328 
331  {
332  complex z(a);
333  return *this -z;
334  }
335 
338  {
339  complex z(a);
340  return *this * z;
341  }
342 
345  {
346  complex z(a);
347  return *this / z;
348  }
349 
352  {
353  *this = *this +a;
354  return *this;
355  }
356 
359  {
360  *this = *this -a;
361  return *this;
362  }
363 
366  {
367  *this = *this * a;
368  return *this;
369  }
370 
373  {
374  *this = *this / a;
375  return *this;
376  }
379  {
380  if (a.size() != size())
381  {
382  std::cout << "\n Error in vector<complex>::operator== (vector): cannot compare vectors of different size" << std::endl;
383  exit(EXIT_FAILURE);
384  }
385  for (size_t i = 0; i < size(); i++)
386  if (a(i) != (*this)(i)) return (false);
387  return (true);
388  }
389 
391  std::ostream& operator<<(std::ostream& output, const vector<complex>& v)
392  {
393  size_t i;
394  output << "(";
395  for (i = 0; i < v.size() - 1; i++)
396  output << v(i) << ", ";
397  output << v(i) << ")";
398  return output;
399  }
400 
402  {
403  return v + z;
404  }
405 
407  {
408  return -v + z;
409  }
410 
412  {
413  return v*z;
414  }
415 
417  {
418  return v + a;
419  }
420 
422  {
423  return -v + a;
424  }
425 
427  {
428  return v*a;
429  }
430 }
gslpp::vector< double >::size
size_t size() const
Definition: gslpp_vector_double.cpp:88
gslpp::vector< complex >
A class for constructing and defining operations on complex vectors.
Definition: gslpp_vector_complex.h:33
gslpp::operator*
vector< complex > operator*(const double &a, vector< complex > v)
Definition: gslpp_vector_complex.cpp:426
gslpp::complex
A class for defining operations on and functions of complex numbers.
Definition: gslpp_complex.h:35
gslpp::complex::as_gsl_type_ptr
gsl_complex * as_gsl_type_ptr() const
Definition: gslpp_complex.cpp:118
gslpp::vector< complex >::assign
void assign(const size_t &i, const complex &z)
Definition: gslpp_vector_complex.cpp:104
gslpp::operator+
vector< complex > operator+(const double &a, vector< complex > v)
Definition: gslpp_vector_complex.cpp:416
gslpp::operator+
complex operator+(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:302
gslpp::vector< complex >::as_gsl_type_ptr
gsl_vector_complex * as_gsl_type_ptr() const
Definition: gslpp_vector_complex.cpp:155
gslpp::operator-
vector< complex > operator-(const double &a, vector< complex > v)
Definition: gslpp_vector_complex.cpp:421
gslpp::complex::i
static const complex & i()
Definition: gslpp_complex.cpp:154
gslpp_vector_complex.h
gslpp::vector< double >
A class for constructing and defining operations on real vectors.
Definition: gslpp_vector_double.h:33
gslpp::operator/
complex operator/(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:320
gslpp::operator<<
std::ostream & operator<<(std::ostream &output, const complex &z)
Definition: gslpp_complex.cpp:143
gslpp::operator*
complex operator*(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:314
gslpp::complex::as_gsl_type
gsl_complex & as_gsl_type()
Definition: gslpp_complex.cpp:123
gslpp::complex::inverse
complex inverse() const
Definition: gslpp_complex.cpp:294
gslpp
Complex number, vector and matrix manipulation using GSL.
Definition: gslpp_complex.cpp:16
gslpp::vector< complex >::size
size_t size() const
Definition: gslpp_vector_complex.cpp:117
gslpp_vector_double.h
gslpp::vector
A base class for defining operations on vectors, both real and complex.
Definition: gslpp_vector_base.h:21
gslpp::operator-
complex operator-(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:308