numerical_methods_functions¶
shortfx.fxNumeric.numerical_methods_functions
¶
Numerical methods: integration, differentiation, root-finding, and ODE solvers.
Classical numerical algorithms from Murray R. Spiegel's Mathematical Handbook of Formulas and Tables: quadrature rules, finite-difference differentiation, root-finding iterations, and ordinary differential equation solvers.
Functions¶
adaptive_rk45(f: Callable[[float, float], float], y0: float, t_start: float, t_end: float, tol: float = 1e-08, h_init: float = 0.01, h_min: float = 1e-12, h_max: float = 1.0) -> List[Tuple[float, float]]
¶
Solves the ODE y' = f(t, y) using the adaptive Runge-Kutta-Fehlberg (RK45) method.
Automatically adjusts step size to maintain the prescribed error tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float], float]
|
Right-hand side function f(t, y). |
required |
y0
|
float
|
Initial condition y(t_start) = y0. |
required |
t_start
|
float
|
Start of the interval. |
required |
t_end
|
float
|
End of the interval. |
required |
tol
|
float
|
Error tolerance per step (default 1e-8). |
1e-08
|
h_init
|
float
|
Initial step size (default 0.01). |
0.01
|
h_min
|
float
|
Minimum step size (default 1e-12). |
1e-12
|
h_max
|
float
|
Maximum step size (default 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float]]
|
List of (t, y) pairs at adaptively chosen steps. |
Example
result = adaptive_rk45(lambda t, y: y, 1.0, 0, 1) abs(result[-1][1] - math.e) < 1e-6 True
Complexity: O(variable) depending on tolerance and function smoothness.
Source code in shortfx/fxNumeric/numerical_methods_functions.py
backward_difference(f: Callable[[float], float], x: float, h: float = 1e-07) -> float
¶
Approximates f'(x) using the backward difference formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to differentiate. |
required |
x
|
float
|
Point of evaluation. |
required |
h
|
float
|
Step size (default 1e-7). |
1e-07
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f'(x). |
Example
round(backward_difference(math.sin, 0), 6) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
bisection_method(f: Callable[[float], float], a: float, b: float, tol: float = 1e-12, max_iter: int = 1000) -> float
¶
Finds a root of f in [a, b] using the bisection method.
Requires f(a) and f(b) to have opposite signs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Continuous function of one variable. |
required |
a
|
float
|
Left endpoint of the interval. |
required |
b
|
float
|
Right endpoint of the interval. |
required |
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
max_iter
|
int
|
Maximum number of iterations (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x such that f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If f(a) and f(b) have the same sign. |
Example
round(bisection_method(lambda x: x**2 - 2, 1, 2), 8) 1.41421356
Complexity: O(log((b - a) / tol))
Source code in shortfx/fxNumeric/numerical_methods_functions.py
central_difference(f: Callable[[float], float], x: float, h: float = 1e-07) -> float
¶
Approximates f'(x) using the central difference formula.
More accurate than forward/backward difference (error O(h^2) vs O(h)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to differentiate. |
required |
x
|
float
|
Point of evaluation. |
required |
h
|
float
|
Step size (default 1e-7). |
1e-07
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f'(x). |
Example
round(central_difference(math.sin, 0), 10) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
euler_method(f: Callable[[float, float], float], y0: float, t_start: float, t_end: float, n: int = 1000) -> List[Tuple[float, float]]
¶
Solves the ODE y' = f(t, y) using Euler's forward method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float], float]
|
Right-hand side function f(t, y). |
required |
y0
|
float
|
Initial condition y(t_start) = y0. |
required |
t_start
|
float
|
Start of the interval. |
required |
t_end
|
float
|
End of the interval. |
required |
n
|
int
|
Number of steps (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float]]
|
List of (t, y) pairs at each step. |
Example
result = euler_method(lambda t, y: y, 1.0, 0, 1, 1000) abs(result[-1][1] - math.e) < 0.01 True
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
fixed_point_iteration(g: Callable[[float], float], x0: float, tol: float = 1e-12, max_iter: int = 1000) -> float
¶
Finds a fixed point x = g(x) via simple iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Callable[[float], float]
|
Iteration function; the fixed point satisfies x = g(x). |
required |
x0
|
float
|
Initial guess. |
required |
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
max_iter
|
int
|
Maximum number of iterations (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate fixed point x. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If convergence is not achieved. |
Example
round(fixed_point_iteration(lambda x: math.cos(x), 1.0), 6) 0.739085
Complexity: O(max_iter), linear convergence when |g'(x*)| < 1.
Source code in shortfx/fxNumeric/numerical_methods_functions.py
forward_difference(f: Callable[[float], float], x: float, h: float = 1e-07) -> float
¶
Approximates f'(x) using the forward difference formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to differentiate. |
required |
x
|
float
|
Point of evaluation. |
required |
h
|
float
|
Step size (default 1e-7). |
1e-07
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f'(x). |
Example
round(forward_difference(math.sin, 0), 6) 1.0
Complexity: O(1)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
gaussian_quadrature(f: Callable[[float], float], a: float, b: float, n: int = 5) -> float
¶
Approximates the definite integral of f from a to b using Gauss-Legendre quadrature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of quadrature points (2, 3, 4, or 5; default 5). |
5
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
ValueError
|
If n not in {2, 3, 4, 5}. |
Example
round(gaussian_quadrature(math.sin, 0, math.pi, 5), 10) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
midpoint_rule(f: Callable[[float], float], a: float, b: float, n: int = 1000) -> float
¶
Approximates the definite integral of f from a to b using the midpoint rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of subintervals (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
ValueError
|
If n < 1. |
Example
round(midpoint_rule(math.sin, 0, math.pi, 10000), 6) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
monte_carlo_integration(f: Callable[[float], float], a: float, b: float, n: int = 100000, seed: Optional[int] = None) -> float
¶
Approximates the definite integral of f from a to b via Monte Carlo sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of random samples (default 100000). |
100000
|
seed
|
Optional[int]
|
Optional random seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
Example
import random; random.seed(42) abs(monte_carlo_integration(math.sin, 0, math.pi, 100000, seed=42) - 2.0) < 0.05 True
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
newton_raphson(f: Callable[[float], float], df: Callable[[float], float], x0: float, tol: float = 1e-12, max_iter: int = 1000) -> float
¶
Finds a root of f using Newton-Raphson iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function whose root is sought. |
required |
df
|
Callable[[float], float]
|
Derivative of f. |
required |
x0
|
float
|
Initial guess. |
required |
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
max_iter
|
int
|
Maximum number of iterations (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x such that f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If derivative is zero during iteration. |
RuntimeError
|
If convergence is not achieved. |
Example
round(newton_raphson(lambda x: x*2 - 2, lambda x: 2x, 1.0), 10) 1.4142135624
Complexity: O(max_iter) per call, quadratic convergence.
Source code in shortfx/fxNumeric/numerical_methods_functions.py
ode_system_rk4(f: Callable[[float, List[float]], List[float]], y0: List[float], t_start: float, t_end: float, n: int = 1000) -> List[Tuple[float, List[float]]]
¶
Solves a system of ODEs y' = f(t, y) using the 4th-order Runge-Kutta method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, List[float]], List[float]]
|
Right-hand side function f(t, y) returning a list of derivatives. |
required |
y0
|
List[float]
|
Initial condition vector y(t_start). |
required |
t_start
|
float
|
Start of the interval. |
required |
t_end
|
float
|
End of the interval. |
required |
n
|
int
|
Number of steps (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, List[float]]]
|
List of (t, y_vector) pairs at each step. |
Example
Solve y'' + y = 0 as system [y, y'] with y(0)=0, y'(0)=1¶
result = ode_system_rk4(lambda t, y: [y[1], -y[0]], [0.0, 1.0], 0, math.pi, 1000) abs(result[-1][1][0]) < 0.001 # sin(pi) ≈ 0 True
Complexity: O(n * dim)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
regula_falsi(f: Callable[[float], float], a: float, b: float, tol: float = 1e-12, max_iter: int = 1000) -> float
¶
Finds a root of f in [a, b] using the false position (regula falsi) method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Continuous function of one variable. |
required |
a
|
float
|
Left endpoint where f(a) and f(b) have opposite signs. |
required |
b
|
float
|
Right endpoint. |
required |
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
max_iter
|
int
|
Maximum number of iterations (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x such that f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If f(a) and f(b) have the same sign. |
Example
round(regula_falsi(lambda x: x**2 - 2, 1, 2), 8) 1.41421356
Complexity: O(max_iter), linear convergence.
Source code in shortfx/fxNumeric/numerical_methods_functions.py
richardson_extrapolation(f: Callable[[float], float], x: float, h: float = 0.1, order: int = 4) -> float
¶
Approximates f'(x) using Richardson extrapolation on central differences.
Iteratively improves the derivative estimate by eliminating error terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to differentiate. |
required |
x
|
float
|
Point of evaluation. |
required |
h
|
float
|
Initial step size (default 0.1). |
0.1
|
order
|
int
|
Number of extrapolation levels (default 4). |
4
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f'(x). |
Example
round(richardson_extrapolation(math.sin, 0, 0.1, 4), 12) 1.0
Complexity: O(order^2)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
romberg_integration(f: Callable[[float], float], a: float, b: float, max_order: int = 10, tol: float = 1e-12) -> float
¶
Approximates the definite integral of f from a to b using Romberg's method.
Iteratively refines trapezoidal estimates via Richardson extrapolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
max_order
|
int
|
Maximum number of refinement levels (default 10). |
10
|
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
Example
round(romberg_integration(math.sin, 0, math.pi), 10) 2.0
Complexity: O(2^max_order)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
runge_kutta_2(f: Callable[[float, float], float], y0: float, t_start: float, t_end: float, n: int = 1000) -> List[Tuple[float, float]]
¶
Solves the ODE y' = f(t, y) using the 2nd-order Runge-Kutta (midpoint) method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float], float]
|
Right-hand side function f(t, y). |
required |
y0
|
float
|
Initial condition y(t_start) = y0. |
required |
t_start
|
float
|
Start of the interval. |
required |
t_end
|
float
|
End of the interval. |
required |
n
|
int
|
Number of steps (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float]]
|
List of (t, y) pairs at each step. |
Example
result = runge_kutta_2(lambda t, y: y, 1.0, 0, 1, 1000) abs(result[-1][1] - math.e) < 0.001 True
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
runge_kutta_4(f: Callable[[float, float], float], y0: float, t_start: float, t_end: float, n: int = 1000) -> List[Tuple[float, float]]
¶
Solves the ODE y' = f(t, y) using the classic 4th-order Runge-Kutta method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float], float]
|
Right-hand side function f(t, y). |
required |
y0
|
float
|
Initial condition y(t_start) = y0. |
required |
t_start
|
float
|
Start of the interval. |
required |
t_end
|
float
|
End of the interval. |
required |
n
|
int
|
Number of steps (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float]]
|
List of (t, y) pairs at each step. |
Example
result = runge_kutta_4(lambda t, y: y, 1.0, 0, 1, 100) abs(result[-1][1] - math.e) < 1e-8 True
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
secant_method(f: Callable[[float], float], x0: float, x1: float, tol: float = 1e-12, max_iter: int = 1000) -> float
¶
Finds a root of f using the secant method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function whose root is sought. |
required |
x0
|
float
|
First initial approximation. |
required |
x1
|
float
|
Second initial approximation. |
required |
tol
|
float
|
Convergence tolerance (default 1e-12). |
1e-12
|
max_iter
|
int
|
Maximum number of iterations (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x such that f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If convergence is not achieved. |
Example
round(secant_method(lambda x: x**2 - 2, 1.0, 2.0), 10) 1.4142135624
Complexity: O(max_iter), superlinear convergence.
Source code in shortfx/fxNumeric/numerical_methods_functions.py
second_derivative_central(f: Callable[[float], float], x: float, h: float = 1e-05) -> float
¶
Approximates f''(x) using the central difference formula for second derivatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to differentiate. |
required |
x
|
float
|
Point of evaluation. |
required |
h
|
float
|
Step size (default 1e-5). |
1e-05
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f''(x). |
Example
round(second_derivative_central(math.sin, 0), 4) 0.0
Complexity: O(1)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
simpsons_38_rule(f: Callable[[float], float], a: float, b: float, n: int = 999) -> float
¶
Approximates the definite integral of f from a to b using Simpson's 3/8 rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of subintervals (must be divisible by 3, default 999). |
999
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
ValueError
|
If n < 3 or n is not divisible by 3. |
Example
round(simpsons_38_rule(math.sin, 0, math.pi, 999), 6) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
simpsons_rule(f: Callable[[float], float], a: float, b: float, n: int = 1000) -> float
¶
Approximates the definite integral of f from a to b using Simpson's 1/3 rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of subintervals (must be even, default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or a, b are not numeric. |
ValueError
|
If n < 2 or n is odd. |
Example
round(simpsons_rule(math.sin, 0, math.pi, 100), 10) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/numerical_methods_functions.py
trapezoidal_rule(f: Callable[[float], float], a: float, b: float, n: int = 1000) -> float
¶
Approximates the definite integral of f from a to b using the trapezoidal rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Integrand function of one variable. |
required |
a
|
float
|
Lower limit of integration. |
required |
b
|
float
|
Upper limit of integration. |
required |
n
|
int
|
Number of subintervals (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of the integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or a, b are not numeric. |
ValueError
|
If n < 1. |
Example
round(trapezoidal_rule(math.sin, 0, math.pi, 10000), 6) 2.0
Complexity: O(n)