Theoretica
Scientific Computing
Loading...
Searching...
No Matches
deriv.h
Go to the documentation of this file.
1
5
6#ifndef DERIVATION_THEORETICA_H
7#define DERIVATION_THEORETICA_H
8
9#include "../core/function.h"
10#include "../polynomial/polynomial.h"
11
12
13namespace theoretica {
14
15
20 template<typename Field = real>
22
23 if (p.size() == 0)
24 return polynomial<Field>();
25
26 if (p.size() == 1)
27 return polynomial<Field>({static_cast<Field>(0.0)});
28
30 Dp.resize(p.size() - 1);
31
32 for (unsigned int i = 1; i < p.size(); ++i)
33 Dp[i - 1] = p[i] * i;
34
35 return Dp;
36 }
37
38
46 inline real deriv(const polynomial<real>& p, real x) {
47
48 real dp = 0.0;
49
50 for (unsigned int i = 0; i + 1 < p.size(); ++i) {
51
52 const unsigned int pos = p.size() - i - 1;
53 dp = pos * p[pos] + x * dp;
54 }
55
56 return dp;
57 }
58
59
67 template <
68 typename RealFunction = std::function<real(real)>,
70 >
72 return (f(x + h) - f(x - h)) / (2.0 * h);
73 }
74
75
83 template <
84 typename RealFunction = std::function<real(real)>,
86 >
88 return (f(x + h) - f(x)) / h;
89 }
90
91
99 template <
100 typename RealFunction = std::function<real(real)>,
102 >
104 return (f(x) - f(x - h)) / h;
105 }
106
107
115 template <
116 typename RealFunction = std::function<real(real)>,
118 >
120 return (4.0 * deriv_central(f, x, h / 2.0) - deriv_central(f, x, h)) / 3.0;
121 }
122
123
132 template <
133 typename RealFunction = std::function<real(real)>,
135 >
136 inline real deriv_ridders(RealFunction f, real x, real h = 0.01, unsigned int degree = 3) {
137
138 const unsigned int MAX_RIDDERS_DEGREE = 16;
140
141 if (degree == 0 || degree > MAX_RIDDERS_DEGREE) {
142 TH_MATH_ERROR("deriv_ridders", degree, MathError::InvalidArgument);
143 return nan();
144 }
145
146 for (unsigned int i = 0; i < degree; ++i) {
147
148 for (unsigned int n = 0; n <= i; ++n) {
149
150 unsigned int m = i - n;
151
152 if(n == 0) {
153 A[n][m] = deriv_central(f, x, h / (1 << m));
154 } else {
155 real coeff = square(1 << n);
156 A[n][m] = (coeff * A[n - 1][m + 1] - A[n - 1][m]) / (coeff - 1);
157 }
158 }
159
160 }
161
162 return A[degree - 1][0];
163 }
164
165
173 template <
174 typename RealFunction = std::function<real(real)>,
176 >
178 return deriv_ridders2(f, x, h);
179 }
180
181
189 template <
190 typename RealFunction = std::function<real(real)>,
192 >
194 return (f(x + h) - (2 * f(x)) + f(x - h)) / (h * h);
195 }
196
197}
198
199
200#endif
A polynomial of arbitrary order with coefficients of a specified type.
Definition polynomial.h:28
void resize(size_t sz)
Change the number of coefficients of the polynomial.
Definition polynomial.h:168
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
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:207
real deriv_forward(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the forward method.
Definition deriv.h:87
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
constexpr real CALCULUS_DERIV_STEP
Default variation for derivative approximation.
Definition constants.h:327
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
real deriv_ridders(RealFunction f, real x, real h=0.01, unsigned int degree=3)
Approximate the first derivative of a real function using Ridder's method of arbitrary degree.
Definition deriv.h:136
real deriv_ridders2(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using Ridder's method of second degree.
Definition deriv.h:119
@ InvalidArgument
Invalid argument.
real deriv_central(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the central method.
Definition deriv.h:71
real deriv2(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the second derivative of a real function using the best available algorithm.
Definition deriv.h:193
real deriv_backward(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the backward method.
Definition deriv.h:103
polynomial< Field > deriv(const polynomial< Field > &p)
Compute the exact derivative of a polynomial function.
Definition deriv.h:21
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23