yat  0.21pre
MatrixTraits.h
1 #ifndef _theplu_yat_utility_expression_matrix_traits
2 #define _theplu_yat_utility_expression_matrix_traits
3 
4 // $Id: MatrixTraits.h 3999 2020-10-08 23:22:32Z 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 
26 #include "ScaledMatrix.h"
27 #include "TransposedMatrix.h"
28 
29 #include "yat/utility/BasicMatrix.h"
30 #include "yat/utility/MatrixExpression.h"
31 
32 #include <gsl/gsl_blas_types.h>
33 #include <gsl/gsl_cblas.h>
34 
35 namespace theplu {
36 namespace yat {
37 namespace utility {
38 
40 
41  namespace expression {
42 
43  // A class that is used to de-couple a BasicMatrix<T> into a
44  // scalar factor, transpose flag and the underlying matrix.
45  template<class MATRIX>
46  class MatrixTraits
47  {
48  public:
49  typedef MATRIX base_type;
50  double factor(const MATRIX&) const { return 1.0; }
51  CBLAS_TRANSPOSE_t transpose_type(void) const { return CblasNoTrans; }
52  const gsl_matrix* get_gsl_matrix_p(const MATRIX& m) const
53  { return m.gsl_matrix_p(); }
54  private:
55  };
56 
57 
58  // Specialization for ScaledMatrix<Derived>
59  //
60  // To allow unfolding of multiple layers of Scaled and Transposed
61  // Matrix, all functions are calculated using
62  // MatrixTraits<Derived>.
63  //
64  // ScaledMatrix only affects the scale factor so all functions
65  // calls MatrixTraits<Derived> directly expect ::factor(void),
66  // which multiplies with ScaledMatrix<Derived>::factor
67  template<class Derived>
68  class MatrixTraits<BasicMatrix<MatrixExpression<ScaledMatrix<Derived> > > >
69  {
70  public:
71  typedef BasicMatrix<MatrixExpression<ScaledMatrix<Derived> > > MATRIX;
72 
73  double factor(const MATRIX& m) const
74  {
75  return static_cast<const ScaledMatrix<Derived>&>(m).factor() *
76  sub_traits_.factor(base(m));
77  }
78 
79 
80  CBLAS_TRANSPOSE_t transpose_type(void) const
81  {
82  return sub_traits_.transpose_type();
83  }
84 
85 
86  const gsl_matrix* get_gsl_matrix_p(const MATRIX& m) const
87  {
88  return sub_traits_.get_gsl_matrix_p(base(m));
89  }
90  private:
91  const Derived& base(const MATRIX& m) const
92  { return static_cast<const ScaledMatrix<Derived>&>(m).base(); }
93  MatrixTraits<Derived> sub_traits_;
94  };
95 
96 
97  // Specialization for TransposedMatrix
98  //
99  // To allow unfolding of multiple layers of Scaled and Transposed
100  // Matrix, all functions are calculated using
101  // MatrixTraits<Derived>.
102  //
103  // TransposedMatrix only affects the transpose flasg so all
104  // functions calls MatrixTraits<Derived> directly, expect
105  // ::transpose_type(void), which "negates" the value.
106  template<class Derived>
107  class MatrixTraits<BasicMatrix<MatrixExpression<TransposedMatrix<Derived> > > >
108  {
109  public:
110  typedef BasicMatrix<MatrixExpression<TransposedMatrix<Derived> > > MATRIX;
111  double factor(const MATRIX& m) const
112  { return sub_traits_.factor(base(m)); }
113 
114 
115  CBLAS_TRANSPOSE_t transpose_type(void) const
116  {
117  return sub_traits_.transpose_type() == CblasNoTrans ?
118  CblasTrans : CblasNoTrans;
119  }
120 
121 
122  const gsl_matrix* get_gsl_matrix_p(const MATRIX& m) const
123  { return sub_traits_.get_gsl_matrix_p(base(m)); }
124 
125  private:
126  const Derived& base(const MATRIX& m) const
127  { return static_cast<const TransposedMatrix<Derived>&>(m).base(); }
128  MatrixTraits<Derived> sub_traits_;
129  };
130 
131  }
132 
134 
135 }}} // end of namespace expression, utility, yat and theplu
136 #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