numericalderivative.SteplemanWinarskyInitialize¶
- class numericalderivative.SteplemanWinarskyInitialize(algorithm, relative_precision=1e-15, verbose=False)¶
Compute an initial step for a search algorithm
- Parameters:
- algorithm
SteplemanWinarsky The algorithm to find a step
- relative_precisionfloat, > 0, optional
The relative precision of evaluation of f.
- algorithm
Methods
find_initial_step(h_min, h_max[, ...])Compute the initial step using bisection.
Returns the number of function evaluations.
Return the history of steps during the search.
Compute the number of (base 10) lost digits.
- Returns:
- None.
References
Adaptive numerical differentiation. R. S. Stepleman and N. D. Winarsky. Journal: Math. Comp. 33 (1979), 1257-1264
Examples
The next example computes the step of a badly scaled function. We first compute an appropriate initial step using
find_initial_step()and set it as the input offind_step().>>> import numericalderivative as nd >>> >>> def scaled_exp(x): >>> alpha = 1.e6 >>> return np.exp(-x / alpha) >>> >>> x = 1.0e-2 >>> initial_step = 1.0e8 >>> algorithm = nd.SteplemanWinarsky(scaled_exp, x) >>> initialize = nd.SteplemanWinarskyInitialize(algorithm) >>> initial_step, number_of_iterations = initialize.find_initial_step( >>> 1.0e-5, >>> 1.0e7, >>> ) >>> h_optimal, number_of_iterations = algorithm.find_step(initial_step) >>> f_prime_approx = algorithm.compute_first_derivative(h_optimal)
- __init__(algorithm, relative_precision=1e-15, verbose=False)¶
Methods
__init__(algorithm[, relative_precision, ...])find_initial_step(h_min, h_max[, ...])Compute the initial step using bisection.
Returns the number of function evaluations.
Return the history of steps during the search.
Compute the number of (base 10) lost digits.
- find_initial_step(h_min, h_max, maximum_bisection=53, log_scale=True)¶
Compute the initial step using bisection.
Search for an initial step \(h_0\) such that:
\[0 < N(h_0) < T := \log_{10}\left(\frac{\epsilon_r^{-1 / 3}}{\beta}\right)\]where \(N\) is the number of lost digits (as computed by
number_of_lost_digits()), \(h_0\) is the initial step and \(\epsilon_r\) is the relative precision of the function evaluation. This heuristic is based on the hypothesis that the absolute value of the third derivative is close to 1.The value returned by
find_initial_step()can be used as input offind_step().This algorithm can fail if the required finite difference step is so large that the points \(x \pm h\) fall beyond the mathematical input domain of the function.
- Parameters:
- h_minfloat
The lower bound to bracket the initial differentiation step.
- h_maxfloat, > h_min
The upper bound to bracket the initial differentiation step. We must have N(h_min) > N(h_max) where N is the number of lost digits.
- maximum_bisectionint, optional
The maximum number of bisection iterations.
- log_scalebool, optional
Set to True to bisect in log scale.
- Returns:
- initial_stepfloat
The initial step.
- number_of_iterationsint
The number of required iterations.
- get_number_of_function_evaluations()¶
Returns the number of function evaluations.
- Returns:
- number_of_function_evaluationsint
The number of function evaluations.
- get_step_history()¶
Return the history of steps during the search.
Let n be the number of iterations. The best step h is not the last one (with index n-1), since this is the step which breaks the monotony. The best step has index n - 2.
- Returns:
- step_historylist(float)
The list of steps h during intermediate iterations of the search. This is updated by
find_initial_step().
- number_of_lost_digits(h)¶
Compute the number of (base 10) lost digits.
The loss of figures produced by the substraction in the finite difference formula is (see (Stepleman & Winarsky, 1979), eq. 3.10 page 1261):
\[\delta(h) = \left|\frac{2hd(h)}{f(x)}\right|\]where \(h > 0\) is the step and \(d(h)\) is the central finite difference formula. The number of decimal digits losts in the substraction is (see (Stepleman & Winarsky, 1979), eq. 3.11 page 1261):
\[N(h) = -\log_{10}(\delta(h))\]where \(\log_{10}\) is the base-10 decimal digit.
- Parameters:
- hfloat
Differentiation step.
- Returns:
- number_of_digitsfloat
The number of digits lost by cancellation.