polykin.math.derivatives¤
gradient_forward ¤
gradient_forward(
f: Callable[[FloatVector], float],
x: FloatVector,
*,
fx: float | None = None,
sclx: FloatVector | None = None,
epsf: float | None = None
) -> FloatVector
Calculate the numerical gradient of a scalar function \(f(\mathbf{x})\) using the forward finite-difference scheme.
The step size \(h_i\) is optimally determined according to the machine precision of the function values. Typically, the gradient is accurate to about half the number of reliable digits returned by the function.
If the function value at \(\mathbf{x}\) is provided, \(N\) function evaluations are required to compute the gradient, where \(N\) is the dimension of \(\mathbf{x}\).
References
- J.E. Dennis Jr., R.B. Schnabel, "Numerical Methods for Unconstrained Optimization and Nonlinear Equations", SIAM, 1996, p. 323.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
Function to be differentiated.
TYPE:
|
x
|
Differentiation point.
TYPE:
|
fx
|
Function value at
TYPE:
|
sclx
|
Scaling factors for
TYPE:
|
epsf
|
Machine precision of the function values. If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FloatVector
|
Gradient vector. |
Examples:
Evaluate the numerical gradient of f(x) = x1**2 * x2**3 at (2, -2).
>>> from polykin.math import gradient_forward
>>> import numpy as np
>>> def f(x): return x[0]**2 * x[1]**3
>>> gradient_forward(f, np.array([2.0, -2.0]))
array([-32.00000024, 47.99999928])
Source code in src/polykin/math/derivatives/ndiff.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |