yat  0.21pre
VectorExpression.h
1 #ifndef _theplu_yat_utility_vector_expression
2 #define _theplu_yat_utility_vector_expression
3 
4 // $Id: VectorExpression.h 3999 2020-10-08 23:22:32Z peter $
5 
6 /*
7  Copyright (C) 2017, 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 "config_public.h"
26 
27 #include "BasicVector.h"
28 #include "MatrixExpression.h"
29 #include "yat_assert.h"
30 
31 #include <gsl/gsl_blas.h>
32 #include <gsl/gsl_matrix.h>
33 #include <gsl/gsl_vector.h>
34 
35 #include <utility>
36 
37 namespace theplu {
38 namespace yat {
39 namespace utility {
40 
66  template<class Derived>
68  : public BasicVector<VectorExpression<Derived> >
69  {
70  public:
72  typedef Derived derived_type;
73 
75  VectorExpression(void) : v_(NULL) {}
76 
79  {
81  }
82 
85  : v_(NULL)
86  {
87  if (other.v_) {
88  allocate_memory(other.size());
89  if (gsl_vector_memcpy(v_, other.v_))
90  throw GSL_error("VectorExpression: gsl_vector_memcpy failed");
91  }
92  }
93 
96  : v_(NULL)
97  {
98  std::swap(v_, other.v_);
99  }
100 
102  size_t size(void) const
103  { return static_cast<const Derived*>(this)->size(); }
104 
106  double operator()(size_t i) const
107  { return (*static_cast<const Derived*>(this))(i); }
108 
110  const gsl_vector* gsl_vector_p(void) const
111  {
112  if (!v_)
113  static_cast<const Derived*>(this)->calculate_gsl_vector_p();
114  return v_;
115  }
116 
118  gsl_vector* gsl_vector_p(void)
119  {
120  if (!v_)
121  static_cast<const Derived*>(this)->calculate_gsl_vector_p();
122  return v_;
123  }
124 
125 
127  void gsl_vector_p(gsl_vector* v)
128  {
129  v_ = v;
130  }
131 
132  protected:
135  mutable gsl_vector* v_; // lazy eval
136 
138  void allocate_memory(size_t n) const
139  {
140  v_ = gsl_vector_alloc(n);
141  if (!v_)
142  throw GSL_error("Vector: failed to allocate memory");
143  }
144 
147  {
148  gsl_vector_free(v_);
149  v_ = NULL;
150  }
151 
152  private:
153  // assignment not allowed
154  VectorExpression& operator=(const VectorExpression& rhs);
155  };
156 
157 
158 }}} // of namespace utility, yat, and theplu
159 
160 #endif
VectorExpression(void)
default constructor
Definition: VectorExpression.h:75
gsl_vector * gsl_vector_p(void)
return a mutable GSL vector pointer
Definition: VectorExpression.h:118
~VectorExpression(void)
destructor, free allocated memory
Definition: VectorExpression.h:78
Class for errors reported from underlying GSL calls.
Definition: Exception.h:102
The Department of Theoretical Physics namespace as we define it.
gsl_vector * v_
Definition: VectorExpression.h:135
void delete_allocated_memory(void)
clean up
Definition: VectorExpression.h:146
An expression that can be converted to a Vector.
Definition: VectorExpression.h:67
void allocate_memory(size_t n) const
allocate memory and throw if fails
Definition: VectorExpression.h:138
VectorExpression(const VectorExpression &other)
Cop constructor.
Definition: VectorExpression.h:84
Definition: BasicVector.h:48
Derived derived_type
Derived class.
Definition: VectorExpression.h:72
void gsl_vector_p(gsl_vector *v)
set underlying GSL vector
Definition: VectorExpression.h:127
size_t size(void) const
Definition: VectorExpression.h:102
const gsl_vector * gsl_vector_p(void) const
return a GSL vector pointer
Definition: VectorExpression.h:110
double operator()(size_t i) const
access an element
Definition: VectorExpression.h:106
VectorExpression(VectorExpression &&other)
Move constructor.
Definition: VectorExpression.h:95

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