sigma  1.0.0
Loading...
Searching...
No Matches
trigonometry.ipp
1#pragma once
3#include <cmath>
4#include <stdexcept>
5
6namespace sigma {
7
8template<typename T>
10 if(a.empty()) { return Interval<T>(); }
11 return detail_::from_boost_(boost::numeric::sin(detail_::to_boost_(a)));
12}
13
14template<typename T>
16 if(a.empty()) { return Interval<T>(); }
17 return detail_::from_boost_(boost::numeric::cos(detail_::to_boost_(a)));
18}
19
20template<typename T>
22 if(a.empty()) { return Interval<T>(); }
23
24 auto y = boost::numeric::tan(detail_::to_boost_(a));
25
26 // Boost reduces the argument against the period and reports "this reaches
27 // a pole" by returning the whole real line, which is the only way it
28 // produces an unbounded result: every other path bounds the argument
29 // strictly inside a single branch, where the tangent is finite. That makes
30 // the test below exactly boost's own pole detection, with no period
31 // arithmetic of ours to get wrong.
32 //
33 // The detection is conservative, as it must be: an odd multiple of pi/2 is
34 // not representable, so for an argument whose bound sits within rounding
35 // distance of one, the reduction cannot certify that the pole is excluded.
36 // Such an argument is refused rather than answered.
37 if(std::isinf(y.lower()) && std::isinf(y.upper())) {
38 throw std::domain_error("Interval contains an odd multiple of pi/2, "
39 "where the tangent is undefined.");
40 }
41
42 return detail_::from_boost_(y);
43}
44
45template<typename T>
47 if(a.empty()) { return Interval<T>(); }
48 // Trimming the argument back to the domain would answer a narrower
49 // question than the one asked. Boost would do exactly that.
50 detail_::assert_within_unit_(a, false, "arcsine");
51 return detail_::from_boost_(boost::numeric::asin(detail_::to_boost_(a)));
52}
53
54template<typename T>
56 if(a.empty()) { return Interval<T>(); }
57 detail_::assert_within_unit_(a, false, "arccosine");
58 return detail_::from_boost_(boost::numeric::acos(detail_::to_boost_(a)));
59}
60
61template<typename T>
63 if(a.empty()) { return Interval<T>(); }
64 return detail_::from_boost_(boost::numeric::atan(detail_::to_boost_(a)));
65}
66
67} // namespace sigma
Models a closed numeric interval.
Definition interval.hpp:29
bool empty() const
Is *this the empty interval?
Definition interval.hpp:108
Conversions between sigma::Interval and the boost interval it wraps.
Interval< T > from_boost_(const boost_interval_t< T > &x)
Convert a boost interval into a sigma::Interval.
Definition convert.hpp:48
void assert_within_unit_(const Interval< T > &a, bool open, const std::string &name)
Throws unless every value in a lies in the unit interval.
Definition convert.hpp:74
boost_interval_t< T > to_boost_(const Interval< T > &a)
Convert a sigma::Interval into the boost interval it wraps.
Definition convert.hpp:31
The primary namespace for the sigma library.
Definition affine.hpp:12
GeneralInterval< T > sin(const GeneralInterval< T > &a)
Calculate the sine of a general interval.
Definition trigonometry.ipp:9
GeneralInterval< T > atan(const GeneralInterval< T > &a)
Calculate the arctangent of a general interval.
Definition trigonometry.ipp:71
GeneralInterval< T > cos(const GeneralInterval< T > &a)
Calculate the cosine of a general interval.
Definition trigonometry.ipp:17
GeneralInterval< T > asin(const GeneralInterval< T > &a)
Calculate the arcsine of a general interval.
Definition trigonometry.ipp:50
GeneralInterval< T > acos(const GeneralInterval< T > &a)
Calculate the arccosine of a general interval.
Definition trigonometry.ipp:61
GeneralInterval< T > tan(const GeneralInterval< T > &a)
Calculate the tangent of a general interval.
Definition trigonometry.ipp:23