dense_subvector.h
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 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 00078 T operator() (const unsigned int i) const; 00079 00083 T & operator() (const unsigned int i); 00084 00088 virtual T el(const unsigned int i) const { return (*this)(i); } 00089 00093 virtual T & el(const unsigned int i) { return (*this)(i); } 00094 00098 virtual unsigned int size() const { return _n; } 00099 00103 unsigned int i_off() const { return _i_off; } 00104 00108 void reposition(const unsigned int ioff, 00109 const unsigned int n); 00110 00116 Real min () const; 00117 00123 Real max () const; 00124 00129 Real l1_norm () const; 00130 00136 Real l2_norm () const; 00137 00143 Real linfty_norm () const; 00144 00145 private: 00146 00147 00151 DenseVector<T>& _parent_vector; 00152 00156 unsigned int _n; 00157 00161 unsigned int _i_off; 00162 }; 00163 00164 00165 00166 // ------------------------------------------------------------ 00167 // Dense Vector member functions 00168 template<typename T> 00169 inline 00170 DenseSubVector<T>::DenseSubVector(DenseVector<T>& new_parent, 00171 const unsigned int ioff, 00172 const unsigned int n) : 00173 _parent_vector(new_parent) 00174 { 00175 reposition (ioff, n); 00176 } 00177 00178 00179 00180 template<typename T> 00181 inline 00182 void DenseSubVector<T>::reposition(const unsigned int ioff, 00183 const unsigned int n) 00184 { 00185 _i_off = ioff; 00186 _n = n; 00187 00188 // Make sure we still fit in the parent vector. 00189 libmesh_assert_less_equal ((this->i_off() + this->size()), _parent_vector.size()); 00190 } 00191 00192 00193 00194 template<typename T> 00195 inline 00196 void DenseSubVector<T>::zero() 00197 { 00198 for (unsigned int i=0; i<this->size(); i++) 00199 _parent_vector (i + this->i_off()) = 0.; 00200 } 00201 00202 00203 00204 template<typename T> 00205 inline 00206 T DenseSubVector<T>::operator () (const unsigned int i) const 00207 { 00208 libmesh_assert_less (i, this->size()); 00209 libmesh_assert_less (i + this->i_off(), _parent_vector.size()); 00210 00211 return _parent_vector (i + this->i_off()); 00212 } 00213 00214 00215 template<typename T> 00216 inline 00217 T & DenseSubVector<T>::operator () (const unsigned int i) 00218 { 00219 libmesh_assert_less (i, this->size()); 00220 libmesh_assert_less (i + this->i_off(), _parent_vector.size()); 00221 00222 return _parent_vector (i + this->i_off()); 00223 } 00224 00225 template<typename T> 00226 inline 00227 Real DenseSubVector<T>::min () const 00228 { 00229 libmesh_assert (this->size()); 00230 Real my_min = libmesh_real(_parent_vector (this->i_off())); 00231 00232 for (unsigned int i=1; i!=this->size(); i++) 00233 { 00234 Real current = libmesh_real(_parent_vector (i + this->i_off())); 00235 my_min = (my_min < current? my_min : current); 00236 } 00237 return my_min; 00238 } 00239 00240 00241 00242 template<typename T> 00243 inline 00244 Real DenseSubVector<T>::max () const 00245 { 00246 libmesh_assert (this->size()); 00247 Real my_max = libmesh_real(_parent_vector (this->i_off())); 00248 00249 for (unsigned int i=1; i!=this->size(); i++) 00250 { 00251 Real current = libmesh_real(_parent_vector (i + this->i_off())); 00252 my_max = (my_max > current? my_max : current); 00253 } 00254 return my_max; 00255 } 00256 00257 00258 00259 template<typename T> 00260 inline 00261 Real DenseSubVector<T>::l1_norm () const 00262 { 00263 Real my_norm = 0.; 00264 for (unsigned int i=0; i!=this->size(); i++) 00265 { 00266 my_norm += std::abs(_parent_vector (i + this->i_off())); 00267 } 00268 return my_norm; 00269 } 00270 00271 00272 00273 template<typename T> 00274 inline 00275 Real DenseSubVector<T>::l2_norm () const 00276 { 00277 Real my_norm = 0.; 00278 for (unsigned int i=0; i!=this->size(); i++) 00279 { 00280 my_norm += TensorTools::norm_sq(_parent_vector (i + this->i_off())); 00281 } 00282 return sqrt(my_norm); 00283 } 00284 00285 00286 00287 template<typename T> 00288 inline 00289 Real DenseSubVector<T>::linfty_norm () const 00290 { 00291 if (!this->size()) 00292 return 0.; 00293 Real my_norm = TensorTools::norm_sq(_parent_vector (this->i_off())); 00294 00295 for (unsigned int i=1; i!=this->size(); i++) 00296 { 00297 Real current = TensorTools::norm_sq(_parent_vector (i + this->i_off())); 00298 my_norm = (my_norm > current? my_norm : current); 00299 } 00300 return sqrt(my_norm); 00301 } 00302 00303 } // namespace libMesh 00304 00305 00306 #endif // LIBMESH_DENSE_SUBVECTOR_H 00307
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:46 UTC
Hosted By: