00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef MATH_IDENTITY_INCLUDE
00014 #define MATH_IDENTITY_INCLUDE
00015
00016 #include <boost/numeric/linear_algebra/operators.hpp>
00017 #include <limits>
00018 #include <string>
00019 #include <functional>
00020
00021 namespace math {
00022
00023 template <typename Operation, typename Element>
00024 struct identity_t {};
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 template <typename Element>
00035 struct identity_t< add<Element>, Element >
00036 : public std::binary_function<add<Element>, Element, Element>
00037 {
00038 Element operator() (const add<Element>&, const Element& ref) const
00039 {
00040 Element tmp(ref);
00041 tmp= 0;
00042 return tmp;
00043 }
00044 };
00045
00046 template <>
00047 struct identity_t< add<std::string>, std::string >
00048 : public std::binary_function<add<std::string>, std::string, std::string>
00049 {
00050 std::string operator() (const add<std::string>&, const std::string&) const
00051 {
00052 return std::string();
00053 }
00054 };
00055
00056
00057
00058
00059 template <typename Element>
00060 struct identity_t< mult<Element>, Element >
00061 : public std::binary_function<mult<Element>, Element, Element>
00062 {
00063 Element operator() (const mult<Element>&, const Element& ref) const
00064 {
00065 Element tmp(ref);
00066 tmp= 1;
00067 return tmp;
00068 }
00069 };
00070
00071
00072
00073 template <typename Element>
00074 struct identity_t< max<Element>, Element >
00075 : public std::binary_function<max<Element>, Element, Element>
00076 {
00077 Element operator() (const max<Element>&, const Element& ref) const
00078 {
00079 using std::numeric_limits;
00080 return numeric_limits<Element>::min();
00081 }
00082 };
00083
00084
00085
00086 template <typename Element>
00087 struct identity_t< min<Element>, Element >
00088 : public std::binary_function<min<Element>, Element, Element>
00089 {
00090 Element operator() (const min<Element>&, const Element& ref) const
00091 {
00092 using std::numeric_limits;
00093 return numeric_limits<Element>::max();
00094 }
00095 };
00096
00097
00098 template <typename Element>
00099 struct identity_t< bitwise_and<Element>, Element >
00100 : public std::binary_function<bitwise_and<Element>, Element, Element>
00101 {
00102 Element operator() (const bitwise_and<Element>&, const Element& ref) const
00103 {
00104 return 0;
00105 }
00106 };
00107
00108
00109 template <typename Element>
00110 struct identity_t< bitwise_or<Element>, Element >
00111 : public std::binary_function<bitwise_or<Element>, Element, Element>
00112 {
00113 Element operator() (const bitwise_or<Element>&, const Element& ref) const
00114 {
00115 return 0 - 1;
00116 }
00117 };
00118
00119
00120 template <typename Operation, typename Element>
00121 inline Element identity(const Operation& op, const Element& v)
00122 {
00123 return identity_t<Operation, Element>() (op, v);
00124 }
00125
00126
00127
00128 template <typename Element>
00129 inline Element zero(const Element& v)
00130 {
00131 return identity_t<math::add<Element>, Element>() (math::add<Element>(), v);
00132 }
00133
00134
00135
00136 template <typename Element>
00137 inline Element one(const Element& v)
00138 {
00139 return identity_t<math::mult<Element>, Element>() (math::mult<Element>(), v);
00140 }
00141
00142
00143 }
00144
00145 #endif // MATH_IDENTITY_INCLUDE