00001 #ifndef VIENNAMATH_RUNTIME_INTERVAL_HPP
00002 #define VIENNAMATH_RUNTIME_INTERVAL_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <ostream>
00021 #include "viennamath/forwards.h"
00022
00027 namespace viennamath
00028 {
00029
00031 template <typename InterfaceType>
00032 class rt_interval
00033 {
00034
00035 public:
00036 typedef rt_expr<InterfaceType> value_type;
00037 typedef InterfaceType interface_type;
00038 typedef typename InterfaceType::numeric_type numeric_type;
00039
00040 explicit rt_interval() : lower_(new rt_constant<numeric_type, InterfaceType>(0)),
00041 upper_(new rt_constant<numeric_type, InterfaceType>(1)) {}
00042
00043 explicit rt_interval(const rt_expr<InterfaceType> & a,
00044 const rt_expr<InterfaceType> & b) : lower_(a.get()->clone()),
00045 upper_(b.get()->clone()) {}
00046
00047 rt_interval(rt_interval const & other) : lower_(other.lower_.get()->clone()),
00048 upper_(other.upper_.get()->clone()) {}
00049
00051 rt_expr<InterfaceType> const & lower() const { return lower_; }
00053 rt_expr<InterfaceType> const & upper() const { return upper_; }
00054
00055 rt_interval const & operator=(rt_interval const & other)
00056 {
00057 lower_ = other.lower().get()->clone();
00058 upper_ = other.upper().get()->clone();
00059 };
00060
00061 private:
00062 rt_expr<InterfaceType> lower_;
00063 rt_expr<InterfaceType> upper_;
00064 };
00065
00067 template <typename InterfaceType>
00068 std::ostream& operator<<(std::ostream & stream, rt_interval<InterfaceType> const & e)
00069 {
00070 stream << "interval(" << e.lower() << ", " << e.upper() << ")";
00071 return stream;
00072 }
00073
00074
00075
00076
00078
00079
00080
00081
00082
00083
00085
00086 inline rt_interval<> make_interval(default_numeric_type lhs, default_numeric_type rhs)
00087 {
00088 rt_expr<> a = rt_constant<>(lhs);
00089 rt_expr<> b = rt_constant<>(rhs);
00090 return rt_interval<>(a, b);
00091 }
00092
00094 template <typename NumericT, typename InterfaceType, typename RHSType>
00095 rt_interval<InterfaceType> make_interval(typename InterfaceType::numeric_type lhs, rt_constant<NumericT, InterfaceType> const & rhs)
00096 {
00097 return rt_interval<InterfaceType>(lhs, rhs);
00098 }
00099
00101 template <typename InterfaceType>
00102 rt_interval<InterfaceType> make_interval(typename InterfaceType::numeric_type lhs, rt_variable<InterfaceType> const & rhs)
00103 {
00104 return rt_interval<InterfaceType>(lhs, rhs);
00105 }
00106
00108 template <id_type id>
00109 rt_interval<> make_interval(default_numeric_type lhs, ct_variable<id> const & rhs)
00110 {
00111 return rt_interval<>(lhs, rhs);
00112 }
00113
00115 template <typename InterfaceType>
00116 rt_interval<InterfaceType> make_interval(typename InterfaceType::numeric_type lhs, rt_unary_expr<InterfaceType> const & rhs)
00117 {
00118 return rt_interval<InterfaceType>(lhs, rhs);
00119 }
00120
00122 template <typename InterfaceType>
00123 rt_interval<InterfaceType> make_interval(typename InterfaceType::numeric_type lhs, rt_binary_expr<InterfaceType> const & rhs)
00124 {
00125 return rt_interval<InterfaceType>(lhs, rhs);
00126 }
00127
00129 template <typename InterfaceType>
00130 rt_interval<InterfaceType> make_interval(typename InterfaceType::numeric_type lhs, rt_expr<InterfaceType> const & rhs)
00131 {
00132 return rt_interval<InterfaceType>(lhs, rhs);
00133 }
00134
00135
00136
00138 template <typename NumericT, typename InterfaceType, typename RHSType>
00139 rt_interval<InterfaceType> make_interval(rt_constant<NumericT, InterfaceType> const & lhs, RHSType const & rhs)
00140 {
00141 return rt_interval<InterfaceType>(lhs, rhs);
00142 }
00143
00144
00146 template <typename InterfaceType, typename RHSType>
00147 rt_interval<InterfaceType> make_interval(rt_variable<InterfaceType> const & lhs, RHSType const & rhs)
00148 {
00149 return rt_interval<InterfaceType>(lhs, rhs);
00150 }
00151
00153 template <id_type id, typename RHSType>
00154 rt_interval<> make_interval(ct_variable<id> const & lhs, RHSType const & rhs)
00155 {
00156 rt_variable<> temp(id);
00157 return rt_interval<>(temp, rhs);
00158 }
00159
00160
00162 template <typename InterfaceType, typename RHSType>
00163 rt_interval<InterfaceType> make_interval(rt_function_symbol<InterfaceType> const & lhs, RHSType const & rhs)
00164 {
00165 return rt_interval<InterfaceType>(lhs, rhs);
00166 }
00167
00168
00170 template <typename InterfaceType, typename RHSType>
00171 rt_interval<InterfaceType> make_interval(rt_unary_expr<InterfaceType> const & lhs, RHSType const & rhs)
00172 {
00173 return rt_interval<InterfaceType>(lhs, rhs);
00174 }
00175
00176
00177
00179 template <typename InterfaceType, typename RHSType>
00180 rt_interval<InterfaceType> make_interval(rt_binary_expr<InterfaceType> const & lhs, RHSType const & rhs)
00181 {
00182 return rt_interval<InterfaceType>(lhs, rhs);
00183 }
00184
00185
00187 template <typename InterfaceType, typename RHSType>
00188 rt_interval<InterfaceType> make_interval(rt_expr<InterfaceType> const & lhs, RHSType const & rhs)
00189 {
00190 return rt_interval<InterfaceType>(lhs, rhs);
00191 }
00192
00193
00195 template <typename LHS, typename OP, typename RHS, typename RHSType>
00196 rt_interval<typename RHSType::interface_type>
00197 make_interval(ct_binary_expr<LHS, OP, RHS> const & lhs, RHSType const & rhs)
00198 {
00199 return rt_interval<typename RHSType::interface_type>(lhs, rhs);
00200 }
00201
00203 template <typename LHS, typename OP, typename RHSType>
00204 rt_interval<typename RHSType::interface_type>
00205 make_interval(ct_unary_expr<LHS, OP> const & lhs, RHSType const & rhs)
00206 {
00207 return rt_interval<typename RHSType::interface_type>(lhs, rhs);
00208 }
00209
00211 template <long val, typename RHSType>
00212 rt_interval<typename RHSType::interface_type>
00213 make_interval(ct_constant<val> const & lhs, RHSType const & rhs)
00214 {
00215 return rt_interval<typename RHSType::interface_type>(lhs, rhs);
00216 }
00217
00219 template <typename TAG, typename RHSType>
00220 rt_interval<typename RHSType::interface_type>
00221 make_interval(ct_function_symbol<TAG> const & lhs, RHSType const & rhs)
00222 {
00223 return rt_interval<typename RHSType::interface_type>(lhs, rhs);
00224 }
00225
00227 template <id_type id, typename RHSType>
00228 rt_interval<typename RHSType::interface_type>
00229 make_interval(ct_variable<id> const & lhs, RHSType const & rhs)
00230 {
00231 return rt_interval<typename RHSType::interface_type>(lhs, rhs);
00232 }
00233
00234 }
00235
00236 #endif