sigma  1.0.0
Loading...
Searching...
No Matches
pow.hpp
Go to the documentation of this file.
1#pragma once
3#include <stdexcept>
4
9
10namespace sigma::detail {
11
54template<typename X, typename U, typename MakePoint>
55X pow_impl(const X& a, const U& power, MakePoint&& make_point) {
56 using value_t = typename X::value_t;
57 U zero(0);
58 if(a.empty()) { return a; }
59 if(power == zero) { return make_point(value_t(1.0)); }
60
61 if(a.contains(zero) && power < zero) {
62 throw std::domain_error(
63 "Can not raise a form containing 0 to a negative power.");
64 } else if(a.contains(zero) && power > zero) {
65 auto a_range = a.range();
66 if(a_range.lower() == value_t(0.0) && a_range.upper() == value_t(0.0)) {
67 // a is exactly the point 0: 0^power = 0 for any power > 0.
68 return make_point(value_t(0.0));
69 }
70 // The range/bound genuinely straddles or touches 0 alongside other
71 // values, so x^power is real-valued only for a positive integer
72 // power (a negative base raised to a fractional power is not a
73 // real number); compute via repeated multiplication instead, which
74 // operator* already handles correctly (and, for TaylorModel,
75 // rigorously -- see Eq. \f$\eqref{eq:tm-mul}\f$) across a
76 // zero-containing range.
77 if(!is_integer_exponent(power)) {
78 throw std::domain_error(
79 "Can not raise a form containing 0 to a non-integer power.");
80 }
81 X result = make_point(value_t(1.0));
82 for(long long i = 0; i < static_cast<long long>(power); ++i) {
83 result = result * a;
84 }
85 return result;
86 }
87
88 // Handle cases where the range/bound is strictly negative
89 if(a.range().upper() < zero) {
90 if(!is_integer_exponent(power)) {
91 throw std::domain_error(
92 "Can not raise a form with negative values to a non-integer "
93 "power.");
94 }
95
96 auto abs_log = log(-a);
97 auto pow_abs = exp(value_t(power) * abs_log);
98 const bool power_is_even = static_cast<long long>(power) % 2 == 0;
99 return power_is_even ? pow_abs : -pow_abs;
100 }
101
102 // Handle cases where the range/bound is strictly positive
103 auto loga = log(a);
104 return exp(loga * power);
105}
106
107} // namespace sigma::detail
Affine< T > exp(const Affine< T > &a)
Calculate the exponential of an affine form.
Definition exponents.ipp:31
Affine< T > log(const Affine< T > &a)
Calculate the natural logarithm of an affine form.
Definition exponents.ipp:51
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
X pow_impl(const X &a, const U &power, MakePoint &&make_point)
Implements x^y, shared by Affine::pow, Taylor::pow, and TaylorModel::pow.
Definition pow.hpp:55