source: NonGTP/Boost/boost/numeric/interval/transc.hpp @ 857

Revision 857, 8.0 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]1/* Boost interval/transc.hpp template implementation file
2 *
3 * Copyright 2000 Jens Maurer
4 * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
5 *
6 * Distributed under the Boost Software License, Version 1.0.
7 * (See accompanying file LICENSE_1_0.txt or
8 * copy at http://www.boost.org/LICENSE_1_0.txt)
9 */
10
11#ifndef BOOST_NUMERIC_INTERVAL_TRANSC_HPP
12#define BOOST_NUMERIC_INTERVAL_TRANSC_HPP
13
14#include <boost/config.hpp>
15#include <boost/numeric/interval/detail/interval_prototype.hpp>
16#include <boost/numeric/interval/detail/bugs.hpp>
17#include <boost/numeric/interval/detail/test_input.hpp>
18#include <boost/numeric/interval/rounding.hpp>
19#include <boost/numeric/interval/constants.hpp>
20#include <boost/numeric/interval/arith.hpp>
21#include <boost/numeric/interval/arith2.hpp>
22#include <algorithm>
23
24namespace boost {
25namespace numeric {
26
27template<class T, class Policies> inline
28interval<T, Policies> exp(const interval<T, Policies>& x)
29{
30  typedef interval<T, Policies> I;
31  if (interval_lib::detail::test_input(x))
32    return I::empty();
33  typename Policies::rounding rnd;
34  return I(rnd.exp_down(x.lower()), rnd.exp_up(x.upper()), true);
35}
36
37template<class T, class Policies> inline
38interval<T, Policies> log(const interval<T, Policies>& x)
39{
40  typedef interval<T, Policies> I;
41  if (interval_lib::detail::test_input(x) ||
42      !interval_lib::user::is_pos(x.upper()))
43    return I::empty();
44  typename Policies::rounding rnd;
45  typedef typename Policies::checking checking;
46  T l = !interval_lib::user::is_pos(x.lower())
47             ? checking::neg_inf() : rnd.log_down(x.lower());
48  return I(l, rnd.log_up(x.upper()), true);
49}
50
51template<class T, class Policies> inline
52interval<T, Policies> cos(const interval<T, Policies>& x)
53{
54  if (interval_lib::detail::test_input(x))
55    return interval<T, Policies>::empty();
56  typename Policies::rounding rnd;
57  typedef interval<T, Policies> I;
58  typedef typename interval_lib::unprotect<I>::type R;
59
60  // get lower bound within [0, pi]
61  const R pi2 = interval_lib::pi_twice<R>();
62  R tmp = fmod((const R&)x, pi2);
63  if (width(tmp) >= pi2.lower())
64    return I(static_cast<T>(-1), static_cast<T>(1), true); // we are covering a full period
65  if (tmp.lower() >= interval_lib::constants::pi_upper<T>())
66    return -cos(tmp - interval_lib::pi<R>());
67  T l = tmp.lower();
68  T u = tmp.upper();
69
70  BOOST_USING_STD_MIN();
71  // separate into monotone subintervals
72  if (u <= interval_lib::constants::pi_lower<T>())
73    return I(rnd.cos_down(u), rnd.cos_up(l), true);
74  else if (u <= pi2.lower())
75    return I(static_cast<T>(-1), rnd.cos_up(min BOOST_PREVENT_MACRO_SUBSTITUTION(rnd.sub_down(pi2.lower(), u), l)), true);
76  else
77    return I(static_cast<T>(-1), static_cast<T>(1), true);
78}
79
80template<class T, class Policies> inline
81interval<T, Policies> sin(const interval<T, Policies>& x)
82{
83  typedef interval<T, Policies> I;
84  if (interval_lib::detail::test_input(x))
85    return I::empty();
86  typename Policies::rounding rnd;
87  typedef typename interval_lib::unprotect<I>::type R;
88  I r = cos((const R&)x - interval_lib::pi_half<R>());
89  (void)&rnd;
90  return r;
91}
92
93template<class T, class Policies> inline
94interval<T, Policies> tan(const interval<T, Policies>& x)
95{
96  typedef interval<T, Policies> I;
97  if (interval_lib::detail::test_input(x))
98    return I::empty();
99  typename Policies::rounding rnd;
100  typedef typename interval_lib::unprotect<I>::type R;
101
102  // get lower bound within [-pi/2, pi/2]
103  const R pi = interval_lib::pi<R>();
104  R tmp = fmod((const R&)x, pi);
105  const T pi_half_d = interval_lib::constants::pi_half_lower<T>();
106  if (tmp.lower() >= pi_half_d)
107    tmp -= pi;
108  if (tmp.lower() <= -pi_half_d || tmp.upper() >= pi_half_d)
109    return I::whole();
110  return I(rnd.tan_down(tmp.lower()), rnd.tan_up(tmp.upper()), true);
111}
112
113template<class T, class Policies> inline
114interval<T, Policies> asin(const interval<T, Policies>& x)
115{
116  typedef interval<T, Policies> I;
117  if (interval_lib::detail::test_input(x)
118     || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
119    return I::empty();
120  typename Policies::rounding rnd;
121  T l = (x.lower() <= static_cast<T>(-1))
122             ? -interval_lib::constants::pi_half_upper<T>()
123             : rnd.asin_down(x.lower());
124  T u = (x.upper() >= static_cast<T>(1) )
125             ?  interval_lib::constants::pi_half_upper<T>()
126             : rnd.asin_up  (x.upper());
127  return I(l, u, true);
128}
129
130template<class T, class Policies> inline
131interval<T, Policies> acos(const interval<T, Policies>& x)
132{
133  typedef interval<T, Policies> I;
134  if (interval_lib::detail::test_input(x)
135     || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
136    return I::empty();
137  typename Policies::rounding rnd;
138  T l = (x.upper() >= static_cast<T>(1) )
139          ? static_cast<T>(0)
140          : rnd.acos_down(x.upper());
141  T u = (x.lower() <= static_cast<T>(-1))
142          ? interval_lib::constants::pi_upper<T>()
143          : rnd.acos_up  (x.lower());
144  return I(l, u, true);
145}
146
147template<class T, class Policies> inline
148interval<T, Policies> atan(const interval<T, Policies>& x)
149{
150  typedef interval<T, Policies> I;
151  if (interval_lib::detail::test_input(x))
152    return I::empty();
153  typename Policies::rounding rnd;
154  return I(rnd.atan_down(x.lower()), rnd.atan_up(x.upper()), true);
155}
156
157template<class T, class Policies> inline
158interval<T, Policies> sinh(const interval<T, Policies>& x)
159{
160  typedef interval<T, Policies> I;
161  if (interval_lib::detail::test_input(x))
162    return I::empty();
163  typename Policies::rounding rnd;
164  return I(rnd.sinh_down(x.lower()), rnd.sinh_up(x.upper()), true);
165}
166
167template<class T, class Policies> inline
168interval<T, Policies> cosh(const interval<T, Policies>& x)
169{
170  typedef interval<T, Policies> I;
171  if (interval_lib::detail::test_input(x))
172    return I::empty();
173  typename Policies::rounding rnd;
174  if (interval_lib::user::is_neg(x.upper()))
175    return I(rnd.cosh_down(x.upper()), rnd.cosh_up(x.lower()), true);
176  else if (!interval_lib::user::is_neg(x.lower()))
177    return I(rnd.cosh_down(x.lower()), rnd.cosh_up(x.upper()), true);
178  else
179    return I(static_cast<T>(0), rnd.cosh_up(-x.lower() > x.upper() ? x.lower() : x.upper()), true);
180}
181
182template<class T, class Policies> inline
183interval<T, Policies> tanh(const interval<T, Policies>& x)
184{
185  typedef interval<T, Policies> I;
186  if (interval_lib::detail::test_input(x))
187    return I::empty();
188  typename Policies::rounding rnd;
189  return I(rnd.tanh_down(x.lower()), rnd.tanh_up(x.upper()), true);
190}
191
192template<class T, class Policies> inline
193interval<T, Policies> asinh(const interval<T, Policies>& x)
194{
195  typedef interval<T, Policies> I;
196  if (interval_lib::detail::test_input(x))
197    return I::empty();
198  typename Policies::rounding rnd;
199  return I(rnd.asinh_down(x.lower()), rnd.asinh_up(x.upper()), true);
200}
201
202template<class T, class Policies> inline
203interval<T, Policies> acosh(const interval<T, Policies>& x)
204{
205  typedef interval<T, Policies> I;
206  if (interval_lib::detail::test_input(x) || x.upper() < static_cast<T>(1))
207    return I::empty();
208  typename Policies::rounding rnd;
209  T l = x.lower() <= static_cast<T>(1) ? static_cast<T>(0) : rnd.acosh_down(x.lower());
210  return I(l, rnd.acosh_up(x.upper()), true);
211}
212
213template<class T, class Policies> inline
214interval<T, Policies> atanh(const interval<T, Policies>& x)
215{
216  typedef interval<T, Policies> I;
217  if (interval_lib::detail::test_input(x)
218      || x.upper() < static_cast<T>(-1) || x.lower() > static_cast<T>(1))
219    return I::empty();
220  typename Policies::rounding rnd;
221  typedef typename Policies::checking checking;
222  T l = (x.lower() <= static_cast<T>(-1))
223             ? checking::neg_inf() : rnd.atanh_down(x.lower());
224  T u = (x.upper() >= static_cast<T>(1) )
225             ? checking::pos_inf() : rnd.atanh_up  (x.upper());
226  return I(l, u, true);
227}
228
229} // namespace numeric
230} // namespace boost
231
232#endif // BOOST_NUMERIC_INTERVAL_TRANSC_HPP
Note: See TracBrowser for help on using the repository browser.