dense_subvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_DENSE_SUBVECTOR_H
21 #define LIBMESH_DENSE_SUBVECTOR_H
22 
23 // Local Includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/dense_vector.h"
26 
27 // C++ includes
28 
29 namespace libMesh
30 {
31 
32 
33 
42 // ------------------------------------------------------------
43 // DenseSubVector class definition
44 template<typename T>
46 {
47 public:
48 
55  DenseSubVector(DenseVector<T>& new_parent,
56  const unsigned int ioff=0,
57  const unsigned int n=0);
58 
62  virtual ~DenseSubVector() {}
63 
64 
68  DenseVector<T>& parent () { return _parent_vector; }
69 
73  virtual void zero();
74 
78  T operator() (const unsigned int i) const;
79 
83  T & operator() (const unsigned int i);
84 
88  virtual T el(const unsigned int i) const { return (*this)(i); }
89 
93  virtual T & el(const unsigned int i) { return (*this)(i); }
94 
98  virtual unsigned int size() const { return _n; }
99 
103  unsigned int i_off() const { return _i_off; }
104 
108  void reposition(const unsigned int ioff,
109  const unsigned int n);
110 
116  Real min () const;
117 
123  Real max () const;
124 
129  Real l1_norm () const;
130 
136  Real l2_norm () const;
137 
143  Real linfty_norm () const;
144 
145 private:
146 
147 
151  DenseVector<T>& _parent_vector;
152 
156  unsigned int _n;
157 
161  unsigned int _i_off;
162 };
163 
164 
165 
166 // ------------------------------------------------------------
167 // Dense Vector member functions
168 template<typename T>
169 inline
171  const unsigned int ioff,
172  const unsigned int n) :
173  _parent_vector(new_parent)
174 {
175  reposition (ioff, n);
176 }
177 
178 
179 
180 template<typename T>
181 inline
182 void DenseSubVector<T>::reposition(const unsigned int ioff,
183  const unsigned int n)
184 {
185  _i_off = ioff;
186  _n = n;
187 
188  // Make sure we still fit in the parent vector.
189  libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size());
190 }
191 
192 
193 
194 template<typename T>
195 inline
197 {
198  for (unsigned int i=0; i<this->size(); i++)
199  _parent_vector (i + this->i_off()) = 0.;
200 }
201 
202 
203 
204 template<typename T>
205 inline
206 T DenseSubVector<T>::operator () (const unsigned int i) const
207 {
208  libmesh_assert_less (i, this->size());
209  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
210 
211  return _parent_vector (i + this->i_off());
212 }
213 
214 
215 template<typename T>
216 inline
217 T & DenseSubVector<T>::operator () (const unsigned int i)
218 {
219  libmesh_assert_less (i, this->size());
220  libmesh_assert_less (i + this->i_off(), _parent_vector.size());
221 
222  return _parent_vector (i + this->i_off());
223 }
224 
225 template<typename T>
226 inline
228 {
229  libmesh_assert (this->size());
230  Real my_min = libmesh_real(_parent_vector (this->i_off()));
231 
232  for (unsigned int i=1; i!=this->size(); i++)
233  {
234  Real current = libmesh_real(_parent_vector (i + this->i_off()));
235  my_min = (my_min < current? my_min : current);
236  }
237  return my_min;
238 }
239 
240 
241 
242 template<typename T>
243 inline
245 {
246  libmesh_assert (this->size());
247  Real my_max = libmesh_real(_parent_vector (this->i_off()));
248 
249  for (unsigned int i=1; i!=this->size(); i++)
250  {
251  Real current = libmesh_real(_parent_vector (i + this->i_off()));
252  my_max = (my_max > current? my_max : current);
253  }
254  return my_max;
255 }
256 
257 
258 
259 template<typename T>
260 inline
261 Real DenseSubVector<T>::l1_norm () const
262 {
263  Real my_norm = 0.;
264  for (unsigned int i=0; i!=this->size(); i++)
265  {
266  my_norm += std::abs(_parent_vector (i + this->i_off()));
267  }
268  return my_norm;
269 }
270 
271 
272 
273 template<typename T>
274 inline
275 Real DenseSubVector<T>::l2_norm () const
276 {
277  Real my_norm = 0.;
278  for (unsigned int i=0; i!=this->size(); i++)
279  {
280  my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off()));
281  }
282  return sqrt(my_norm);
283 }
284 
285 
286 
287 template<typename T>
288 inline
289 Real DenseSubVector<T>::linfty_norm () const
290 {
291  if (!this->size())
292  return 0.;
293  Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off()));
294 
295  for (unsigned int i=1; i!=this->size(); i++)
296  {
297  Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off()));
298  my_norm = (my_norm > current? my_norm : current);
299  }
300  return sqrt(my_norm);
301 }
302 
303 } // namespace libMesh
304 
305 
306 #endif // LIBMESH_DENSE_SUBVECTOR_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:04 UTC

Hosted By:
SourceForge.net Logo