yat  0.21pre
DiagonalMatrix.h
1 #ifndef theplu_yat_utility_diagonal_matrix
2 #define theplu_yat_utility_diagonal_matrix
3 
4 // $Id: DiagonalMatrix.h 4207 2022-08-26 04:36:28Z peter $
5 
6 /*
7  Copyright (C) 2017, 2021, 2022 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "BasicMatrix.h"
26 #include "BasicVector.h"
27 #include "BLAS_utility.h"
28 
29 #include "MatrixExpression.h"
30 #include "VectorExpression.h"
31 
32 #include "Vector.h"
33 #include "yat_assert.h"
34 
35 namespace theplu {
36 namespace yat {
37 namespace utility {
38 
39  class MatrixBase;
40 
50  {
51  public:
55  DiagonalMatrix(void);
56 
60  DiagonalMatrix(size_t row, size_t column, double x=0.0);
61 
65  explicit DiagonalMatrix(const VectorBase& diagonal);
66 
71  explicit DiagonalMatrix(const MatrixBase& m);
72 
76  size_t rows(void) const;
77 
81  size_t columns(void) const;
82 
86  const double operator()(size_t row, size_t col) const;
87 
93  double& operator()(size_t i);
94 
103 
112 
121  private:
122  Vector data_;
123  size_t row_;
124  size_t col_;
125  };
126 
128 
129  // expression classes
130  namespace expression
131  {
132  template<typename T>
133  class DiagonalMatrixMatrix :
134  public MatrixExpression<DiagonalMatrixMatrix<T> >
135  {
136  public:
137  DiagonalMatrixMatrix(const DiagonalMatrix& lhs,const BasicMatrix<T>& rhs)
138  : lhs_(lhs), rhs_(rhs) {}
139  size_t rows(void) const { return lhs_.rows(); }
140  size_t columns(void) const { return rhs_.columns(); }
141  double operator()(size_t i, size_t j) const
142  { return lhs_(i, i) * rhs_(i, j); }
143 
144  void calculate_matrix(gsl_matrix*& result) const
145  {
146  detail::reallocate(result, rows(), columns());
147  for (size_t i=0; i<rows(); ++i)
148  for (size_t j=0; j<columns(); ++j)
149  gsl_matrix_set(result, i, j, (*this)(i, j));
150  }
151 
152  private:
153  const DiagonalMatrix& lhs_;
154  const BasicMatrix<T>& rhs_;
155  };
156 
157 
158  template<class T>
159  class DiagonalMatrixVector
160  : public VectorExpression<DiagonalMatrixVector<T> >
161  {
162  public:
163  DiagonalMatrixVector(const DiagonalMatrix& lhs, const BasicVector<T>& rhs)
164  : m_(lhs), vec_(rhs), size_(lhs.rows())
165  {
166  }
167 
168  DiagonalMatrixVector(const BasicVector<T>& lhs, const DiagonalMatrix& rhs)
169  : m_(rhs), vec_(lhs), size_(rhs.columns())
170  {
171  }
172 
173  double operator()(size_t i) const
174  {
175  YAT_ASSERT(i < size());
176  return m_(i, i) * vec_(i);
177  }
178 
179 
180  size_t size(void) const
181  {
182  return size_;
183  }
184 
185 
186  void calculate_gsl_vector_p(void) const
187  {
188  this->allocate_memory(size_);
189  for (size_t i=0; i<size_; ++i)
190  gsl_vector_set(this->v_, i, (*this)(i));
191  }
192  private:
193  const DiagonalMatrix& m_;
194  const BasicVector<T>& vec_;
195  size_t size_;
196  };
197 
198 
199 
200  template<typename T>
201  class MatrixDiagonalMatrix :
202  public MatrixExpression<MatrixDiagonalMatrix<T> >
203  {
204  public:
205  MatrixDiagonalMatrix(const BasicMatrix<T>& lhs, const DiagonalMatrix& rhs)
206  : lhs_(lhs), rhs_(rhs) {}
207  size_t rows(void) const { return lhs_.rows(); }
208  size_t columns(void) const { return rhs_.columns(); }
209  double operator()(size_t i, size_t j) const
210  { return lhs_(i, j) * rhs_(j, j); }
211 
212  void calculate_matrix(gsl_matrix*& result) const
213  {
214  detail::reallocate(result, rows(), columns());
215  for (size_t i=0; i<rows(); ++i)
216  for (size_t j=0; j<columns(); ++j)
217  gsl_matrix_set(result, i, j, (*this)(i, j));
218  }
219 
220  private:
221  const BasicMatrix<T>& lhs_;
222  const DiagonalMatrix& rhs_;
223  };
224 
225 
226  template<typename T>
227  class VectorDiagonalMatrix :
228  public BasicVector<VectorDiagonalMatrix<T> >
229  {
230  public:
231  VectorDiagonalMatrix(const BasicVector<T>& rhs, const DiagonalMatrix& lhs)
232  : lhs_(lhs), rhs_(rhs) {}
233 
234  double operator()(size_t i) const
235  {
236  YAT_ASSERT(i < size());
237  return lhs_(i) * rhs_(i, i);
238  }
239 
240  size_t size(void) const
241  {
242  return rhs_.columns();
243  }
244 
245 
246  const gsl_vector* gsl_vector_p(void) const
247  {
248  size_t n = size();
249  gsl_vector* vec = detail::create_gsl_vector(n);
250  for (size_t i=0; i<n; ++i)
251  gsl_vector_set(vec, i, (*this)(i));
252  return vec;
253  }
254 
255  private:
256  const BasicVector<T>& lhs_;
257  const DiagonalMatrix& rhs_;
258  };
259 
260 
261  template<class M1, class M2, class OP>
262  class DiagonalMatrix
263  : public MatrixExpression<DiagonalMatrix<M1, M2, OP> >
264  {
265  public:
266  DiagonalMatrix(const M1& lhs, const M2& rhs)
267  : lhs_(lhs), rhs_(rhs)
268  {
269  YAT_ASSERT(lhs.rows() == rhs.rows());
270  YAT_ASSERT(lhs.columns() == rhs.columns());
271  }
272  double operator()(size_t i, size_t j) const
273  {
274  return get(i, j, OP());
275  }
276  size_t rows(void) const { return lhs_.rows(); }
277  size_t columns(void) const { return rhs_.columns(); }
278  void calculate_matrix(gsl_matrix*& m) const
279  {
280  detail::reallocate(m, rows(), columns());
281  for (size_t i=0; i<rows(); ++i)
282  for (size_t j=0; j<columns(); ++j)
283  gsl_matrix_set(m, i, j, (*this)(i,j));
284  }
285 
286  private:
287  const M1& lhs_;
288  const M2& rhs_;
289 
290  double get(size_t i, size_t j, Plus) const
291  { return lhs_(i, j) + rhs_(i, j); }
292 
293  double get(size_t i, size_t j, Minus) const
294  { return lhs_(i, j) - rhs_(i, j); }
295  };
296 
297  } // end of namespace expression
298 
300 
301 
311  DiagonalMatrix operator*(const DiagonalMatrix& lhs,
312  const DiagonalMatrix& rhs);
313 
321  template<class T>
322  expression::DiagonalMatrixMatrix<T>
323  operator*(const DiagonalMatrix& lhs, const BasicMatrix<T>& rhs)
324  {
325  YAT_ASSERT(lhs.columns() == rhs.rows());
326  return expression::DiagonalMatrixMatrix<T>(lhs, rhs);
327  }
328 
329 
337  template<class T>
338  expression::MatrixDiagonalMatrix<T>
339  operator*(const BasicMatrix<T>& lhs, const DiagonalMatrix& rhs)
340  {
341  YAT_ASSERT(lhs.columns() == rhs.rows());
342  return expression::MatrixDiagonalMatrix<T>(lhs, rhs);
343  }
344 
345 
354  operator+(const DiagonalMatrix& lhs, const DiagonalMatrix& rhs);
355 
356 
364  template<class T>
365  expression::DiagonalMatrix<DiagonalMatrix, BasicMatrix<T>, expression::Plus>
366  operator+(const DiagonalMatrix& lhs, const BasicMatrix<T>& rhs)
367  {
368  YAT_ASSERT(lhs.rows() == rhs.rows());
369  YAT_ASSERT(lhs.columns() == rhs.columns());
370  return expression::DiagonalMatrix<DiagonalMatrix, BasicMatrix<T>,
371  expression::Plus>(lhs, rhs);
372  }
373 
374 
382  template<class T>
383  expression::DiagonalMatrix<BasicMatrix<T>, DiagonalMatrix, expression::Plus>
384  operator+(const BasicMatrix<T>& lhs, const DiagonalMatrix& rhs)
385  {
386  YAT_ASSERT(lhs.rows() == rhs.rows());
387  YAT_ASSERT(lhs.columns() == rhs.columns());
388  return expression::DiagonalMatrix<BasicMatrix<T>, DiagonalMatrix,
389  expression::Plus>(lhs, rhs);
390  }
391 
392 
401  operator-(const DiagonalMatrix& lhs, const DiagonalMatrix& rhs);
402 
403 
411  template<class T>
412  expression::DiagonalMatrix<DiagonalMatrix, BasicMatrix<T>, expression::Minus>
413  operator-(const DiagonalMatrix& lhs, const BasicMatrix<T>& rhs)
414  {
415  YAT_ASSERT(lhs.rows() == rhs.rows());
416  YAT_ASSERT(lhs.columns() == rhs.columns());
417  return expression::DiagonalMatrix<DiagonalMatrix, BasicMatrix<T>,
418  expression::Minus>(lhs, rhs);
419  }
420 
421 
429  template<class T>
430  expression::DiagonalMatrix<BasicMatrix<T>, DiagonalMatrix, expression::Minus>
431  operator-(const BasicMatrix<T>& lhs, const DiagonalMatrix& rhs)
432  {
433  YAT_ASSERT(lhs.rows() == rhs.rows());
434  YAT_ASSERT(lhs.columns() == rhs.columns());
435  return expression::DiagonalMatrix<BasicMatrix<T>, DiagonalMatrix,
436  expression::Minus>(lhs, rhs);
437  }
438 
439 
447  template<class T>
448  expression::DiagonalMatrixVector<T>
449  operator*(const DiagonalMatrix& lhs, const BasicVector<T>& rhs)
450  {
451  YAT_ASSERT(lhs.columns() == rhs.size());
452  return expression::DiagonalMatrixVector<T>(lhs, rhs);
453  }
454 
455 
463  template<class T>
464  expression::DiagonalMatrixVector<T>
465  operator*(const BasicVector<T>& lhs, const DiagonalMatrix& rhs)
466  {
467  YAT_ASSERT(lhs.size() == rhs.rows());
468  return expression::DiagonalMatrixVector<T>(lhs, rhs);
469  }
470 
471 
472 }}}
473 #endif
Definition: MatrixBase.h:54
size_t size(void) const
Definition: BasicVector.h:71
expression::ScaledMatrix< T > operator*(const BasicMatrix< MatrixExpression< expression::ScaledMatrix< T > > > &A, double k)
Definition: BLAS_level3.h:162
expression::MatrixDiagonalMatrix< T > operator*(const BasicMatrix< T > &lhs, const DiagonalMatrix &rhs)
matrix matrix multiplication
Definition: DiagonalMatrix.h:339
The Department of Theoretical Physics namespace as we define it.
DiagonalMatrix & operator-=(const DiagonalMatrix &rhs)
Subtract and assign operator.
expression::DiagonalMatrixVector< T > operator*(const BasicVector< T > &lhs, const DiagonalMatrix &rhs)
vector matrix multiplication
Definition: DiagonalMatrix.h:465
size_t columns(void) const
Definition: BasicMatrix.h:67
expression::DiagonalMatrix< DiagonalMatrix, BasicMatrix< T >, expression::Minus > operator-(const DiagonalMatrix &lhs, const BasicMatrix< T > &rhs)
matrix matrix subtraction
Definition: DiagonalMatrix.h:413
expression::DiagonalMatrix< BasicMatrix< T >, DiagonalMatrix, expression::Minus > operator-(const BasicMatrix< T > &lhs, const DiagonalMatrix &rhs)
matrix matrix subtraction
Definition: DiagonalMatrix.h:431
This is the yat interface to GSL vector.
Definition: Vector.h:59
Definition: BLAS_utility.h:31
This is the yat interface to GSL vector.
Definition: VectorBase.h:55
const double operator()(size_t row, size_t col) const
size_t rows(void) const
Definition: BasicMatrix.h:61
An expression that can be converted to a Matrix.
Definition: MatrixExpression.h:46
Definition: BasicVector.h:48
Definition: BLAS_utility.h:30
expression::DiagonalMatrixMatrix< T > operator*(const DiagonalMatrix &lhs, const BasicMatrix< T > &rhs)
matrix matrix multiplication
Definition: DiagonalMatrix.h:323
Definition: BasicMatrix.h:38
DiagonalMatrix(void)
Default constructor.
DiagonalMatrix & operator*=(const DiagonalMatrix &rhs)
Multiplication and assign operator.
expression::DiagonalMatrixVector< T > operator*(const DiagonalMatrix &lhs, const BasicVector< T > &rhs)
matrix vector multiplication
Definition: DiagonalMatrix.h:449
expression::DiagonalMatrix< DiagonalMatrix, BasicMatrix< T >, expression::Plus > operator+(const DiagonalMatrix &lhs, const BasicMatrix< T > &rhs)
matrix matrix addition
Definition: DiagonalMatrix.h:366
Diagonal Matrix.
Definition: DiagonalMatrix.h:49
DiagonalMatrix & operator+=(const DiagonalMatrix &rhs)
Add and assign operator.
expression::ScaledMatrix< T > operator-(const BasicMatrix< MatrixExpression< expression::ScaledMatrix< T > > > &A)
Definition: BLAS_level3.h:194
expression::DiagonalMatrix< BasicMatrix< T >, DiagonalMatrix, expression::Plus > operator+(const BasicMatrix< T > &lhs, const DiagonalMatrix &rhs)
matrix matrix addition
Definition: DiagonalMatrix.h:384

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14