gslpp::matrix< double > Class Template Reference

A class for constructing and defining operations on real matrices. More...

#include <gslpp_matrix_double.h>

Detailed Description

template<>
class gslpp::matrix< double >

A class for constructing and defining operations on real matrices.

Author
HEPfit Collaboration

This class defines some common operations on real matrices using the GSL.

Definition at line 48 of file gslpp_matrix_double.h.

Public Member Functions

gsl_matrix & as_gsl_type ()
 
const gsl_matrix & as_gsl_type () const
 
gsl_matrix * as_gsl_type_ptr () const
 
void assign (const size_t &i, const size_t &j, const double &a)
 
void assign (const size_t &i, const size_t &j, const matrix< double > &a)
 
double determinant ()
 
void eigensystem (matrix< complex > &U, vector< complex > &S)
 
matrix< double > inverse ()
 
 matrix (const size_t &size_i, const size_t &size_j, const double &a)
 
 matrix (const size_t &size_i, const double &a)
 
 matrix (const matrix< double > &m)
 
 matrix (const gsl_matrix &m)
 
 matrix (const gsl_matrix *m)
 
 matrix (const vector< double > &v)
 
double operator() (const size_t &i, const size_t &j) const
 
double & operator() (const size_t &i, const size_t &j)
 
matrix< double > operator* (const matrix< double > &m)
 
vector< double > operator* (const vector< double > &v)
 
vector< complexoperator* (const vector< complex > &v)
 
matrix< double > operator* (const double &a)
 
matrix< complexoperator* (const complex &z)
 
matrix< double > & operator*= (const matrix< double > &m)
 
matrix< double > & operator*= (const double &a)
 
matrix< double > operator+ (const matrix< double > &m)
 
matrix< double > operator+ (const double &a)
 
matrix< complexoperator+ (const complex &z)
 
matrix< double > & operator+= (const matrix< double > &m)
 
matrix< double > & operator+= (const double &a)
 
matrix< double > operator- () const
 
matrix< double > operator- (const matrix< double > &m)
 
matrix< double > operator- (const double &a)
 
matrix< complexoperator- (const complex &z)
 
matrix< double > & operator-= (const matrix< double > &m)
 
matrix< double > & operator-= (const double &a)
 
matrix< double > operator/ (const double &a)
 
matrix< complexoperator/ (const complex &z)
 
matrix< double > & operator/= (const double &a)
 
matrix< double > & operator= (const matrix< double > &m)
 
matrix< double > & operator= (double a)
 
size_t size_i () const
 
size_t size_j () const
 
matrix< double > transpose ()
 
 ~matrix ()
 

Static Public Member Functions

static matrix< double > Id (size_t size)
 

Private Attributes

gsl_matrix * _matrix
 

Friends

std::ostream & operator<< (std::ostream &output, const matrix< double > &m)
 
Operations on matrix<double>
matrix< double > operator+ (const double &a, matrix< double > m)
 
matrix< double > operator- (const double &a, matrix< double > m)
 
matrix< double > operator* (const double &a, matrix< double > m)
 
vector< double > operator* (const vector< double > &v, matrix< double > m)
 
vector< complexoperator* (const vector< complex > &v, matrix< double > m)
 
matrix< complexoperator+ (const complex &z, matrix< double > m)
 
matrix< complexoperator- (const complex &z, matrix< double > m)
 
matrix< complexoperator* (const complex &z, matrix< double > m)
 

Constructor & Destructor Documentation

gslpp::matrix< double >::matrix ( const size_t &  size_i,
const size_t &  size_j,
const double &  a 
)

Constructor

Definition at line 24 of file gslpp_matrix_double.cpp.

25  {
26  _matrix = gsl_matrix_alloc(size_i, size_j);
27  gsl_matrix_set_all(_matrix, a);
28  }
gslpp::matrix< double >::matrix ( const size_t &  size_i,
const double &  a 
)

Definition at line 30 of file gslpp_matrix_double.cpp.

31  {
32  _matrix = gsl_matrix_alloc(size_i, size_i);
33  gsl_matrix_set_all(_matrix, a);
34  }
gslpp::matrix< double >::matrix ( const matrix< double > &  m)

