sigma  1.0.0
Loading...
Searching...
No Matches
exponents.ipp
1#pragma once
4#include <cmath>
5#include <limits>
6#include <stdexcept>
7
8namespace sigma {
9namespace detail_ {
10
27template<typename T, typename U>
29 const auto value = static_cast<T>(exp);
30 if(static_cast<U>(value) == exp) { return boost_interval_t<T>(value); }
31 constexpr auto inf = std::numeric_limits<T>::infinity();
32 return boost_interval_t<T>(std::nextafter(value, -inf),
33 std::nextafter(value, inf));
34}
35
36} // namespace detail_
37
38template<typename T>
40 if(a.empty()) { return Interval<T>(); }
41 // Silently dropping the part of a that is out of domain would answer a
42 // question that was not asked, so say so instead. Boost would clamp the
43 // lower bound to zero.
44 if(a.lower() < T(0)) {
45 throw std::domain_error("Interval has negative values.");
46 }
47 return detail_::from_boost_(boost::numeric::sqrt(detail_::to_boost_(a)));
48}
49
50template<typename T>
52 if(a.empty()) { return Interval<T>(); }
53 return detail_::from_boost_(boost::numeric::exp(detail_::to_boost_(a)));
54}
55
56template<typename T>
58 if(a.empty()) { return Interval<T>(); }
59 // The domain is the strictly positive reals, so a lower bound at zero is
60 // out of domain. Boost would clamp instead.
61 if(a.lower() <= T(0)) {
62 throw std::domain_error("Interval has non-positive values.");
63 }
64 return detail_::from_boost_(boost::numeric::log(detail_::to_boost_(a)));
65}
66
67template<typename T, typename U>
68Interval<T> pow(const Interval<T>& a, const U& exp) {
69 if(a.empty()) { return Interval<T>(); }
70 if(exp == U(0)) { return Interval<T>(T(1), T(1)); }
71
72 // A negative exponent is a reciprocal, and dividing by an interval that
73 // contains zero is the error operator/= already rejects.
74 if(exp < U(0) && a.contains(T(0))) {
75 throw std::domain_error(
76 "Can not raise an interval containing 0 to a negative power.");
77 }
78
79 auto x = detail_::to_boost_(a);
80
82 // A negative base raised to a fractional power is not a real number,
83 // so the base must be non-negative. Zero itself is fine for a positive
84 // exponent: the evaluation below runs through log, whose unbounded
85 // result exponentiates back to the 0 that 0^exp really is.
86 if(a.lower() < T(0)) {
87 throw std::domain_error("Can not raise an interval with negative "
88 "values to a non-integer power.");
89 }
90 // Away from that, a non-integer power is monotonic: increasing for a
91 // positive exponent, decreasing for a negative one.
92 auto y = boost::numeric::exp(detail_::exponent_interval_<T>(exp) *
93 boost::numeric::log(x));
94 return detail_::from_boost_(y);
95 }
96
97 const auto power = static_cast<long long>(exp);
99 boost::numeric::pow(x, static_cast<int>(power)));
100}
101
102template<typename T>
103Interval<T> pow(const Interval<T>& a, const Interval<T>& exponent) {
104 if(a.empty() || exponent.empty()) { return Interval<T>(); }
105 // The point-exponent case is expected to be far more common than a
106 // genuinely uncertain exponent, so it is forwarded to the scalar
107 // overload for tighter bounds and that overload's more permissive
108 // domain restriction (e.g., a negative base raised to an integer power
109 // is fine, even though it is not a valid base for log()).
110 if(exponent.width() == T(0)) { return pow(a, exponent.lower()); }
111 // log() enforces the strictly-positive-base domain restriction and
112 // throws std::domain_error itself, so there's nothing left to check
113 // here.
114 return sigma::exp(exponent * sigma::log(a));
115}
116
117} // namespace sigma
Models a closed numeric interval.
Definition interval.hpp:29
value_t width() const
Returns the distance between the interval's bounds.
Definition interval.hpp:73
value_t lower() const
Returns the lower bound of the interval.
Definition interval.hpp:116
bool contains(value_t value) const
Whether a scalar lies in this interval.
Definition interval.hpp:144
bool empty() const
Is *this the empty interval?
Definition interval.hpp:108
Conversions between sigma::Interval and the boost interval it wraps.
The namespace that contains the implementation details of the library.
Definition convert.hpp:18
Interval< T > from_boost_(const boost_interval_t< T > &x)
Convert a boost interval into a sigma::Interval.
Definition convert.hpp:48
boost_interval_t< T > exponent_interval_(const U &exp)
The exponent of a non-integer pow(), as an interval.
Definition exponents.ipp:28
boost_interval_t< T > to_boost_(const Interval< T > &a)
Convert a sigma::Interval into the boost interval it wraps.
Definition convert.hpp:31
boost::numeric::interval< T, interval_policies_t< T > > boost_interval_t
The boost interval type sigma::Interval wraps.
Definition policies.hpp:485
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