sigma  1.0.0
Loading...
Searching...
No Matches
exponents.ipp
1#pragma once
5#include <algorithm>
6#include <cmath>
7#include <cstdlib>
8#include <stdexcept>
9#include <utility>
10
11namespace sigma {
12namespace detail_ {
13
30template<typename T>
31std::pair<bool, bool> integer_pow_openness_(const GeneralInterval<T>& a,
32 long long n) {
33 const bool is_odd = (n % 2 != 0);
34 bool left_open = a.left_open();
35 bool right_open = a.right_open();
36
37 if(a.upper() <= T(0)) {
38 // Entirely non-positive. An odd power is increasing, an even one is
39 // decreasing, which swaps which input bound each output bound came
40 // from. A zero upper bound belongs here, not to the next branch: it
41 // is an input bound, so it is attained only if it is closed.
42 if(!is_odd) { std::swap(left_open, right_open); }
43 } else if(a.lower() < T(0)) {
44 // Spans zero. An odd power is still increasing across all of it, but
45 // an even power dips to an interior minimum at x == 0 and takes its
46 // maximum at whichever endpoint is farther from zero.
47 if(!is_odd) {
48 const T abs_lower = -a.lower();
49 left_open = false; // 0 is attained, and is an interior
50 // point rather than an input bound
51 if(abs_lower > a.upper()) {
52 right_open = a.left_open();
53 } else if(abs_lower < a.upper()) {
54 right_open = a.right_open();
55 } else {
56 right_open = a.left_open() && a.right_open();
57 }
58 }
59 }
60 // Entirely non-negative: increasing, so the openness carries over as-is.
61
62 return {left_open, right_open};
63}
64
65} // namespace detail_
66
67template<typename T>
69 if(a.empty()) { return GeneralInterval<T>(); }
70 // Silently dropping the part of a that is out of domain would answer a
71 // question that was not asked, so say so instead. Boost would clamp the
72 // lower bound to zero.
73 if(a.lower() < T(0)) {
74 throw std::domain_error("Interval has negative values.");
75 }
76 // Square root is increasing, so the openness of each bound carries over.
78 boost::numeric::sqrt(detail_::to_boost_(a)), a.left_open(),
79 a.right_open());
80}
81
82template<typename T>
84 if(a.empty()) { return GeneralInterval<T>(); }
86 boost::numeric::exp(detail_::to_boost_(a)), a.left_open(),
87 a.right_open());
88}
89
90template<typename T>
92 if(a.empty()) { return GeneralInterval<T>(); }
93 // The domain is the strictly positive reals, so a closed bound at zero is
94 // out of domain while an open one is not: (0, 1] contains no non-positive
95 // value even though its lower bound is zero. Boost would clamp instead.
96 if(a.lower() < T(0) || (a.lower() == T(0) && a.left_closed())) {
97 throw std::domain_error("Interval has non-positive values.");
98 }
99 // An argument bounded below by an open zero is unbounded below in the
100 // result, and from_boost_ opens that infinite bound for us. Logarithm is
101 // increasing, so the openness of each bound otherwise carries over.
103 boost::numeric::log(detail_::to_boost_(a)), a.left_open(),
104 a.right_open());
105}
106
107template<typename T, typename U>
109 if(a.empty()) { return GeneralInterval<T>(); }
110 if(exp == U(0)) { return GeneralInterval<T>(T(1), T(1)); }
111
112 // A negative exponent is a reciprocal, and dividing by an interval that
113 // contains zero is the error operator/= already rejects.
114 if(exp < U(0) && a.contains(T(0))) {
115 throw std::domain_error(
116 "Can not raise an interval containing 0 to a negative power.");
117 }
118
119 auto x = detail_::to_boost_(a);
120
122 // A negative base raised to a fractional power is not a real number,
123 // so the base must be non-negative. Zero itself is fine for a positive
124 // exponent: the evaluation below runs through log, whose unbounded
125 // result exponentiates back to the 0 that 0^exp really is.
126 if(a.lower() < T(0)) {
127 throw std::domain_error("Can not raise an interval with negative "
128 "values to a non-integer power.");
129 }
130 // Away from that, a non-integer power is monotonic: increasing for a
131 // positive exponent, decreasing for a negative one.
132 auto y = boost::numeric::exp(detail_::exponent_interval_<T>(exp) *
133 boost::numeric::log(x));
134 if(exp < U(0)) {
136 a.left_open());
137 }
139 }
140
141 const auto power = static_cast<long long>(exp);
142 auto [left_open, right_open] =
143 detail_::integer_pow_openness_(a, std::abs(power));
144 // A negative power is the reciprocal of the positive one, which reverses
145 // the interval and so exchanges the openness of its bounds.
146 if(power < 0) { std::swap(left_open, right_open); }
147
149 boost::numeric::pow(x, static_cast<int>(power)), left_open, right_open);
150}
151
152template<typename T>
154 const GeneralInterval<T>& exponent) {
155 if(a.empty() || exponent.empty()) { return GeneralInterval<T>(); }
156 // The point-exponent case is expected to be far more common than a
157 // genuinely uncertain exponent, so it is forwarded to the scalar
158 // overload for tighter bounds and that overload's more permissive
159 // domain restriction (e.g., a negative base raised to an integer power
160 // is fine, even though it is not a valid base for log()).
161 if(exponent.width() == T(0)) { return pow(a, exponent.lower()); }
162 // log() enforces the strictly-positive-base domain restriction and
163 // throws std::domain_error itself, so there's nothing left to check
164 // here.
165 return sigma::exp(exponent * sigma::log(a));
166}
167
168} // namespace sigma
Models an interval whose bounds may be open.
Definition general_interval.hpp:36
value_t width() const
Returns the distance between the interval's bounds.
Definition general_interval.hpp:103
bool contains(value_t value) const
Whether a scalar lies in this interval.
Definition general_interval.hpp:492
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
bool left_closed() const
Is lower() contained in the interval?
Definition general_interval.hpp:144
value_t lower() const
Returns the lower bound of the interval.
Definition general_interval.hpp:169
value_t upper() const
Returns the upper bound of the interval.
Definition general_interval.hpp:177
Conversions between sigma::GeneralInterval and the boost interval its closure wraps.
Exponent operations for interval variables.
The namespace that contains the implementation details of the library.
Definition convert.hpp:18
boost_interval_t< T > exponent_interval_(const U &exp)
The exponent of a non-integer pow(), as an interval.
Definition exponents.ipp:28
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
std::pair< bool, bool > integer_pow_openness_(const GeneralInterval< T > &a, long long n)
The openness of the bounds of pow(a, n) for a positive integer n.
Definition exponents.ipp:31
The primary namespace for the sigma library.
Definition affine.hpp:12
Affine< T > exp(const Affine< T > &a)
Calculate the exponential of an affine form.
Definition exponents.ipp:31
Affine< T > pow(const Affine< T > &a, const U &exp)
Calculate the power of an affine form.
Definition exponents.ipp:72
Affine< T > log(const Affine< T > &a)
Calculate the natural logarithm of an affine form.
Definition exponents.ipp:51
Affine< T > sqrt(const Affine< T > &a)
Calculate the square root of an affine form.
Definition exponents.ipp:9
Small numeric helpers shared across Sigma's representations.
bool is_integer_exponent(const U &exp)
True if exp has no fractional part.
Definition numeric.hpp:27