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>
31 std::vector<Vector>
res;
34 for (
unsigned int i = 0; i <
steps; ++i) {
39 if(f(
x1) * f(
x2) <= 0)
57 template<
typename RealFunction>
61 unsigned int max_iter = OPTIMIZATION_BISECTION_ITER) {
68 if(f(a) * f(b) >= 0) {
77 unsigned int iter = 0;
119 template<
typename RealFunction>
122 unsigned int n0 = 1,
real k1 = 0.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>
219 real tol = OPTIMIZATION_TOL,
unsigned int max_iter = OPTIMIZATION_NEWTON_ITER) {
223 unsigned int iter = 0;
228 x = x - (
f_x /
Df(x));
256 unsigned int max_iter = OPTIMIZATION_NEWTON_ITER) {
259 unsigned int iter = 0;
268 x = x - (
s.Re() /
s.Dual());
293 typename Type = real,
294 typename ComplexFunction = std::function<complex<Type>(complex<Type>)>
299 unsigned int max_iter = OPTIMIZATION_NEWTON_ITER) {
303 unsigned int iter = 0;
333 template<
typename RealFunction>
338 unsigned int max_iter = OPTIMIZATION_HALLEY_ITER) {
342 unsigned int iter = 0;
378 unsigned int max_iter = OPTIMIZATION_HALLEY_ITER) {
382 unsigned int iter = 0;
417 template<
typename RealFunction>
421 unsigned int max_iter = OPTIMIZATION_STEFFENSEN_ITER) {
425 unsigned int iter = 0;
462 template<
typename RealFunction>
467 unsigned int max_iter = OPTIMIZATION_CHEBYSHEV_ITER) {
471 unsigned int iter = 0;
479 x = x -
u - square(
u) *
D2f(x) / (2.0 *
df_x);
514 unsigned int max_iter = OPTIMIZATION_CHEBYSHEV_ITER) {
518 unsigned int iter = 0;
522 s = f(
dual2(x, 1.0, 0.0));
528 x = x -
u - square(
u) *
s.Dual2() / (2.0 *
df_x);
557 template<
typename RealFunction>
561 unsigned int max_iter = OPTIMIZATION_OSTROWSKI_ITER) {
565 unsigned int iter = 0;
605 template<
typename RealFunction>
609 unsigned int max_iter = OPTIMIZATION_JARRAT_ITER) {
613 unsigned int iter = 0;
649 template<
typename RealFunction>
662 std::vector<real>
res;
666 for (
unsigned int i = 0; i <
intervals.size(); ++i) {
697 template<
typename Field>
701 unsigned int steps = 0) {
704 const unsigned int n =
p.find_order();
712 for (
unsigned int i = 0; i <
n; ++i)
721 [
p](
real x) {
return p(x); },
Second order dual number class.
Definition dual2.h:29
Dual number class.
Definition dual.h:28
#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
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
real root_newton(RealFunction f, RealFunction Df, real guess=0.0, real tol=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_NEWTON_ITER)
Find a root of a univariate real function using Newton's method.
Definition roots.h:217
real root_halley(RealFunction f, RealFunction Df, RealFunction D2f, 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.
Definition roots.h:334
real inf()
Return positive infinity in floating point representation.
Definition error.h:76
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:166
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
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
std::vector< real > roots(RealFunction f, real a, real b, real tol=OPTIMIZATION_TOL, real steps=10)
Find the roots of a univariate real function inside a given interval, by first searching for candidat...
Definition roots.h:650
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:330
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
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
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
real root_chebyshev(RealFunction f, RealFunction Df, RealFunction D2f, 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.
Definition roots.h:463
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition real_analysis.h:259
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