a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
gslpp_vector_double.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_DOUBLE_H
14 #include "gslpp_vector_double.h"
15 #endif
16 #ifndef GSLPP_VECTOR_COMPLEX_H
17 #include "gslpp_vector_complex.h"
18 #endif
19 
20 namespace gslpp
21 {
23  vector<double>::vector(const size_t& size, const double& a)
24  {
25  _vector = gsl_vector_alloc(size);
26  gsl_vector_set_all(_vector, a);
27  }
28 
29  vector<double>::vector(const size_t& size)
30  {
31  _vector = gsl_vector_alloc(size);
32  gsl_vector_set_all(_vector, 0.);
33  }
34 
37  {
38  _vector = gsl_vector_alloc(v.size());
39  gsl_vector_memcpy(_vector, v.as_gsl_type_ptr());
40  }
41 
42  vector<double>::vector(const gsl_vector& v)
43  {
44  _vector = gsl_vector_alloc(v.size);
45  gsl_vector_memcpy(_vector, &v);
46  }
47 
48  vector<double>::vector(const gsl_vector* v)
49  {
50  _vector = gsl_vector_alloc(v->size);
51  gsl_vector_memcpy(_vector, v);
52  }
53 
56  {
57  gsl_vector_free(_vector);
58  }
59 
61  const double& vector<double>::operator()(const size_t& i) const
62  {
63  const double *x = gsl_vector_const_ptr(_vector, i);
64  return *x;
65  }
66 
68  double& vector<double>::operator()(const size_t& i)
69  {
70  double *x = gsl_vector_ptr(_vector, i);
71  return *x;
72  }
73 
76  {
77  gsl_vector_set_zero(_vector);
78  }
79 
80 
82  {
83  gsl_vector_memcpy(_vector, v.as_gsl_type_ptr());
84  return *this;
85  }
86 
88  size_t vector<double>::size() const
89  {
90  return _vector->size;
91  }
92 
94  double vector<double>::mod() const
95  {
96  return gsl_blas_dnrm2(_vector);
97  }
98 
100  double vector<double>::max() const
101  {
102  return gsl_vector_max(_vector);
103  }
104 
106  double vector<double>::min() const
107  {
108  return gsl_vector_min(_vector);
109  }
110 
113  {
114  return _vector;
115  }
116 
118  {
119  return const_cast<gsl_vector&>(*_vector);
120  }
121 
122  const gsl_vector& vector<double>::as_gsl_type() const
123  {
124  return const_cast<gsl_vector&>(*_vector);
125  }
126 
129  {
130  vector<double> v1(_vector);
131  if (gsl_vector_scale(v1.as_gsl_type_ptr(), -1.))
132  {
133  std::cout << "\n Error in vector<double> unary -" << std::endl;
134  exit(EXIT_FAILURE);
135  }
136  return v1;
137  }
138 
141  {
142  vector<double> v1(_vector);
143  if (gsl_vector_add(v1.as_gsl_type_ptr(), v.as_gsl_type_ptr()))
144  {
145  std::cout << "\n Error in vector<double> +" << std::endl;
146  exit(EXIT_FAILURE);
147  }
148  return v1;
149  }
151  {
152  vector<complex> v1(_vector);
153  return v1 + v;
154  }
157  {
158  vector<double> v1(_vector);
159  if (gsl_vector_sub(v1.as_gsl_type_ptr(), v.as_gsl_type_ptr()))
160  {
161  std::cout << "\n Error in vector<double> -" << std::endl;
162  exit(EXIT_FAILURE);
163  }
164  return v1;
165  }
167  {
168  vector<complex> v1(_vector);
169  return v1-v;
170  }
173  {
174  double a=0.;
175  vector<double> v1(_vector);
176  if(gsl_blas_ddot(v1.as_gsl_type_ptr(), v.as_gsl_type_ptr(), &a))
177  {
178  std::cout << "\n Error in vector<double> *" << std::endl;
179  exit(EXIT_FAILURE);
180  }
181  return a;
182  }
184  {
185  vector<complex> v1(_vector);
186  return v*(*this);
187  }
188 
190 // vector<double> vector<double>::operator^(const vector<double>& v)
191 // {
192 // std::cout << "\n To be implemented" << std::endl;
193 // exit(EXIT_FAILURE);
194 // }
195 
198  {
199  *this = *this + v;
200  return *this;
201  }
204  {
205  *this = *this - v;
206  return *this;
207  }
208 
211  {
212  vector<double> v1(_vector);
213  if (gsl_vector_add_constant(v1.as_gsl_type_ptr(), a))
214  {
215  std::cout << "\n Error in vector<double> + (double)" << std::endl;
216  exit(EXIT_FAILURE);
217  }
218  return v1;
219  }
222  {
223  vector<double> v1(_vector);
224  if (gsl_vector_add_constant(v1.as_gsl_type_ptr(), -a))
225  {
226  std::cout << "\n Error in vector<double> - (double)" << std::endl;
227  exit(EXIT_FAILURE);
228  }
229  return v1;
230  }
233  {
234  vector<double> v1(_vector);
235  if (gsl_vector_scale(v1.as_gsl_type_ptr(), a))
236  {
237  std::cout << "\n Error in vector<double> * (double)" << std::endl;
238  exit(EXIT_FAILURE);
239  }
240  return v1;
241  }
244  {
245  vector<double> v1(_vector);
246  if (gsl_vector_scale(v1.as_gsl_type_ptr(), 1./a))
247  {
248  std::cout << "\n Error in vector<double> / (double)" << std::endl;
249  exit(EXIT_FAILURE);
250  }
251  return v1;
252  }
255  {
256  *this = *this + a;
257  return *this;
258  }
261  {
262  *this = *this - a;
263  return *this;
264  }
267  {
268  *this = (*this) * a;
269  return *this;
270  }
273  {
274  *this = *this / a;
275  return *this;
276  }
279  {
280  vector<complex> v1(*this);
281  return v1+z;
282  }
285  {
286  vector<complex> v1(*this);
287  return v1-z;
288  }
291  {
292  vector<complex> v1(*this);
293  return v1*z;
294  }
297  {
298  vector<complex> v1(*this);
299  return v1/z;
300  }
303  {
304  if(a.size() != size())
305  {
306  std::cout << "\n Error in vector<double>::operator== (vector): cannot compare vectors of different size" << std::endl;
307  exit(EXIT_FAILURE);
308  }
309  for(size_t i = 0; i < size(); i++)
310  if(a(i) != (*this)(i)) return(false);
311  return(true);
312  }
313 
315  std::ostream& operator<<(std::ostream& output, const vector<double>& v)
316  {
317  size_t i;
318  output << "(";
319  for (i=0; i<v.size()-1; i++)
320  output << v(i) << ",";
321  output << v(i) << ")";
322  return output;
323  }
324 
326  {
327  return v+a;
328  }
329 
331  {
332  return -v+a;
333  }
334 
336  {
337  return v*a;
338  }
339 
341  {
342  return v+z;
343  }
344 
346  {
347  return -v+z;
348  }
349 
351  {
352  return v*z;
353  }
354 }
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::complex
A class for defining operations on and functions of complex numbers.
Definition: gslpp_complex.h:35
gslpp::operator+
complex operator+(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:302
gslpp::vector< double >::as_gsl_type_ptr
gsl_vector * as_gsl_type_ptr() const
Definition: gslpp_vector_double.cpp:112
gslpp::operator*
vector< complex > operator*(const complex &z, vector< double > v)
Definition: gslpp_vector_double.cpp:350
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+
vector< complex > operator+(const complex &z, vector< double > v)
Definition: gslpp_vector_double.cpp:340
gslpp::operator-
vector< complex > operator-(const complex &z, vector< double > v)
Definition: gslpp_vector_double.cpp:345
gslpp::operator*
complex operator*(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:314
gslpp
Complex number, vector and matrix manipulation using GSL.
Definition: gslpp_complex.cpp:16
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