a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
gslpp_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 #ifndef GSLPP_COMPLEX_H
9 #include "gslpp_complex.h"
10 #include <gsl/gsl_sf.h>
11 #endif
12 #include <math.h>
13 
14 #define GSLEPS 1.e-15
15 
16 namespace gslpp
17 {
19  {
20  assign(0., 0., false);
21  }
22 
23  complex::complex(const double& real, const double& imag, bool polar)
24  {
25  assign(real, imag, polar);
26  }
27 
29  {
30  assign(z.real(), z.imag(), false);
31  }
32  complex::complex(const double& a)
33  {
34  assign(a, 0., false);
35  }
36 
38  {
39  }
40 
41  bool complex::is_real() const
42  {
43  return ::fabs(imag()/real()) < GSLEPS;
44  }
45 
46 
47  bool complex::is_imag() const
48  {
49  return ::fabs(real()/imag()) < GSLEPS;
50  }
51 
52 
53  const double& complex::real() const
54  {
55  return GSL_REAL(_complex);
56  }
57 
58 
59  const double& complex::imag() const
60  {
61  return GSL_IMAG(_complex);
62  }
63 
64 
65  double& complex::real()
66  {
67  return GSL_REAL(_complex);
68  }
69 
70 
71  double& complex::imag()
72  {
73  return GSL_IMAG(_complex);
74  }
75 
76  double complex::arg() const
77  {
78  return gsl_complex_arg(_complex);
79  }
80 
81  double complex::abs() const
82  {
83  return gsl_complex_abs(_complex);
84  }
85 
86  double complex::abs2() const
87  {
88  return gsl_complex_abs2(_complex);
89  }
90 
91  double complex::log_of_abs() const
92  {
93  return gsl_complex_logabs(_complex);
94  }
95 
97  {
98  GSL_SET_COMPLEX(&_complex, z.real(), z.imag());
99  return *this;
100  }
101 
102  complex& complex::operator=(const double& x)
103  {
104  GSL_SET_COMPLEX(&_complex, x, 0);
105  return *this;
106  }
107 
108  bool complex::operator==(const complex& z1) const
109  {
110  return GSL_COMPLEX_EQ(_complex, z1._complex);
111  }
112 
113  bool complex::operator!=(const complex& z1) const
114  {
115  return !(GSL_COMPLEX_EQ(_complex, z1._complex));
116  }
117 
118  gsl_complex* complex::as_gsl_type_ptr() const
119  {
120  return const_cast<gsl_complex*>(&_complex);
121  }
122 
123  gsl_complex& complex::as_gsl_type()
124  {
125  return _complex;
126  }
127 
128  const gsl_complex& complex::as_gsl_type() const
129  {
130  return _complex;
131  }
132 
133  complex::operator gsl_complex& ()
134  {
135  return _complex;
136  }
137 
138  complex::operator const gsl_complex& () const
139  {
140  return _complex;
141  }
142 
143  std::ostream& operator<<(std::ostream& output, const complex& z)
144  {
145  double absim = ::fabs(z.imag());
146  output << z.real() << (z.imag() < 0.? "-" : "+");
147  if (absim != 1.)
148  output << fabs(z.imag()) << "*";
149  output << "i";
150  return output;
151  }
152 
153  // const complex* complex::_i = 0;
155  {
156  static complex _i(0.,1.);
157  return _i;
158  }
159 
160  complex::complex(const gsl_complex* z)
161  {
162 // _complex = (gsl_complex*)malloc(sizeof(gsl_complex));
163  GSL_SET_COMPLEX(&_complex, GSL_REAL(*z), GSL_IMAG(*z));
164  }
165 
166  complex::complex(const gsl_complex& z)
167  {
168 // _complex = (gsl_complex*)malloc(sizeof(gsl_complex));
169  GSL_SET_COMPLEX(&_complex, GSL_REAL(z), GSL_IMAG(z));
170  }
171 
172  void
173  complex::assign(const double& real=0., const double& imag=0.,
174  bool polar=false)
175  {
176  if (polar)
177  _complex = gsl_complex_polar(real,imag);
178  else
179  {
180  GSL_SET_COMPLEX(&_complex, real, imag);
181  // *_complex = gsl_complex_rect(real,imag);
182  }
183  }
184 
186  {
187  gsl_complex t = gsl_complex_negative(_complex);
188  return complex(&t);
189  }
190 
192  {
193  gsl_complex rl = gsl_complex_add(_complex, z1._complex);
194  return complex(rl);
195  }
196 
198  {
199  gsl_complex rl = gsl_complex_sub(_complex, z1._complex);
200  return complex(rl);
201  }
202 
204  {
205  gsl_complex rl = gsl_complex_mul(_complex, z1._complex);
206  return complex(rl);
207  }
208 
210  {
211  gsl_complex rl = gsl_complex_div(_complex, z1._complex);
212  return complex(rl);
213  }
214 
216  {
217  _complex = gsl_complex_add(_complex, z1._complex);
218  return *this;
219  }
220 
222  {
223  _complex = gsl_complex_sub(_complex, z1._complex);
224  return *this;
225  }
226 
228  {
229  _complex = gsl_complex_mul(_complex, z1._complex);
230  return *this;
231  }
232 
234  {
235  _complex = gsl_complex_div(_complex, z1._complex);
236  return *this;
237  }
238 
239  complex complex::operator+(const double& a) const
240  {
241  gsl_complex rl = gsl_complex_add_real(_complex,a);
242  return complex(rl);
243  }
244 
245  complex complex::operator-(const double& a) const
246  {
247  gsl_complex rl = gsl_complex_sub_real(_complex,a);
248  return complex(rl);
249  }
250 
251  complex complex::operator*(const double& a) const
252  {
253  gsl_complex rl = gsl_complex_mul_real(_complex,a);
254  return complex(rl);
255  }
256 
257  complex complex::operator/(const double& a) const
258  {
259  gsl_complex rl = gsl_complex_div_real(_complex,a);
260  return complex(rl);
261  }
262 
263  complex& complex::operator+=(const double& a)
264  {
265  _complex = gsl_complex_add_real(_complex,a);
266  return *this;
267  }
268 
269  complex& complex::operator-=(const double& a)
270  {
271  _complex = gsl_complex_sub_real(_complex,a);
272  return *this;
273  }
274 
275  complex&
276  complex::operator*=(const double& a)
277  {
278  _complex = gsl_complex_mul_real(_complex,a);
279  return *this;
280  }
281 
282  complex& complex::operator/=(const double& a)
283  {
284  _complex = gsl_complex_div_real(_complex,a);
285  return *this;
286  }
287 
289  {
290  gsl_complex t = gsl_complex_conjugate(_complex);
291  return complex(t);
292  }
293 
295  {
296  gsl_complex t = gsl_complex_inverse(_complex);
297  return complex(t);
298  }
299 
300  //==================================================================
301 
302  complex operator+(const double& x1, const complex& z2)
303  {
304  complex z1(x1, 0.);
305  return z1 + z2;
306  }
307 
308  complex operator-(const double& x1, const complex& z2)
309  {
310  complex z1(x1, 0.);
311  return z1 - z2;
312  }
313 
314  complex operator*(const double& x1, const complex& z2)
315  {
316  complex z1(x1, 0.);
317  return z1 * z2;
318  }
319 
320  complex operator/(const double& x1, const complex& z2)
321  {
322  complex z1(x1, 0);
323  return z1 / z2;
324  }
325 
326  //==================================================================
333  complex exp(const complex& z)
334  {
335  return complex(gsl_complex_exp(z.as_gsl_type()));
336  }
337 
342  complex log(const complex& z)
343  {
344  return complex(gsl_complex_log(z.as_gsl_type()));
345  }
346 
352  {
353  return complex(gsl_complex_log10(z.as_gsl_type()));
354  }
355 
361  complex log(const complex& z,
362  const complex& b)
363  {
364  return complex(gsl_complex_log_b(z.as_gsl_type(),b.as_gsl_type()));
365  }
371  {
372  gsl_sf_result re, im;
373  gsl_sf_complex_dilog_xy_e(z.real(), z.imag(), &re, &im);
374  return complex(re.val, im.val, false);
375  }
378  //==================================================================
385  complex sqrt(const complex& z)
386  {
387  return complex(gsl_complex_sqrt(z.as_gsl_type()));
388  }
389 
395  complex pow(const complex& z1,
396  const complex& z2)
397  {
398  return complex(gsl_complex_pow(z1.as_gsl_type(),
399  z2.as_gsl_type()));
400  }
401 
407  complex pow(const complex& z, const double x)
408  {
409  return complex(gsl_complex_pow_real(z.as_gsl_type(), x));
410  }
413  //==================================================================
420  complex sin(const complex& z)
421  {
422  return complex(gsl_complex_sin(z.as_gsl_type()));
423  }
424 
429  complex cos(const complex& z)
430  {
431  return complex(gsl_complex_cos(z.as_gsl_type()));
432  }
433 
438  complex tan(const complex& z)
439  {
440  return complex(gsl_complex_tan(z.as_gsl_type()));
441  }
442 
447  complex sec(const complex& z)
448  {
449  return complex(gsl_complex_sec(z.as_gsl_type()));
450  }
451 
456  complex csc(const complex& z)
457  {
458  return complex(gsl_complex_csc(z.as_gsl_type()));
459  }
460 
465  complex cot(const complex& z)
466  {
467  return complex(gsl_complex_cot(z.as_gsl_type()));
468  }
469 
475  {
476  return complex(gsl_complex_arcsin(z.as_gsl_type()));
477  }
478 
484  {
485  return complex(gsl_complex_arccos(z.as_gsl_type()));
486  }
487 
493  {
494  return complex(gsl_complex_arctan(z.as_gsl_type()));
495  }
496 
502  {
503  return complex(gsl_complex_arcsec(z.as_gsl_type()));
504  }
505 
511  {
512  return complex(gsl_complex_arccsc(z.as_gsl_type()));
513  }
514 
520  {
521  return complex(gsl_complex_arccot(z.as_gsl_type()));
522  }
523 
526  //==================================================================
533  complex sinh(const complex& z)
534  {
535  return complex(gsl_complex_sinh(z.as_gsl_type()));
536  }
537 
542  complex cosh(const complex& z)
543  {
544  return complex(gsl_complex_cosh(z.as_gsl_type()));
545  }
546 
551  complex tanh(const complex& z)
552  {
553  return complex(gsl_complex_tanh(z.as_gsl_type()));
554  }
555 
560  complex sech(const complex& z)
561  {
562  return complex(gsl_complex_sech(z.as_gsl_type()));
563  }
564 
569  complex csch(const complex& z)
570  {
571  return complex(gsl_complex_csch(z.as_gsl_type()));
572  }
573 
578  complex coth(const complex& z)
579  {
580  return complex(gsl_complex_coth(z.as_gsl_type()));
581  }
582 
588  {
589  return complex(gsl_complex_arcsinh(z.as_gsl_type()));
590  }
591 
597  {
598  return complex(gsl_complex_arccosh(z.as_gsl_type()));
599  }
600 
606  {
607  return complex(gsl_complex_arctanh(z.as_gsl_type()));
608  }
609 
615  {
616  return complex(gsl_complex_arcsech(z.as_gsl_type()));
617  }
618 
624  {
625  return complex(gsl_complex_arccsch(z.as_gsl_type()));
626  }
627 
633  {
634  return complex(gsl_complex_arccoth(z.as_gsl_type()));
635  }
636 
638 }
639 //
640 // EOF
641 //
gslpp::complex::is_imag
bool is_imag() const
Check if complex number is purely imaginary.
Definition: gslpp_complex.cpp:47
gslpp::cos
complex cos(const complex &z)
Definition: gslpp_complex.cpp:429
gslpp::arccoth
complex arccoth(const complex &z)
Definition: gslpp_complex.cpp:632
gslpp::sec
complex sec(const complex &z)
Definition: gslpp_complex.cpp:447
gslpp::sinh
complex sinh(const complex &z)
Definition: gslpp_complex.cpp:533
gslpp::arccot
complex arccot(const complex &z)
Definition: gslpp_complex.cpp:519
gslpp::arctan
complex arctan(const complex &z)
Definition: gslpp_complex.cpp:492
gslpp::complex::operator=
complex & operator=(const complex &z)
Assignment operator for a complex variable of complex type.
Definition: gslpp_complex.cpp:96
gslpp::dilog
complex dilog(const complex &z)
Definition: gslpp_complex.cpp:370
gslpp::arctanh
complex arctanh(const complex &z)
Definition: gslpp_complex.cpp:605
gslpp::sin
complex sin(const complex &z)
Definition: gslpp_complex.cpp:420
gslpp::csch
complex csch(const complex &z)
Definition: gslpp_complex.cpp:569
gslpp::arccosh
complex arccosh(const complex &z)
Definition: gslpp_complex.cpp:596
gslpp::complex::log_of_abs
double log_of_abs() const
Definition: gslpp_complex.cpp:91
gslpp::arcsech
complex arcsech(const complex &z)
Definition: gslpp_complex.cpp:614
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::log
complex log(const complex &z)
Definition: gslpp_complex.cpp:342
gslpp::complex::abs2
double abs2() const
Definition: gslpp_complex.cpp:86
gslpp::tanh
complex tanh(const complex &z)
Definition: gslpp_complex.cpp:551
gslpp::complex::operator+=
complex & operator+=(const complex &z1)
Addition assignment operator for a complex number.
Definition: gslpp_complex.cpp:215
gslpp::arccos
complex arccos(const complex &z)
Definition: gslpp_complex.cpp:483
gslpp::operator+
complex operator+(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:302
gslpp::complex::operator/
complex operator/(const complex &z1) const
Division operator for a complex number.
Definition: gslpp_complex.cpp:209
gslpp::complex::operator+
complex operator+(const complex &z1) const
Addition operator for a complex number.
Definition: gslpp_complex.cpp:191
gslpp::arccsc
complex arccsc(const complex &z)
Definition: gslpp_complex.cpp:510
gslpp::arcsec
complex arcsec(const complex &z)
Definition: gslpp_complex.cpp:501
gslpp::complex::imag
const double & imag() const
Definition: gslpp_complex.cpp:59
gslpp::cosh
complex cosh(const complex &z)
Definition: gslpp_complex.cpp:542
gslpp::complex::conjugate
complex conjugate() const
Definition: gslpp_complex.cpp:288
gslpp::tan
complex tan(const complex &z)
Definition: gslpp_complex.cpp:438
gslpp::complex::operator!=
bool operator!=(const complex &z1) const
Inequivalence operator between two complex variables.
Definition: gslpp_complex.cpp:113
gslpp::complex::operator*
complex operator*(const complex &z1) const
Multiplication operator for a complex number.
Definition: gslpp_complex.cpp:203
gslpp::complex::abs
double abs() const
Definition: gslpp_complex.cpp:81
gslpp::complex::_complex
gsl_complex _complex
Definition: gslpp_complex.h:37
gslpp::complex::arg
double arg() const
Definition: gslpp_complex.cpp:76
gslpp::complex::complex
complex()
Default constructor for the complex class.
Definition: gslpp_complex.cpp:18
gslpp::pow
complex pow(const complex &z1, const complex &z2)
Definition: gslpp_complex.cpp:395
gslpp::sqrt
complex sqrt(const complex &z)
Definition: gslpp_complex.cpp:385
gslpp::complex::i
static const complex & i()
Definition: gslpp_complex.cpp:154
gslpp::arcsinh
complex arcsinh(const complex &z)
Definition: gslpp_complex.cpp:587
gslpp::complex::operator==
bool operator==(const complex &z1) const
Equivalence operator between two complex variables.
Definition: gslpp_complex.cpp:108
gslpp::complex::assign
void assign(const double &real, const double &imag, bool polar)
Definition: gslpp_complex.cpp:173
gslpp::arcsin
complex arcsin(const complex &z)
Definition: gslpp_complex.cpp:474
gslpp::complex::operator*=
complex & operator*=(const complex &z1)
Muliplication assignment operator for a complex number.
Definition: gslpp_complex.cpp:227
gslpp::arccsch
complex arccsch(const complex &z)
Definition: gslpp_complex.cpp:623
gslpp::operator/
complex operator/(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:320
gslpp::csc
complex csc(const complex &z)
Definition: gslpp_complex.cpp:456
gslpp::log10
complex log10(const complex &z)
Definition: gslpp_complex.cpp:351
gslpp::complex::~complex
virtual ~complex()
Default destructor for the complex class.
Definition: gslpp_complex.cpp:37
gslpp::operator<<
std::ostream & operator<<(std::ostream &output, const complex &z)
Definition: gslpp_complex.cpp:143
gslpp::complex::operator-
complex operator-() const
Unary minus operator for a complex number.
Definition: gslpp_complex.cpp:185
gslpp::complex::real
const double & real() const
Definition: gslpp_complex.cpp:53
gslpp::complex::operator/=
complex & operator/=(const complex &z1)
Division assignment operator for a complex number.
Definition: gslpp_complex.cpp:233
gslpp::cot
complex cot(const complex &z)
Definition: gslpp_complex.cpp:465
gslpp_complex.h
gslpp::operator*
complex operator*(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:314
gslpp::sech
complex sech(const complex &z)
Definition: gslpp_complex.cpp:560
gslpp::complex::is_real
bool is_real() const
Check if complex number is purely real.
Definition: gslpp_complex.cpp:41
gslpp::complex::as_gsl_type
gsl_complex & as_gsl_type()
Definition: gslpp_complex.cpp:123
gslpp::exp
complex exp(const complex &z)
Definition: gslpp_complex.cpp:333
gslpp::complex::inverse
complex inverse() const
Definition: gslpp_complex.cpp:294
gslpp::complex::operator-=
complex & operator-=(const complex &z1)
Subtraction assignment operator for a complex number.
Definition: gslpp_complex.cpp:221
gslpp
Complex number, vector and matrix manipulation using GSL.
Definition: gslpp_complex.cpp:16
gslpp::coth
complex coth(const complex &z)
Definition: gslpp_complex.cpp:578
gslpp::operator-
complex operator-(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:308