Go to the documentation of this file.00001 #ifndef VIENNAGRID_ALGORITHM_NORM_GUARD
00002 #define VIENNAGRID_ALGORITHM_NORM_GUARD
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 }
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
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
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