00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_REDUCTION_FUNCTORS_INCLUDE
00013 #define MTL_REDUCTION_FUNCTORS_INCLUDE
00014
00015 #include <cmath>
00016 #include <boost/numeric/linear_algebra/identity.hpp>
00017
00018 namespace mtl { namespace vector {
00019
00020 struct one_norm_functor
00021 {
00022 template <typename Value>
00023 static inline void init(Value& value)
00024 {
00025 using math::zero;
00026 value= zero(value);
00027 }
00028
00029 template <typename Value, typename Element>
00030 static inline void update(Value& value, const Element& x)
00031 {
00032 using std::abs;
00033 value+= abs(x);
00034 }
00035
00036 template <typename Value>
00037 static inline void finish(Value& value, const Value& value2)
00038 {
00039 value+= value2;
00040 }
00041 };
00042
00043
00044
00045 struct two_norm_functor
00046 {
00047 template <typename Value>
00048 static inline void init(Value& value)
00049 {
00050 using math::zero;
00051 value= zero(value);
00052 }
00053
00054 template <typename Value, typename Element>
00055 static inline void update(Value& value, const Element& x)
00056 {
00057 using std::abs;
00058 value+= abs(x) * abs(x);
00059 }
00060
00061 template <typename Value>
00062 static inline void finish(Value& value, const Value& value2)
00063 {
00064 value+= value2;
00065 }
00066 };
00067
00068
00069 struct infinity_norm_functor
00070 {
00071 template <typename Value>
00072 static inline void init(Value& value)
00073 {
00074 using math::zero;
00075 value= zero(value);
00076 }
00077
00078 template <typename Value, typename Element>
00079 static inline void update(Value& value, const Element& x)
00080 {
00081 using std::abs; using std::max;
00082 value= max(value, abs(x));
00083 }
00084
00085 template <typename Value>
00086 static inline void finish(Value& value, const Value& value2)
00087 {
00088 using std::abs; using std::max;
00089 value= max(value, abs(value2));
00090 }
00091 };
00092
00093
00094 struct sum_functor
00095 {
00096 template <typename Value>
00097 static inline void init(Value& value)
00098 {
00099 using math::zero;
00100 value= zero(value);
00101 }
00102
00103 template <typename Value, typename Element>
00104 static inline void update(Value& value, const Element& x)
00105 {
00106 value+= x;
00107 }
00108
00109 template <typename Value>
00110 static inline void finish(Value& value, const Value& value2)
00111 {
00112 value+= value2;
00113 }
00114 };
00115
00116
00117 struct product_functor
00118 {
00119 template <typename Value>
00120 static inline void init(Value& value)
00121 {
00122 using math::one;
00123 value= one(value);
00124 }
00125
00126 template <typename Value, typename Element>
00127 static inline void update(Value& value, const Element& x)
00128 {
00129 value*= x;
00130 }
00131
00132 template <typename Value>
00133 static inline void finish(Value& value, const Value& value2)
00134 {
00135 value*= value2;
00136 }
00137 };
00138
00139
00140 struct max_functor
00141 {
00142 template <typename Value>
00143 static inline void init(Value& value)
00144 {
00145 using math::identity;
00146 value= identity(math::max<Value>(), value);
00147 }
00148
00149 template <typename Value, typename Element>
00150 static inline void update(Value& value, const Element& x)
00151 {
00152 value= math::max<Value>()(value, x);
00153 }
00154
00155 template <typename Value>
00156 static inline void finish(Value& value, const Value& value2)
00157 {
00158 value= math::max<Value>()(value, value2);
00159 }
00160 };
00161
00162
00163 struct min_functor
00164 {
00165 template <typename Value>
00166 static inline void init(Value& value)
00167 {
00168 using math::identity;
00169 value= identity(math::min<Value>(), value);
00170 }
00171
00172 template <typename Value, typename Element>
00173 static inline void update(Value& value, const Element& x)
00174 {
00175 value= math::min<Value>()(value, x);
00176 }
00177
00178 template <typename Value>
00179 static inline void finish(Value& value, const Value& value2)
00180 {
00181 value= math::min<Value>()(value, value2);
00182 }
00183 };
00184
00185
00186 }}
00187
00188 #endif // MTL_REDUCTION_FUNCTORS_INCLUDE