• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

/export/development/ViennaGrid/release/ViennaGrid-1.0.0/viennagrid/algorithm/norm.hpp

Go to the documentation of this file.
00001 #ifndef VIENNAGRID_ALGORITHM_NORM_GUARD
00002 #define VIENNAGRID_ALGORITHM_NORM_GUARD
00003 
00004 /* =======================================================================
00005    Copyright (c) 2011, Institute for Microelectronics,
00006                        Institute for Analysis and Scientific Computing,
00007                        TU Wien.
00008 
00009                             -----------------
00010                      ViennaGrid - The Vienna Grid Library
00011                             -----------------
00012 
00013    Authors:      Karl Rupp                           rupp@iue.tuwien.ac.at
00014                  Josef Weinbub                    weinbub@iue.tuwien.ac.at
00015                
00016    (A list of additional contributors can be found in the PDF manual)
00017 
00018    License:      MIT (X11), see file LICENSE in the base directory
00019 ======================================================================= */
00020 
00021 #include <cmath>
00022 #include "viennagrid/forwards.h"
00023 #include "viennagrid/traits/point.hpp"
00024 #include "viennagrid/point.hpp"
00025 
00030 namespace viennagrid 
00031 {
00032   
00033   namespace detail
00034   {
00035 
00036     // -----------------------------------------------------------------------------
00037     //
00038     // norm algorithm specialization hierarchy
00039     //
00040     template<typename Tag> 
00041     struct norm_impl 
00042     {
00043       template<typename PointType>
00044       typename traits::value_type<PointType>::type operator()(PointType const& p)
00045       {
00046           std::cerr << "ViennaGrid - Norm Error - this error type is not implemented" << std::endl;
00047           return 0.0;
00048       }
00049     };
00050 
00052     template<>
00053     struct norm_impl<viennagrid::one_tag>
00054     {
00055       template<typename PointType>
00056       typename traits::value_type<PointType>::type operator()(PointType const& p)
00057       {
00058           typename traits::value_type<PointType>::type result(0);
00059           for(std::size_t i = 0; i < traits::dynamic_size(p); i++)
00060             result += std::fabs(p[i]);
00061           return result;
00062       }
00063     };
00064 
00066     template<>
00067     struct norm_impl<viennagrid::two_tag>
00068     {
00069       template<typename PointType>
00070       typename traits::value_type<PointType>::type operator()(PointType const& p)
00071       {
00072           typename traits::value_type<PointType>::type result(0);
00073           for(std::size_t i = 0; i < traits::dynamic_size(p); i++)
00074             result += p[i]*p[i];
00075           return std::sqrt(result);   
00076       }
00077     };
00078 
00080     template<>
00081     struct norm_impl<viennagrid::inf_tag>
00082     {
00083       template<typename PointType>
00084       typename traits::value_type<PointType>::type operator()(PointType const& p)
00085       {
00086           typename traits::value_type<PointType>::type result(0);
00087           for(std::size_t i = 0; i < traits::dynamic_size(p); i++)
00088           {
00089             if(std::fabs(p[i]) > result)
00090                 result = std::fabs(p[i]);
00091           }
00092           return result;
00093       }
00094     };
00095     
00096   } //namespace detail
00097 
00099   template<typename NormTag, typename PointType, typename CSystem>
00100   typename traits::value_type<PointType>::type
00101   norm_impl(PointType const & p, CSystem const &)
00102   {
00103     typedef typename traits::value_type<PointType>::type    value_type;
00104     typedef typename result_of::cartesian_point<PointType>::type   CartesianPoint1;
00105     
00106     return detail::norm_impl<NormTag>()(to_cartesian(p));
00107   }
00108 
00110   template<typename NormTag, typename PointType1, long d>
00111   typename traits::value_type<PointType1>::type
00112   norm_impl(PointType1 const & p, cartesian_cs<d>)
00113   {
00114     return detail::norm_impl<NormTag>()(p);
00115   }
00116 
00117 
00118   // -----------------------------------------------------------------------------
00119   //
00120   // norm algorithm generic interface functions
00121   //
00126   template<typename PointType, typename Tag>
00127   typename traits::value_type<PointType>::type
00128   norm(PointType const & p, Tag)
00129   {
00130     return norm_impl<Tag>(p, typename traits::coordinate_system<PointType>::type());
00131   }
00132 
00134   template<typename PointType>
00135   typename traits::value_type<PointType>::type
00136   norm(PointType const & p)
00137   {
00138     return norm_impl<viennagrid::two_tag>(p, typename traits::coordinate_system<PointType>::type());
00139   }
00140 
00141 
00142   //convenience shortcuts:
00144   template<typename PointType>
00145   typename traits::value_type<PointType>::type
00146   norm_1(PointType const & p)
00147   {
00148     return norm_impl<viennagrid::one_tag>(p, typename traits::coordinate_system<PointType>::type());
00149   }
00150 
00152   template<typename PointType>
00153   typename traits::value_type<PointType>::type
00154   norm_2(PointType const & p)
00155   {
00156     return norm_impl<viennagrid::two_tag>(p, typename traits::coordinate_system<PointType>::type());
00157   }
00158 
00160   template<typename PointType>
00161   typename traits::value_type<PointType>::type
00162   norm_inf(PointType const & p)
00163   {
00164     return norm_impl<viennagrid::inf_tag>(p, typename traits::coordinate_system<PointType>::type());
00165   }
00166 
00167 } 
00168 
00169 #endif
00170 

Generated on Wed Sep 14 2011 19:21:30 for ViennaGrid by  doxygen 1.7.1