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 GeneralInterval<T>(); }
11 // Each bound may come from a peak interior to a rather than from a bound
12 // of a, so both are reported closed.
13 return detail_::general_from_boost_(boost::numeric::sin(detail_::to_boost_(a)));
14}
15
16template<typename T>
18 if(a.empty()) { return GeneralInterval<T>(); }
19 return detail_::general_from_boost_(boost::numeric::cos(detail_::to_boost_(a)));
20}
21
22template<typename T>
24 if(a.empty()) { return GeneralInterval<T>(); }
25
26 auto y = boost::numeric::tan(detail_::to_boost_(a));
27
28 // Boost reduces the argument against the period and reports "this reaches
29 // a pole" by returning the whole real line, which is the only way it
30 // produces an unbounded result: every other path bounds the argument
31 // strictly inside a single branch, where the tangent is finite. That makes
32 // the test below exactly boost's own pole detection, with no period
33 // arithmetic of ours to get wrong.
34 //
35 // The detection is conservative, as it must be: an odd multiple of pi/2 is
36 // not representable, so for an argument whose bound sits within rounding
37 // distance of one, the reduction cannot certify that the pole is excluded.
38 // Such an argument is refused rather than answered.
39 if(std::isinf(y.lower()) && std::isinf(y.upper())) {
40 throw std::domain_error("Interval contains an odd multiple of pi/2, "
41 "where the tangent is undefined.");
42 }
43
44 // Every remaining argument lies strictly inside one branch, on which the
45 // tangent is increasing, so the openness of each bound carries over.
47}
48
49template<typename T>
51 if(a.empty()) { return GeneralInterval<T>(); }
52 // Trimming the argument back to the domain would answer a narrower
53 // question than the one asked. Boost would do exactly that.
54 detail_::assert_within_unit_(a, false, "arcsine");
55 // Increasing, so the openness of each bound carries over.
56 return detail_::general_from_boost_(boost::numeric::asin(detail_::to_boost_(a)),
57 a.left_open(), a.right_open());
58}
59
60template<typename T>
62 if(a.empty()) { return GeneralInterval<T>(); }
63 detail_::assert_within_unit_(a, false, "arccosine");
64 // Decreasing, so the lower bound of the result comes from the upper bound
65 // of a and vice versa.
66 return detail_::general_from_boost_(boost::numeric::acos(detail_::to_boost_(a)),
67 a.right_open(), a.left_open());
68}
69
70template<typename T>
72 if(a.empty()) { return GeneralInterval<T>(); }
73 return detail_::general_from_boost_(boost::numeric::atan(detail_::to_boost_(a)),
74 a.left_open(), a.right_open());
75}
76
77} // namespace sigma
Models an interval whose bounds may be open.
Definition general_interval.hpp:36
bool empty() const
Is *this the empty interval?
Definition general_interval.hpp:127
bool left_open() const
Is lower() NOT contained in the interval?
Definition general_interval.hpp:136
bool right_open() const
Is upper() NOT contained in the interval?
Definition general_interval.hpp:153
Conversions between sigma::GeneralInterval and the boost interval its closure wraps.
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
GeneralInterval< T > general_from_boost_(const boost_interval_t< T > &x, bool left_open=false, bool right_open=false)
Convert a boost interval into a sigma::GeneralInterval.
Definition general_convert.hpp:65
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