Line data Source code
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 :
42 : /// \addtogroup small_algebra
43 : /// \{
44 :
45 : template<typename T>
46 : class TE_TRANSPOSED
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 : }
89 : /////////////////////////////////////////////////////////////////////////////////////////////
90 : // DenseVector
91 : /**
92 : * DenseVector is a one-dimensional mathematical vector of objects TStorage::value_type.
93 : * Use DenseVector with FixedArray1, VariableArray1 or stl::vector. Depending on
94 : * TStorage, DenseVector is of fixed size or variable size, and inheritates the interface
95 : * or TStorage.
96 : * \param TStorage storage policy with interface of VariableArray1.
97 : * \sa FixedArray1, VariableArray1
98 : */
99 : template<typename TStorage>
100 0 : class DenseVector : public TStorage
101 : {
102 : public:
103 : typedef typename TStorage::value_type value_type;
104 : typedef typename TStorage::size_type size_type;
105 :
106 : // use traits so we are able to use std::vector
107 : enum { is_static = storage_traits1<TStorage>::is_static};
108 : enum { static_size = storage_traits1<TStorage>::static_size};
109 :
110 : typedef DenseVector<TStorage> this_type;
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);
122 : DenseVector(const DenseVector<TStorage> &rhs);
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&
153 : operator+= (const value_type& alpha);
154 :
155 : inline this_type&
156 : operator-= (const value_type& alpha);
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 : }
206 : ////////////////////////////////////////////////////////////////////
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 0 : this_type operator - (const this_type &other ) const
221 : {
222 : UG_ASSERT(size() == other.size(), "");
223 : this_type erg;
224 0 : erg.resize(size());
225 0 : for(size_t i=0; i<size(); i++)
226 0 : erg[i] = entry(i) - other[i];
227 0 : 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 :
240 : this_type operator - () const
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 :
249 : ////////////////////////////////////////////////////////////////////
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>
261 : DenseVector<TStorage> &
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 0 : 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 0 : for(size_t i1=0; i1<t.size(); i1++)
301 0 : entry(i+i1) = t[i1];
302 : }
303 : };
304 :
305 : template<typename TStorage>
306 : inline
307 : DenseVector<TStorage>
308 : operator * (double alpha, const DenseVector<TStorage> &vec)
309 : {
310 : return vec*alpha;
311 : }
312 :
313 : template<typename TStorage>
314 : inline
315 : bool
316 : operator > (double alpha, const DenseVector<TStorage> &vec)
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
331 : operator >= (double alpha, const DenseVector<TStorage> &vec)
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
345 : /// \}
346 :
347 : }
348 : #include "densevector_impl.h"
349 :
350 : #endif // __H__UG__COMMON__DENSEVECTOR_H__
|