ug4
densevector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-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__COMMON__DENSEVECTOR_H__
34 #define __H__UG__COMMON__DENSEVECTOR_H__
35 
36 #include <iostream>
37 #include <cassert>
38 #include "../storage/storage.h"
39 
40 namespace ug{
41 
44 
45 template<typename T>
47 {
48 public:
49  typedef typename T::value_type value_type;
50  TE_TRANSPOSED(const T&_t) : t(_t) {}
51  inline size_t num_rows() const
52  {
53  return t.num_cols();
54  }
55 
56  inline size_t num_cols() const
57  {
58  return t.num_rows();
59  }
60 
61  const value_type &operator() (size_t r, size_t c) const
62  {
63  return t(c, r);
64  }
65 
66  value_type &operator() (size_t r, size_t c)
67  {
68  return t(c, r);
69  }
70 private:
71  const T &t;
72 };
73 
74 template<typename T>
75 inline TE_TRANSPOSED<T> te_transpose(const T &t)
76 {
77  return TE_TRANSPOSED<T>(t);
78 }
79 
80 inline const double &te_transpose(const double &t)
81 {
82  return t;
83 }
84 
85 inline double &te_transpose(double &t)
86 {
87  return t;
88 }
90 // DenseVector
99 template<typename TStorage>
100 class DenseVector : public TStorage
101 {
102 public:
104  typedef typename TStorage::size_type size_type;
105 
106  // use traits so we are able to use std::vector
109 
111 
112  // use the interface of TStorage
113  typedef TStorage base;
114  using base::operator [];
115  using base::at;
116  using base::size;
117  using base::resize;
118 
119 public:
120  // constructors
121  DenseVector(double alpha=0.0);
123 
124  // ~DenseVector(); // dont implement a destructor, since ~base may not be virtual
125 
126  // operations with vectors
127  inline this_type &
128  operator= (const this_type &rhs);
129 
130 
131  inline this_type &
132  operator+= (const this_type &rhs);
133 
134  inline this_type &
135  operator-= (const this_type &rhs);
136 
137 
138  // operations with scalars
139  template<typename T>
140  inline this_type&
141  operator= (const T& alpha);
142 
143  template<typename T>
144  inline this_type&
145  operator= (const double & alpha)
146  {
147  for(size_t i=0; i<size(); i++)
148  entry(i) = alpha;
149  return *this;
150  }
151 
152  inline this_type&
154 
155  inline this_type&
157 
158  template<typename T>
159  inline this_type&
160  operator *= (const T &alpha);
161 
162  inline this_type&
163  operator/= (const value_type& alpha);
164 
165 
166  bool operator > (double d) const
167  {
168  for(size_t i=0; i<size(); i++)
169  if(entry(i) <= d) return false;
170  return true;
171  }
172 
173  bool operator < (double d) const
174  {
175  for(size_t i=0; i<size(); i++)
176  if(entry(i) >= d) return false;
177  return true;
178  }
179 
180  bool operator >= (double d) const
181  {
182  for(size_t i=0; i<size(); i++)
183  if(entry(i) < d) return false;
184  return true;
185  }
186 
187  bool operator <= (double d) const
188  {
189  for(size_t i=0; i<size(); i++)
190  if(entry(i) > d) return false;
191  return true;
192  }
193  bool operator == (double d) const
194  {
195  for(size_t i=0; i<size(); i++)
196  if(entry(i) != d) return false;
197  return true;
198  }
199 
200  bool operator != (double d) const
201  {
202  for(size_t i=0; i<size(); i++)
203  if(entry(i) != d) return true;
204  return false;
205  }
207  // this will be removed soon
208 
209 
210  this_type operator + (const this_type &other ) const
211  {
212  UG_ASSERT(size() == other.size(), "");
213  this_type erg;
214  erg.resize(size());
215  for(size_t i=0; i<size(); i++)
216  erg[i] = entry(i) + other[i];
217  return erg;
218  }
219 
220  this_type operator - (const this_type &other ) const
221  {
222  UG_ASSERT(size() == other.size(), "");
223  this_type erg;
224  erg.resize(size());
225  for(size_t i=0; i<size(); i++)
226  erg[i] = entry(i) - other[i];
227  return erg;
228  }
229 
230 // multiply
231  this_type operator * (double alpha ) const
232  {
233  this_type erg;
234  erg.resize(size());
235  for(size_t i=0; i<size(); i++)
236  erg[i] = alpha*entry(i);
237  return erg;
238  }
239 
241  {
242  this_type erg;
243  erg.resize(size());
244  for(size_t i=0; i<size(); i++)
245  erg[i] *= -1.0;
246  return erg;
247  }
248 
250 
251  inline const value_type &entry(size_t i) const
252  {
253  return operator[] (i);
254  }
255  inline value_type &entry(size_t i)
256  {
257  return operator[] (i);
258  }
259 
260  template<typename Type>
262  assign(const Type &t);
263 
264  void maple_print(const char *name);
265 
266 
267  inline size_t num_rows() const
268  {
269  return size();
270  }
271 
272  inline size_t num_cols() const
273  {
274  return 1;
275  }
276 
277  inline const value_type &operator() (size_t r, size_t c) const
278  {
279  UG_ASSERT(c==0, "vector only has one column");
280  return entry(r);
281  }
282 
283  inline value_type &operator() (size_t r, size_t c)
284  {
285  UG_ASSERT(c==0, "vector only has one column");
286  return entry(r);
287  }
288 
289  void
290  subassign(size_t i, const value_type &t)
291  {
292  entry(i) = t;
293  }
294 
295  template<typename T2>
296  void
297  subassign(size_t i, const T2 &t)
298  {
299  UG_ASSERT(i+t.size() <= size(), "");
300  for(size_t i1=0; i1<t.size(); i1++)
301  entry(i+i1) = t[i1];
302  }
303 };
304 
305 template<typename TStorage>
306 inline
307 DenseVector<TStorage>
309 {
310  return vec*alpha;
311 }
312 
313 template<typename TStorage>
314 inline
315 bool
317 {
318  return vec < alpha;
319 }
320 template<typename TStorage>
321 inline
322 bool
323 operator < (double alpha, const DenseVector<TStorage> &vec)
324 {
325  return vec > alpha;
326 }
327 
328 template<typename TStorage>
329 inline
330 bool
332 {
333  return vec <= alpha;
334 }
335 
336 template<typename TStorage>
337 inline
338 bool
339 operator <= (double alpha, const DenseVector<TStorage> &vec)
340 {
341  return vec >= alpha;
342 }
343 
344 // end group small_algebra
346 
347 }
348 #include "densevector_impl.h"
349 
350 #endif // __H__UG__COMMON__DENSEVECTOR_H__
location name
Definition: checkpoint_util.lua:128
Definition: densevector.h:101
@ static_size
Definition: densevector.h:108
this_type & operator-=(const this_type &rhs)
Definition: densevector_impl.h:78
this_type & operator=(const this_type &rhs)
Definition: densevector_impl.h:55
size_t num_rows() const
Definition: densevector.h:267
DenseVector(double alpha=0.0)
Definition: densevector_impl.h:42
this_type operator+(const this_type &other) const
Definition: densevector.h:210
value_type & entry(size_t i)
Definition: densevector.h:255
bool operator<(double d) const
Definition: densevector.h:173
const value_type & operator()(size_t r, size_t c) const
Definition: densevector.h:277
bool operator==(double d) const
Definition: densevector.h:193
@ is_static
Definition: densevector.h:107
bool operator!=(double d) const
Definition: densevector.h:200
this_type & operator*=(const T &alpha)
size_t num_cols() const
Definition: densevector.h:272
this_type operator*(double alpha) const
Definition: densevector.h:231
void maple_print(const char *name)
Definition: densevector_impl.h:137
void subassign(size_t i, const T2 &t)
Definition: densevector.h:297
bool operator>(double d) const
Definition: densevector.h:166
DenseVector< TStorage > this_type
Definition: densevector.h:110
DenseVector< TStorage > & assign(const Type &t)
Definition: densevector_impl.h:168
const value_type & entry(size_t i) const
Definition: densevector.h:251
bool operator<=(double d) const
Definition: densevector.h:187
void subassign(size_t i, const value_type &t)
Definition: densevector.h:290
TStorage base
Definition: densevector.h:113
bool operator>=(double d) const
Definition: densevector.h:180
this_type & operator/=(const value_type &alpha)
Definition: densevector_impl.h:128
TStorage::size_type size_type
Definition: densevector.h:104
TStorage::value_type value_type
Definition: densevector.h:103
this_type operator-() const
Definition: densevector.h:240
this_type & operator+=(const this_type &rhs)
Definition: densevector_impl.h:68
Definition: densevector.h:47
const T & t
Definition: densevector.h:71
size_t num_rows() const
Definition: densevector.h:51
size_t num_cols() const
Definition: densevector.h:56
TE_TRANSPOSED(const T &_t)
Definition: densevector.h:50
T::value_type value_type
Definition: densevector.h:49
const value_type & operator()(size_t r, size_t c) const
Definition: densevector.h:61
bool operator<=(double alpha, const DenseVector< TStorage > &vec)
Definition: densevector.h:339
bool operator>(double alpha, const DenseVector< TStorage > &vec)
Definition: densevector.h:316
bool operator>=(double alpha, const DenseVector< TStorage > &vec)
Definition: densevector.h:331
TE_TRANSPOSED< T > te_transpose(const T &t)
Definition: densevector.h:75
number alpha
PrandtlReuss< TDomain > this_type
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
bool operator<(const MathVector< N, T > &v, const MathVector< N, T > &w)
Definition: math_vector.h:541
the ug namespace
MatVec_Expression< L, R > operator*(const AlphaMat_Expression< L > &l, const R &r)
create a MatVec_Expression by (alpha*MATRIX) * VECTOR
Definition: template_expressions.h:223
T value_type
Definition: sparsematrix_interface.h:2
bool resize(size_t newRows, size_t newCols)
Definition: storage.h:58