50template<
typename ValueType>
76 using Order =
typename taylor_t::Order;
141 m_polynomial_(interval, order) {
142 if(interval.empty()) { return; }
143 auto radius = interval.radius();
144 if(radius > 0 && m_polynomial_.coefficients().empty()) {
145 m_remainder_ = interval_t(-radius, radius);
147 m_remainder_ = interval_t(value_t{0},
value_t{0});
207 return m_polynomial_.coefficients();
219 bool empty() const noexcept {
return m_polynomial_.empty(); }
243 if(
empty()) {
return false; }
244 return range().contains(value);
256 if(interval.empty()) {
return true; }
257 if(
empty()) {
return false; }
258 return range().contains(interval);
271 if(other.
empty()) {
return true; }
286 return m_polynomial_.print_polynomial_form();
300 return range().print_interval_form();
319 if(
empty()) {
return *
this; }
337 m_polynomial_ =
taylor_t(value,
Order(m_polynomial_.max_order()));
341 m_polynomial_ += value;
404 m_polynomial_ =
taylor_t(-value,
Order(m_polynomial_.max_order()));
408 m_polynomial_ -= value;
425 return *
this += -other;
470 if(
empty()) {
return *
this; }
471 m_polynomial_ *= value;
472 m_remainder_ *= value;
545 if(value == 0) {
throw std::domain_error(
"Division by zero"); }
546 return *
this *=
value_t(1.0 / value);
649 const std::vector<value_t>& outer_coeffs,
709 if(
empty() != other.
empty()) {
return false; }
710 if(
empty()) {
return true; }
711 return m_polynomial_ == other.m_polynomial_ &&
712 m_remainder_ == other.m_remainder_;
725 return !(*
this == other);
730 void assert_not_empty_()
const {
731 if(empty()) {
throw std::domain_error(
"TaylorModel is empty"); }
735 taylor_t m_polynomial_;
738 interval_t m_remainder_;
761template<
typename ValueType>
786template<
typename ValueType>
794template<
typename ValueType>
797 if(
empty()) {
return *
this = other; }
798 if(other.empty()) {
return *
this; }
799 auto new_order = std::min(
max_order(), other.max_order());
801 auto rhs = other.sweep_to_order(new_order);
802 return *
this =
TaylorModel(lhs.m_polynomial_ + rhs.m_polynomial_,
803 lhs.m_remainder_ + rhs.m_remainder_);
806template<
typename ValueType>
810 auto new_order = std::min(
max_order(), other.max_order());
812 auto rhs = other.sweep_to_order(new_order);
814 const auto& p1 = lhs.m_polynomial_;
815 const auto& p2 = rhs.m_polynomial_;
817 value_t new_constant = p1.constant() * p2.constant();
820 if(mono.degree() <= new_order) {
826 for(
auto&& [mono, coeff] : p2.coefficients()) {
827 insert(mono, p1.constant() * coeff);
829 for(
auto&& [mono, coeff] : p1.coefficients()) {
830 insert(mono, coeff * p2.constant());
832 for(
auto&& [mono1, coeff1] : p1.coefficients()) {
833 for(
auto&& [mono2, coeff2] : p2.coefficients()) {
834 insert(mono1 * mono2, coeff1 * coeff2);
841 tail_poly.bound() + p1.bound() * rhs.m_remainder_ +
842 p2.bound() * lhs.m_remainder_ + lhs.m_remainder_ * rhs.m_remainder_;
843 taylor_t new_poly(new_constant, std::move(kept),
Order(new_order));
844 return *
this =
TaylorModel(std::move(new_poly), std::move(new_remainder));
847template<
typename ValueType>
849 const std::vector<value_t>& outer_coeffs,
854 taylor_t new_poly = m_polynomial_.compose_(outer_coeffs);
855 interval_t bar_bound = m_polynomial_.bound() - m_polynomial_.constant();
856 interval_t enclosure = bar_bound + m_remainder_;
859 interval_t deriv_val = nth_derivative(xi_range);
862 deriv_val *
pow(enclosure,
static_cast<int>(order + 1)) / fact;
863 return TaylorModel(std::move(new_poly), std::move(new_remainder));
866template<
typename ValueType>
870 throw std::domain_error(
"Division by zero");
878 pow(xi,
static_cast<int>(order + 2));
882template<
typename ValueType>
887 for(
auto&& [mono, coeff] : m_polynomial_.coefficients()) {
888 if(mono.degree() <= new_order) {
895 Order(m_polynomial_.max_order()));
896 auto new_remainder = m_remainder_ + tail_poly.bound();
897 taylor_t new_poly(m_polynomial_.constant(), std::move(kept),
899 return TaylorModel(std::move(new_poly), std::move(new_remainder));
902template<
typename ValueType>
906 auto total_r =
range().radius();
908 for(
auto&& [mono, coeff] : m_polynomial_.coefficients()) {
909 if(total_r !=
value_t{0} && std::fabs(coeff) / total_r < threshold) {
916 Order(m_polynomial_.max_order()));
917 auto new_remainder = m_remainder_ + swept_poly.bound();
918 taylor_t new_poly(m_polynomial_.constant(), std::move(kept),
919 Order(m_polynomial_.max_order()));
920 return TaylorModel(std::move(new_poly), std::move(new_remainder));
951 if(a.
empty()) {
return a; }
952 auto a_range = a.
range();
953 if(a_range.lower() >= T(0.0)) {
return a; }
954 if(a_range.upper() <= T(0.0)) {
return -a; }
955 auto m = std::max(std::fabs(a_range.lower()), a_range.upper());
1001 if(a.
empty()) {
return a; }
1002 if(a.
range().lower() <= T(0.0)) {
1003 throw std::domain_error(
"TaylorModel has non-positive values.");
1010 return coeff *
pow(xi, T(0.5) -
static_cast<T
>(order + 1));
1034 if(a.
empty()) {
return a; }
1062 if(a.
empty()) {
return a; }
1063 if(a.
range().lower() <= T(0.0)) {
1064 throw std::domain_error(
"TaylorModel has non-positive values.");
1070 T sign = (order % 2 == 0) ? T{1} : T{-1};
1072 pow(xi,
static_cast<int>(order + 1));
1106template<
typename T,
typename U>
Pairs a Taylor polynomial with a remainder interval to guarantee Eq. .
Definition taylor_model.hpp:51
TaylorModel & operator+=(const TaylorModel &other)
Overwrites *this with the sum of *this and other.
Definition taylor_model.hpp:795
TaylorModel(value_t lo, value_t hi, Order order=default_max_order())
Constructs a TaylorModel from a lower and upper bound.
Definition taylor_model.hpp:116
typename taylor_t::interval_t interval_t
Definition taylor_model.hpp:72
float value_t
Definition taylor_model.hpp:57
TaylorModel compose_(const std::vector< value_t > &outer_coeffs, const std::function< interval_t(const interval_t &)> &nth_derivative) const
Evaluates a Taylor-model outer series about constant(), with a Lagrange remainder bound.
Definition taylor_model.hpp:848
TaylorModel operator*(const TaylorModel &other) const
Returns the product of *this and other.
Definition taylor_model.hpp:532
TaylorModel(const TaylorModel &other)=default
Makes a deep copy of other.
TaylorModel< T > abs(const TaylorModel< T > &a)
Absolute value of a TaylorModel.
Definition taylor_model.hpp:950
size_type max_order() const noexcept
Forwards to polynomial().max_order().
Definition taylor_model.hpp:200
std::string print_polynomial_form() const
Creates a string of the polynomial form of *this.
Definition taylor_model.hpp:285
TaylorModel operator*(value_t value) const
Returns the product of *this and value.
Definition taylor_model.hpp:517
TaylorModel< T > fabs(const TaylorModel< T > &a)
Absolute value of a TaylorModel.
Definition taylor_model.hpp:976
bool contains(const TaylorModel &other) const
Checks if *this contains other.
Definition taylor_model.hpp:270
TaylorModel operator+(value_t value) const
Returns the sum of *this and value.
Definition taylor_model.hpp:375
size_type n_terms() const noexcept
Forwards to polynomial().n_terms().
Definition taylor_model.hpp:211
typename taylor_t::monomial_t monomial_t
Definition taylor_model.hpp:66
typename taylor_t::coeffs_t coeffs_t
Definition taylor_model.hpp:69
TaylorModel operator-(value_t value) const
Returns the difference of *this and value.
Definition taylor_model.hpp:439
TaylorModel operator/(const TaylorModel &other) const
Returns the quotient of *this and other.
Definition taylor_model.hpp:593
TaylorModel & operator/=(const TaylorModel &other)
Overwrites *this with the quotient of *this and other.
Definition taylor_model.hpp:563
const taylor_t & polynomial() const noexcept
Definition taylor_model.hpp:189
TaylorModel multiplicative_inverse() const
Returns the multiplicative inverse of *this.
Definition taylor_model.hpp:867
const interval_t & remainder() const noexcept
Definition taylor_model.hpp:197
TaylorModel sweep_small(value_t threshold) const
Removes individually small coefficients, bounding them into the remainder.
Definition taylor_model.hpp:903
TaylorModel & operator=(const TaylorModel &other)=default
Assigns the value of other to this model.
TaylorModel(taylor_t polynomial, interval_t remainder)
Constructs a TaylorModel by re-wrapping a polynomial and a remainder.
Definition taylor_model.hpp:165
const coeffs_t & coefficients() const noexcept
Forwards to polynomial().coefficients().
Definition taylor_model.hpp:206
value_t constant() const
Forwards to polynomial().constant().
Definition taylor_model.hpp:203
bool operator!=(const TaylorModel &other) const
Checks if *this and other represent different models.
Definition taylor_model.hpp:724
TaylorModel operator/(value_t value) const
Returns the quotient of *this and value.
Definition taylor_model.hpp:577
TaylorModel operator-() const
Returns the additive inverse of *this.
Definition taylor_model.hpp:318
std::string print_interval_form() const
Creates a string of the interval form of *this.
Definition taylor_model.hpp:299
TaylorModel & operator-=(const TaylorModel &other)
Overwrites *this with the difference of *this and other.
Definition taylor_model.hpp:424
TaylorModel operator-(const TaylorModel &other) const
Returns the difference of *this and other.
Definition taylor_model.hpp:454
typename taylor_t::deviation_t deviation_t
Definition taylor_model.hpp:63
std::size_t size_type
Definition taylor_model.hpp:54
TaylorModel(const interval_t &interval, Order order=default_max_order())
Constructs a TaylorModel from an interval.
Definition taylor_model.hpp:139
bool empty() const noexcept
Checks if this TaylorModel represents the empty set.
Definition taylor_model.hpp:219
TaylorModel< T > pow(const TaylorModel< T > &a, const U &exp)
Power of a TaylorModel.
Definition taylor_model.hpp:1107
TaylorModel & operator+=(value_t value)
Overwrites *this with the sum of *this and value.
Definition taylor_model.hpp:335
TaylorModel & operator-=(value_t value)
Overwrites *this with the difference of *this and value.
Definition taylor_model.hpp:402
static deviation_t make_deviation()
Forwards to Taylor::make_deviation().
Definition taylor_model.hpp:304
TaylorModel(TaylorModel &&other) noexcept=default
Constructs a TaylorModel by moving other.
TaylorModel sweep_to_order(size_type new_order) const
Reduces *this to a lower truncation order, bounding the removed terms into the remainder.
Definition taylor_model.hpp:883
TaylorModel< ValueType > operator*(ValueType value, const TaylorModel< ValueType > &t)
Multiplies a scalar by a TaylorModel.
Definition taylor_model.hpp:787
Taylor< float > taylor_t
Definition taylor_model.hpp:60
TaylorModel & operator*=(value_t value)
Overwrites *this with the product of *this and value.
Definition taylor_model.hpp:469
TaylorModel & operator=(TaylorModel &&other) noexcept=default
Moves the value of other to this model.
TaylorModel & operator/=(value_t value)
Overwrites *this with the quotient of *this and value.
Definition taylor_model.hpp:544
bool contains(const interval_t &interval) const
Checks if *this contains interval.
Definition taylor_model.hpp:255
TaylorModel()=default
Constructs an empty TaylorModel.
interval_t range() const
Returns an interval enclosing the values *this models.
Definition taylor_model.hpp:232
std::ostream & operator<<(std::ostream &os, const TaylorModel< ValueType > &t)
Outputs the range of a TaylorModel to an output stream.
Definition taylor_model.hpp:762
TaylorModel< T > sqrt(const TaylorModel< T > &a)
Square root of a TaylorModel.
Definition taylor_model.hpp:1000
bool operator==(const TaylorModel &other) const
Checks if *this and other represent the same model.
Definition taylor_model.hpp:708
TaylorModel operator+(const TaylorModel &other) const
Returns the sum of *this and other.
Definition taylor_model.hpp:390
typename taylor_t::Order Order
Definition taylor_model.hpp:76
TaylorModel< T > log(const TaylorModel< T > &a)
Natural logarithm of a TaylorModel.
Definition taylor_model.hpp:1061
TaylorModel(value_t center, Order order=default_max_order())
Constructs a TaylorModel from a constant value.
Definition taylor_model.hpp:104
TaylorModel< T > exp(const TaylorModel< T > &a)
Exponential of a TaylorModel.
Definition taylor_model.hpp:1033
TaylorModel & operator*=(const TaylorModel &other)
Overwrites *this with the product of *this and other.
Definition taylor_model.hpp:807
bool contains(value_t value) const
Checks if *this contains value.
Definition taylor_model.hpp:242
static constexpr Order default_max_order()
The truncation order used when none is specified.
Definition taylor_model.hpp:79
Implements truncated multivariate polynomial algebra.
Definition taylor.hpp:46
Monomial monomial_t
Definition taylor.hpp:58
static deviation_t make_deviation()
Definition taylor.hpp:721
Interval< value_t > interval_t
Definition taylor.hpp:64
std::map< monomial_t, value_t > coeffs_t
Definition taylor.hpp:61
static constexpr Order default_max_order()
The truncation order used when none is specified.
Definition taylor.hpp:85
Monomial::variable_t deviation_t
Definition taylor.hpp:55
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
TaylorModel< float > TMFloat
Typedef for a Taylor model of floats.
Definition taylor_model.hpp:1115
TaylorModel< double > TMDouble
Typedef for a Taylor model of doubles.
Definition taylor_model.hpp:1118
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
Shared helpers for building the outer Taylor series of the elementary functions (exp,...
T half_falling_factorial(std::size_t k)
Definition series_detail.hpp:48
T factorial(std::size_t k)
Returns k! as a value_t.
Definition series_detail.hpp:25
std::vector< T > log_outer_coeffs(T c, std::size_t order)
Definition series_detail.hpp:72
std::vector< T > reciprocal_outer_coeffs(T c, std::size_t order)
Outer-series coefficients of 1/x about c: (-1)^k / c^(k+1).
Definition series_detail.hpp:86
std::vector< T > exp_outer_coeffs(T c, std::size_t order)
Definition series_detail.hpp:60
std::vector< T > sqrt_outer_coeffs(T c, std::size_t order)
Outer-series coefficients of sqrt(x) about c: sqrt(c) * C(1/2,k) / c^k.
Definition series_detail.hpp:99
Defines the Taylor class.