Copy constructor

Definition at line 37 of file gslpp_matrix_double.cpp.

38  {
39  _matrix = gsl_matrix_alloc(m.size_i(), m.size_j());
40  gsl_matrix_memcpy(_matrix, m.as_gsl_type_ptr());
41  }
gslpp::matrix< double >::matrix ( const gsl_matrix< double > &  m)

Definition at line 43 of file gslpp_matrix_double.cpp.

44  {
45  _matrix = gsl_matrix_alloc(m.size1,m.size2);
46  gsl_matrix_memcpy(_matrix, &m);
47  }
gslpp::matrix< double >::matrix ( const gsl_matrix< double > *  m)

Definition at line 49 of file gslpp_matrix_double.cpp.

50  {
51  _matrix = gsl_matrix_alloc(m->size1,m->size2);
52  gsl_matrix_memcpy(_matrix, m);
53  }
gslpp::matrix< double >::matrix ( const vector< double > &  v)

Definition at line 55 of file gslpp_matrix_double.cpp.

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  }
gslpp::matrix< double >::~matrix ( )

Destructor

Definition at line 65 of file gslpp_matrix_double.cpp.

66  {
67  gsl_matrix_free(_matrix);
68  }

Member Function Documentation

gsl_matrix & gslpp::matrix< double >::as_gsl_type ( )

Definition at line 247 of file gslpp_matrix_double.cpp.

248  {
249  return const_cast<gsl_matrix&>(*_matrix);
250  }
const gsl_matrix & gslpp::matrix< double >::as_gsl_type ( ) const

Definition at line 252 of file gslpp_matrix_double.cpp.

253  {
254  return const_cast<gsl_matrix&>(*_matrix);
255  }
gsl_matrix * gslpp::matrix< double >::as_gsl_type_ptr ( ) const

Conversion

Definition at line 242 of file gslpp_matrix_double.cpp.

243  {
244  return _matrix;
245  }
void gslpp::matrix< double >::assign ( const size_t &  i,
const size_t &  j,
const double &  a 
)

Definition at line 103 of file gslpp_matrix_double.cpp.

104  {
105  gsl_matrix_set(_matrix, i, j, a);
106  }
void gslpp::matrix< double >::assign ( const size_t &  i,
const size_t &  j,
const matrix< double > &  a 
)

Assign submatrix

Definition at line 109 of file gslpp_matrix_double.cpp.

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  }
double gslpp::matrix< double >::determinant ( )

Determinant matrix

Determinant of matrix

Definition at line 200 of file gslpp_matrix_double.cpp.

200  {
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  }
void gslpp::matrix< double >::eigensystem ( matrix< complex > &  U,
vector< complex > &  S 
)

Eigenvalues and eigenvectors

Definition at line 228 of file gslpp_matrix_double.cpp.

228  {
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  }
A class for the form factor in .
matrix< double > gslpp::matrix< double >::Id ( size_t  size)
static

Identity matrix

Definition at line 139 of file gslpp_matrix_double.cpp.

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  }
matrix< double > gslpp::matrix< double >::inverse ( )

Inverse matrix

Definition at line 163 of file gslpp_matrix_double.cpp.

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  }
double gslpp::matrix< double >::operator() ( const size_t &  i,
const size_t &  j 
) const

Get element (i,j)

Definition at line 71 of file gslpp_matrix_double.cpp.

72  {
73  const double *x = gsl_matrix_const_ptr(_matrix, i, j);
74  return *x;
75  }
double & gslpp::matrix< double >::operator() ( const size_t &  i,
const size_t &  j 
)

Set i-th element

Set element (i,j)

Definition at line 78 of file gslpp_matrix_double.cpp.

79  {
80  double *x = gsl_matrix_ptr(_matrix, i, j);
81  return *x;
82  }
matrix< double > gslpp::matrix< double >::operator* ( const matrix< double > &  m)

Product (matrix)

Multiplication operator (matrix)

Definition at line 295 of file gslpp_matrix_double.cpp.

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  }
vector< double > gslpp::matrix< double >::operator* ( const vector< double > &  v)

Multiplication (vector double)

