@tangent.to/opt 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Approximate the gradient of a scalar function by central finite differences.
3
+ *
4
+ * @param {(x: number[]) => number} f - Scalar function
5
+ * @param {number[]} x - Point at which to evaluate the gradient
6
+ * @param {Object} [options]
7
+ * @param {number} [options.h=1e-6] - Absolute step size
8
+ * @returns {number[]} Gradient approximation
9
+ * @example
10
+ * numericalGradient(([a, b]) => a * a + 3 * b, [1, 2]); // ≈ [2, 3]
11
+ */
12
+ export function numericalGradient(f: (x: number[]) => number, x: number[], options?: {
13
+ h?: number | undefined;
14
+ }): number[];
15
+ /**
16
+ * Approximate the Jacobian of a vector-valued function by central finite differences.
17
+ *
18
+ * Per-component step h_j = step * max(1, |x_j|).
19
+ *
20
+ * @param {(x: number[]) => ArrayLike<number>} f - Function returning m values
21
+ * @param {number[]} x - Point of length n
22
+ * @param {Object} [options]
23
+ * @param {number} [options.step=1e-6] - Relative step size
24
+ * @returns {number[][]} m-by-n Jacobian, `J[i][j] = d f_i / d x_j`
25
+ */
26
+ export function numericalJacobian(f: (x: number[]) => ArrayLike<number>, x: number[], options?: {
27
+ step?: number | undefined;
28
+ }): number[][];
29
+ /**
30
+ * Approximate the Hessian of a scalar function by central finite differences.
31
+ *
32
+ * Per-component step h_j = step * max(1, |x_j|).
33
+ *
34
+ * @param {(x: number[]) => number} f - Scalar function
35
+ * @param {number[]} x - Point of length n
36
+ * @param {Object} [options]
37
+ * @param {number} [options.step=1e-4] - Relative step size
38
+ * @returns {number[][]} n-by-n symmetric Hessian
39
+ */
40
+ export function numericalHessian(f: (x: number[]) => number, x: number[], options?: {
41
+ step?: number | undefined;
42
+ }): number[][];
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Minimize a univariate function.
3
+ *
4
+ * @param {(x: number) => number} f - Objective
5
+ * @param {Object} [options]
6
+ * @param {'brent'|'golden'} [options.method='brent'] - Brent's method or
7
+ * golden-section search; case-insensitive
8
+ * @param {number[]} [options.bracket=[0, 1]] - [a, b] to auto-bracket
9
+ * from (downhill), or a full bracketing triple [a, b, c] with f(b) <= f(a), f(b) <= f(c)
10
+ * @param {number} [options.xTol=1e-8] - Relative tolerance on x
11
+ * @param {number} [options.maxIter=500] - Maximum iterations
12
+ * @returns {{x: number, fx: number, iterations: number, nfev: number, fevals: number, converged: boolean, success: boolean}}
13
+ * `fevals` is a deprecated alias of `nfev`; `success` equals `converged`
14
+ * @throws {Error} if no minimum can be bracketed (f monotonic or unbounded below)
15
+ * @example
16
+ * minimizeScalar(Math.cos, { bracket: [2, 4] }).x; // 3.14159265441603
17
+ */
18
+ export function minimizeScalar(f: (x: number) => number, options?: {
19
+ method?: "brent" | "golden" | undefined;
20
+ bracket?: number[] | undefined;
21
+ xTol?: number | undefined;
22
+ maxIter?: number | undefined;
23
+ }): {
24
+ x: number;
25
+ fx: number;
26
+ iterations: number;
27
+ nfev: number;
28
+ fevals: number;
29
+ converged: boolean;
30
+ success: boolean;
31
+ };
32
+ /**
33
+ * Find a root of a univariate function inside a sign-changing bracket.
34
+ *
35
+ * @param {(x: number) => number} f - Function
36
+ * @param {Object} options
37
+ * @param {number[]} options.bracket - [a, b] with f(a) and f(b) of opposite
38
+ * signs (required)
39
+ * @param {'brent'|'bisect'} [options.method='brent'] - Brent-Dekker or
40
+ * bisection; case-insensitive
41
+ * @param {number} [options.xTol=1e-12] - Absolute tolerance on x
42
+ * @param {number} [options.rTol=4 * Number.EPSILON] - Relative tolerance on x
43
+ * @param {number} [options.maxIter=100] - Maximum iterations
44
+ * @returns {{x: number, fx: number, iterations: number, nfev: number, fevals: number, converged: boolean, success: boolean}}
45
+ * `fevals` is a deprecated alias of `nfev`; `success` equals `converged`
46
+ * @throws {Error} if f(a) and f(b) have the same sign, or f is not finite
47
+ * @example
48
+ * rootScalar(Math.cos, { bracket: [1, 2] }).x; // 1.5707963267948966
49
+ */
50
+ export function rootScalar(f: (x: number) => number, options?: {
51
+ bracket: number[];
52
+ method?: "brent" | "bisect" | undefined;
53
+ xTol?: number | undefined;
54
+ rTol?: number | undefined;
55
+ maxIter?: number | undefined;
56
+ }): {
57
+ x: number;
58
+ fx: number;
59
+ iterations: number;
60
+ nfev: number;
61
+ fevals: number;
62
+ converged: boolean;
63
+ success: boolean;
64
+ };
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Take one plain gradient-descent step in place: `x -= lr · g`.
3
+ *
4
+ * Stateless; returns `null` so it has the signature of the other steps,
5
+ * `state = step(x, gradient, state, options)`.
6
+ *
7
+ * @param {number[]|Float64Array} x - moved in place
8
+ * @param {ArrayLike<number>} gradient - same length as x
9
+ * @param {null} [_state] - unused
10
+ * @param {Object} [options] - read on every call
11
+ * @param {number} [options.learningRate=0.01] - Step size
12
+ * @returns {null}
13
+ */
14
+ export function gradientStep(x: number[] | Float64Array, gradient: ArrayLike<number>, _state?: null, options?: {
15
+ learningRate?: number | undefined;
16
+ }): null;
17
+ /**
18
+ * Take one gradient-descent-with-momentum step in place: `v = μ v + lr · g; x -= v`.
19
+ *
20
+ * @param {number[]|Float64Array} x - moved in place
21
+ * @param {ArrayLike<number>} gradient - same length as x
22
+ * @param {{ velocity: Float64Array }|null} [state] - from the previous call;
23
+ * null to start
24
+ * @param {Object} [options] - read on every call
25
+ * @param {number} [options.learningRate=0.01] - Step size
26
+ * @param {number} [options.momentum=0.9] - μ
27
+ * @returns {{ velocity: Float64Array }} the state, to pass to the next call
28
+ */
29
+ export function momentumStep(x: number[] | Float64Array, gradient: ArrayLike<number>, state?: {
30
+ velocity: Float64Array;
31
+ } | null, options?: {
32
+ learningRate?: number | undefined;
33
+ momentum?: number | undefined;
34
+ }): {
35
+ velocity: Float64Array;
36
+ };
37
+ /**
38
+ * Take one RMSProp step in place: a running mean of squared gradients scales each coordinate's step.
39
+ *
40
+ * @param {number[]|Float64Array} x - moved in place
41
+ * @param {ArrayLike<number>} gradient - same length as x
42
+ * @param {{ cache: Float64Array }|null} [state] - from the previous call;
43
+ * null to start
44
+ * @param {Object} [options] - read on every call
45
+ * @param {number} [options.learningRate=0.01] - Step size
46
+ * @param {number} [options.decay=0.9] - Decay of the running mean
47
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
48
+ * @returns {{ cache: Float64Array }} the state, to pass to the next call
49
+ */
50
+ export function rmspropStep(x: number[] | Float64Array, gradient: ArrayLike<number>, state?: {
51
+ cache: Float64Array;
52
+ } | null, options?: {
53
+ learningRate?: number | undefined;
54
+ decay?: number | undefined;
55
+ epsilon?: number | undefined;
56
+ }): {
57
+ cache: Float64Array;
58
+ };
59
+ /**
60
+ * Take one Adam step in place (Kingma and Ba, 2015): bias-corrected first and second moments.
61
+ *
62
+ * Keep one state per parameter tensor. The step count lives in the state, so
63
+ * bias correction is right however many tensors share one options object.
64
+ *
65
+ * @param {number[]|Float64Array} x - moved in place
66
+ * @param {ArrayLike<number>} gradient - same length as x
67
+ * @param {{ m: Float64Array, v: Float64Array, t: number }|null} [state] - from
68
+ * the previous call; null to start
69
+ * @param {Object} [options] - read on every call
70
+ * @param {number} [options.learningRate=0.01] - Step size
71
+ * @param {number} [options.beta1=0.9] - First-moment decay
72
+ * @param {number} [options.beta2=0.999] - Second-moment decay
73
+ * @param {number} [options.epsilon=1e-8] - Added to the denominator
74
+ * @returns {{ m: Float64Array, v: Float64Array, t: number }} the state, to
75
+ * pass to the next call
76
+ * @example
77
+ * const x = [3, -2];
78
+ * let state = null;
79
+ * for (let i = 0; i < 500; i++) {
80
+ * state = adamStep(x, [2 * x[0], 2 * x[1]], state, { learningRate: 0.1 });
81
+ * }
82
+ * // x ≈ [0, 0]
83
+ */
84
+ export function adamStep(x: number[] | Float64Array, gradient: ArrayLike<number>, state?: {
85
+ m: Float64Array;
86
+ v: Float64Array;
87
+ t: number;
88
+ } | null, options?: {
89
+ learningRate?: number | undefined;
90
+ beta1?: number | undefined;
91
+ beta2?: number | undefined;
92
+ epsilon?: number | undefined;
93
+ }): {
94
+ m: Float64Array;
95
+ v: Float64Array;
96
+ t: number;
97
+ };
@@ -0,0 +1,2 @@
1
+ /** This package's version. */
2
+ export const version: "0.2.2";