yat  0.21pre
MatrixBinary.h
1 #ifndef _theplu_yat_utility_expression_matrix_binary
2 #define _theplu_yat_utility_expression_matrix_binary
3 
4 // $Id: MatrixBinary.h 3908 2020-05-13 06:58:38Z peter $
5 
6 /*
7  Copyright (C) 2020 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 "yat/utility/BasicMatrix.h"
26 #include "yat/utility/BLAS_utility.h"
27 #include "yat/utility/MatrixExpression.h"
28 #include "yat/utility/yat_assert.h"
29 
30 #include <gsl/gsl_matrix.h>
31 
32 namespace theplu {
33 namespace yat {
34 namespace utility {
35 
37 
38 namespace expression {
39 
40 
41 
42  template<typename LHS, typename RHS, class OP>
43  class MatrixBinary
44  : public MatrixExpression<MatrixBinary<LHS, RHS, OP> >
45  {
46  public:
47  MatrixBinary(const BasicMatrix<LHS>& lhs, const BasicMatrix<RHS>& rhs)
48  : lhs_(lhs), rhs_(rhs)
49  {
50  }
51 
52  size_t rows(void) const { return lhs_.rows(); }
53  size_t columns(void) const { return rhs_.columns(); }
54 
55  double operator()(size_t row, size_t column) const
56  { return get(row, column, op_); }
57 
58  void calculate_matrix(gsl_matrix*& result) const
59  {
60  detail::reallocate(result, this->rows(), this->columns());
61  calculate_matrix(result, op_);
62  }
63 
64  private:
65  const BasicMatrix<LHS>& lhs_;
66  const BasicMatrix<RHS>& rhs_;
67  OP op_;
68 
69  template<class T>
70  void calculate_matrix(gsl_matrix*& result, T) const
71  {
72  YAT_ASSERT(detail::rows(result) == this->rows());
73  YAT_ASSERT(detail::columns(result) == this->columns());
74  for (size_t i=0; i<rows(); ++i)
75  for (size_t j=0; j<columns(); ++j)
76  gsl_matrix_set(result, i, j, (*this)(i, j));
77  }
78 
79 
80  double get(size_t row, size_t column, Plus) const
81  {
82  return lhs_(row, column) + rhs_(row, column);
83  }
84 
85 
86  double get(size_t row, size_t column, Minus) const
87  {
88  return lhs_(row, column) - rhs_(row, column);
89  }
90  };
91 
92 
93 } // end namespace expression
94 
96 
97 }}} // of namespace utility, yat, and theplu
98 
99 #endif
The Department of Theoretical Physics namespace as we define it.

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