Multiplication assignment (vector double)

Definition at line 315 of file gslpp_matrix_double.cpp.

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  }
vector< complex > gslpp::matrix< double >::operator* ( const vector< complex > &  v)

Multiplication (vector complex)

Multiplication assignment (vector complex)

Definition at line 336 of file gslpp_matrix_double.cpp.

337  {
338  matrix<complex> m1(*this);
339  return m1 * v;
340  }
matrix< double > gslpp::matrix< double >::operator* ( const double &  a)

Multiplication operator (double)

Definition at line 390 of file gslpp_matrix_double.cpp.

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  }
matrix< complex > gslpp::matrix< double >::operator* ( const complex z)

Multiplication operator (complex)

Definition at line 448 of file gslpp_matrix_double.cpp.

449  {
450  matrix<complex> v1(*this);
451  return v1*z;
452  }
matrix< double > & gslpp::matrix< double >::operator*= ( const matrix< double > &  m)

Multiplication assignment (matrix)

Definition at line 356 of file gslpp_matrix_double.cpp.

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  }
matrix< double > & gslpp::matrix< double >::operator*= ( const double &  a)

Multiplication assignment (double)

Definition at line 424 of file gslpp_matrix_double.cpp.

425  {
426  *this = *this * a;
427  return *this;
428  }
matrix< double > gslpp::matrix< double >::operator+ ( const matrix< double > &  m)

Addition operator (matrix)

Definition at line 273 of file gslpp_matrix_double.cpp.

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  }
matrix< double > gslpp::matrix< double >::operator+ ( const double &  a)

Addition operator (double)

Definition at line 368 of file gslpp_matrix_double.cpp.

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  }
matrix< complex > gslpp::matrix< double >::operator+ ( const complex z)

Addition operator (complex)

Definition at line 436 of file gslpp_matrix_double.cpp.

437  {
438  matrix<complex> v1(*this);
439  return v1+z;
440  }
matrix< double > & gslpp::matrix< double >::operator+= ( const matrix< double > &  m)

Addition assignment (matrix)

Definition at line 343 of file gslpp_matrix_double.cpp.

344  {
345  *this = *this + m;
346  return *this;
347  }
matrix< double > & gslpp::matrix< double >::operator+= ( const double &  a)

Addition assignment (double)

Definition at line 412 of file gslpp_matrix_double.cpp.

413  {
414  *this = *this + a;
415  return *this;
416  }
matrix< double > gslpp::matrix< double >::operator- ( ) const

check whether two matrices are equal Unary minus (matrix)

Unary minus

Definition at line 262 of file gslpp_matrix_double.cpp.

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  }
matrix< double > gslpp::matrix< double >::operator- ( const matrix< double > &  m)

Subtraction operator (matrix)

Definition at line 284 of file gslpp_matrix_double.cpp.

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  }
matrix< double > gslpp::matrix< double >::operator- ( const double &  a)

Subtraction assignment (double)

Subtraction operator (double)

Definition at line 379 of file gslpp_matrix_double.cpp.

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  }
matrix< complex > gslpp::matrix< double >::operator- ( const complex z)

Subtraction assignment (complex)

Subtraction operator (complex)

Definition at line 442 of file gslpp_matrix_double.cpp.

443  {
444  matrix<complex> v1(*this);
445  return v1-z;
446  }
matrix< double > & gslpp::matrix< double >::operator-= ( const matrix< double > &  m)

Subtraction assignment (matrix)

Definition at line 349 of file gslpp_matrix_double.cpp.

350  {
351  *this = *this - m;
352  return *this;
353  }
matrix< double > & gslpp::matrix< double >::operator-= ( const double &  a)

Subtraction assignment (double)

Definition at line 418 of file gslpp_matrix_double.cpp.

419  {
420  *this = *this - a;
421  return *this;
422  }
matrix< double > gslpp::matrix< double >::operator/ ( const double &  a)

Division operator (double)

Definition at line 401 of file gslpp_matrix_double.cpp.

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  }
matrix< complex > gslpp::matrix< double >::operator/ ( const complex z)

Division operator (complex)

Definition at line 454 of file gslpp_matrix_double.cpp.

