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"
26 template<
typename RealFunction>
28 RealFunction f,
real a,
real b,
unsigned int steps = 10) {
30 std::vector<vec2> res;
31 const real dx = (b - a) / (
real) steps;
33 for (
unsigned int i = 0; i < steps; ++i) {
35 const real x1 = a + i * dx;
36 const real x2 = a + (i + 1) * dx;
38 if(f(x1) * f(x2) <= 0)
39 res.push_back({x1, x2});
55 template<
typename RealFunction>
59 if(f(a) * f(b) >= 0) {
60 TH_MATH_ERROR(
"root_bisection", f(a) * f(b), INVALID_ARGUMENT);
69 unsigned int iter = 0;
73 x_avg = (x_max + x_min) / 2.0;
75 if(f(x_avg) * f(x_min) > 0)
99 template<
typename RealFunction>
104 unsigned int iter = 0;
107 x = x - (f(x) / Df(x));
131 unsigned int iter = 0;
139 x = x - (s.
Re() / s.
Dual());
162 unsigned int iter = 0;
165 x = x - (p(x) / Dp(x));
198 unsigned int iter = 0;
200 while(
abs(f(z)) > tolerance && iter <= max_iter) {
201 z = z - (f(z) / df(z));
205 if(iter > max_iter) {
222 template<
typename RealFunction>
224 RealFunction D2f,
real guess = 0) {
227 unsigned int iter = 0;
230 x = x - (2 * f(x) * Df(x)) / (2 * Df(x) - f(x) * D2f(x));
255 unsigned int iter = 0;
261 s = f(
dual2(x, 1, 0));
263 x = x - (2 * s.
Re() * s.
Dual1())
289 unsigned int iter = 0;
292 x = x - (2 * p(x) * Dp(x)) / (2 * Dp(x) - p(x) * D2p(x));
311 template<
typename RealFunction>
315 unsigned int iter = 0;
319 const real f_x = f(x);
320 x = x - (f_x / ((f(x + f_x) / f_x) - 1));
342 unsigned int iter = 0;
345 x = x - (p(x) / ((p(x + p(x)) / p(x)) - 1));
366 template<
typename RealFunction>
368 RealFunction f, RealFunction Df,
369 RealFunction D2f,
real guess = 0) {
372 unsigned int iter = 0;
375 x = x - (f(x) / Df(x)) -
square((f(x) / Df(x))) * (Df(x) / (D2f(x) * 2));
399 unsigned int iter = 0;
405 s = f(
dual2(x, 1, 0));
434 unsigned int iter = 0;
437 x = x - (p(x) / Dp(x)) -
square((p(x) / Dp(x))) * (Dp(x) / (D2p(x) * 2));
461 template<
typename RealFunction>
474 std::vector<real> res;
475 res.reserve(intervals.size());
478 for (
unsigned int i = 0; i < intervals.size(); ++i) {
483 res.push_back(intervals[i][0]);
488 res.push_back(intervals[i][1]);
508 template<
typename Field>
512 unsigned int steps = 0) {
515 const unsigned int n = p.find_order();
519 Field a_hi =
abs(p.coeff[n]);
523 for (
unsigned int i = 0; i < n; ++i)
524 a_sum +=
abs(p.coeff[i]);
527 const Field M =
max(a_hi, a_sum);
532 [p](
real x) {
return p(x); },
533 -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
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
#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
real deriv(DualFunction f, real x)
Compute the derivative of a function at the given point using univariate automatic differentiation.
Definition: autodiff.h:41
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
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
real root_halley(const polynomial< real > &p, real guess=0)
Approximate a root of a polynomial using Halley's method.
Definition: roots.h:283
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real root_steffensen(const polynomial< real > &p, real guess=0)
Approximate a root of a polynomial using Steffensen's method.
Definition: roots.h:339
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition: constants.h:278
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
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:509
complex root_newton(complex<>(*f)(complex<>), complex<>(*df)(complex<>), complex<> guess=complex<>(0, 0), real tolerance=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_TOL)
Approximate a root of an arbitrary complex function using Newton's method,.
Definition: roots.h:190
std::vector< vec2 > find_root_intervals(RealFunction f, real a, real b, unsigned int steps=10)
Find candidate intervals for root finding.
Definition: roots.h:27
constexpr unsigned int OPTIMIZATION_CHEBYSHEV_ITER
Maximum number of iterations for the Chebyshev algorithm.
Definition: constants.h:296
constexpr unsigned int OPTIMIZATION_BISECTION_ITER
Maximum number of iterations for the bisection algorithm.
Definition: constants.h:281
constexpr unsigned int OPTIMIZATION_STEFFENSEN_ITER
Maximum number of iterations for the Steffensen algorithm.
Definition: constants.h:293
constexpr unsigned int OPTIMIZATION_NEWTON_ITER
Maximum number of iterations for the Newton-Raphson algorithm.
Definition: constants.h:290
constexpr unsigned int OPTIMIZATION_HALLEY_ITER
Maximum number of iterations for Halley's method.
Definition: constants.h:287
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
real root_chebyshev(const polynomial< real > &p, real guess=0)
Approximate a root of a polynomial using Chebyshev's method.
Definition: roots.h:428
real root_bisection(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL)
Approximate a root of an arbitrary function using bisection inside a compact interval [a,...
Definition: roots.h:56