yat  0.16.4pre
VectorExpression.h
1 #ifndef _theplu_yat_utility_vector_expression
2 #define _theplu_yat_utility_vector_expression
3 
4 // $Id: VectorExpression.h 3606 2017-01-27 05:37:37Z 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 "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 
94 #ifdef YAT_HAVE_RVALUE
97  : v_(NULL)
98  {
99  std::swap(v_, other.v_);
100  }
101 #endif
102 
104  size_t size(void) const
105  { return static_cast<const Derived*>(this)->size(); }
106 
108  double operator()(size_t i) const
109  { return (*static_cast<const Derived*>(this))(i); }
110 
112  const gsl_vector* gsl_vector_p(void) const
113  {
114  if (!v_)
115  static_cast<const Derived*>(this)->calculate_gsl_vector_p();
116  return v_;
117  }
118 
120  gsl_vector* gsl_vector_p(void)
121  {
122  if (!v_)
123  static_cast<const Derived*>(this)->calculate_gsl_vector_p();
124  return v_;
125  }
126 
127 
129  void gsl_vector_p(gsl_vector* v)
130  {
131  v_ = v;
132  }
133 
134  protected:
137  mutable gsl_vector* v_; // lazy eval
138 
140  void allocate_memory(size_t n) const
141  {
142  v_ = gsl_vector_alloc(n);
143  if (!v_)
144  throw GSL_error("Vector: failed to allocate memory");
145  }
146 
149  {
150  if (v_)
151  gsl_vector_free(v_);
152  v_ = NULL;
153  }
154 
155  private:
156  // assignment not allowed
157  VectorExpression& operator=(const VectorExpression& rhs);
158  };
159 
160 
161 }}} // of namespace utility, yat, and theplu
162 
163 #endif
VectorExpression(void)
default constructor
Definition: VectorExpression.h:75
gsl_vector * gsl_vector_p(void)
return a mutable GSL vector pointer
Definition: VectorExpression.h:120
size_t size(void) const
Definition: VectorExpression.h:104
~VectorExpression(void)
destructor, free allocated memory
Definition: VectorExpression.h:78
Class for errors reported from underlying GSL calls.
Definition: Exception.h:87
The Department of Theoretical Physics namespace as we define it.
gsl_vector * v_
Definition: VectorExpression.h:137
void allocate_memory(size_t n) const
allocate memory and throw if fails
Definition: VectorExpression.h:140
void delete_allocated_memory(void)
clean up
Definition: VectorExpression.h:148
const gsl_vector * gsl_vector_p(void) const
return a GSL vector pointer
Definition: VectorExpression.h:112
double operator()(size_t i) const
access an element
Definition: VectorExpression.h:108
An expression that can be converted to a Vector.
Definition: VectorExpression.h:67
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:129

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