a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
gslpp_matrix_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 #ifndef __GSL_BLAS_H__
9 #include <gsl/gsl_blas.h>
10 #endif
11 #ifndef __GSL_LINALG_H__
12 #include <gsl/gsl_linalg.h>
13 #endif
14 #ifndef __GSL_EIGEN_H__
15 #include <gsl/gsl_eigen.h>
16 #endif
17 #ifndef GSLPP_MATRIX_DOUBLE_H
18 #include "gslpp_matrix_double.h"
19 #endif
20 
21 namespace gslpp {
22 
24  matrix<double>::matrix(const size_t& size_i, const size_t& size_j, const double &a)
25  {
26  _matrix = gsl_matrix_alloc(size_i, size_j);
27  gsl_matrix_set_all(_matrix, a);
28  }
29 
30  matrix<double>::matrix(const size_t& size_i, const double &a)
31  {
32  _matrix = gsl_matrix_alloc(size_i, size_i);
33  gsl_matrix_set_all(_matrix, a);
34  }
35 
38  {
39  _matrix = gsl_matrix_alloc(m.size_i(), m.size_j());
40  gsl_matrix_memcpy(_matrix, m.as_gsl_type_ptr());
41  }
42 
43  matrix<double>::matrix(const gsl_matrix& m)
44  {
45  _matrix = gsl_matrix_alloc(m.size1, m.size2);
46  gsl_matrix_memcpy(_matrix, &m);
47  }
48 
49  matrix<double>::matrix(const gsl_matrix* m)
50  {
51  _matrix = gsl_matrix_alloc(m->size1, m->size2);
52  gsl_matrix_memcpy(_matrix, m);
53  }
54 
56  {
57  size_t i, size = v.size();
58  _matrix = gsl_matrix_alloc(size, size);
59  gsl_matrix_set_all(_matrix, 0.);
60  for (i = 0; i < size; ++i)
61  gsl_matrix_set(_matrix, i, i, v(i));
62  }
63 
66  {
67  gsl_matrix_free(_matrix);
68  }
69 
71  double matrix<double>::operator()(const size_t& i, const size_t& j) const
72  {
73  const double *x = gsl_matrix_const_ptr(_matrix, i, j);
74  return *x;
75  }
76 
78  double& matrix<double>::operator()(const size_t& i, const size_t& j)
79  {
80  double *x = gsl_matrix_ptr(_matrix, i, j);
81  return *x;
82  }
83 
86  {
87  gsl_matrix_set_zero(_matrix);
88  }
89 
91  {
92  if (size_i() == m.size_i() && size_j() == m.size_j())
93  gsl_matrix_memcpy(_matrix, m.as_gsl_type_ptr());
94  else
95  {
96  std::cout << "\n ** Wrong size assign in matrix<complex> operator =" << std::endl;
97  exit(EXIT_FAILURE);
98  }
99  return *this;
100  }
101 
103  {
104  gsl_matrix_set_all(_matrix, a);
105  return *this;
106  }
107 
108  void matrix<double>::assign(const size_t& i, const size_t& j, const double& a)
109  {
110  gsl_matrix_set(_matrix, i, j, a);
111  }
112 
114  void matrix<double>::assign(const size_t& i, const size_t& j, const matrix<double>& a)
115  {
116  size_t ki, kj;
117  double *x;
118  if (i + a.size_i() <= size_i() && j + a.size_j() <= size_j())
119  for (ki = i; ki < i + a.size_i(); ki++)
120  for (kj = j; kj < j + a.size_j(); kj++)
121  {
122  x = gsl_matrix_ptr(_matrix, ki, kj);
123  *x = a(ki - i, kj - j);
124  } else
125  {
126  std::cout << "\n ** Wrong size assign in matrix<double> assign submatrix" << std::endl;
127  exit(EXIT_FAILURE);
128  }
129  }
130 
132  size_t matrix<double>::size_i() const
133  {
134  return _matrix->size1;
135  }
136 
137  size_t matrix<double>::size_j() const
138  {
139  return _matrix->size2;
140  }
141 
142  size_t matrix<double>::size() const
143  {
144  if (_matrix->size1 == _matrix->size2)
145  return _matrix->size2;
146  else
147  {
148  std::cout << "\n ** Non square matrix" << std::endl;
149  exit(EXIT_FAILURE);
150  }
151  }
152 
155  {
156  matrix<double> m1(size, size, 0.);
157  if (gsl_matrix_add_diagonal(m1.as_gsl_type_ptr(), 1.))
158  {
159  std::cout << "\n Error in matrix<double> Id" << std::endl;
160  exit(EXIT_FAILURE);
161  }
162  return m1;
163  }
164 
167  {
168  matrix<double> m1(size_j(), size_i(), 0.);
169  if (gsl_matrix_transpose_memcpy(m1.as_gsl_type_ptr(), _matrix))
170  {
171  std::cout << "\n Error in matrix<double> transpose" << std::endl;
172  exit(EXIT_FAILURE);
173  }
174  return m1;
175  }
176 
179  {
180  matrix<double> m1(_matrix);
181  matrix<double> m2(_matrix);
182  int signum;
183  gsl_permutation *p;
184 
185  if (size_j() != size_i())
186  {
187  std::cout << "\n ** Size mismatch in matrix<double> inverse" << std::endl;
188  exit(EXIT_FAILURE);
189  }
190 
191  if ((p = gsl_permutation_alloc(size_i())) == NULL)
192  {
193  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
194  exit(EXIT_FAILURE);
195  }
196 
197  if (gsl_linalg_LU_decomp(m1.as_gsl_type_ptr(), p, &signum))
198  {
199  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
200  gsl_permutation_free(p);
201  exit(EXIT_FAILURE);
202  }
203 
204  if (gsl_linalg_LU_invert(m1.as_gsl_type_ptr(), p, m2.as_gsl_type_ptr()))
205  {
206  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
207  gsl_permutation_free(p);
208  exit(EXIT_FAILURE);
209  }
210  gsl_permutation_free(p);
211  return m2;
212  }
213 
217  {
218  matrix<double> m1(_matrix);
219  int signum;
220  gsl_permutation *p;
221 
222  if (size_j() != size_i())
223  {
224  std::cout << "\n ** Size mismatch in bool isSingular" << std::endl;
225  exit(EXIT_FAILURE);
226  }
227 
228  if ((p = gsl_permutation_alloc(size_i())) == NULL)
229  {
230  std::cout << "\n ** Error in bool isSingular" << std::endl;
231  exit(EXIT_FAILURE);
232  }
233 
234  if (gsl_linalg_LU_decomp(m1.as_gsl_type_ptr(), p, &signum))
235  {
236  std::cout << "\n ** Error in bool isSingular" << std::endl;
237  gsl_permutation_free(p);
238  exit(EXIT_FAILURE);
239  }
240 
241  size_t i, n = m1.size_i();
242 
243  for (i = 0; i < n; i++) {
244  double u = gsl_matrix_get(m1.as_gsl_type_ptr(), i, i);
245  if (u == 0) return 1;
246  }
247  return 0;
248  }
249 
252  {
253 
254  matrix<double> m1(_matrix);
255  int signum;
256  gsl_permutation *p;
257 
258  if (size_j() != size_i())
259  {
260  std::cout << "\n ** Size mismatch in matrix<double> determinant" << std::endl;
261  exit(EXIT_FAILURE);
262  }
263 
264  if ((p = gsl_permutation_alloc(size_i())) == NULL)
265  {
266  std::cout << "\n ** Error in matrix<double> determinant" << std::endl;
267  exit(EXIT_FAILURE);
268  }
269 
270  if (gsl_linalg_LU_decomp(m1.as_gsl_type_ptr(), p, &signum))
271  {
272  std::cout << "\n ** Error in matrix<double> determinant" << std::endl;
273  gsl_permutation_free(p);
274  exit(EXIT_FAILURE);
275  }
276  gsl_permutation_free(p);
277  return gsl_linalg_LU_det(m1.as_gsl_type_ptr(), signum);
278  }
279 
281  {
282  matrix<double> m(*this);
283 
284  gsl_eigen_nonsymmv_workspace *ws;
285 
286  ws = gsl_eigen_nonsymmv_alloc(size_i());
287 
288  gsl_eigen_nonsymmv(m.as_gsl_type_ptr(), S.as_gsl_type_ptr(),
289  U.as_gsl_type_ptr(), ws);
290 
291  gsl_eigen_nonsymmv_free(ws);
292  }
293 
296  {
297  return _matrix;
298  }
299 
301  {
302  return const_cast<gsl_matrix&> (*_matrix);
303  }
304 
305  const gsl_matrix& matrix<double>::as_gsl_type() const
306  {
307  return const_cast<gsl_matrix&> (*_matrix);
308  }
309 
310  // bool matrix<double>::is_equal(const matrix<double>& m1, const matrix<double>& m2){
311  // return gsl_matrix_equal (m1.as_gsl_type_ptr(),m2.as_gsl_type_ptr());
312  // }
313 
316  {
317  matrix<double> m1(_matrix);
318  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), -1.))
319  {
320  std::cout << "\n Error in matrix<double> unary -" << std::endl;
321  exit(EXIT_FAILURE);
322  }
323  return m1;
324  }
325 
328  {
329  matrix<double> m1(_matrix);
330  if (gsl_matrix_add(m1.as_gsl_type_ptr(), m.as_gsl_type_ptr()))
331  {
332  std::cout << "\n Error in matrix<double> +" << std::endl;
333  exit(EXIT_FAILURE);
334  }
335  return m1;
336  }
337 
340  {
341  matrix<double> m1(_matrix);
342  if (gsl_matrix_sub(m1.as_gsl_type_ptr(), m.as_gsl_type_ptr()))
343  {
344  std::cout << "\n Error in matrix<double> -" << std::endl;
345  exit(EXIT_FAILURE);
346  }
347  return m1;
348  }
349 
352  {
353  unsigned int i, j, k;
354  matrix<double> m1(size_i(), m.size_i(), 0.);
355 
356  if (size_j() != m.size_i())
357  {
358  std::cout << "\n Error in matrix<double> *" << std::endl;
359  exit(EXIT_FAILURE);
360  }
361 
362  for (i = 0; i < size_i(); i++)
363  for (j = 0; j < m.size_j(); j++)
364  for (k = 0; k < m.size_i(); k++)
365  m1(i, j) += (*this)(i, k) * m(k, j);
366 
367  return m1;
368  }
369 
371  {
372  matrix<complex> m1(*this);
373  return m1 * m;
374  }
375 
378  {
379  vector<double> v1(size_i(), 0.);
380 
381  if (size_j() != v.size())
382  {
383  std::cout << "\n ** Size mismatch in matrix<double> * (vector double)"
384  << std::endl;
385  exit(EXIT_FAILURE);
386  }
387  if (gsl_blas_dgemv(CblasNoTrans, 1., _matrix, v.as_gsl_type_ptr(),
388  0., v1.as_gsl_type_ptr()))
389  {
390  std::cout << "\n ** Error in matrix<double> * (vector double)"
391  << std::endl;
392  exit(EXIT_FAILURE);
393  }
394  return v1;
395  }
396 
399  {
400  matrix<complex> m1(*this);
401  return m1 * v;
402  }
403 
406  {
407  *this = *this +m;
408  return *this;
409  }
410 
413  {
414  *this = *this -m;
415  return *this;
416  }
417 
420  {
421  if (!((size_i() == size_j()) && (size_i() == m.size_i()) && (size_j() == m.size_j())))
422  {
423  std::cout << "\n Error in matrix<double> *= (matrix)" << std::endl;
424  exit(EXIT_FAILURE);
425  }
426  *this = *this * m;
427  return *this;
428  }
429 
432  {
433  matrix<double> m1(_matrix);
434  if (gsl_matrix_add_constant(m1.as_gsl_type_ptr(), a))
435  {
436  std::cout << "\n Error in matrix<double> + (double)" << std::endl;
437  exit(EXIT_FAILURE);
438  }
439  return m1;
440  }
441 
444  {
445  matrix<double> m1(_matrix);
446  if (gsl_matrix_add_constant(m1.as_gsl_type_ptr(), -a))
447  {
448  std::cout << "\n Error in matrix<double> - (double)" << std::endl;
449  exit(EXIT_FAILURE);
450  }
451  return m1;
452  }
453 
456  {
457  matrix<double> m1(_matrix);
458  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), a))
459  {
460  std::cout << "\n Error in matrix<double> * (double)" << std::endl;
461  exit(EXIT_FAILURE);
462  }
463  return m1;
464  }
465 
468  {
469  matrix<double> m1(_matrix);
470  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), 1. / a))
471  {
472  std::cout << "\n Error in matrix<double> / (double)" << std::endl;
473  exit(EXIT_FAILURE);
474  }
475  return m1;
476  }
477 
480  {
481  *this = *this +a;
482  return *this;
483  }
484 
487  {
488  *this = *this -a;
489  return *this;
490  }
491 
494  {
495  *this = *this * a;
496  return *this;
497  }
498 
501  {
502  *this = *this / a;
503  return *this;
504  }
505 
508  {
509  matrix<complex> v1(*this);
510  return v1 + z;
511  }
512 
515  {
516  matrix<complex> v1(*this);
517  return v1 - z;
518  }
519 
522  {
523  matrix<complex> v1(*this);
524  return v1*z;
525  }
526 
529  {
530  matrix<complex> v1(*this);
531  return v1 / z;
532  }
533 
536  {
537  if (a.size_i() != size_i() || a.size_j() != size_j())
538  {
539  std::cout << "\n Error in matrix<double>::operator== (matrix): cannot compare matrices of different size" << std::endl;
540  exit(EXIT_FAILURE);
541  }
542  for (size_t i = 0; i < size_i(); i++)
543  for (size_t j = 0; j < size_j(); j++)
544  if (a(i,j) != (*this)(i,j)) return (false);
545  return (true);
546  }
547 
549  std::ostream& operator<<(std::ostream& output, const matrix<double>& m)
550  {
551  size_t i, j;
552  for (i = 0; i < m.size_i(); i++)
553  {
554  output << std::endl;
555  output << "\t(";
556  for (j = 0; j < m.size_j() - 1; j++)
557  output << m(i, j) << ",";
558  output << m(i, j) << ")";
559  }
560  return output;
561  }
562 
564  {
565  return m + a;
566  }
567 
569  {
570  return -m + a;
571  }
572 
574  {
575  return m*a;
576  }
577 
579  {
580  return m.transpose() * v;
581  }
582 
584  {
585  return m.transpose() * v;
586  }
587 
589  {
590  return m + z;
591  }
592 
594  {
595  return -m + z;
596  }
597 
599  {
600  return m*z;
601  }
602 }
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::matrix< double >
A class for constructing and defining operations on real matrices.
Definition: gslpp_matrix_double.h:48
gslpp::operator-
matrix< complex > operator-(const complex &z, matrix< double > m)
Definition: gslpp_matrix_double.cpp:593
gslpp::matrix< double >::size_j
size_t size_j() const
Definition: gslpp_matrix_double.cpp:137
gslpp::matrix< double >::transpose
matrix< double > transpose() const
Definition: gslpp_matrix_double.cpp:166
gslpp::complex
A class for defining operations on and functions of complex numbers.
Definition: gslpp_complex.h:35
gslpp::matrix< double >::as_gsl_type_ptr
gsl_matrix * as_gsl_type_ptr() const
Definition: gslpp_matrix_double.cpp:295
gslpp::matrix
A base class for defining operations on matrices, both real and complex.
Definition: gslpp_matrix_base.h:21
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_matrix_double.h
gslpp::operator+
matrix< complex > operator+(const complex &z, matrix< double > m)
Definition: gslpp_matrix_double.cpp:588
gslpp::matrix< double >::size_i
size_t size_i() const
Definition: gslpp_matrix_double.cpp:132
gslpp::matrix< complex >::as_gsl_type_ptr
gsl_matrix_complex * as_gsl_type_ptr() const
Definition: gslpp_matrix_complex.cpp:384
gslpp::operator*
matrix< complex > operator*(const complex &z, matrix< double > m)
Definition: gslpp_matrix_double.cpp:598
gslpp::vector< double >
A class for constructing and defining operations on real vectors.
Definition: gslpp_vector_double.h:33
S
A class for the form factor in .
Definition: MVllObservables.h:1436
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 number, vector and matrix manipulation using GSL.
Definition: gslpp_complex.cpp:16
gslpp::matrix< complex >
A class for constructing and defining operations on complex matrices.
Definition: gslpp_matrix_complex.h:45
gslpp::operator-
complex operator-(const double &x1, const complex &z2)
Definition: gslpp_complex.cpp:308