yat  0.21pre
MatrixExpression.h
1 #ifndef _theplu_yat_utility_matrix_expression
2 #define _theplu_yat_utility_matrix_expression
3 
4 // $Id: MatrixExpression.h 3938 2020-07-16 13:16:56Z peter $
5 
6 /*
7  Copyright (C) 2017 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 "yat_assert.h"
27 
28 #include <gsl/gsl_matrix.h>
29 
30 #include <cstddef>
31 #include <utility>
32 
33 namespace theplu {
34 namespace yat {
35 namespace utility {
36 
45  template<class Derived>
47  : public BasicMatrix<MatrixExpression<Derived> >
48  {
49  public:
53  typedef Derived derived_type;
54 
58  MatrixExpression(void) : m_(NULL) {}
59 
64  {
65  detail::deallocate(m_);
66  }
67 
68 
71  : m_(NULL)
72  {
73  detail::copy(m_, other.m_);
74  }
75 
78  : m_(NULL)
79  {
80  std::swap(m_, other.m_);
81  }
82 
86  size_t rows(void) const
87  { return static_cast<const Derived*>(this)->rows(); }
88 
89 
93  size_t columns(void) const
94  { return static_cast<const Derived*>(this)->columns();}
95 
96 
100  double operator()(size_t row, size_t column) const
101  { return (*static_cast<const Derived*>(this))(row, column); }
102 
103 
110  void get(gsl_matrix*& m) const
111  {
112  static_cast<const Derived*>(this)->calculate_matrix(m);
113  }
114 
115 
121  void move(gsl_matrix*& m)
122  {
123  // If we already have m_, just steal it.
124  if (m_) {
125  YAT_ASSERT(m_ != m);
126  std::swap(m_, m);
127  return;
128  }
129  // Since *this is not supposed to be used after calling this
130  // function, there is no reason to store access matrix in m_.
131  get(m);
132  }
133 
134 
138  const gsl_matrix* gsl_matrix_p(void) const
139  {
140  if (!m_)
141  this->get(m_);
142  return m_;
143  }
144 
145 
146 
150  gsl_matrix* gsl_matrix_p(void)
151  {
152  if (!m_)
153  this->get(m_);
154  return m_;
155  }
156 
157 
158  protected:
162  mutable gsl_matrix* m_; // lazy eval
163 
164  private:
165  // assignment not allowed
166  MatrixExpression& operator=(const MatrixExpression& other);
167  };
168 
169 }}} // of namespace utility, yat, and theplu
170 
171 #endif
size_t columns(void) const
Definition: MatrixExpression.h:93
MatrixExpression(MatrixExpression &&other)
Move constructor.
Definition: MatrixExpression.h:77
The Department of Theoretical Physics namespace as we define it.
const gsl_matrix * gsl_matrix_p(void) const
Definition: MatrixExpression.h:138
MatrixExpression(const MatrixExpression &other)
Cop constructor.
Definition: MatrixExpression.h:70
gsl_matrix * gsl_matrix_p(void)
Definition: MatrixExpression.h:150
void move(gsl_matrix *&m)
Definition: MatrixExpression.h:121
An expression that can be converted to a Matrix.
Definition: MatrixExpression.h:46
~MatrixExpression(void)
Destructor.
Definition: MatrixExpression.h:63
MatrixExpression(void)
Defaulf constructor.
Definition: MatrixExpression.h:58
Definition: BasicMatrix.h:38
size_t rows(void) const
Definition: MatrixExpression.h:86
double operator()(size_t row, size_t column) const
Definition: MatrixExpression.h:100
Derived derived_type
Definition: MatrixExpression.h:53
gsl_matrix * m_
Definition: MatrixExpression.h:162

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