gslpp_matrix_double.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 #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  if(size_i()==m.size_i() && size_j()==m.size_j())
88  gsl_matrix_memcpy(_matrix, m.as_gsl_type_ptr());
89  else
90  {
91  std::cout << "\n ** Wrong size assign in matrix<complex> operator =" << std::endl;
92  exit(EXIT_FAILURE);
93  }
94  return *this;
95  }
96 
98  {
99  gsl_matrix_set_all(_matrix, a);
100  return *this;
101  }
102 
103  void matrix<double>::assign(const size_t& i, const size_t& j, const double& a)
104  {
105  gsl_matrix_set(_matrix, i, j, a);
106  }
107 
109  void matrix<double>::assign(const size_t& i, const size_t& j, const matrix<double>& a)
110  {
111  size_t ki,kj;
112  double *x;
113  if(i+a.size_i() <= size_i() && j+a.size_j() <= size_j())
114  for(ki=i;ki<i+a.size_i();ki++)
115  for(kj=j;kj<j+a.size_j();kj++)
116  {
117  x = gsl_matrix_ptr(_matrix, ki, kj);
118  *x = a(ki-i,kj-j);
119  }
120  else
121  {
122  std::cout << "\n ** Wrong size assign in matrix<double> assign submatrix" << std::endl;
123  exit(EXIT_FAILURE);
124  }
125  }
126 
128  size_t matrix<double>::size_i() const
129  {
130  return _matrix->size1;
131  }
132 
133  size_t matrix<double>::size_j() const
134  {
135  return _matrix->size2;
136  }
137 
140  {
141  matrix<double> m1(size, size, 0.);
142  if (gsl_matrix_add_diagonal(m1.as_gsl_type_ptr(), 1.))
143  {
144  std::cout << "\n Error in matrix<double> Id" << std::endl;
145  exit(EXIT_FAILURE);
146  }
147  return m1;
148  }
149 
152  {
153  matrix<double> m1(size_j(), size_i(), 0.);
154  if (gsl_matrix_transpose_memcpy(m1.as_gsl_type_ptr(), _matrix))
155  {
156  std::cout << "\n Error in matrix<double> transpose" << std::endl;
157  exit(EXIT_FAILURE);
158  }
159  return m1;
160  }
161 
164  {
165  matrix<double> m1(_matrix);
166  matrix<double> m2(_matrix);
167  int signum;
168  gsl_permutation *p;
169 
170  if (size_j() != size_i())
171  {
172  std::cout << "\n ** Size mismatch in matrix<double> inverse" << std::endl;
173  exit(EXIT_FAILURE);
174  }
175 
176  if ((p = gsl_permutation_alloc(size_i())) == NULL)
177  {
178  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
179  exit(EXIT_FAILURE);
180  }
181 
182  if(gsl_linalg_LU_decomp (m1.as_gsl_type_ptr(), p, &signum))
183  {
184  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
185  gsl_permutation_free(p);
186  exit(EXIT_FAILURE);
187  }
188 
189  if(gsl_linalg_LU_invert(m1.as_gsl_type_ptr(), p, m2.as_gsl_type_ptr()))
190  {
191  std::cout << "\n ** Error in matrix<double> inverse" << std::endl;
192  gsl_permutation_free(p);
193  exit(EXIT_FAILURE);
194  }
195  gsl_permutation_free(p);
196  return m2;
197  }
198 
201 
202  matrix<double> m1(_matrix);
203  int signum;
204  gsl_permutation *p;
205 
206  if (size_j() != size_i())
207  {
208  std::cout << "\n ** Size mismatch in matrix<double> determinant" << std::endl;
209  exit(EXIT_FAILURE);
210  }
211 
212  if ((p = gsl_permutation_alloc(size_i())) == NULL)
213  {
214  std::cout << "\n ** Error in matrix<double> determinant" << std::endl;
215  exit(EXIT_FAILURE);
216  }
217 
218  if(gsl_linalg_LU_decomp (m1.as_gsl_type_ptr(), p, &signum))
219  {
220  std::cout << "\n ** Error in matrix<double> determinant" << std::endl;
221  gsl_permutation_free(p);
222  exit(EXIT_FAILURE);
223  }
224  gsl_permutation_free(p);
225  return gsl_linalg_LU_det(m1.as_gsl_type_ptr() , signum);
226  }
227 
229  matrix<double> m(*this);
230 
231  gsl_eigen_nonsymmv_workspace *ws;
232 
233  ws = gsl_eigen_nonsymmv_alloc(size_i());
234 
235  gsl_eigen_nonsymmv(m.as_gsl_type_ptr(), S.as_gsl_type_ptr(),
236  U.as_gsl_type_ptr(), ws);
237 
238  gsl_eigen_nonsymmv_free(ws);
239  }
240 
243  {
244  return _matrix;
245  }
246 
248  {
249  return const_cast<gsl_matrix&>(*_matrix);
250  }
251 
252  const gsl_matrix& matrix<double>::as_gsl_type() const
253  {
254  return const_cast<gsl_matrix&>(*_matrix);
255  }
256 
257 // bool matrix<double>::is_equal(const matrix<double>& m1, const matrix<double>& m2){
258 // return gsl_matrix_equal (m1.as_gsl_type_ptr(),m2.as_gsl_type_ptr());
259 // }
260 
263  {
264  matrix<double> m1(_matrix);
265  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), -1.))
266  {
267  std::cout << "\n Error in matrix<double> unary -" << std::endl;
268  exit(EXIT_FAILURE);
269  }
270  return m1;
271  }
274  {
275  matrix<double> m1(_matrix);
276  if (gsl_matrix_add(m1.as_gsl_type_ptr(), m.as_gsl_type_ptr()))
277  {
278  std::cout << "\n Error in matrix<double> +" << std::endl;
279  exit(EXIT_FAILURE);
280  }
281  return m1;
282  }
285  {
286  matrix<double> m1(_matrix);
287  if (gsl_matrix_sub(m1.as_gsl_type_ptr(), m.as_gsl_type_ptr()))
288  {
289  std::cout << "\n Error in matrix<double> -" << std::endl;
290  exit(EXIT_FAILURE);
291  }
292  return m1;
293  }
296  {
297  unsigned int i,j,k;
298  matrix<double> m1(size_i(),m.size_i(),0.);
299 
300  if (size_j() != m.size_i())
301  {
302  std::cout << "\n Error in matrix<double> *" << std::endl;
303  exit(EXIT_FAILURE);
304  }
305 
306  for(i=0; i<size_i(); i++)
307  for(j=0; j<m.size_j(); j++)
308  for(k=0; k<m.size_i(); k++)
309  m1(i,j)+=(*this)(i,k)*m(k,j);
310 
311  return m1;
312  }
313 
316  {
317  vector<double> v1(size_i(),0.);
318 
319  if (size_j() != v.size())
320  {
321  std::cout << "\n ** Size mismatch in matrix<double> * (vector double)"
322  << std::endl;
323  exit(EXIT_FAILURE);
324  }
325  if(gsl_blas_dgemv(CblasNoTrans, 1., _matrix, v.as_gsl_type_ptr(),
326  0., v1.as_gsl_type_ptr()))
327  {
328  std::cout << "\n ** Error in matrix<double> * (vector double)"
329  << std::endl;
330  exit(EXIT_FAILURE);
331  }
332  return v1;
333  }
334 
337  {
338  matrix<complex> m1(*this);
339  return m1 * v;
340  }
341 
344  {
345  *this = *this + m;
346  return *this;
347  }
350  {
351  *this = *this - m;
352  return *this;
353  }
354 
357  {
358  if (!((size_i() == size_j()) && (size_i() == m.size_i()) && (size_j() == m.size_j())))
359  {
360  std::cout << "\n Error in matrix<double> *= (matrix)" << std::endl;
361  exit(EXIT_FAILURE);
362  }
363  *this = *this * m;
364  return *this;
365  }
366 
369  {
370  matrix<double> m1(_matrix);
371  if (gsl_matrix_add_constant(m1.as_gsl_type_ptr(), a))
372  {
373  std::cout << "\n Error in matrix<double> + (double)" << std::endl;
374  exit(EXIT_FAILURE);
375  }
376  return m1;
377  }
380  {
381  matrix<double> m1(_matrix);
382  if (gsl_matrix_add_constant(m1.as_gsl_type_ptr(), -a))
383  {
384  std::cout << "\n Error in matrix<double> - (double)" << std::endl;
385  exit(EXIT_FAILURE);
386  }
387  return m1;
388  }
391  {
392  matrix<double> m1(_matrix);
393  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), a))
394  {
395  std::cout << "\n Error in matrix<double> * (double)" << std::endl;
396  exit(EXIT_FAILURE);
397  }
398  return m1;
399  }
402  {
403  matrix<double> m1(_matrix);
404  if (gsl_matrix_scale(m1.as_gsl_type_ptr(), 1./a))
405  {
406  std::cout << "\n Error in matrix<double> / (double)" << std::endl;
407  exit(EXIT_FAILURE);
408  }
409  return m1;
410  }
413  {
414  *this = *this + a;
415  return *this;
416  }
419  {
420  *this = *this - a;
421  return *this;
422  }
425  {
426  *this = *this * a;
427  return *this;
428  }
431  {
432  *this = *this / a;
433  return *this;
434  }
437  {
438  matrix<complex> v1(*this);
439  return v1+z;
440  }
443  {
444  matrix<complex> v1(*this);
445  return v1-z;
446  }
449  {
450  matrix<complex> v1(*this);
451  return v1*z;
452  }
455  {
456  matrix<complex> v1(*this);
457  return v1/z;
458  }
459 
461  std::ostream& operator<<(std::ostream& output, const matrix<double>& m)
462  {
463  size_t i,j;
464  for (i=0; i<m.size_i(); i++)
465  {
466  output << std::endl;
467  output << "\t(";
468  for (j=0; j<m.size_j()-1; j++)
469  output << m(i,j) << ",";
470  output << m(i,j) << ")";
471  }
472  return output;
473  }
474 
476  {
477  return m+a;
478  }
479 
481  {
482  return -m+a;
483  }
484 
486  {
487  return m*a;
488  }
489 
491  {
492  return m.transpose() * v;
493  }
494 
496  {
497  return m.transpose() * v;
498  }
499 
501  {
502  return m+z;
503  }
504 
506  {
507  return -m+z;
508  }
509 
511  {
512  return m*z;
513  }
514 }
complex operator*(const double &x1, const complex &z2)
A class for the form factor in .
A class for constructing and defining operations on real matrices.
A base class for defining operations on matrices, both real and complex.
gsl_matrix * as_gsl_type_ptr() const
matrix< complex > operator+(const complex &z, matrix< double > m)
A class for constructing and defining operations on complex vectors.
A class for constructing and defining operations on complex matrices.
matrix< complex > operator*(const complex &z, matrix< double > m)
gsl_matrix_complex * as_gsl_type_ptr() const
complex operator/(const double &x1, const complex &z2)
matrix< complex > operator-(const complex &z, matrix< double > m)
gsl_vector_complex * as_gsl_type_ptr() const
A class for constructing and defining operations on real vectors.
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.
gsl_vector * as_gsl_type_ptr() const