Package de.labathome.trustconstr
Class NumDiff
java.lang.Object
de.labathome.trustconstr.NumDiff
Finite-difference Jacobians and gradients (mirrors scipy
_numdiff.approx_derivative). Provides:
approxDerivative(java.util.function.DoubleUnaryOperator, double, de.labathome.trustconstr.records.FiniteDifferenceOptions)-- vector-valued FD with optional sparsity hint;adjustSchemeToBounds(de.labathome.trustconstr.matrix.Matrix, de.labathome.trustconstr.matrix.Matrix, int, de.labathome.trustconstr.enums.FiniteDifferenceMethod, de.labathome.trustconstr.matrix.Matrix, de.labathome.trustconstr.matrix.Matrix)-- bound-aware step-direction selection;computeAbsoluteStep(de.labathome.trustconstr.matrix.Matrix, de.labathome.trustconstr.matrix.Matrix, de.labathome.trustconstr.matrix.Matrix, de.labathome.trustconstr.enums.FiniteDifferenceMethod)/epsForMethod(java.lang.Class<?>, java.lang.Class<?>, de.labathome.trustconstr.enums.FiniteDifferenceMethod)-- step-size helpers;groupColumns(de.labathome.trustconstr.matrix.Matrix)-- Curtis-Powell-Reid column grouping for sparse FD.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic AdjustedDifferencingSchemeadjustSchemeToBounds(Matrix x0, Matrix h, int numSteps, FiniteDifferenceMethod scheme, Matrix lb, Matrix ub) Adjust finite-difference step sizes so that the perturbed point stays within[lb, ub]per dimension, flipping direction or switching forward / backward as needed.static MatrixapproxDerivative(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Finite-difference Jacobian of a vector-valued function with extra args.static doubleapproxDerivative(DoubleUnaryOperator f, double x0, FiniteDifferenceOptions options) Finite-difference gradient of a scalar function of a scalar argument.static MatrixapproxDerivative(Function<Matrix, Matrix> f, Matrix x0, Matrix f0, FiniteDifferenceOptions options) Finite-difference Jacobian of a vector-valued function.static doubleapproxDerivative(ToDoubleFunction<Matrix> f, double x0, double f0, FiniteDifferenceOptions options) Finite-difference gradient of a scalar function of a scalar argument with a precomputedf0.static doubleapproxDerivative(ToDoubleFunction<Matrix> f, double x0, FiniteDifferenceOptions options) Finite-difference gradient of aToDoubleFunctionof a scalar argument.static MatrixapproxDerivative(ToDoubleFunction<Matrix> f, Matrix x0, double f0, FiniteDifferenceOptions options) Finite-difference gradient of a real-valued function of a vector argument.static LinearOperatorapproxDerivativeAsLinearOperator(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Strict scipyapprox_derivative(..., as_linear_operator=True)counterpart: returns a matrix-freeLinearOperatorthat applies the FD JacobianJ(x0) * pon demand.static ApproxDerivativeResultapproxDerivativeFullOutput(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Strict scipyapprox_derivative(..., full_output=True)counterpart: returns the FD Jacobian alongside an info dict carrying metadata about the computation.static MatrixapproxDerivativeWithWorkers(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args, ExecutorService executor) Strict scipyapprox_derivative(..., workers=...)counterpart: computes a dense FD Jacobian column-by-column with the per-column evaluations dispatched in parallel throughexecutor.static MatrixcomputeAbsoluteStep(Matrix relStep, Matrix x0, Matrix f0, FiniteDifferenceMethod method) Compute the absolute finite-difference step from a relative step.static doubleepsForMethod(Class<?> x0Type, Class<?> f0Type, FiniteDifferenceMethod method) Calculate the relative EPS step for a given data type and FD method.static int[]Group columns of a 2-D matrix for sparse finite differencing.static int[]groupColumns(Matrix A, int[] order) Group columns of a 2-D matrix for sparse FD with a user-supplied permutation that controls greedy-grouping order.static LinearOperatorlinearOperatorDifference(Function<Matrix, Matrix> fun, Matrix x0, Matrix f0, Matrix relStep, FiniteDifferenceMethod method) Build aLinearOperatorthat applies the FD JacobianJ(x0)*pon demand, without materialising the full Jacobian.
-
Constructor Details
-
NumDiff
public NumDiff()
-
-
Method Details
-
groupColumns
Group columns of a 2-D matrix for sparse finite differencing. Two columns are in the same group if in each row at least one of them has zero. A greedy sequential algorithm constructs the groups (Curtis-Powell-Reid, 1974).- Parameters:
A-m x nsparsity pattern (non-zero entries mark structural non-zeros of the Jacobian).- Returns:
- length-
narray of group indices in[0, n_groups).
-
groupColumns
Group columns of a 2-D matrix for sparse FD with a user-supplied permutation that controls greedy-grouping order.- Parameters:
A-m x nsparsity patternorder- length-npermutation; ifnullor length≤ 1, a reproducible random permutation seeded byorder[0](defaulting to0) is used- Returns:
- length-
narray of group indices
-
adjustSchemeToBounds
public static AdjustedDifferencingScheme adjustSchemeToBounds(Matrix x0, Matrix h, int numSteps, FiniteDifferenceMethod scheme, Matrix lb, Matrix ub) Adjust finite-difference step sizes so that the perturbed point stays within[lb, ub]per dimension, flipping direction or switching forward / backward as needed. Pure per-element logic on length-nvectors.- Parameters:
x0- point at which to estimate the derivative (n x 1)h- desired absolute finite-difference steps (n x 1)numSteps- number ofhsteps taken per directionscheme- one-sided or two-sided differencinglb- per-variable lower bounds (n x 1)ub- per-variable upper bounds (n x 1)- Returns:
- adjusted step sizes plus per-dimension one-sided fall-back flags
-
epsForMethod
Calculate the relative EPS step for a given data type and FD method. Progressively smaller steps are used for larger floating-point types. Ifx0orf0isfloat, the smaller (less precise) machine epsilon is chosen.- Parameters:
x0Type- element type ofx0(floatordouble)f0Type- element type off0(floatordouble)method- FD scheme: TWO_POINT, THREE_POINT, or COMPLEX_STEP- Returns:
- relative step size for this combination
-
computeAbsoluteStep
public static Matrix computeAbsoluteStep(Matrix relStep, Matrix x0, Matrix f0, FiniteDifferenceMethod method) Compute the absolute finite-difference step from a relative step.- Parameters:
relStep- per-variable relative step (n x 1); passnullto derive a default fromepsForMethodx0- point of evaluation (n x 1)f0- function value atx0(m x 1); used only for value-type dispatch in this port (always treated asdouble)method- FD scheme: TWO_POINT, THREE_POINT, or COMPLEX_STEP- Returns:
- absolute step (
n x 1)
-
approxDerivative
public static double approxDerivative(DoubleUnaryOperator f, double x0, FiniteDifferenceOptions options) Finite-difference gradient of a scalar function of a scalar argument.- Parameters:
f- scalar functionR -> Rx0- evaluation pointoptions- FD configuration- Returns:
- scalar derivative
df/dxatx0
-
approxDerivative
public static double approxDerivative(ToDoubleFunction<Matrix> f, double x0, FiniteDifferenceOptions options) Finite-difference gradient of aToDoubleFunctionof a scalar argument.- Parameters:
f- scalar function evaluated on a1 x 1Matrixx0- evaluation pointoptions- FD configuration- Returns:
- scalar derivative
df/dxatx0
-
approxDerivative
public static double approxDerivative(ToDoubleFunction<Matrix> f, double x0, double f0, FiniteDifferenceOptions options) Finite-difference gradient of a scalar function of a scalar argument with a precomputedf0.- Parameters:
f- scalar function evaluated on a1 x 1Matrixx0- evaluation pointf0- precomputedf(x0)(saves one evaluation)options- FD configuration- Returns:
- scalar derivative
df/dxatx0
-
approxDerivative
public static Matrix approxDerivative(ToDoubleFunction<Matrix> f, Matrix x0, double f0, FiniteDifferenceOptions options) Finite-difference gradient of a real-valued function of a vector argument.- Parameters:
f- functionR^n -> Rx0- evaluation point (n x 1)f0- precomputedf(x0)options- FD configuration- Returns:
- gradient
gradf(x0)(1 x norn x 1depending on the underlying call shape)
-
approxDerivative
public static Matrix approxDerivative(Function<Matrix, Matrix> f, Matrix x0, Matrix f0, FiniteDifferenceOptions options) Finite-difference Jacobian of a vector-valued function.- Parameters:
f- functionR^n -> R^mx0- evaluation point (n x 1)f0- precomputedf(x0)(m x 1); passnullto evaluate insideoptions- FD configuration- Returns:
- Jacobian
J(m x n)
-
approxDerivative
public static Matrix approxDerivative(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Finite-difference Jacobian of a vector-valued function with extra args. See the class doc / scipy_numdiff.approx_derivativefor the full specification.- Parameters:
fun- function(x, args) -> f(x, args)x0- evaluation point (n x 1)f0- precomputedfun(x0, args)(m x 1); passnullto evaluate insideoptions- FD configurationargs- extra arguments forwarded tofun- Returns:
- Jacobian
J(m x n); aLinearOperatorwhenoptions.asLinearOperator()istrue
-
linearOperatorDifference
public static LinearOperator linearOperatorDifference(Function<Matrix, Matrix> fun, Matrix x0, Matrix f0, Matrix relStep, FiniteDifferenceMethod method) Build aLinearOperatorthat applies the FD JacobianJ(x0)*pon demand, without materialising the full Jacobian. Used byas_linear_operator=Trueconsumers.- Parameters:
fun- vector-valued function being differentiatedx0- evaluation pointf0- cachedfun(x0)relStep- relative step sizemethod- FD scheme (TWO_POINT, THREE_POINT, COMPLEX_STEP)- Returns:
- matrix-free FD-Jacobian linear operator
-
approxDerivativeAsLinearOperator
public static LinearOperator approxDerivativeAsLinearOperator(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Strict scipyapprox_derivative(..., as_linear_operator=True)counterpart: returns a matrix-freeLinearOperatorthat applies the FD JacobianJ(x0) * pon demand. Bounds are not supported in this mode (mirrors scipy).- Parameters:
fun- vector-valued function(x, args) -> f(x, args)x0- evaluation point (n x 1)f0- precomputedfun(x0, args)(m x 1);nullto evaluate insideoptions- FD configuration (onlymethodandrelStepare honored)args- extra args forwarded tofun- Returns:
- matrix-free FD-Jacobian linear operator
-
approxDerivativeWithWorkers
public static Matrix approxDerivativeWithWorkers(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args, ExecutorService executor) Strict scipyapprox_derivative(..., workers=...)counterpart: computes a dense FD Jacobian column-by-column with the per-column evaluations dispatched in parallel throughexecutor. Returns the samem x nJacobian as the serial overload.Thread safety:
funmust be safe to invoke concurrently (matches scipy's caveat forworkersmode).Sparse-FD and
as_linear_operatormodes are not supported by this overload; use the dedicated methods for those.- Parameters:
fun- vector-valued function(x, args) -> f(x, args)x0- evaluation point (n x 1)f0- precomputedfun(x0, args)(m x 1);nullto evaluate insideoptions- FD configurationargs- extra argsexecutor- pool to dispatch per-column evaluations on;nullfalls back to the serial path- Returns:
- dense FD Jacobian (
m x n)
-
approxDerivativeFullOutput
public static ApproxDerivativeResult approxDerivativeFullOutput(BiFunction<Matrix, Object, Matrix> fun, Matrix x0, Matrix f0, FiniteDifferenceOptions options, Object args) Strict scipyapprox_derivative(..., full_output=True)counterpart: returns the FD Jacobian alongside an info dict carrying metadata about the computation. The info dict has at minimum"nfev"(function-evaluation count) and"method"keys.- Parameters:
fun- vector-valued functionx0- evaluation point (n x 1)f0- precomputedfun(x0, args)(m x 1);nullto evaluate insideoptions- FD configurationargs- extra args- Returns:
- Jacobian and info dict bundled in
ApproxDerivativeResult
-