ug4
vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__CPU_ALGEBRA__VECTOR__
34 #define __H__UG__CPU_ALGEBRA__VECTOR__
35 
36 #include "sparsematrix.h"
37 
38 #include "../common/template_expressions.h"
39 #include "../common/operations.h"
41 #include <vector>
42 //#include "../vector_interface/ivector.h"
43 
44 namespace ug{
46 // Vector
48 
51 
53 template <typename TValueType>
54 class Vector //: public IVector
55 {
56 public:
57  typedef TValueType value_type;
58  //typedef subvector<value_type> subvector_type;
60 
61 // IVECTOR_TO_VEC_FUNCTIONS(vector_type)
62 
64  Vector();
65 
67  Vector(size_t size);
68 
70  virtual ~Vector();
71 
72  Vector(const vector_type & v)
73  {
74  m_capacity = 0;
75  m_size = 0; values = NULL;
76  create(v.m_size);
77  operator =(v);
78  }
79 
80 public:
82  void create(size_t size);
84  void create(const Vector &v);
85 
88 
91 
96  void resize_sloppy(size_t newSize, bool bCopyValues=true);
97 
99  void resize_exactly(size_t newSize, bool bCopyValues=true);
100 
103  void reserve_exactly(size_t newCapacity, bool bCopyValues);
104 
107  void reserve_sloppy(size_t newCapacity, bool bCopyValues=true);
108 
109  void resize(size_t newSize, bool bCopyValues=true)
110  {
111  resize_exactly(newSize, bCopyValues);
112  }
113  void reserve(size_t newCapacity, bool bCopyValues=true)
114  {
115  reserve_exactly(newCapacity, bCopyValues);
116  }
117 
118  size_t capacity() const
119  {
120  return m_capacity;
121  }
122 
124  inline value_type &operator [] (size_t i);
125  inline const value_type &operator [] (size_t i) const;
126 
127 
129  double dotprod(const Vector &w); //const;
130 
131  // deprecated, use x.T() * y.
132  //inline double operator *(const Vector &w); ///< shortcut for .dotprod(w)
133 
134  //double energynorm2(const SparseMatrix &A) const;
135  /*double energynorm(const SparseMatrix &A) const
136  {
137  return sqrt(energynorm2(A));
138  }*/
139 
141  double operator = (double d);
143  void set(double d) { operator = (d);}
144  void set_random(double from, double to);
145 
153  template <typename V> void add(const V& u);
154  template <typename V> void set(const V& u);
155  template <typename V> void get(V& u) const;
156 
157 
158  void add(const value_type *u, const size_t *indices, size_t nr);
159  void set(const value_type *u, const size_t *indices, size_t nr);
160  void get(value_type *u, const size_t *indices, size_t nr) const;
161 
162 
163  //template<typename T> inline void apply(Operation_type op, const T &t);
164 
166  inline void operator = (const Vector &v);
167  inline void operator += (const Vector &v);
168  inline void operator -= (const Vector &v);
169 
170  inline void operator *= (const number &a)
171  {
172  for(size_t i=0; i<size(); i++) values[i] *= a;
173  }
174 
176  inline double norm() const;
177 
179  inline double maxnorm() const;
180 
181  size_t size() const { return m_size; }
182 
183 
184 public: // output functions
186  void print(const char * const text = NULL) const;
187  void p() {print(); }
188 
190  friend std::ostream &operator<<(std::ostream &output, const Vector &v)
191  {
192  output << "Vector " << "[" << v.m_size << "]";
193  return output;
194  }
195 
196  size_t defragment() { return true; }
197 
198 public:
199  /*size_t begin_index() { return 0;}
200  size_t end_index() { return size();}
201 
202  value_type *begin() { return values + begin_index(); }
203  value_type *end() { return values + end_index(); }*/
204 
205 protected:
207 
212  virtual vector_type* virtual_clone() const;
213 
215 
220  virtual vector_type* virtual_clone_without_values() const;
221 
222 private:
223  void destroy();
224 
225  size_t m_size;
226  size_t m_capacity;
228 
229  //mutable vector_mode dist_mode;
230 };
231 
232 // end group cpu_algebra
234 
235 } // namespace ug
236 
237 #include "vector_impl.h"
238 
239 #endif
Definition: vector.h:55
virtual ~Vector()
virtual destructor
Definition: vector_impl.h:140
void resize_exactly(size_t newSize, bool bCopyValues=true)
resize the vector to be EXACTLY newSize big (no overhead)
Definition: vector_impl.h:230
virtual vector_type * virtual_clone_without_values() const
virtual clone using covariant return type excluding values
Definition: vector_impl.h:181
void set(double d)
assign double d to whole Vector
Definition: vector.h:143
void resize_sloppy(size_t newSize, bool bCopyValues=true)
Definition: vector_impl.h:219
size_t capacity() const
Definition: vector.h:118
void reserve_sloppy(size_t newCapacity, bool bCopyValues=true)
Definition: vector_impl.h:212
TValueType value_type
Definition: vector.h:57
friend std::ostream & operator<<(std::ostream &output, const Vector &v)
ostream << operator
Definition: vector.h:190
void operator-=(const Vector &v)
Definition: vector_impl.h:116
void set_random(double from, double to)
Definition: vector_impl.h:91
void print(const char *const text=NULL) const
print vector to console
Definition: vector_impl.h:255
void operator*=(const number &a)
Definition: vector.h:170
size_t m_capacity
size of the vector (vector is from 0..size-1)
Definition: vector.h:226
void operator+=(const Vector &v)
Definition: vector_impl.h:108
size_t defragment()
Definition: vector.h:196
SmartPtr< vector_type > clone() const
clones the vector (deep-copy) including values
Definition: vector_impl.h:175
void add(const V &u)
Definition: vector_impl.h:269
virtual vector_type * virtual_clone() const
virtual clone using covariant return type
Definition: vector_impl.h:169
double norm() const
return sqrt(sum values[i]^2) (euclidian norm)
Definition: vector_impl.h:323
void destroy()
Definition: vector_impl.h:146
void create(size_t size)
create a vector with specific size
Definition: vector_impl.h:158
double dotprod(const Vector &w)
returns v.T w, that is the dotprod of this vector and w
Definition: vector_impl.h:72
value_type & operator[](size_t i)
access element i of the vector
Definition: vector_impl.h:47
void get(V &u) const
Definition: vector_impl.h:285
void reserve_exactly(size_t newCapacity, bool bCopyValues)
Definition: vector_impl.h:193
void resize(size_t newSize, bool bCopyValues=true)
Definition: vector.h:109
double operator=(double d)
assign double d to whole Vector
Definition: vector_impl.h:83
value_type * values
array where the values are stored, size m_size
Definition: vector.h:227
void p()
gdb shortcut for print
Definition: vector.h:187
Vector(const vector_type &v)
Definition: vector.h:72
Vector< TValueType > vector_type
Definition: vector.h:59
double maxnorm() const
return max values[i] (max norm)
Definition: vector_impl.h:332
size_t size() const
Definition: vector.h:181
size_t m_size
size of the vector (vector is from 0..size-1)
Definition: vector.h:225
SmartPtr< vector_type > clone_without_values() const
clones the vector (deep-copy) excluding values
Definition: vector_impl.h:187
void reserve(size_t newCapacity, bool bCopyValues=true)
Definition: vector.h:113
Vector()
constructor
Definition: vector_impl.h:127
double number
Definition: types.h:124
CPUAlgebra::vector_type vector_type
the ug namespace
T value_type
Definition: sparsematrix_interface.h:2