yat  0.21pre
MatrixProduct.h
1 #ifndef _theplu_yat_utility_expression_matrix_product
2 #define _theplu_yat_utility_expression_matrix_product
3 
4 // $Id: MatrixProduct.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 "MatrixTraits.h"
26 #include "ScaledMatrix.h"
27 #include "TransposedMatrix.h"
28 
29 #include "yat/utility/BasicMatrix.h"
30 #include "yat/utility/MatrixExpression.h"
31 #include "yat/utility/yat_assert.h"
32 
33 #include <gsl/gsl_blas_types.h>
34 #include <gsl/gsl_blas.h>
35 #include <gsl/gsl_cblas.h>
36 #include <gsl/gsl_matrix.h>
37 
38 namespace theplu {
39 namespace yat {
40 namespace utility {
41 
43 
44 namespace expression {
45 
46 
47  template<class MATRIX1, class MATRIX2>
48  class MatrixProduct : public MatrixExpression<MatrixProduct<MATRIX1,MATRIX2> >
49  {
50  public:
51  MatrixProduct(const MATRIX1& A, const MATRIX2& B)
52  : A_(A), B_(B) {}
53 
54  size_t rows(void) const { return A_.rows(); }
55  size_t columns(void) const { return B_.columns(); }
56 
57  double operator()(size_t row, size_t column) const
58  { return gsl_matrix_get(this->gsl_matrix_p(), row, column); }
59 
60  void calculate_matrix(gsl_matrix*& result) const
61  {
62  detail::reallocate(result, this->rows(), this->columns());
63  YAT_ASSERT(detail::rows(result) == this->rows());
64  YAT_ASSERT(detail::columns(result) == this->columns());
65  YAT_ASSERT(A_.columns() == B_.rows());
66  MatrixTraits<MATRIX1> traits1;
67  MatrixTraits<MATRIX2> traits2;
68  gsl_blas_dgemm(traits1.transpose_type(), traits2.transpose_type(),
69  traits1.factor(A_) * traits2.factor(B_),
70  traits1.get_gsl_matrix_p(A_),
71  traits2.get_gsl_matrix_p(B_),
72  0.0, result);
73  YAT_ASSERT(result);
74  }
75  private:
76  const MATRIX1& A_;
77  const MATRIX2& B_;
78  };
79 } // end namespace expression
80 
82 
83 }}} // of namespace utility, yat, and theplu
84 
85 #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