sigma  1.0.0
Loading...
Searching...
No Matches
exponents.ipp
1#pragma once
2#include <cmath>
5
6namespace sigma {
7
8template<typename T>
10 if(a.empty()) { return a; }
11 auto a_range = a.range();
12 auto lo = a_range.lower();
13 auto hi = a_range.upper();
14 if(lo < 0) { throw std::domain_error("Affine form has negative values."); }
15
16 auto sqrt_lower = std::sqrt(lo);
17 auto sqrt_upper = std::sqrt(hi);
18
19 auto sqrt_sum = sqrt_upper + sqrt_lower;
20 auto sqrt_diff = sqrt_upper - sqrt_lower;
21 auto sqrt_prod = sqrt_upper * sqrt_lower;
22
23 auto alpha = T(1.0) / (sqrt_sum);
24 auto zeta = sqrt_sum / T(8.0) + T(0.5) * sqrt_prod / sqrt_sum;
25 auto delta = sqrt_diff * sqrt_diff / (T(8.0) * sqrt_sum);
26
27 return a.apply_affine_transform(alpha, zeta, delta);
28}
29
30template<typename T>
32 auto a_range = a.range();
33 auto hi = a_range.upper();
34 auto lo = a_range.lower();
35 if(hi - lo == 0) { return Affine<T>(std::exp(lo)); }
36
37 T one(1.0);
38 T two(2.0);
39 auto exp_lower = std::exp(lo);
40 auto exp_upper = std::exp(hi);
41 auto alpha = (exp_upper - exp_lower) / (hi - lo);
42 auto log_alpha = std::log(alpha);
43 auto c_chord = exp_lower - alpha * lo;
44 auto c_tangent = alpha * (one - log_alpha);
45 auto zeta = (c_chord + c_tangent) / two;
46 auto delta = (c_chord - c_tangent) / two;
47 return a.apply_affine_transform(alpha, zeta, delta);
48}
49
50template<typename T>
52 auto a_range = a.range();
53 auto hi = a_range.upper();
54 auto lo = a_range.lower();
55 if(lo <= 0) {
56 throw std::domain_error("Affine form has non-positive values.");
57 }
58 if(hi - lo == 0) { return Affine<T>(std::log(lo)); }
59
60 auto log_lo = std::log(lo);
61 auto alpha = (std::log(hi) - log_lo) / (hi - lo);
62 auto xs = T(1.0) / alpha;
63 auto ys = alpha * (xs - lo) + log_lo;
64 auto log_xs = std::log(xs);
65 auto zeta = (log_xs + ys) / 2 - alpha * xs;
66 auto delta = (log_xs - ys) / 2;
67
68 return a.apply_affine_transform(alpha, zeta, delta);
69}
70
71template<typename T, typename U>
72Affine<T> pow(const Affine<T>& a, const U& exp) {
73 return detail::pow_impl(a, exp, [](T v) { return Affine<T>(v); });
74}
75
76} // namespace sigma
Defines the Affine class.
Implements affine arithmetic.
Definition affine.hpp:48
Affine apply_affine_transform(value_t alpha, value_t zeta, value_t delta) const
Applies an affine transformation to *this.
Definition affine.hpp:673
bool empty() const noexcept
Checks if this affine form is representing an empty interval.
Definition affine.hpp:313
interval_t range() const
Returns the interval represented by the affine form.
Definition affine.hpp:856
value_t upper() const
Returns the upper bound of the interval.
Definition interval.hpp:127
value_t lower() const
Returns the lower bound of the interval.
Definition interval.hpp:116
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
Shared implementation of pow() for Affine, Taylor, and TaylorModel.
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