yat  0.16.4pre
MatrixExpression.h
1 #ifndef _theplu_yat_utility_matrix_expression
2 #define _theplu_yat_utility_matrix_expression
3 
4 // $Id: MatrixExpression.h 3656 2017-07-13 00:37:26Z 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 
76 #ifdef YAT_HAVE_RVALUE
79  : m_(NULL)
80  {
81  std::swap(m_, other.m_);
82  }
83 #endif
84 
88  size_t rows(void) const
89  { return static_cast<const Derived*>(this)->rows(); }
90 
91 
95  size_t columns(void) const
96  { return static_cast<const Derived*>(this)->columns();}
97 
98 
102  double operator()(size_t row, size_t column) const
103  { return (*static_cast<const Derived*>(this))(row, column); }
104 
105 
112  void get(gsl_matrix*& m) const
113  {
114  static_cast<const Derived*>(this)->calculate_matrix(m);
115  }
116 
117 
123  void move(gsl_matrix*& m)
124  {
125  // If we already have m_, just steal it.
126  if (m_) {
127  YAT_ASSERT(m_ != m);
128  std::swap(m_, m);
129  return;
130  }
131  // Since *this is not supposed to be used after calling this
132  // function, there is no reason to store access matrix in m_.
133  get(m);
134  }
135 
136 
140  const gsl_matrix* gsl_matrix_p(void) const
141  {
142  if (!m_)
143  this->get(m_);
144  return m_;
145  }
146 
147 
148 
152  gsl_matrix* gsl_matrix_p(void)
153  {
154  if (!m_)
155  this->get(m_);
156  return m_;
157  }
158 
159 
160  protected:
164  mutable gsl_matrix* m_; // lazy eval
165 
166  private:
167  // assignment not allowed
168  MatrixExpression& operator=(const MatrixExpression& other);
169  };
170 
171 }}} // of namespace utility, yat, and theplu
172 
173 #endif
size_t rows(void) const
Definition: MatrixExpression.h:88
The Department of Theoretical Physics namespace as we define it.
MatrixExpression(const MatrixExpression &other)
Cop constructor.
Definition: MatrixExpression.h:70
double operator()(size_t row, size_t column) const
Definition: MatrixExpression.h:102
const gsl_matrix * gsl_matrix_p(void) const
Definition: MatrixExpression.h:140
size_t columns(void) const
Definition: MatrixExpression.h:95
gsl_matrix * gsl_matrix_p(void)
Definition: MatrixExpression.h:152
void move(gsl_matrix *&m)
Definition: MatrixExpression.h:123
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
Derived derived_type
Definition: MatrixExpression.h:53
gsl_matrix * m_
Definition: MatrixExpression.h:164

Generated on Thu Dec 12 2019 03:12:08 for yat by  doxygen 1.8.11