arith2.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Boost interval/arith2.hpp template implementation file
  2. *
  3. * This header provides some auxiliary arithmetic
  4. * functions: fmod, sqrt, square, pov, inverse and
  5. * a multi-interval division.
  6. *
  7. * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
  8. *
  9. * Distributed under the Boost Software License, Version 1.0.
  10. * (See accompanying file LICENSE_1_0.txt or
  11. * copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #ifndef BOOST_NUMERIC_INTERVAL_ARITH2_HPP
  14. #define BOOST_NUMERIC_INTERVAL_ARITH2_HPP
  15. #include <boost/config.hpp>
  16. #include <boost/numeric/interval/detail/interval_prototype.hpp>
  17. #include <boost/numeric/interval/detail/test_input.hpp>
  18. #include <boost/numeric/interval/detail/bugs.hpp>
  19. #include <boost/numeric/interval/detail/division.hpp>
  20. #include <boost/numeric/interval/arith.hpp>
  21. #include <boost/numeric/interval/policies.hpp>
  22. #include <algorithm>
  23. #include <cassert>
  24. #include <boost/config/no_tr1/cmath.hpp>
  25. namespace boost {
  26. namespace numeric {
  27. template<class T, class Policies> inline
  28. interval<T, Policies> fmod(const interval<T, Policies>& x,
  29. const interval<T, Policies>& y)
  30. {
  31. if (interval_lib::detail::test_input(x, y))
  32. return interval<T, Policies>::empty();
  33. typename Policies::rounding rnd;
  34. typedef typename interval_lib::unprotect<interval<T, Policies> >::type I;
  35. T const &yb = interval_lib::user::is_neg(x.lower()) ? y.lower() : y.upper();
  36. T n = rnd.int_down(rnd.div_down(x.lower(), yb));
  37. return (const I&)x - n * (const I&)y;
  38. }
  39. template<class T, class Policies> inline
  40. interval<T, Policies> fmod(const interval<T, Policies>& x, const T& y)
  41. {
  42. if (interval_lib::detail::test_input(x, y))
  43. return interval<T, Policies>::empty();
  44. typename Policies::rounding rnd;
  45. typedef typename interval_lib::unprotect<interval<T, Policies> >::type I;
  46. T n = rnd.int_down(rnd.div_down(x.lower(), y));
  47. return (const I&)x - n * I(y);
  48. }
  49. template<class T, class Policies> inline
  50. interval<T, Policies> fmod(const T& x, const interval<T, Policies>& y)
  51. {
  52. if (interval_lib::detail::test_input(x, y))
  53. return interval<T, Policies>::empty();
  54. typename Policies::rounding rnd;
  55. typedef typename interval_lib::unprotect<interval<T, Policies> >::type I;
  56. T const &yb = interval_lib::user::is_neg(x) ? y.lower() : y.upper();
  57. T n = rnd.int_down(rnd.div_down(x, yb));
  58. return x - n * (const I&)y;
  59. }
  60. namespace interval_lib {
  61. template<class T, class Policies> inline
  62. interval<T, Policies> division_part1(const interval<T, Policies>& x,
  63. const interval<T, Policies>& y, bool& b)
  64. {
  65. typedef interval<T, Policies> I;
  66. b = false;
  67. if (detail::test_input(x, y))
  68. return I::empty();
  69. if (zero_in(y))
  70. if (!user::is_zero(y.lower()))
  71. if (!user::is_zero(y.upper()))
  72. return detail::div_zero_part1(x, y, b);
  73. else
  74. return detail::div_negative(x, y.lower());
  75. else
  76. if (!user::is_zero(y.upper()))
  77. return detail::div_positive(x, y.upper());
  78. else
  79. return I::empty();
  80. else
  81. return detail::div_non_zero(x, y);
  82. }
  83. template<class T, class Policies> inline
  84. interval<T, Policies> division_part2(const interval<T, Policies>& x,
  85. const interval<T, Policies>& y, bool b = true)
  86. {
  87. if (!b) return interval<T, Policies>::empty();
  88. return detail::div_zero_part2(x, y);
  89. }
  90. template<class T, class Policies> inline
  91. interval<T, Policies> multiplicative_inverse(const interval<T, Policies>& x)
  92. {
  93. typedef interval<T, Policies> I;
  94. if (detail::test_input(x))
  95. return I::empty();
  96. T one = static_cast<T>(1);
  97. typename Policies::rounding rnd;
  98. if (zero_in(x)) {
  99. typedef typename Policies::checking checking;
  100. if (!user::is_zero(x.lower()))
  101. if (!user::is_zero(x.upper()))
  102. return I::whole();
  103. else
  104. return I(checking::neg_inf(), rnd.div_up(one, x.lower()), true);
  105. else
  106. if (!user::is_zero(x.upper()))
  107. return I(rnd.div_down(one, x.upper()), checking::pos_inf(), true);
  108. else
  109. return I::empty();
  110. } else
  111. return I(rnd.div_down(one, x.upper()), rnd.div_up(one, x.lower()), true);
  112. }
  113. namespace detail {
  114. template<class T, class Rounding> inline
  115. T pow_dn(const T& x_, int pwr, Rounding& rnd) // x and pwr are positive
  116. {
  117. T x = x_;
  118. T y = (pwr & 1) ? x_ : static_cast<T>(1);
  119. pwr >>= 1;
  120. while (pwr > 0) {
  121. x = rnd.mul_down(x, x);
  122. if (pwr & 1) y = rnd.mul_down(x, y);
  123. pwr >>= 1;
  124. }
  125. return y;
  126. }
  127. template<class T, class Rounding> inline
  128. T pow_up(const T& x_, int pwr, Rounding& rnd) // x and pwr are positive
  129. {
  130. T x = x_;
  131. T y = (pwr & 1) ? x_ : static_cast<T>(1);
  132. pwr >>= 1;
  133. while (pwr > 0) {
  134. x = rnd.mul_up(x, x);
  135. if (pwr & 1) y = rnd.mul_up(x, y);
  136. pwr >>= 1;
  137. }
  138. return y;
  139. }
  140. } // namespace detail
  141. } // namespace interval_lib
  142. template<class T, class Policies> inline
  143. interval<T, Policies> pow(const interval<T, Policies>& x, int pwr)
  144. {
  145. BOOST_USING_STD_MAX();
  146. using interval_lib::detail::pow_dn;
  147. using interval_lib::detail::pow_up;
  148. typedef interval<T, Policies> I;
  149. if (interval_lib::detail::test_input(x))
  150. return I::empty();
  151. if (pwr == 0)
  152. if (interval_lib::user::is_zero(x.lower())
  153. && interval_lib::user::is_zero(x.upper()))
  154. return I::empty();
  155. else
  156. return I(static_cast<T>(1));
  157. else if (pwr < 0)
  158. return interval_lib::multiplicative_inverse(pow(x, -pwr));
  159. typename Policies::rounding rnd;
  160. if (interval_lib::user::is_neg(x.upper())) { // [-2,-1]
  161. T yl = pow_dn(static_cast<T>(-x.upper()), pwr, rnd);
  162. T yu = pow_up(static_cast<T>(-x.lower()), pwr, rnd);
  163. if (pwr & 1) // [-2,-1]^1
  164. return I(-yu, -yl, true);
  165. else // [-2,-1]^2
  166. return I(yl, yu, true);
  167. } else if (interval_lib::user::is_neg(x.lower())) { // [-1,1]
  168. if (pwr & 1) { // [-1,1]^1
  169. return I(-pow_up(static_cast<T>(-x.lower()), pwr, rnd), pow_up(x.upper(), pwr, rnd), true);
  170. } else { // [-1,1]^2
  171. return I(static_cast<T>(0), pow_up(max BOOST_PREVENT_MACRO_SUBSTITUTION(static_cast<T>(-x.lower()), x.upper()), pwr, rnd), true);
  172. }
  173. } else { // [1,2]
  174. return I(pow_dn(x.lower(), pwr, rnd), pow_up(x.upper(), pwr, rnd), true);
  175. }
  176. }
  177. template<class T, class Policies> inline
  178. interval<T, Policies> sqrt(const interval<T, Policies>& x)
  179. {
  180. typedef interval<T, Policies> I;
  181. if (interval_lib::detail::test_input(x) || interval_lib::user::is_neg(x.upper()))
  182. return I::empty();
  183. typename Policies::rounding rnd;
  184. T l = !interval_lib::user::is_pos(x.lower()) ? static_cast<T>(0) : rnd.sqrt_down(x.lower());
  185. return I(l, rnd.sqrt_up(x.upper()), true);
  186. }
  187. template<class T, class Policies> inline
  188. interval<T, Policies> square(const interval<T, Policies>& x)
  189. {
  190. typedef interval<T, Policies> I;
  191. if (interval_lib::detail::test_input(x))
  192. return I::empty();
  193. typename Policies::rounding rnd;
  194. const T& xl = x.lower();
  195. const T& xu = x.upper();
  196. if (interval_lib::user::is_neg(xu))
  197. return I(rnd.mul_down(xu, xu), rnd.mul_up(xl, xl), true);
  198. else if (interval_lib::user::is_pos(x.lower()))
  199. return I(rnd.mul_down(xl, xl), rnd.mul_up(xu, xu), true);
  200. else
  201. return I(static_cast<T>(0), (-xl > xu ? rnd.mul_up(xl, xl) : rnd.mul_up(xu, xu)), true);
  202. }
  203. namespace interval_lib {
  204. namespace detail {
  205. template< class I > inline
  206. I root_aux(typename I::base_type const &x, int k) // x and k are bigger than one
  207. {
  208. typedef typename I::base_type T;
  209. T tk(k);
  210. I y(static_cast<T>(1), x, true);
  211. for(;;) {
  212. T y0 = median(y);
  213. I yy = intersect(y, y0 - (pow(I(y0, y0, true), k) - x) / (tk * pow(y, k - 1)));
  214. if (equal(y, yy)) return y;
  215. y = yy;
  216. }
  217. }
  218. template< class I > inline // x is positive and k bigger than one
  219. typename I::base_type root_aux_dn(typename I::base_type const &x, int k)
  220. {
  221. typedef typename I::base_type T;
  222. typedef typename I::traits_type Policies;
  223. typename Policies::rounding rnd;
  224. T one(1);
  225. if (x > one) return root_aux<I>(x, k).lower();
  226. if (x == one) return one;
  227. return rnd.div_down(one, root_aux<I>(rnd.div_up(one, x), k).upper());
  228. }
  229. template< class I > inline // x is positive and k bigger than one
  230. typename I::base_type root_aux_up(typename I::base_type const &x, int k)
  231. {
  232. typedef typename I::base_type T;
  233. typedef typename I::traits_type Policies;
  234. typename Policies::rounding rnd;
  235. T one(1);
  236. if (x > one) return root_aux<I>(x, k).upper();
  237. if (x == one) return one;
  238. return rnd.div_up(one, root_aux<I>(rnd.div_down(one, x), k).lower());
  239. }
  240. } // namespace detail
  241. } // namespace interval_lib
  242. template< class T, class Policies > inline
  243. interval<T, Policies> nth_root(interval<T, Policies> const &x, int k)
  244. {
  245. typedef interval<T, Policies> I;
  246. if (interval_lib::detail::test_input(x)) return I::empty();
  247. assert(k > 0);
  248. if (k == 1) return x;
  249. typename Policies::rounding rnd;
  250. typedef typename interval_lib::unprotect<I>::type R;
  251. if (!interval_lib::user::is_pos(x.upper())) {
  252. if (interval_lib::user::is_zero(x.upper())) {
  253. T zero(0);
  254. if (!(k & 1) || interval_lib::user::is_zero(x.lower())) // [-1,0]^/2 or [0,0]
  255. return I(zero, zero, true);
  256. else // [-1,0]^/3
  257. return I(-interval_lib::detail::root_aux_up<R>(-x.lower(), k), zero, true);
  258. } else if (!(k & 1)) // [-2,-1]^/2
  259. return I::empty();
  260. else { // [-2,-1]^/3
  261. return I(-interval_lib::detail::root_aux_up<R>(-x.lower(), k),
  262. -interval_lib::detail::root_aux_dn<R>(-x.upper(), k), true);
  263. }
  264. }
  265. T u = interval_lib::detail::root_aux_up<R>(x.upper(), k);
  266. if (!interval_lib::user::is_pos(x.lower()))
  267. if (!(k & 1) || interval_lib::user::is_zero(x.lower())) // [-1,1]^/2 or [0,1]
  268. return I(static_cast<T>(0), u, true);
  269. else // [-1,1]^/3
  270. return I(-interval_lib::detail::root_aux_up<R>(-x.lower(), k), u, true);
  271. else // [1,2]
  272. return I(interval_lib::detail::root_aux_dn<R>(x.lower(), k), u, true);
  273. }
  274. } // namespace numeric
  275. } // namespace boost
  276. #endif // BOOST_NUMERIC_INTERVAL_ARITH2_HPP