numopt-js 0.1.0
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.
- package/CODING_RULES.md +161 -0
- package/LICENSE +22 -0
- package/README.md +807 -0
- package/dist/core/adjointGradientDescent.d.ts +61 -0
- package/dist/core/adjointGradientDescent.d.ts.map +1 -0
- package/dist/core/adjointGradientDescent.js +764 -0
- package/dist/core/adjointGradientDescent.js.map +1 -0
- package/dist/core/constrainedGaussNewton.d.ts +44 -0
- package/dist/core/constrainedGaussNewton.d.ts.map +1 -0
- package/dist/core/constrainedGaussNewton.js +314 -0
- package/dist/core/constrainedGaussNewton.js.map +1 -0
- package/dist/core/constrainedLevenbergMarquardt.d.ts +46 -0
- package/dist/core/constrainedLevenbergMarquardt.d.ts.map +1 -0
- package/dist/core/constrainedLevenbergMarquardt.js +469 -0
- package/dist/core/constrainedLevenbergMarquardt.js.map +1 -0
- package/dist/core/constrainedUtils.d.ts +92 -0
- package/dist/core/constrainedUtils.d.ts.map +1 -0
- package/dist/core/constrainedUtils.js +364 -0
- package/dist/core/constrainedUtils.js.map +1 -0
- package/dist/core/convergence.d.ts +35 -0
- package/dist/core/convergence.d.ts.map +1 -0
- package/dist/core/convergence.js +51 -0
- package/dist/core/convergence.js.map +1 -0
- package/dist/core/createGradientFunction.d.ts +85 -0
- package/dist/core/createGradientFunction.d.ts.map +1 -0
- package/dist/core/createGradientFunction.js +93 -0
- package/dist/core/createGradientFunction.js.map +1 -0
- package/dist/core/effectiveJacobian.d.ts +90 -0
- package/dist/core/effectiveJacobian.d.ts.map +1 -0
- package/dist/core/effectiveJacobian.js +128 -0
- package/dist/core/effectiveJacobian.js.map +1 -0
- package/dist/core/finiteDiff.d.ts +171 -0
- package/dist/core/finiteDiff.d.ts.map +1 -0
- package/dist/core/finiteDiff.js +363 -0
- package/dist/core/finiteDiff.js.map +1 -0
- package/dist/core/gaussNewton.d.ts +29 -0
- package/dist/core/gaussNewton.d.ts.map +1 -0
- package/dist/core/gaussNewton.js +151 -0
- package/dist/core/gaussNewton.js.map +1 -0
- package/dist/core/gradientDescent.d.ts +35 -0
- package/dist/core/gradientDescent.d.ts.map +1 -0
- package/dist/core/gradientDescent.js +204 -0
- package/dist/core/gradientDescent.js.map +1 -0
- package/dist/core/jacobianComputation.d.ts +24 -0
- package/dist/core/jacobianComputation.d.ts.map +1 -0
- package/dist/core/jacobianComputation.js +38 -0
- package/dist/core/jacobianComputation.js.map +1 -0
- package/dist/core/levenbergMarquardt.d.ts +36 -0
- package/dist/core/levenbergMarquardt.d.ts.map +1 -0
- package/dist/core/levenbergMarquardt.js +286 -0
- package/dist/core/levenbergMarquardt.js.map +1 -0
- package/dist/core/lineSearch.d.ts +42 -0
- package/dist/core/lineSearch.d.ts.map +1 -0
- package/dist/core/lineSearch.js +106 -0
- package/dist/core/lineSearch.js.map +1 -0
- package/dist/core/logger.d.ts +77 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +162 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/types.d.ts +427 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +15 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/formatting.d.ts +27 -0
- package/dist/utils/formatting.d.ts.map +1 -0
- package/dist/utils/formatting.js +54 -0
- package/dist/utils/formatting.js.map +1 -0
- package/dist/utils/matrix.d.ts +63 -0
- package/dist/utils/matrix.d.ts.map +1 -0
- package/dist/utils/matrix.js +129 -0
- package/dist/utils/matrix.js.map +1 -0
- package/dist/utils/resultFormatter.d.ts +122 -0
- package/dist/utils/resultFormatter.d.ts.map +1 -0
- package/dist/utils/resultFormatter.js +342 -0
- package/dist/utils/resultFormatter.js.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file provides utility functions for converting between Float64Array
|
|
3
|
+
* and ml-matrix Matrix types, and for vector operations.
|
|
4
|
+
*
|
|
5
|
+
* Role in system:
|
|
6
|
+
* - Bridges the gap between native JavaScript arrays and ml-matrix library
|
|
7
|
+
* - Provides efficient conversion utilities used throughout the codebase
|
|
8
|
+
* - Implements common vector operations needed by optimization algorithms
|
|
9
|
+
*
|
|
10
|
+
* For first-time readers:
|
|
11
|
+
* - These are helper functions used by core algorithms
|
|
12
|
+
* - Focus on understanding the conversion functions first
|
|
13
|
+
* - Vector norm computation is used for convergence checks
|
|
14
|
+
*/
|
|
15
|
+
import { Matrix } from 'ml-matrix';
|
|
16
|
+
/**
|
|
17
|
+
* Converts a Float64Array to an ml-matrix Matrix (column vector).
|
|
18
|
+
* This is efficient for single-column matrices used in optimization.
|
|
19
|
+
*/
|
|
20
|
+
export declare function float64ArrayToMatrix(vector: Float64Array): Matrix;
|
|
21
|
+
/**
|
|
22
|
+
* Converts an ml-matrix Matrix to a Float64Array.
|
|
23
|
+
* Assumes the matrix is a column vector (single column).
|
|
24
|
+
*/
|
|
25
|
+
export declare function matrixToFloat64Array(matrix: Matrix): Float64Array;
|
|
26
|
+
/**
|
|
27
|
+
* Converts a 2D ml-matrix Matrix to a Float64Array (row-major order).
|
|
28
|
+
* Used for converting Jacobian matrices to flat arrays when needed.
|
|
29
|
+
*/
|
|
30
|
+
export declare function matrixToFloat64Array2D(matrix: Matrix): Float64Array;
|
|
31
|
+
/**
|
|
32
|
+
* Computes the L2 (Euclidean) norm of a Float64Array vector.
|
|
33
|
+
* Used for convergence checks and gradient norm calculations.
|
|
34
|
+
*/
|
|
35
|
+
export declare function vectorNorm(vector: Float64Array): number;
|
|
36
|
+
/**
|
|
37
|
+
* Computes the dot product of two Float64Array vectors.
|
|
38
|
+
* Both vectors must have the same length.
|
|
39
|
+
*/
|
|
40
|
+
export declare function dotProduct(vectorA: Float64Array, vectorB: Float64Array): number;
|
|
41
|
+
/**
|
|
42
|
+
* Adds two Float64Array vectors element-wise.
|
|
43
|
+
* Returns a new Float64Array with the result.
|
|
44
|
+
*/
|
|
45
|
+
export declare function addVectors(vectorA: Float64Array, vectorB: Float64Array): Float64Array;
|
|
46
|
+
/**
|
|
47
|
+
* Subtracts vectorB from vectorA element-wise.
|
|
48
|
+
* Returns a new Float64Array with the result.
|
|
49
|
+
*/
|
|
50
|
+
export declare function subtractVectors(vectorA: Float64Array, vectorB: Float64Array): Float64Array;
|
|
51
|
+
/**
|
|
52
|
+
* Multiplies a Float64Array vector by a scalar.
|
|
53
|
+
* Returns a new Float64Array with the result.
|
|
54
|
+
*/
|
|
55
|
+
export declare function scaleVector(vector: Float64Array, scalar: number): Float64Array;
|
|
56
|
+
/**
|
|
57
|
+
* Computes the sum of squared residuals (cost function for least squares problems).
|
|
58
|
+
* This is the squared L2 norm: ||r||^2 = r^T r = sum(r_i^2)
|
|
59
|
+
*
|
|
60
|
+
* Used to avoid code duplication when computing cost from residual norm.
|
|
61
|
+
*/
|
|
62
|
+
export declare function computeSumOfSquaredResiduals(residualNorm: number): number;
|
|
63
|
+
//# sourceMappingURL=matrix.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matrix.d.ts","sourceRoot":"","sources":["../../src/utils/matrix.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CASjE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CASjE;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAYnE;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CASvD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CAY/E;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,GAAG,YAAY,CAYrF;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,GAAG,YAAY,CAY1F;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAQ9E;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEzE"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file provides utility functions for converting between Float64Array
|
|
3
|
+
* and ml-matrix Matrix types, and for vector operations.
|
|
4
|
+
*
|
|
5
|
+
* Role in system:
|
|
6
|
+
* - Bridges the gap between native JavaScript arrays and ml-matrix library
|
|
7
|
+
* - Provides efficient conversion utilities used throughout the codebase
|
|
8
|
+
* - Implements common vector operations needed by optimization algorithms
|
|
9
|
+
*
|
|
10
|
+
* For first-time readers:
|
|
11
|
+
* - These are helper functions used by core algorithms
|
|
12
|
+
* - Focus on understanding the conversion functions first
|
|
13
|
+
* - Vector norm computation is used for convergence checks
|
|
14
|
+
*/
|
|
15
|
+
import { Matrix } from 'ml-matrix';
|
|
16
|
+
/**
|
|
17
|
+
* Converts a Float64Array to an ml-matrix Matrix (column vector).
|
|
18
|
+
* This is efficient for single-column matrices used in optimization.
|
|
19
|
+
*/
|
|
20
|
+
export function float64ArrayToMatrix(vector) {
|
|
21
|
+
const rows = vector.length;
|
|
22
|
+
const matrixData = [];
|
|
23
|
+
for (let i = 0; i < rows; i++) {
|
|
24
|
+
matrixData.push([vector[i]]);
|
|
25
|
+
}
|
|
26
|
+
return new Matrix(matrixData);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts an ml-matrix Matrix to a Float64Array.
|
|
30
|
+
* Assumes the matrix is a column vector (single column).
|
|
31
|
+
*/
|
|
32
|
+
export function matrixToFloat64Array(matrix) {
|
|
33
|
+
const rows = matrix.rows;
|
|
34
|
+
const result = new Float64Array(rows);
|
|
35
|
+
for (let i = 0; i < rows; i++) {
|
|
36
|
+
result[i] = matrix.get(i, 0);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts a 2D ml-matrix Matrix to a Float64Array (row-major order).
|
|
42
|
+
* Used for converting Jacobian matrices to flat arrays when needed.
|
|
43
|
+
*/
|
|
44
|
+
export function matrixToFloat64Array2D(matrix) {
|
|
45
|
+
const rows = matrix.rows;
|
|
46
|
+
const cols = matrix.columns;
|
|
47
|
+
const result = new Float64Array(rows * cols);
|
|
48
|
+
for (let i = 0; i < rows; i++) {
|
|
49
|
+
for (let j = 0; j < cols; j++) {
|
|
50
|
+
result[i * cols + j] = matrix.get(i, j);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Computes the L2 (Euclidean) norm of a Float64Array vector.
|
|
57
|
+
* Used for convergence checks and gradient norm calculations.
|
|
58
|
+
*/
|
|
59
|
+
export function vectorNorm(vector) {
|
|
60
|
+
let sumOfSquares = 0.0;
|
|
61
|
+
for (let i = 0; i < vector.length; i++) {
|
|
62
|
+
const value = vector[i];
|
|
63
|
+
sumOfSquares += value * value;
|
|
64
|
+
}
|
|
65
|
+
return Math.sqrt(sumOfSquares);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Computes the dot product of two Float64Array vectors.
|
|
69
|
+
* Both vectors must have the same length.
|
|
70
|
+
*/
|
|
71
|
+
export function dotProduct(vectorA, vectorB) {
|
|
72
|
+
if (vectorA.length !== vectorB.length) {
|
|
73
|
+
throw new Error('Vectors must have the same length for dot product');
|
|
74
|
+
}
|
|
75
|
+
let sum = 0.0;
|
|
76
|
+
for (let i = 0; i < vectorA.length; i++) {
|
|
77
|
+
sum += vectorA[i] * vectorB[i];
|
|
78
|
+
}
|
|
79
|
+
return sum;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Adds two Float64Array vectors element-wise.
|
|
83
|
+
* Returns a new Float64Array with the result.
|
|
84
|
+
*/
|
|
85
|
+
export function addVectors(vectorA, vectorB) {
|
|
86
|
+
if (vectorA.length !== vectorB.length) {
|
|
87
|
+
throw new Error('Vectors must have the same length for addition');
|
|
88
|
+
}
|
|
89
|
+
const result = new Float64Array(vectorA.length);
|
|
90
|
+
for (let i = 0; i < vectorA.length; i++) {
|
|
91
|
+
result[i] = vectorA[i] + vectorB[i];
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Subtracts vectorB from vectorA element-wise.
|
|
97
|
+
* Returns a new Float64Array with the result.
|
|
98
|
+
*/
|
|
99
|
+
export function subtractVectors(vectorA, vectorB) {
|
|
100
|
+
if (vectorA.length !== vectorB.length) {
|
|
101
|
+
throw new Error('Vectors must have the same length for subtraction');
|
|
102
|
+
}
|
|
103
|
+
const result = new Float64Array(vectorA.length);
|
|
104
|
+
for (let i = 0; i < vectorA.length; i++) {
|
|
105
|
+
result[i] = vectorA[i] - vectorB[i];
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Multiplies a Float64Array vector by a scalar.
|
|
111
|
+
* Returns a new Float64Array with the result.
|
|
112
|
+
*/
|
|
113
|
+
export function scaleVector(vector, scalar) {
|
|
114
|
+
const result = new Float64Array(vector.length);
|
|
115
|
+
for (let i = 0; i < vector.length; i++) {
|
|
116
|
+
result[i] = vector[i] * scalar;
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Computes the sum of squared residuals (cost function for least squares problems).
|
|
122
|
+
* This is the squared L2 norm: ||r||^2 = r^T r = sum(r_i^2)
|
|
123
|
+
*
|
|
124
|
+
* Used to avoid code duplication when computing cost from residual norm.
|
|
125
|
+
*/
|
|
126
|
+
export function computeSumOfSquaredResiduals(residualNorm) {
|
|
127
|
+
return residualNorm * residualNorm;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=matrix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../../src/utils/matrix.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAoB;IACvD,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3B,MAAM,UAAU,GAAe,EAAE,CAAC;IAElC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,UAAU,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;AAChC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACzB,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC;IAEtC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAs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AAI,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;IACjC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,4BAA4B,CAAC,YAAoB;IAC/D,OAAO,YAAY,GAAG,YAAY,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file provides helper functions for formatting and displaying
|
|
3
|
+
* optimization results in a consistent, user-friendly manner.
|
|
4
|
+
*
|
|
5
|
+
* Role in system:
|
|
6
|
+
* - Replaces repetitive console.log statements in examples
|
|
7
|
+
* - Provides consistent formatting across all result types
|
|
8
|
+
* - Improves UI/UX with better readability and structure
|
|
9
|
+
*
|
|
10
|
+
* For first-time readers:
|
|
11
|
+
* - Use printOptimizationResult() for direct console output
|
|
12
|
+
* - Use formatOptimizationResult() to get formatted string
|
|
13
|
+
* - Functions automatically handle different result types via overloading
|
|
14
|
+
*/
|
|
15
|
+
import type { OptimizationResult, GradientDescentResult, LevenbergMarquardtResult, AdjointGradientDescentResult, ConstrainedGaussNewtonResult, ConstrainedLevenbergMarquardtResult } from '../core/types.js';
|
|
16
|
+
/**
|
|
17
|
+
* Options for customizing result formatting.
|
|
18
|
+
*/
|
|
19
|
+
export interface ResultFormatterOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Whether to show section headers (e.g., "=== Optimization Results ===").
|
|
22
|
+
* Default: true
|
|
23
|
+
*/
|
|
24
|
+
showSectionHeaders?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Whether to show execution time information.
|
|
27
|
+
* Default: false (requires elapsedTimeMs to be provided)
|
|
28
|
+
*/
|
|
29
|
+
showExecutionTime?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Execution time in milliseconds (for display).
|
|
32
|
+
* Only used if showExecutionTime is true.
|
|
33
|
+
*/
|
|
34
|
+
elapsedTimeMs?: number;
|
|
35
|
+
/**
|
|
36
|
+
* Maximum number of parameters to display before truncating.
|
|
37
|
+
* Default: 10
|
|
38
|
+
*/
|
|
39
|
+
maxParametersToShow?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Precision for parameter values.
|
|
42
|
+
* Default: 6
|
|
43
|
+
*/
|
|
44
|
+
parameterPrecision?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Precision for cost and norm values.
|
|
47
|
+
* Default: 8
|
|
48
|
+
*/
|
|
49
|
+
costPrecision?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Precision for constraint violation values.
|
|
52
|
+
* Default: 10
|
|
53
|
+
*/
|
|
54
|
+
constraintPrecision?: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Formats a basic OptimizationResult.
|
|
58
|
+
*/
|
|
59
|
+
export declare function formatOptimizationResult(result: OptimizationResult, options?: ResultFormatterOptions): string;
|
|
60
|
+
/**
|
|
61
|
+
* Formats a GradientDescentResult.
|
|
62
|
+
*/
|
|
63
|
+
export declare function formatGradientDescentResult(result: GradientDescentResult, options?: ResultFormatterOptions): string;
|
|
64
|
+
/**
|
|
65
|
+
* Formats a LevenbergMarquardtResult.
|
|
66
|
+
*/
|
|
67
|
+
export declare function formatLevenbergMarquardtResult(result: LevenbergMarquardtResult, options?: ResultFormatterOptions): string;
|
|
68
|
+
/**
|
|
69
|
+
* Formats a ConstrainedGaussNewtonResult.
|
|
70
|
+
*/
|
|
71
|
+
export declare function formatConstrainedGaussNewtonResult(result: ConstrainedGaussNewtonResult, options?: ResultFormatterOptions): string;
|
|
72
|
+
/**
|
|
73
|
+
* Formats a ConstrainedLevenbergMarquardtResult.
|
|
74
|
+
*/
|
|
75
|
+
export declare function formatConstrainedLevenbergMarquardtResult(result: ConstrainedLevenbergMarquardtResult, options?: ResultFormatterOptions): string;
|
|
76
|
+
/**
|
|
77
|
+
* Formats an AdjointGradientDescentResult.
|
|
78
|
+
*/
|
|
79
|
+
export declare function formatAdjointGradientDescentResult(result: AdjointGradientDescentResult, options?: ResultFormatterOptions): string;
|
|
80
|
+
/**
|
|
81
|
+
* Type-safe overloaded function for formatting any optimization result.
|
|
82
|
+
*/
|
|
83
|
+
export declare function formatResult(result: OptimizationResult, options?: ResultFormatterOptions): string;
|
|
84
|
+
export declare function formatResult(result: GradientDescentResult, options?: ResultFormatterOptions): string;
|
|
85
|
+
export declare function formatResult(result: LevenbergMarquardtResult, options?: ResultFormatterOptions): string;
|
|
86
|
+
export declare function formatResult(result: ConstrainedGaussNewtonResult, options?: ResultFormatterOptions): string;
|
|
87
|
+
export declare function formatResult(result: ConstrainedLevenbergMarquardtResult, options?: ResultFormatterOptions): string;
|
|
88
|
+
export declare function formatResult(result: AdjointGradientDescentResult, options?: ResultFormatterOptions): string;
|
|
89
|
+
/**
|
|
90
|
+
* Prints an optimization result directly to console.
|
|
91
|
+
*/
|
|
92
|
+
export declare function printOptimizationResult(result: OptimizationResult, options?: ResultFormatterOptions): void;
|
|
93
|
+
/**
|
|
94
|
+
* Prints a gradient descent result directly to console.
|
|
95
|
+
*/
|
|
96
|
+
export declare function printGradientDescentResult(result: GradientDescentResult, options?: ResultFormatterOptions): void;
|
|
97
|
+
/**
|
|
98
|
+
* Prints a Levenberg-Marquardt result directly to console.
|
|
99
|
+
*/
|
|
100
|
+
export declare function printLevenbergMarquardtResult(result: LevenbergMarquardtResult, options?: ResultFormatterOptions): void;
|
|
101
|
+
/**
|
|
102
|
+
* Prints a constrained Gauss-Newton result directly to console.
|
|
103
|
+
*/
|
|
104
|
+
export declare function printConstrainedGaussNewtonResult(result: ConstrainedGaussNewtonResult, options?: ResultFormatterOptions): void;
|
|
105
|
+
/**
|
|
106
|
+
* Prints a constrained Levenberg-Marquardt result directly to console.
|
|
107
|
+
*/
|
|
108
|
+
export declare function printConstrainedLevenbergMarquardtResult(result: ConstrainedLevenbergMarquardtResult, options?: ResultFormatterOptions): void;
|
|
109
|
+
/**
|
|
110
|
+
* Prints an adjoint gradient descent result directly to console.
|
|
111
|
+
*/
|
|
112
|
+
export declare function printAdjointGradientDescentResult(result: AdjointGradientDescentResult, options?: ResultFormatterOptions): void;
|
|
113
|
+
/**
|
|
114
|
+
* Type-safe overloaded function for printing any optimization result.
|
|
115
|
+
*/
|
|
116
|
+
export declare function printResult(result: OptimizationResult, options?: ResultFormatterOptions): void;
|
|
117
|
+
export declare function printResult(result: GradientDescentResult, options?: ResultFormatterOptions): void;
|
|
118
|
+
export declare function printResult(result: LevenbergMarquardtResult, options?: ResultFormatterOptions): void;
|
|
119
|
+
export declare function printResult(result: ConstrainedGaussNewtonResult, options?: ResultFormatterOptions): void;
|
|
120
|
+
export declare function printResult(result: ConstrainedLevenbergMarquardtResult, options?: ResultFormatterOptions): void;
|
|
121
|
+
export declare function printResult(result: AdjointGradientDescentResult, options?: ResultFormatterOptions): void;
|
|
122
|
+
//# sourceMappingURL=resultFormatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resultFormatter.d.ts","sourceRoot":"","sources":["../../src/utils/resultFormatter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,qBAAqB,EACrB,wBAAwB,EACxB,4BAA4B,EAC5B,4BAA4B,EAC5B,mCAAmC,EACpC,MAAM,kBAAkB,CAAC;AAG1B;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAE7B;;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EAAE,4BAA4B,EACpC,OAAO,CAAC,EAAE,sBAAsB,GAC/B,IAAI,CAAC;AACR,wBAAgB,WAAW,CACzB,MAAM,EAAE,mCAAmC,EAC3C,OAAO,CAAC,EAAE,sBAAsB,GAC/B,IAAI,CAAC;AACR,wBAAgB,WAAW,CACzB,MAAM,EAAE,4BAA4B,EACpC,OAAO,CAAC,EAAE,sBAAsB,GAC/B,IAAI,CAAC"}
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file provides helper functions for formatting and displaying
|
|
3
|
+
* optimization results in a consistent, user-friendly manner.
|
|
4
|
+
*
|
|
5
|
+
* Role in system:
|
|
6
|
+
* - Replaces repetitive console.log statements in examples
|
|
7
|
+
* - Provides consistent formatting across all result types
|
|
8
|
+
* - Improves UI/UX with better readability and structure
|
|
9
|
+
*
|
|
10
|
+
* For first-time readers:
|
|
11
|
+
* - Use printOptimizationResult() for direct console output
|
|
12
|
+
* - Use formatOptimizationResult() to get formatted string
|
|
13
|
+
* - Functions automatically handle different result types via overloading
|
|
14
|
+
*/
|
|
15
|
+
import { formatNumberWithPrecision } from './formatting.js';
|
|
16
|
+
const DEFAULT_OPTIONS = {
|
|
17
|
+
showSectionHeaders: true,
|
|
18
|
+
showExecutionTime: false,
|
|
19
|
+
elapsedTimeMs: 0,
|
|
20
|
+
maxParametersToShow: 10,
|
|
21
|
+
parameterPrecision: 6,
|
|
22
|
+
costPrecision: 8,
|
|
23
|
+
constraintPrecision: 10
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Formats a parameter array for display.
|
|
27
|
+
* Automatically switches between individual and array format based on size.
|
|
28
|
+
*/
|
|
29
|
+
function formatParameters(parameters, options) {
|
|
30
|
+
const length = parameters.length;
|
|
31
|
+
const maxShow = options.maxParametersToShow;
|
|
32
|
+
const precision = options.parameterPrecision;
|
|
33
|
+
if (length === 0) {
|
|
34
|
+
return '[]';
|
|
35
|
+
}
|
|
36
|
+
// For small arrays (≤3 elements), show individually with labels
|
|
37
|
+
if (length <= 3) {
|
|
38
|
+
const labels = ['p', 'x', 'y'];
|
|
39
|
+
return Array.from(parameters)
|
|
40
|
+
.map((value, index) => {
|
|
41
|
+
const label = labels[index] || `param${index}`;
|
|
42
|
+
return `${label} = ${formatNumberWithPrecision(value, precision)}`;
|
|
43
|
+
})
|
|
44
|
+
.join(', ');
|
|
45
|
+
}
|
|
46
|
+
// For medium arrays (4-10 elements), show as array
|
|
47
|
+
if (length <= maxShow) {
|
|
48
|
+
const formatted = Array.from(parameters)
|
|
49
|
+
.map(value => formatNumberWithPrecision(value, precision))
|
|
50
|
+
.join(', ');
|
|
51
|
+
return `[${formatted}]`;
|
|
52
|
+
}
|
|
53
|
+
// For large arrays, show first 5 elements + "... and N more"
|
|
54
|
+
const firstFew = Array.from(parameters.slice(0, 5))
|
|
55
|
+
.map(value => formatNumberWithPrecision(value, precision))
|
|
56
|
+
.join(', ');
|
|
57
|
+
const remaining = length - 5;
|
|
58
|
+
return `[${firstFew}, ... and ${remaining} more]`;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Formats a state array for display (used in constrained optimization).
|
|
62
|
+
*/
|
|
63
|
+
function formatStates(states, options) {
|
|
64
|
+
return formatParameters(states, options);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Formats basic optimization result information.
|
|
68
|
+
*/
|
|
69
|
+
function formatBasicResult(result, options) {
|
|
70
|
+
const lines = [];
|
|
71
|
+
if (options.showSectionHeaders) {
|
|
72
|
+
lines.push('\n=== Optimization Results ===');
|
|
73
|
+
}
|
|
74
|
+
// Parameters
|
|
75
|
+
lines.push('Optimized parameters:');
|
|
76
|
+
const paramStr = formatParameters(result.parameters, options);
|
|
77
|
+
lines.push(` ${paramStr}`);
|
|
78
|
+
// Cost
|
|
79
|
+
lines.push('\nCost:');
|
|
80
|
+
lines.push(` f(p) = ${formatNumberWithPrecision(result.finalCost, options.costPrecision)}`);
|
|
81
|
+
// Convergence information
|
|
82
|
+
lines.push('\nConvergence:');
|
|
83
|
+
lines.push(` Converged: ${result.converged}`);
|
|
84
|
+
lines.push(` Iterations: ${result.iterations}`);
|
|
85
|
+
if (result.finalGradientNorm !== undefined) {
|
|
86
|
+
lines.push(` Final gradient norm: ${formatNumberWithPrecision(result.finalGradientNorm, options.costPrecision)}`);
|
|
87
|
+
}
|
|
88
|
+
if (result.finalResidualNorm !== undefined) {
|
|
89
|
+
lines.push(` Final residual norm: ${formatNumberWithPrecision(result.finalResidualNorm, options.costPrecision)}`);
|
|
90
|
+
}
|
|
91
|
+
// Execution time
|
|
92
|
+
if (options.showExecutionTime && options.elapsedTimeMs > 0) {
|
|
93
|
+
lines.push(`\nExecution time: ${formatNumberWithPrecision(options.elapsedTimeMs, 2)} ms`);
|
|
94
|
+
if (result.iterations > 0) {
|
|
95
|
+
const timePerIteration = options.elapsedTimeMs / result.iterations;
|
|
96
|
+
lines.push(`Time per iteration: ${formatNumberWithPrecision(timePerIteration, 3)} ms`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return lines;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Formats a basic OptimizationResult.
|
|
103
|
+
*/
|
|
104
|
+
export function formatOptimizationResult(result, options) {
|
|
105
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
106
|
+
const lines = formatBasicResult(result, opts);
|
|
107
|
+
return lines.join('\n');
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Formats a GradientDescentResult.
|
|
111
|
+
*/
|
|
112
|
+
export function formatGradientDescentResult(result, options) {
|
|
113
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
114
|
+
const lines = formatBasicResult(result, opts);
|
|
115
|
+
// Add line search information
|
|
116
|
+
const lineSearchIndex = lines.findIndex(line => line.includes('Final gradient norm') || line.includes('Final residual norm'));
|
|
117
|
+
if (lineSearchIndex >= 0) {
|
|
118
|
+
lines.splice(lineSearchIndex + 1, 0, ` Used line search: ${result.usedLineSearch}`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
lines.push(` Used line search: ${result.usedLineSearch}`);
|
|
122
|
+
}
|
|
123
|
+
return lines.join('\n');
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Formats a LevenbergMarquardtResult.
|
|
127
|
+
*/
|
|
128
|
+
export function formatLevenbergMarquardtResult(result, options) {
|
|
129
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
130
|
+
const lines = formatBasicResult(result, opts);
|
|
131
|
+
// Add lambda information
|
|
132
|
+
const lambdaIndex = lines.findIndex(line => line.includes('Final residual norm'));
|
|
133
|
+
if (lambdaIndex >= 0) {
|
|
134
|
+
lines.splice(lambdaIndex + 1, 0, ` Final lambda: ${formatNumberWithPrecision(result.finalLambda, 6)}`);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
lines.push(` Final lambda: ${formatNumberWithPrecision(result.finalLambda, 6)}`);
|
|
138
|
+
}
|
|
139
|
+
return lines.join('\n');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Formats a ConstrainedGaussNewtonResult.
|
|
143
|
+
*/
|
|
144
|
+
export function formatConstrainedGaussNewtonResult(result, options) {
|
|
145
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
146
|
+
const lines = [];
|
|
147
|
+
if (opts.showSectionHeaders) {
|
|
148
|
+
lines.push('\n=== Optimization Results ===');
|
|
149
|
+
}
|
|
150
|
+
// Parameters
|
|
151
|
+
lines.push('Optimized parameters:');
|
|
152
|
+
const paramStr = formatParameters(result.parameters, opts);
|
|
153
|
+
lines.push(` ${paramStr}`);
|
|
154
|
+
// States
|
|
155
|
+
lines.push('Optimized states:');
|
|
156
|
+
const stateStr = formatStates(result.finalStates, opts);
|
|
157
|
+
lines.push(` ${stateStr}`);
|
|
158
|
+
// Cost
|
|
159
|
+
lines.push('\nCost:');
|
|
160
|
+
lines.push(` f(p, x) = ${formatNumberWithPrecision(result.finalCost, opts.costPrecision)}`);
|
|
161
|
+
// Constraint satisfaction
|
|
162
|
+
lines.push('\nConstraint satisfaction:');
|
|
163
|
+
if (result.finalConstraintNorm !== undefined) {
|
|
164
|
+
lines.push(` ||c(p, x)|| = ${formatNumberWithPrecision(result.finalConstraintNorm, opts.constraintPrecision)}`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
lines.push(` ||c(p, x)|| = N/A`);
|
|
168
|
+
}
|
|
169
|
+
// Convergence information
|
|
170
|
+
lines.push('\nConvergence:');
|
|
171
|
+
lines.push(` Converged: ${result.converged}`);
|
|
172
|
+
lines.push(` Iterations: ${result.iterations}`);
|
|
173
|
+
if (result.finalGradientNorm !== undefined) {
|
|
174
|
+
lines.push(` Final gradient norm: ${formatNumberWithPrecision(result.finalGradientNorm, opts.costPrecision)}`);
|
|
175
|
+
}
|
|
176
|
+
if (result.finalResidualNorm !== undefined) {
|
|
177
|
+
lines.push(` Final residual norm: ${formatNumberWithPrecision(result.finalResidualNorm, opts.costPrecision)}`);
|
|
178
|
+
}
|
|
179
|
+
// Execution time
|
|
180
|
+
if (opts.showExecutionTime && opts.elapsedTimeMs > 0) {
|
|
181
|
+
lines.push(`\nExecution time: ${formatNumberWithPrecision(opts.elapsedTimeMs, 2)} ms`);
|
|
182
|
+
if (result.iterations > 0) {
|
|
183
|
+
const timePerIteration = opts.elapsedTimeMs / result.iterations;
|
|
184
|
+
lines.push(`Time per iteration: ${formatNumberWithPrecision(timePerIteration, 3)} ms`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return lines.join('\n');
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Formats a ConstrainedLevenbergMarquardtResult.
|
|
191
|
+
*/
|
|
192
|
+
export function formatConstrainedLevenbergMarquardtResult(result, options) {
|
|
193
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
194
|
+
const lines = [];
|
|
195
|
+
if (opts.showSectionHeaders) {
|
|
196
|
+
lines.push('\n=== Optimization Results ===');
|
|
197
|
+
}
|
|
198
|
+
// Parameters
|
|
199
|
+
lines.push('Optimized parameters:');
|
|
200
|
+
const paramStr = formatParameters(result.parameters, opts);
|
|
201
|
+
lines.push(` ${paramStr}`);
|
|
202
|
+
// States
|
|
203
|
+
lines.push('Optimized states:');
|
|
204
|
+
const stateStr = formatStates(result.finalStates, opts);
|
|
205
|
+
lines.push(` ${stateStr}`);
|
|
206
|
+
// Cost
|
|
207
|
+
lines.push('\nCost:');
|
|
208
|
+
lines.push(` f(p, x) = ${formatNumberWithPrecision(result.finalCost, opts.costPrecision)}`);
|
|
209
|
+
// Constraint satisfaction
|
|
210
|
+
lines.push('\nConstraint satisfaction:');
|
|
211
|
+
if (result.finalConstraintNorm !== undefined) {
|
|
212
|
+
lines.push(` ||c(p, x)|| = ${formatNumberWithPrecision(result.finalConstraintNorm, opts.constraintPrecision)}`);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
lines.push(` ||c(p, x)|| = N/A`);
|
|
216
|
+
}
|
|
217
|
+
// Convergence information
|
|
218
|
+
lines.push('\nConvergence:');
|
|
219
|
+
lines.push(` Converged: ${result.converged}`);
|
|
220
|
+
lines.push(` Iterations: ${result.iterations}`);
|
|
221
|
+
if (result.finalGradientNorm !== undefined) {
|
|
222
|
+
lines.push(` Final gradient norm: ${formatNumberWithPrecision(result.finalGradientNorm, opts.costPrecision)}`);
|
|
223
|
+
}
|
|
224
|
+
if (result.finalResidualNorm !== undefined) {
|
|
225
|
+
lines.push(` Final residual norm: ${formatNumberWithPrecision(result.finalResidualNorm, opts.costPrecision)}`);
|
|
226
|
+
}
|
|
227
|
+
lines.push(` Final lambda: ${formatNumberWithPrecision(result.finalLambda, 6)}`);
|
|
228
|
+
// Execution time
|
|
229
|
+
if (opts.showExecutionTime && opts.elapsedTimeMs > 0) {
|
|
230
|
+
lines.push(`\nExecution time: ${formatNumberWithPrecision(opts.elapsedTimeMs, 2)} ms`);
|
|
231
|
+
if (result.iterations > 0) {
|
|
232
|
+
const timePerIteration = opts.elapsedTimeMs / result.iterations;
|
|
233
|
+
lines.push(`Time per iteration: ${formatNumberWithPrecision(timePerIteration, 3)} ms`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return lines.join('\n');
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Formats an AdjointGradientDescentResult.
|
|
240
|
+
*/
|
|
241
|
+
export function formatAdjointGradientDescentResult(result, options) {
|
|
242
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
243
|
+
const lines = [];
|
|
244
|
+
if (opts.showSectionHeaders) {
|
|
245
|
+
lines.push('\n=== Optimization Results ===');
|
|
246
|
+
}
|
|
247
|
+
// Parameters
|
|
248
|
+
lines.push('Optimized parameters:');
|
|
249
|
+
const paramStr = formatParameters(result.parameters, opts);
|
|
250
|
+
lines.push(` ${paramStr}`);
|
|
251
|
+
// States
|
|
252
|
+
lines.push('Optimized states:');
|
|
253
|
+
const stateStr = formatStates(result.finalStates, opts);
|
|
254
|
+
lines.push(` ${stateStr}`);
|
|
255
|
+
// Cost
|
|
256
|
+
lines.push('\nCost:');
|
|
257
|
+
lines.push(` f(p, x) = ${formatNumberWithPrecision(result.finalCost, opts.costPrecision)}`);
|
|
258
|
+
// Constraint satisfaction
|
|
259
|
+
lines.push('\nConstraint satisfaction:');
|
|
260
|
+
if (result.finalConstraintNorm !== undefined) {
|
|
261
|
+
lines.push(` ||c(p, x)|| = ${formatNumberWithPrecision(result.finalConstraintNorm, opts.constraintPrecision)}`);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
lines.push(` ||c(p, x)|| = N/A`);
|
|
265
|
+
}
|
|
266
|
+
// Convergence information
|
|
267
|
+
lines.push('\nConvergence:');
|
|
268
|
+
lines.push(` Converged: ${result.converged}`);
|
|
269
|
+
lines.push(` Iterations: ${result.iterations}`);
|
|
270
|
+
if (result.finalGradientNorm !== undefined) {
|
|
271
|
+
lines.push(` Final gradient norm: ${formatNumberWithPrecision(result.finalGradientNorm, opts.costPrecision)}`);
|
|
272
|
+
}
|
|
273
|
+
lines.push(` Used line search: ${result.usedLineSearch}`);
|
|
274
|
+
// Execution time
|
|
275
|
+
if (opts.showExecutionTime && opts.elapsedTimeMs > 0) {
|
|
276
|
+
lines.push(`\nExecution time: ${formatNumberWithPrecision(opts.elapsedTimeMs, 2)} ms`);
|
|
277
|
+
if (result.iterations > 0) {
|
|
278
|
+
const timePerIteration = opts.elapsedTimeMs / result.iterations;
|
|
279
|
+
lines.push(`Time per iteration: ${formatNumberWithPrecision(timePerIteration, 3)} ms`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return lines.join('\n');
|
|
283
|
+
}
|
|
284
|
+
export function formatResult(result, options) {
|
|
285
|
+
// Type guards to determine which formatter to use
|
|
286
|
+
if ('finalStates' in result && 'finalLambda' in result) {
|
|
287
|
+
return formatConstrainedLevenbergMarquardtResult(result, options);
|
|
288
|
+
}
|
|
289
|
+
if ('finalStates' in result && 'finalConstraintNorm' in result) {
|
|
290
|
+
if ('usedLineSearch' in result) {
|
|
291
|
+
return formatAdjointGradientDescentResult(result, options);
|
|
292
|
+
}
|
|
293
|
+
return formatConstrainedGaussNewtonResult(result, options);
|
|
294
|
+
}
|
|
295
|
+
if ('finalLambda' in result) {
|
|
296
|
+
return formatLevenbergMarquardtResult(result, options);
|
|
297
|
+
}
|
|
298
|
+
if ('usedLineSearch' in result) {
|
|
299
|
+
return formatGradientDescentResult(result, options);
|
|
300
|
+
}
|
|
301
|
+
return formatOptimizationResult(result, options);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Prints an optimization result directly to console.
|
|
305
|
+
*/
|
|
306
|
+
export function printOptimizationResult(result, options) {
|
|
307
|
+
console.log(formatOptimizationResult(result, options));
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Prints a gradient descent result directly to console.
|
|
311
|
+
*/
|
|
312
|
+
export function printGradientDescentResult(result, options) {
|
|
313
|
+
console.log(formatGradientDescentResult(result, options));
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Prints a Levenberg-Marquardt result directly to console.
|
|
317
|
+
*/
|
|
318
|
+
export function printLevenbergMarquardtResult(result, options) {
|
|
319
|
+
console.log(formatLevenbergMarquardtResult(result, options));
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Prints a constrained Gauss-Newton result directly to console.
|
|
323
|
+
*/
|
|
324
|
+
export function printConstrainedGaussNewtonResult(result, options) {
|
|
325
|
+
console.log(formatConstrainedGaussNewtonResult(result, options));
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Prints a constrained Levenberg-Marquardt result directly to console.
|
|
329
|
+
*/
|
|
330
|
+
export function printConstrainedLevenbergMarquardtResult(result, options) {
|
|
331
|
+
console.log(formatConstrainedLevenbergMarquardtResult(result, options));
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Prints an adjoint gradient descent result directly to console.
|
|
335
|
+
*/
|
|
336
|
+
export function printAdjointGradientDescentResult(result, options) {
|
|
337
|
+
console.log(formatAdjointGradientDescentResult(result, options));
|
|
338
|
+
}
|
|
339
|
+
export function printResult(result, options) {
|
|
340
|
+
console.log(formatResult(result, options));
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=resultFormatter.js.map
|