ePDF  1.1.0
A QED evolution library
matrix.h
Go to the documentation of this file.
1 //
2 // Authors: Rabah Abdul Khalek: rabah.khalek@gmail.com
3 // Valerio Bertone: valerio.bertone@cern.ch
4 //
5 
6 #pragma once
7 
8 #include <vector>
9 #include <stdexcept>
10 #include <complex>
11 
12 namespace ePDF
13 {
17  template <class T>
19  class Matrix
20  {
21  public:
22  //_________________________________________________________________________________
23  Matrix(int const& Lines = 0, int const& Columns = 0, std::vector<T> const& Entries = {}):
24  _Lines(Lines),
25  _Columns(Columns),
26  _Matrix(Entries)
27  {
28  // Check that the size of the Entries match the size of the matrix
29  if (_Lines * _Columns != (int) Entries.size())
30  throw std::runtime_error("[Matrix::Matrix]: the size of the input vector does not match the size of the matrix.");
31  }
32 
33  //_________________________________________________________________________________
34  void SetElement(int const& i, int const& j, T const& value)
35  {
36  if (i < 0 || i > _Lines)
37  throw std::runtime_error("[Matrix::SetElement]: line index out of range.");
38 
39  if (j < 0 || j > _Columns)
40  throw std::runtime_error("[Matrix::SetElement]: column index out of range.");
41 
42  _Matrix[i * _Columns + j] = value;
43  }
44 
45  //_________________________________________________________________________________
46  T GetElement(int const& i, int const& j) const
47  {
48  if (i < 0 || i > _Lines)
49  throw std::runtime_error("[Matrix::GetElement]: line index out of range.");
50 
51  if (j < 0 || j > _Columns)
52  throw std::runtime_error("[Matrix::GetElement]: column index out of range.");
53 
54  return _Matrix[i * _Columns + j];
55  }
56 
57  //_________________________________________________________________________________
58  T operator () (int const& i, int const& j) const
59  {
60  return GetElement(i, j);
61  }
62 
63  //_________________________________________________________________________________
64  int GetLines() const
65  {
66  return _Lines;
67  }
68 
69  //_________________________________________________________________________________
70  int GetColumns() const
71  {
72  return _Columns;
73  }
74 
75  //_________________________________________________________________________________
76  std::vector<T> GetVector() const
77  {
78  return _Matrix;
79  }
80 
81  //_________________________________________________________________________________
83  {
84  _Lines = term.GetLines();
85  _Columns = term.GetColumns();
86  _Matrix = term.GetVector();
87  return *this;
88  }
89 
90  //_________________________________________________________________________________
92  {
93  if (_Lines != term.GetLines() || _Columns != term.GetColumns())
94  throw std::runtime_error("[Matrix::operator +=]: Lines or Columns don't match adding the two matrices.");
95 
96  const std::vector<T> v = term.GetVector();
97  for (int i = 0; i < _Lines; i++)
98  for (int j = 0; j < _Columns; j++)
99  _Matrix[i * _Columns + j] += v[i * _Columns + j];
100  return *this;
101  }
102 
103  //_________________________________________________________________________________
105  {
106  if (_Lines != term.GetLines() || _Columns != term.GetColumns())
107  throw std::runtime_error("[Matrix::operator -=]: Lines or Columns don't match adding the two matrices.");
108 
109  const std::vector<T> v = term.GetVector();
110  for (int i = 0; i < _Lines; i++)
111  for (int j = 0; j < _Columns; j++)
112  _Matrix[i * _Columns + j] -= v[i * _Columns + j];
113  return *this;
114  }
115 
116  //_________________________________________________________________________________
118  {
119  const int l1 = _Lines;
120  const int c1 = _Columns;
121  const int l2 = term.GetLines();
122  const int c2 = term.GetColumns();
123  if (c1 != l2)
124  throw std::runtime_error("[Matrix::operator *=]: Lines or Columns don't match multiplying the two matrices.");
125 
126  const std::vector<T> v = term.GetVector();
127  std::vector<T> out(l1 * c2, 0.);
128  for (int i = 0; i < l1; i++)
129  for (int j = 0; j < c2; j++)
130  for (int k = 0; k < c1; k++)
131  out[i * c2 + j] += _Matrix[i * c1 + k] * v[k * c2 + j];
132 
133  _Lines = l1;
134  _Columns = c2;
135  _Matrix = out;
136  return *this;
137  }
138 
139  //_________________________________________________________________________________
140  Matrix<T> operator *= (T const& coef)
141  {
142  for (int i = 0; i < _Lines; i++)
143  for (int j = 0; j < _Columns; j++)
144  _Matrix[i * _Columns + j] *= coef;
145  return *this;
146  }
147 
148  //_________________________________________________________________________________
149  Matrix<T> operator /= (T const& coef)
150  {
151  for (int i = 0; i < _Lines; i++)
152  for (int j = 0; j < _Columns; j++)
153  _Matrix[i * _Columns + j] /= coef;
154  return *this;
155  }
156 
157  //_________________________________________________________________________________
158  Matrix<T> operator *= (double const& coef)
159  {
160  for (int i = 0; i < _Lines; i++)
161  for (int j = 0; j < _Columns; j++)
162  _Matrix[i * _Columns + j] *= coef;
163  return *this;
164  }
165 
166  //_________________________________________________________________________________
167  Matrix<T> operator /= (double const& coef)
168  {
169  for (int i = 0; i < _Lines; i++)
170  for (int j = 0; j < _Columns; j++)
171  _Matrix[i * _Columns + j] /= coef;
172  return *this;
173  }
174 
175  private:
176  int _Lines;
177  int _Columns;
178  std::vector<T> _Matrix;
179 
180  template<class Y>
181  friend std::ostream& operator << (std::ostream& os, Matrix<Y> const& sg);
182  };
183 
184  //_________________________________________________________________________________
185  std::ostream& operator << (std::ostream& os, Matrix<std::complex<double>> const& m);
186 
187  //_________________________________________________________________________
188  template<class T>
190  {
191  return lhs += rhs;
192  }
193 
194  //_________________________________________________________________________
195  template<class T>
197  {
198  return lhs -= rhs;
199  }
200 
201  //_________________________________________________________________________
202  template<class T>
204  {
205  return lhs *= rhs;
206  }
207 
208  //_________________________________________________________________________
209  template<class T>
210  Matrix<T> operator * (Matrix<T> lhs, T const& rhs)
211  {
212  return lhs *= rhs;
213  }
214 
215  //_________________________________________________________________________
216  template<class T>
217  Matrix<T> operator * (T const& lhs, Matrix<T> rhs)
218  {
219  return rhs *= lhs;
220  }
221 
222  //_________________________________________________________________________
223  template<class T>
224  Matrix<T> operator / (Matrix<T> lhs, T const& rhs)
225  {
226  return lhs /= rhs;
227  }
228 
229  //_________________________________________________________________________
230  template<class T>
231  Matrix<T> operator / (T const& lhs, Matrix<T> rhs)
232  {
233  return rhs /= lhs;
234  }
235 
236  //_________________________________________________________________________
237  template<class T>
238  Matrix<T> operator * (Matrix<T> lhs, double const& rhs)
239  {
240  return lhs *= rhs;
241  }
242 
243  //_________________________________________________________________________
244  template<class T>
245  Matrix<T> operator * (double const& lhs, Matrix<T> rhs)
246  {
247  return rhs *= lhs;
248  }
249 
250  //_________________________________________________________________________
251  template<class T>
252  Matrix<T> operator / (Matrix<T> lhs, double const& rhs)
253  {
254  return lhs /= rhs;
255  }
256 
257  //_________________________________________________________________________
258  template<class T>
259  Matrix<T> operator / (double const& lhs, Matrix<T> rhs)
260  {
261  return rhs /= lhs;
262  }
264 }
ePDF::Matrix::_Matrix
std::vector< T > _Matrix
Definition: matrix.h:178
ePDF::operator-
Matrix< T > operator-(Matrix< T > lhs, Matrix< T > const &rhs)
Definition: matrix.h:196
ePDF::Matrix::Matrix
Matrix(int const &Lines=0, int const &Columns=0, std::vector< T > const &Entries={})
Definition: matrix.h:23
ePDF::Matrix::GetElement
T GetElement(int const &i, int const &j) const
Definition: matrix.h:46
ePDF::Matrix::operator/=
Matrix< T > operator/=(T const &coef)
Definition: matrix.h:149
ePDF::operator/
Matrix< T > operator/(Matrix< T > lhs, T const &rhs)
Definition: matrix.h:224
ePDF::Matrix::GetColumns
int GetColumns() const
Definition: matrix.h:70
ePDF::Matrix::GetLines
int GetLines() const
Definition: matrix.h:64
ePDF::Matrix::operator*=
Matrix< T > operator*=(Matrix< T > const &term)
Definition: matrix.h:117
ePDF::Matrix::_Lines
int _Lines
Definition: matrix.h:176
ePDF::Matrix::GetVector
std::vector< T > GetVector() const
Definition: matrix.h:76
ePDF::operator*
Matrix< T > operator*(Matrix< T > lhs, Matrix< T > const &rhs)
Definition: matrix.h:203
ePDF::Matrix::SetElement
void SetElement(int const &i, int const &j, T const &value)
Definition: matrix.h:34
ePDF::Matrix::operator<<
friend std::ostream & operator<<(std::ostream &os, Matrix< Y > const &sg)
ePDF::operator<<
std::ostream & operator<<(std::ostream &os, Matrix< std::complex< double >> const &m)
ePDF::Matrix::operator-=
Matrix< T > operator-=(Matrix< T > const &term)
Definition: matrix.h:104
ePDF::Matrix::_Columns
int _Columns
Definition: matrix.h:177
ePDF::Matrix::operator()
T operator()(int const &i, int const &j) const
Definition: matrix.h:58
ePDF::operator+
Matrix< T > operator+(Matrix< T > lhs, Matrix< T > const &rhs)
Definition: matrix.h:189
ePDF::Matrix::operator=
Matrix< T > operator=(Matrix< T > const &term)
Definition: matrix.h:82
ePDF
Definition: alphaem.h:12
ePDF::Matrix::operator+=
Matrix< T > operator+=(Matrix< T > const &term)
Definition: matrix.h:91
ePDF::Matrix
Definition: matrix.h:19