6 #ifndef THEORETICA_ROOTS_H
7 #define THEORETICA_ROOTS_H
9 #include "../core/function.h"
10 #include "../calculus/deriv.h"
11 #include "../autodiff/dual.h"
12 #include "../autodiff/dual2.h"
13 #include "../algebra/vec.h"
14 #include "../complex/complex.h"
27 template<
typename RealFunction,
typename Vector = vec2>
29 RealFunction f,
real a,
real b,
unsigned int steps = 10) {
31 std::vector<Vector> res;
32 const real dx = (b - a) / (
real) steps;
34 for (
unsigned int i = 0; i < steps; ++i) {
36 const real x1 = a + i * dx;
37 const real x2 = a + (i + 1) * dx;
39 if(f(x1) * f(x2) <= 0)
40 res.push_back({x1, x2});
57 template<
typename RealFunction>
68 if(f(a) * f(b) >= 0) {
77 unsigned int iter = 0;
79 while((x_max - x_min) > (2 * tol) && iter <= max_iter) {
81 x_avg = (x_max + x_min) / 2.0;
83 if(f(x_avg) * f(x_min) > 0)
119 template<
typename RealFunction>
122 unsigned int n0 = 1,
real k1 = 0.0) {
142 const int monotone = (y_a < y_b) ? 1 : -1;
147 const long int n_max = n_half + n0;
149 real eps = tol *
pow(2.0, n_max);
152 while((b - a) > (2 * tol) && iter <= n_max) {
156 const real x_f = (a * y_b - b * y_a) / (y_b - y_a);
157 const real x_half = 0.5 * (a + b);
161 const int sigma =
sgn(x_half - x_f);
164 if (delta <=
abs(x_half - x_f))
165 x_t = x_f + sigma * delta;
171 const real r = eps - (b - a) / 2.0;
173 if (
abs(x_t - x_half) <= r)
176 x_new = x_half - sigma * r;
180 const real y_new = f(x_new);
182 if (monotone * y_new > 0) {
185 }
else if (monotone * y_new < 0) {
196 if(
abs(b - a) > 2 * tol) {
197 TH_MATH_ERROR(
"root_itp", (a + b) / 2.0, NO_ALGO_CONVERGENCE);
201 return (a + b) / 2.0;
216 template<
typename RealFunction>
218 RealFunction f, RealFunction Df,
real guess = 0.0,
223 unsigned int iter = 0;
225 while(
abs(f_x) > tol && iter <= max_iter) {
228 x = x - (f_x / Df(x));
232 if(iter > max_iter) {
259 unsigned int iter = 0;
263 while(
abs(s.
Re()) > tol && iter <= max_iter) {
268 x = x - (s.
Re() / s.
Dual());
272 if(iter > max_iter) {
293 typename Type =
real,
294 typename ComplexFunction = std::function<complex<Type>(complex<Type>)>
303 unsigned int iter = 0;
305 while(f_z * f_z.
conjugate() > tol * tol && iter <= max_iter) {
308 z = z - (f_z / df(z));
312 if(iter > max_iter) {
333 template<
typename RealFunction>
335 RealFunction f, RealFunction Df,
336 RealFunction D2f,
real guess = 0,
342 unsigned int iter = 0;
344 while(
abs(f_x) > tol && iter <= max_iter) {
347 const real df_x = Df(x);
349 x = x - (2 * f_x * df_x) / (2 *
square(df_x) - f_x * D2f(x));
353 if(iter > max_iter) {
382 unsigned int iter = 0;
384 while(
abs(s.
Re()) > tol && iter <= max_iter) {
388 s = f(
dual2(x, 1, 0));
394 x = x - (2 * f_x * df_x) / (2 *
square(df_x) - f_x * d2f_x);
398 if(iter > max_iter) {
417 template<
typename RealFunction>
419 RealFunction f,
real guess = 0,
425 unsigned int iter = 0;
427 while(
abs(f_x) > tol && iter <= max_iter) {
429 const real f_x = f(x);
430 const real g_x = (f(x + f_x) / f_x) - 1;
436 if(iter > max_iter) {
462 template<
typename RealFunction>
464 RealFunction f, RealFunction Df,
465 RealFunction D2f,
real guess = 0,
471 unsigned int iter = 0;
473 while(
abs(f_x) > tol && iter <= max_iter) {
476 const real df_x = Df(x);
477 const real u = f_x / df_x;
479 x = x - u -
square(u) * D2f(x) / (2.0 * df_x);
484 if(iter > max_iter) {
518 unsigned int iter = 0;
520 while(
abs(s.
Re()) > tol && iter <= max_iter) {
522 s = f(
dual2(x, 1.0, 0.0));
526 const real u = f_x / df_x;
532 if(iter > max_iter) {
557 template<
typename RealFunction>
559 RealFunction f, RealFunction Df,
real guess = 0.0,
565 unsigned int iter = 0;
567 while(
abs(f_x) > tol && iter <= max_iter) {
570 const real df_x = Df(x);
571 const real u = f_x / df_x;
572 const real f_xu = f(x - u);
574 x = x - u - (f_xu / df_x) * (f_x / (f_x - 2 * f_xu));
579 if(iter > max_iter) {
605 template<
typename RealFunction>
607 RealFunction f, RealFunction Df,
real guess = 0.0,
613 unsigned int iter = 0;
615 while(
abs(f_x) > tol && iter <= max_iter) {
618 const real df_x = Df(x);
619 const real u = f_x / df_x;
620 const real f_xu = Df(x - 2.0 * u / 3.0);
622 x = x - 0.5 * u + f_x / (df_x - 3 * f_xu);
626 if(iter > max_iter) {
649 template<
typename RealFunction>
662 std::vector<real> res;
663 res.reserve(intervals.size());
666 for (
unsigned int i = 0; i < intervals.size(); ++i) {
671 res.push_back(intervals[i][0]);
676 res.push_back(intervals[i][1]);
682 root_bisect(f, intervals[i][0], intervals[i][1], tol)
697 template<
typename Field>
701 unsigned int steps = 0) {
708 Field a_hi =
abs(p.coeff[n]);
712 for (
unsigned int i = 0; i < n; ++i)
713 a_sum +=
abs(p.coeff[i]);
716 const Field M =
max(a_hi, a_sum);
721 [p](
real x) {
return p(x); },
722 -M, M, tolerance, steps ? steps : (2 * n));
Complex number in algebraic form .
Definition: complex.h:26
Type Re() const
Get the real part of the complex number.
Definition: complex.h:74
complex conjugate() const
Compute the conjugate of the complex number.
Definition: complex.h:122
Second order dual number class.
Definition: dual2.h:29
real Dual2() const
Return second order dual part.
Definition: dual2.h:95
real Re() const
Return real part.
Definition: dual2.h:85
real Dual1() const
Return first order dual part.
Definition: dual2.h:90
Dual number class.
Definition: dual.h:28
const real & Re() const
Return real part.
Definition: dual.h:75
const real & Dual() const
Return dual part.
Definition: dual.h:96
A polynomial of arbitrary order.
Definition: polynomial.h:25
unsigned int find_order() const
Find the true order of the polynomial (ignoring trailing null coefficients)
Definition: polynomial.h:95
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Vector square(const Vector &v)
Parallel element-wise evaluation of the square function.
Definition: parallel.h:53
Vector abs(const Vector &v)
Parallel element-wise evaluation of the abs function.
Definition: parallel.h:89
Vector pow(const Vector &v, int n)
Parallel element-wise evaluation of the pow function.
Definition: parallel.h:107
Vector log2(const Vector &v)
Parallel element-wise evaluation of the log2 function.
Definition: parallel.h:215
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:198
constexpr unsigned int OPTIMIZATION_JARRAT_ITER
Maximum number of iterations for the Jarrat algorithm.
Definition: constants.h:312
real root_halley(dual2(*f)(dual2), real guess=0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_HALLEY_ITER)
Find a root of a univariate real function using Halley's method, leveraging automatic differentiation...
Definition: roots.h:375
real inf()
Return positive infinity in floating point representation.
Definition: error.h:76
real root_chebyshev(dual2(*f)(dual2), real guess=0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_CHEBYSHEV_ITER)
Find a root of a univariate real function using Chebyshev's method, by computing the first and second...
Definition: roots.h:511
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition: constants.h:288
constexpr unsigned int OPTIMIZATION_OSTROWSKI_ITER
Maximum number of iterations for the Ostrowski algorithm.
Definition: constants.h:309
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
std::vector< Vector > find_root_intervals(RealFunction f, real a, real b, unsigned int steps=10)
Find candidate intervals for root finding by evaluating a function at equidistant points inside an in...
Definition: roots.h:28
real root_bisect(RealFunction f, real a, real b, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_BISECTION_ITER)
Find the root of a univariate real function using bisection inside a compact interval [a,...
Definition: roots.h:58
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:207
real root_itp(RealFunction f, real a, real b, real tol=OPTIMIZATION_TOL, unsigned int n0=1, real k1=0.0)
Find a root of a univariate real function using the ITP (Interpolate-Truncate-Project) method,...
Definition: roots.h:120
std::vector< Field > roots(const polynomial< Field > &p, real tolerance=OPTIMIZATION_TOL, unsigned int steps=0)
Find all the roots of a polynomial.
Definition: roots.h:698
real root_steffensen(RealFunction f, real guess=0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_STEFFENSEN_ITER)
Find a root of a univariate real function using Steffensen's method.
Definition: roots.h:418
constexpr unsigned int OPTIMIZATION_CHEBYSHEV_ITER
Maximum number of iterations for the Chebyshev algorithm.
Definition: constants.h:306
constexpr unsigned int OPTIMIZATION_BISECTION_ITER
Maximum number of iterations for the bisection algorithm.
Definition: constants.h:291
constexpr unsigned int OPTIMIZATION_STEFFENSEN_ITER
Maximum number of iterations for the Steffensen algorithm.
Definition: constants.h:303
real root_ostrowski(RealFunction f, RealFunction Df, real guess=0.0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_OSTROWSKI_ITER)
Find a root of a univariate real function using Ostrowski's method.
Definition: roots.h:558
complex< Type > root_newton(ComplexFunction f, ComplexFunction Df, complex< Type > guess, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_NEWTON_ITER)
Find a root of a complex function using Newton's method.
Definition: roots.h:296
constexpr unsigned int OPTIMIZATION_NEWTON_ITER
Maximum number of iterations for the Newton-Raphson algorithm.
Definition: constants.h:300
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition: real_analysis.h:259
constexpr unsigned int OPTIMIZATION_HALLEY_ITER
Maximum number of iterations for Halley's method.
Definition: constants.h:297
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
real root_jarrat(RealFunction f, RealFunction Df, real guess=0.0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_JARRAT_ITER)
Find a root of a univariate real function using Jarrat's method.
Definition: roots.h:606
TH_CONSTEXPR int floor(real x)
Compute the floor of x Computes the maximum integer number that is smaller than x.
Definition: real_analysis.h:271