$extrastylesheet
dense_subvector.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_DENSE_SUBVECTOR_H
00021 #define LIBMESH_DENSE_SUBVECTOR_H
00022 
00023 // Local Includes
00024 #include "libmesh/libmesh_common.h"
00025 #include "libmesh/dense_vector.h"
00026 
00027 // C++ includes
00028 
00029 namespace libMesh
00030 {
00031 
00032 
00033 
00042 // ------------------------------------------------------------
00043 // DenseSubVector class definition
00044 template<typename T>
00045 class DenseSubVector : public DenseVectorBase<T>
00046 {
00047 public:
00048 
00055   DenseSubVector(DenseVector<T>& new_parent,
00056                  const unsigned int ioff=0,
00057                  const unsigned int n=0);
00058 
00062   virtual ~DenseSubVector() {}
00063 
00064 
00068   DenseVector<T>& parent () { return _parent_vector; }
00069 
00073   virtual void zero();
00074 
00079   const T & operator() (const unsigned int i) const;
00080 
00084   T & operator() (const unsigned int i);
00085 
00089   virtual T el(const unsigned int i) const { return (*this)(i); }
00090 
00094   virtual T & el(const unsigned int i)     { return (*this)(i); }
00095 
00099   virtual unsigned int size() const { return _n; }
00100 
00104   virtual bool empty() const { return (_n == 0); }
00105 
00109   unsigned int i_off() const { return _i_off; }
00110 
00114   void reposition(const unsigned int ioff,
00115                   const unsigned int n);
00116 
00122   Real min () const;
00123 
00129   Real max () const;
00130 
00135   Real l1_norm () const;
00136 
00142   Real l2_norm () const;
00143 
00149   Real linfty_norm () const;
00150 
00151 private:
00152 
00153 
00157   DenseVector<T>& _parent_vector;
00158 
00162   unsigned int _n;
00163 
00167   unsigned int _i_off;
00168 };
00169 
00170 
00171 
00172 // ------------------------------------------------------------
00173 // Dense Vector member functions
00174 template<typename T>
00175 inline
00176 DenseSubVector<T>::DenseSubVector(DenseVector<T>& new_parent,
00177                                   const unsigned int ioff,
00178                                   const unsigned int n) :
00179   _parent_vector(new_parent)
00180 {
00181   reposition (ioff, n);
00182 }
00183 
00184 
00185 
00186 template<typename T>
00187 inline
00188 void DenseSubVector<T>::reposition(const unsigned int ioff,
00189                                    const unsigned int n)
00190 {
00191   _i_off = ioff;
00192   _n = n;
00193 
00194   // Make sure we still fit in the parent vector.
00195   libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size());
00196 }
00197 
00198 
00199 
00200 template<typename T>
00201 inline
00202 void DenseSubVector<T>::zero()
00203 {
00204   for (unsigned int i=0; i<this->size(); i++)
00205     _parent_vector (i + this->i_off()) = 0.;
00206 }
00207 
00208 
00209 
00210 template<typename T>
00211 inline
00212 const T & DenseSubVector<T>::operator () (const unsigned int i) const
00213 {
00214   libmesh_assert_less (i, this->size());
00215   libmesh_assert_less (i + this->i_off(), _parent_vector.size());
00216 
00217   return _parent_vector (i + this->i_off());
00218 }
00219 
00220 
00221 template<typename T>
00222 inline
00223 T & DenseSubVector<T>::operator () (const unsigned int i)
00224 {
00225   libmesh_assert_less (i, this->size());
00226   libmesh_assert_less (i + this->i_off(), _parent_vector.size());
00227 
00228   return _parent_vector (i + this->i_off());
00229 }
00230 
00231 template<typename T>
00232 inline
00233 Real DenseSubVector<T>::min () const
00234 {
00235   libmesh_assert (this->size());
00236   Real my_min = libmesh_real(_parent_vector (this->i_off()));
00237 
00238   for (unsigned int i=1; i!=this->size(); i++)
00239     {
00240       Real current = libmesh_real(_parent_vector (i + this->i_off()));
00241       my_min = (my_min < current? my_min : current);
00242     }
00243   return my_min;
00244 }
00245 
00246 
00247 
00248 template<typename T>
00249 inline
00250 Real DenseSubVector<T>::max () const
00251 {
00252   libmesh_assert (this->size());
00253   Real my_max = libmesh_real(_parent_vector (this->i_off()));
00254 
00255   for (unsigned int i=1; i!=this->size(); i++)
00256     {
00257       Real current = libmesh_real(_parent_vector (i + this->i_off()));
00258       my_max = (my_max > current? my_max : current);
00259     }
00260   return my_max;
00261 }
00262 
00263 
00264 
00265 template<typename T>
00266 inline
00267 Real DenseSubVector<T>::l1_norm () const
00268 {
00269   Real my_norm = 0.;
00270   for (unsigned int i=0; i!=this->size(); i++)
00271     {
00272       my_norm += std::abs(_parent_vector (i + this->i_off()));
00273     }
00274   return my_norm;
00275 }
00276 
00277 
00278 
00279 template<typename T>
00280 inline
00281 Real DenseSubVector<T>::l2_norm () const
00282 {
00283   Real my_norm = 0.;
00284   for (unsigned int i=0; i!=this->size(); i++)
00285     {
00286       my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off()));
00287     }
00288   return sqrt(my_norm);
00289 }
00290 
00291 
00292 
00293 template<typename T>
00294 inline
00295 Real DenseSubVector<T>::linfty_norm () const
00296 {
00297   if (!this->size())
00298     return 0.;
00299   Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off()));
00300 
00301   for (unsigned int i=1; i!=this->size(); i++)
00302     {
00303       Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off()));
00304       my_norm = (my_norm > current? my_norm : current);
00305     }
00306   return sqrt(my_norm);
00307 }
00308 
00309 } // namespace libMesh
00310 
00311 
00312 #endif // LIBMESH_DENSE_SUBVECTOR_H