455  {
456  matrix<complex> v1(*this);
457  return v1/z;
458  }
matrix< double > & gslpp::matrix< double >::operator/= ( const double &  a)

Division assignment (double)

Definition at line 430 of file gslpp_matrix_double.cpp.

431  {
432  *this = *this / a;
433  return *this;
434  }
matrix< double > & gslpp::matrix< double >::operator= ( const matrix< double > &  m)

Assign

Definition at line 85 of file gslpp_matrix_double.cpp.

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  }
matrix< double > & gslpp::matrix< double >::operator= ( double  a)

Definition at line 97 of file gslpp_matrix_double.cpp.

98  {
99  gsl_matrix_set_all(_matrix, a);
100  return *this;
101  }
size_t gslpp::matrix< double >::size_i ( ) const

Get matrix size

Get matrx size

Definition at line 128 of file gslpp_matrix_double.cpp.

129  {
130  return _matrix->size1;
131  }
size_t gslpp::matrix< double >::size_j ( ) const

Definition at line 133 of file gslpp_matrix_double.cpp.

134  {
135  return _matrix->size2;
136  }
matrix< double > gslpp::matrix< double >::transpose ( )

Transpose matrix

Definition at line 151 of file gslpp_matrix_double.cpp.

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  }

Friends And Related Function Documentation

matrix<double> operator* ( const double &  a,
matrix< double >  m 
)
friend

Multiply a real number by real matrix

Parameters
aReal number
mReal matrix
Returns
\( a*m \)

Definition at line 485 of file gslpp_matrix_double.cpp.

486  {
487  return m*a;
488  }
vector<double> operator* ( const vector< double > &  v,
matrix< double >  m 
)
friend

Multiply a real vector by a real matrix

Parameters
vReal vector
mReal matrix
Returns
\( v*m \)

Definition at line 490 of file gslpp_matrix_double.cpp.

491  {
492  return m.transpose() * v;
493  }
vector<complex> operator* ( const vector< complex > &  v,
matrix< double >  m 
)
friend

Multiply a complex vector by a real matrix

Parameters
vComplex vector
mReal matrix
Returns
\( v*m \)

Definition at line 495 of file gslpp_matrix_double.cpp.

496  {
497  return m.transpose() * v;
498  }
matrix<complex> operator* ( const complex z,
matrix< double >  m 
)
friend

Multiply a complex number by a real matrix

Parameters
zComplex number
mReal matrix
Returns
\( a*m \)

Definition at line 510 of file gslpp_matrix_double.cpp.

511  {
512  return m*z;
513  }
matrix<double> operator+ ( const double &  a,
matrix< double >  m 
)
friend

Add a real number to a real vector

Parameters
aReal number
mReal vector
Returns
\( a + m \)

Definition at line 475 of file gslpp_matrix_double.cpp.

476  {
477  return m+a;
478  }
matrix<complex> operator+ ( const complex z,
matrix< double >  m 
)
friend

Add a complex number to a real matrix

Parameters
zComplex number
mReal matrix
Returns
\( z + m \)

Definition at line 500 of file gslpp_matrix_double.cpp.

501  {
502  return m+z;
503  }
matrix<double> operator- ( const double &  a,
matrix< double >  m 
)
friend

Subtract a real number from a real matrix

Parameters
aReal number
mReal matrix
Returns
\( a - m \)

Definition at line 480 of file gslpp_matrix_double.cpp.

481  {
482  return -m+a;
483  }
matrix<complex> operator- ( const complex z,
matrix< double >  m 
)
friend

Subtract a complex number from a real matrix

Parameters
zComplex number
mReal matrix
Returns
\( z - m \)

Definition at line 505 of file gslpp_matrix_double.cpp.

506  {
507  return -m+z;
508  }
std::ostream& operator<< ( std::ostream &  output,
const matrix< double > &  m 
)
friend

friend functions

Definition at line 461 of file gslpp_matrix_double.cpp.

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  }

Member Data Documentation

gsl_matrix* gslpp::matrix< double >::_matrix
private

Definition at line 50 of file gslpp_matrix_double.h.


The documentation for this class was generated from the following files: