@pawells/math-extended 1.1.1 → 2.0.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/build/angles.d.ts +28 -18
- package/build/angles.d.ts.map +1 -1
- package/build/angles.js +28 -18
- package/build/angles.js.map +1 -1
- package/build/clamp.d.ts +5 -3
- package/build/clamp.d.ts.map +1 -1
- package/build/clamp.js +5 -3
- package/build/clamp.js.map +1 -1
- package/build/interpolation.d.ts +158 -96
- package/build/interpolation.d.ts.map +1 -1
- package/build/interpolation.js +158 -96
- package/build/interpolation.js.map +1 -1
- package/build/matrices/_exports.d.ts +13 -0
- package/build/matrices/_exports.d.ts.map +1 -0
- package/build/matrices/_exports.js +13 -0
- package/build/matrices/_exports.js.map +1 -0
- package/build/matrices/arithmetic.d.ts +170 -181
- package/build/matrices/arithmetic.d.ts.map +1 -1
- package/build/matrices/arithmetic.js +192 -202
- package/build/matrices/arithmetic.js.map +1 -1
- package/build/matrices/asserts.d.ts +240 -109
- package/build/matrices/asserts.d.ts.map +1 -1
- package/build/matrices/asserts.js +287 -87
- package/build/matrices/asserts.js.map +1 -1
- package/build/matrices/core.d.ts +29 -29
- package/build/matrices/core.js +1 -1
- package/build/matrices/decompositions.d.ts +102 -104
- package/build/matrices/decompositions.d.ts.map +1 -1
- package/build/matrices/decompositions.js +87 -89
- package/build/matrices/decompositions.js.map +1 -1
- package/build/matrices/index.d.ts +3 -2
- package/build/matrices/index.d.ts.map +1 -1
- package/build/matrices/index.js +5 -2
- package/build/matrices/index.js.map +1 -1
- package/build/matrices/linear-algebra.d.ts +12 -12
- package/build/matrices/linear-algebra.js +4 -4
- package/build/matrices/normalization.d.ts +8 -8
- package/build/matrices/transformations.d.ts +184 -165
- package/build/matrices/transformations.d.ts.map +1 -1
- package/build/matrices/transformations.js +99 -80
- package/build/matrices/transformations.js.map +1 -1
- package/build/matrices/types.d.ts +12 -12
- package/build/matrices/types.d.ts.map +1 -1
- package/build/quaternions/_exports.d.ts +11 -0
- package/build/quaternions/_exports.d.ts.map +1 -0
- package/build/quaternions/_exports.js +11 -0
- package/build/quaternions/_exports.js.map +1 -0
- package/build/quaternions/asserts.d.ts +115 -7
- package/build/quaternions/asserts.d.ts.map +1 -1
- package/build/quaternions/asserts.js +162 -8
- package/build/quaternions/asserts.js.map +1 -1
- package/build/quaternions/conversions.d.ts +31 -21
- package/build/quaternions/conversions.d.ts.map +1 -1
- package/build/quaternions/conversions.js +28 -18
- package/build/quaternions/conversions.js.map +1 -1
- package/build/quaternions/core.d.ts +67 -39
- package/build/quaternions/core.d.ts.map +1 -1
- package/build/quaternions/core.js +67 -39
- package/build/quaternions/core.js.map +1 -1
- package/build/quaternions/index.d.ts +3 -2
- package/build/quaternions/index.d.ts.map +1 -1
- package/build/quaternions/index.js +5 -2
- package/build/quaternions/index.js.map +1 -1
- package/build/quaternions/interpolation.d.ts +15 -9
- package/build/quaternions/interpolation.d.ts.map +1 -1
- package/build/quaternions/interpolation.js +15 -9
- package/build/quaternions/interpolation.js.map +1 -1
- package/build/quaternions/predefined.d.ts +9 -3
- package/build/quaternions/predefined.d.ts.map +1 -1
- package/build/quaternions/predefined.js +9 -3
- package/build/quaternions/predefined.js.map +1 -1
- package/build/quaternions/types.d.ts +3 -3
- package/build/vectors/_exports.d.ts +10 -0
- package/build/vectors/_exports.d.ts.map +1 -0
- package/build/vectors/_exports.js +10 -0
- package/build/vectors/_exports.js.map +1 -0
- package/build/vectors/asserts.d.ts +153 -49
- package/build/vectors/asserts.d.ts.map +1 -1
- package/build/vectors/asserts.js +202 -52
- package/build/vectors/asserts.js.map +1 -1
- package/build/vectors/core.d.ts +216 -137
- package/build/vectors/core.d.ts.map +1 -1
- package/build/vectors/core.js +212 -149
- package/build/vectors/core.js.map +1 -1
- package/build/vectors/index.d.ts +1 -0
- package/build/vectors/index.d.ts.map +1 -1
- package/build/vectors/index.js +3 -0
- package/build/vectors/index.js.map +1 -1
- package/build/vectors/interpolation.d.ts +39 -27
- package/build/vectors/interpolation.d.ts.map +1 -1
- package/build/vectors/interpolation.js +39 -27
- package/build/vectors/interpolation.js.map +1 -1
- package/build/vectors/predefined.d.ts +48 -24
- package/build/vectors/predefined.d.ts.map +1 -1
- package/build/vectors/predefined.js +38 -18
- package/build/vectors/predefined.js.map +1 -1
- package/package.json +3 -3
package/build/matrices/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { AssertMatrix, AssertMatrixRow, AssertMatrixValue } from './asserts.js';
|
|
|
4
4
|
* Creates a matrix with specified dimensions, initialized with zeros.
|
|
5
5
|
* @param rows - Number of rows (non-negative integer). If only this parameter is provided, creates a square matrix.
|
|
6
6
|
* @param cols - Number of columns (non-negative integer). Optional for square matrices.
|
|
7
|
-
* @returns {
|
|
7
|
+
* @returns {TMatrix | TMatrix1 | TMatrix2 | TMatrix3 | TMatrix4} Zero-filled matrix with specified dimensions
|
|
8
8
|
* @throws {Error} If rows or cols are negative or not integers
|
|
9
9
|
* @example MatrixCreate() // [[0]] (1x1 matrix)
|
|
10
10
|
* @example MatrixCreate(2) // [[0, 0], [0, 0]] (2x2 matrix)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TMatrix } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Result of eigenvalue decomposition containing eigenvalues and their corresponding eigenvectors.
|
|
4
4
|
*
|
|
@@ -10,7 +10,7 @@ type TEigenDecompositionResult = {
|
|
|
10
10
|
/** Array of eigenvalues λ in descending order */
|
|
11
11
|
eigenvalues: number[];
|
|
12
12
|
/** Matrix where each column is an eigenvector corresponding to the eigenvalue at the same index */
|
|
13
|
-
eigenvectors:
|
|
13
|
+
eigenvectors: TMatrix;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
16
|
* Result of LU decomposition with partial pivoting where P × A = L × U.
|
|
@@ -22,9 +22,9 @@ type TEigenDecompositionResult = {
|
|
|
22
22
|
*/
|
|
23
23
|
type TLUDecompositionResult = {
|
|
24
24
|
/** Lower triangular matrix with 1's on the diagonal */
|
|
25
|
-
readonly L:
|
|
25
|
+
readonly L: TMatrix;
|
|
26
26
|
/** Upper triangular matrix containing the pivots */
|
|
27
|
-
readonly U:
|
|
27
|
+
readonly U: TMatrix;
|
|
28
28
|
/** Permutation vector: P[i] is the original row index that now occupies row i */
|
|
29
29
|
readonly P: number[];
|
|
30
30
|
};
|
|
@@ -37,9 +37,9 @@ type TLUDecompositionResult = {
|
|
|
37
37
|
*/
|
|
38
38
|
type TQRDecompositionResult = {
|
|
39
39
|
/** Orthogonal matrix where Q^T × Q = I */
|
|
40
|
-
readonly Q:
|
|
40
|
+
readonly Q: TMatrix;
|
|
41
41
|
/** Upper triangular matrix */
|
|
42
|
-
readonly R:
|
|
42
|
+
readonly R: TMatrix;
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
45
45
|
* Result of Singular Value Decomposition (SVD) where A = U × Σ × V^T.
|
|
@@ -51,11 +51,11 @@ type TQRDecompositionResult = {
|
|
|
51
51
|
*/
|
|
52
52
|
type TSVDDecompositionResult = {
|
|
53
53
|
/** Matrix of left singular vectors (orthogonal columns) */
|
|
54
|
-
readonly U:
|
|
54
|
+
readonly U: TMatrix;
|
|
55
55
|
/** Array of singular values σ in descending order (diagonal elements of Σ matrix) */
|
|
56
56
|
readonly S: number[];
|
|
57
57
|
/** Transpose of the matrix of right singular vectors (V^T where V has orthogonal columns) */
|
|
58
|
-
readonly VT:
|
|
58
|
+
readonly VT: TMatrix;
|
|
59
59
|
};
|
|
60
60
|
/**
|
|
61
61
|
* Performs Cholesky decomposition for symmetric positive definite matrices A = L × L^T.
|
|
@@ -77,22 +77,22 @@ type TSVDDecompositionResult = {
|
|
|
77
77
|
* @throws {Error} If matrix is not square, not symmetric, or not positive definite
|
|
78
78
|
*
|
|
79
79
|
* @example
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
* ```typescript
|
|
81
|
+
* ```ts
|
|
82
|
+
* // Symmetric positive definite matrix
|
|
83
|
+
* const A = [[4, 2], [2, 3]];
|
|
84
|
+
* const L = MatrixCholesky(A);
|
|
85
|
+
* // L = [[2, 0], [1, √2]] ≈ [[2, 0], [1, 1.414]]
|
|
86
|
+
* // Verify: L × L^T should equal A
|
|
87
|
+
* const LT = MatrixTranspose(L);
|
|
88
|
+
* const reconstructed = MatrixMultiply(L, LT);
|
|
89
|
+
* // reconstructed ≈ [[4, 2], [2, 3]]
|
|
90
|
+
* ```
|
|
91
|
+
* @complexity Time: O(n³/3), Space: O(n²) - About 2x faster than general LU decomposition
|
|
92
|
+
* @see {@link MatrixLU} For general square matrices that may not be positive definite
|
|
93
|
+
* ```
|
|
94
94
|
*/
|
|
95
|
-
export declare function MatrixCholesky(matrix:
|
|
95
|
+
export declare function MatrixCholesky(matrix: TMatrix): TMatrix;
|
|
96
96
|
/**
|
|
97
97
|
* Performs eigenvalue decomposition for square matrices to find A × v = λ × v.
|
|
98
98
|
*
|
|
@@ -117,24 +117,24 @@ export declare function MatrixCholesky(matrix: IMatrix): IMatrix;
|
|
|
117
117
|
* @throws {Error} If matrix is not square, contains invalid values, or has complex eigenvalues
|
|
118
118
|
*
|
|
119
119
|
* @example
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
120
|
+
* ```typescript
|
|
121
|
+
* ```ts
|
|
122
|
+
* // Simple 2x2 matrix
|
|
123
|
+
* const A = [[3, 1], [0, 2]];
|
|
124
|
+
* const { eigenvalues, eigenvectors } = MatrixEigen(A);
|
|
125
|
+
* // eigenvalues: [3, 2]
|
|
126
|
+
* // eigenvectors: matrix where each column corresponds to an eigenvalue
|
|
127
|
+
* // Verify eigenvalue equation: A × v = λ × v
|
|
128
|
+
* const v = Matrix_GetColumn(eigenvectors, 0); // First eigenvector
|
|
129
|
+
* const Av = MatrixMultiplyVector(A, v);
|
|
130
|
+
* const lambdaV = Matrix_ScaleVector(v, eigenvalues[0]);
|
|
131
|
+
* // Av should approximately equal lambdaV
|
|
132
|
+
* ```
|
|
133
|
+
* @complexity O(n³) time for an n×n matrix
|
|
134
|
+
* @see {@link MatrixEigenQRIteration} For the iterative algorithm used for larger matrices
|
|
135
|
+
* ```
|
|
136
136
|
*/
|
|
137
|
-
export declare function MatrixEigen(matrix:
|
|
137
|
+
export declare function MatrixEigen(matrix: TMatrix): TEigenDecompositionResult;
|
|
138
138
|
/**
|
|
139
139
|
* Simplified QR iteration algorithm for eigenvalue computation of larger matrices.
|
|
140
140
|
*
|
|
@@ -158,7 +158,7 @@ export declare function MatrixEigen(matrix: IMatrix): TEigenDecompositionResult;
|
|
|
158
158
|
* @complexity O(n³) per iteration, typically converges in O(n) iterations
|
|
159
159
|
* @see {@link MatrixQR} For the QR decomposition used in each iteration
|
|
160
160
|
*/
|
|
161
|
-
export declare function MatrixEigenQRIteration(matrix:
|
|
161
|
+
export declare function MatrixEigenQRIteration(matrix: TMatrix, iterations?: number): TEigenDecompositionResult;
|
|
162
162
|
/**
|
|
163
163
|
* Performs LU decomposition of a square matrix with partial pivoting: P × A = L × U.
|
|
164
164
|
*
|
|
@@ -183,21 +183,21 @@ export declare function MatrixEigenQRIteration(matrix: IMatrix, iterations?: num
|
|
|
183
183
|
* @throws {MatrixError} If matrix is not square, singular (zero pivot), or contains invalid values
|
|
184
184
|
*
|
|
185
185
|
* @example
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
186
|
+
* ```typescript
|
|
187
|
+
* ```ts
|
|
188
|
+
* const A = [[2, 1], [1, 1]];
|
|
189
|
+
* const { L, U, P } = MatrixLU(A);
|
|
190
|
+
* // L = [[1, 0], [0.5, 1]], U = [[2, 1], [0, 0.5]], P = [0, 1]
|
|
191
|
+
* // Verify: L × U should equal P-permuted A
|
|
192
|
+
* const product = MatrixMultiply(L, U);
|
|
193
|
+
* // product ≈ [[2, 1], [1, 1]]
|
|
194
|
+
* ```
|
|
195
|
+
* @complexity Time: O(n³/3), Space: O(n²)
|
|
196
|
+
* @see {@link MatrixCholesky} For symmetric positive definite matrices (more efficient)
|
|
197
|
+
* @see {@link MatrixSolve} For solving Ax = b directly
|
|
198
|
+
* ```
|
|
199
199
|
*/
|
|
200
|
-
export declare function MatrixLU(matrix:
|
|
200
|
+
export declare function MatrixLU(matrix: TMatrix): TLUDecompositionResult;
|
|
201
201
|
/**
|
|
202
202
|
* Performs QR decomposition A = Q × R using Modified Gram-Schmidt orthogonalization.
|
|
203
203
|
*
|
|
@@ -222,25 +222,24 @@ export declare function MatrixLU(matrix: IMatrix): TLUDecompositionResult;
|
|
|
222
222
|
* @throws {Error} If matrix has more columns than rows or columns are linearly dependent
|
|
223
223
|
*
|
|
224
224
|
* @example
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
* @see {@link MatrixGramSchmidt} {@link MatrixLU} {@link MatrixEigenQRIteration}
|
|
225
|
+
* ```typescript
|
|
226
|
+
* ```ts
|
|
227
|
+
* const A = [[1, 1], [1, 0], [0, 1]]; // 3×2 matrix
|
|
228
|
+
* const { Q, R } = MatrixQR(A);
|
|
229
|
+
* // Q: 3×2 orthogonal matrix with Q^T × Q = I₂
|
|
230
|
+
* // R: 2×2 upper triangular matrix
|
|
231
|
+
* // Verify: Q × R should equal A
|
|
232
|
+
* const reconstructed = MatrixMultiply(Q, R);
|
|
233
|
+
* // reconstructed ≈ A
|
|
234
|
+
* // Check orthogonality: Q^T × Q should be identity
|
|
235
|
+
* const QT = MatrixTranspose(Q);
|
|
236
|
+
* const identity = MatrixMultiply(QT, Q);
|
|
237
|
+
* ```
|
|
238
|
+
* @complexity Time: O(mn²), Space: O(mn) where m ≥ n
|
|
239
|
+
* @see {@link MatrixGramSchmidt} {@link MatrixLU} {@link MatrixEigenQRIteration}
|
|
240
|
+
* ```
|
|
242
241
|
*/
|
|
243
|
-
export declare function MatrixQR(matrix:
|
|
242
|
+
export declare function MatrixQR(matrix: TMatrix, allowDependentColumns?: boolean): TQRDecompositionResult;
|
|
244
243
|
/**
|
|
245
244
|
* Performs Singular Value Decomposition (SVD) of a matrix A = U × Σ × V^T.
|
|
246
245
|
*
|
|
@@ -274,28 +273,26 @@ export declare function MatrixQR(matrix: IMatrix, allowDependentColumns?: boolea
|
|
|
274
273
|
* @throws {Error} If matrix contains invalid values (NaN, Infinity)
|
|
275
274
|
*
|
|
276
275
|
* @example
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
* @complexity Time: O(min(m²n, mn²)), Space: O(m² + n²)
|
|
296
|
-
* @see {@link MatrixQR} {@link MatrixEigenQRIteration} {@link Matrix_PseudoInverse}
|
|
276
|
+
* ```typescript
|
|
277
|
+
* ```ts
|
|
278
|
+
* const A = [[1, 2], [3, 4], [5, 6]]; // 3×2 matrix
|
|
279
|
+
* const { U, S, VT } = MatrixSVD(A);
|
|
280
|
+
* // U: 3×2 matrix with orthonormal columns
|
|
281
|
+
* // S: [σ₁, σ₂] singular values in descending order
|
|
282
|
+
* // VT: 2×2 orthogonal matrix (V transposed)
|
|
283
|
+
* // Verify reconstruction: U × diag(S) × VT ≈ A
|
|
284
|
+
* const Sigma = Matrix_Diagonal(S);
|
|
285
|
+
* const reconstructed = MatrixMultiply(MatrixMultiply(U, Sigma), VT);
|
|
286
|
+
* // Matrix rank from singular values (count non-zero values)
|
|
287
|
+
* const rank = S.filter(s => s > 1e-10).length;
|
|
288
|
+
* // Condition number for stability analysis
|
|
289
|
+
* const conditionNumber = S[0] / S[S.length - 1];
|
|
290
|
+
* ```
|
|
291
|
+
* @complexity Time: O(min(m²n, mn²)), Space: O(m² + n²)
|
|
292
|
+
* @see {@link MatrixQR} {@link MatrixEigenQRIteration} {@link Matrix_PseudoInverse}
|
|
293
|
+
* ```
|
|
297
294
|
*/
|
|
298
|
-
export declare function MatrixSVD(matrix:
|
|
295
|
+
export declare function MatrixSVD(matrix: TMatrix): TSVDDecompositionResult;
|
|
299
296
|
/**
|
|
300
297
|
* Solves the linear system Ax = b for the unknown vector x.
|
|
301
298
|
* Uses LU decomposition with partial pivoting internally: decomposes A into P, L, U,
|
|
@@ -310,16 +307,17 @@ export declare function MatrixSVD(matrix: IMatrix): TSVDDecompositionResult;
|
|
|
310
307
|
* @throws {MatrixError} If A is not square, singular, or b has the wrong length
|
|
311
308
|
*
|
|
312
309
|
* @example
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
310
|
+
* ```typescript
|
|
311
|
+
* // 2x + y = 8
|
|
312
|
+
* // 5x + 3y = 20
|
|
313
|
+
* MatrixSolve([[2, 1], [5, 3]], [8, 20]); // [4, 0]
|
|
314
|
+
* @example
|
|
315
|
+
* // Solve a 3×3 system
|
|
316
|
+
* const A = [[1, 2, -1], [2, 1, 1], [3, -1, 2]];
|
|
317
|
+
* const b = [4, 7, 2];
|
|
318
|
+
* MatrixSolve(A, b); // solution vector x
|
|
319
|
+
* ```
|
|
322
320
|
*/
|
|
323
|
-
export declare function MatrixSolve(a:
|
|
321
|
+
export declare function MatrixSolve(a: TMatrix, b: number[]): number[];
|
|
324
322
|
export {};
|
|
325
323
|
//# sourceMappingURL=decompositions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decompositions.d.ts","sourceRoot":"","sources":["../../src/matrices/decompositions.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAKrC;;;;;;GAMG;AACH,KAAK,yBAAyB,GAAG;IAChC,iDAAiD;IACjD,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,mGAAmG;IACnG,YAAY,EAAE,OAAO,CAAC;CACtB,CAAC;AACF;;;;;;;GAOG;AACH,KAAK,sBAAsB,GAAG;IAC7B,uDAAuD;IAEvD,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IACpB,oDAAoD;IAEpD,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IACpB,iFAAiF;IAEjF,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC;AACF;;;;;;GAMG;AACH,KAAK,sBAAsB,GAAG;IAC7B,0CAA0C;IAE1C,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IACpB,8BAA8B;IAE9B,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AACF;;;;;;;GAOG;AACH,KAAK,uBAAuB,GAAG;IAC9B,2DAA2D;IAE3D,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC;IACpB,qFAAqF;IAErF,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,6FAA6F;IAE7F,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;G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
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
* ```typescript
|
|
29
|
+
* ```ts
|
|
30
|
+
* // Symmetric positive definite matrix
|
|
31
|
+
* const A = [[4, 2], [2, 3]];
|
|
32
|
+
* const L = MatrixCholesky(A);
|
|
33
|
+
* // L = [[2, 0], [1, √2]] ≈ [[2, 0], [1, 1.414]]
|
|
34
|
+
* // Verify: L × L^T should equal A
|
|
35
|
+
* const LT = MatrixTranspose(L);
|
|
36
|
+
* const reconstructed = MatrixMultiply(L, LT);
|
|
37
|
+
* // reconstructed ≈ [[4, 2], [2, 3]]
|
|
38
|
+
* ```
|
|
39
|
+
* @complexity Time: O(n³/3), Space: O(n²) - About 2x faster than general LU decomposition
|
|
40
|
+
* @see {@link MatrixLU} For general square matrices that may not be positive definite
|
|
41
|
+
* ```
|
|
42
42
|
*/
|
|
43
43
|
export function MatrixCholesky(matrix) {
|
|
44
44
|
AssertMatrix(matrix, { square: true });
|
|
@@ -120,22 +120,22 @@ export function MatrixCholesky(matrix) {
|
|
|
120
120
|
* @throws {Error} If matrix is not square, contains invalid values, or has complex eigenvalues
|
|
121
121
|
*
|
|
122
122
|
* @example
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
123
|
+
* ```typescript
|
|
124
|
+
* ```ts
|
|
125
|
+
* // Simple 2x2 matrix
|
|
126
|
+
* const A = [[3, 1], [0, 2]];
|
|
127
|
+
* const { eigenvalues, eigenvectors } = MatrixEigen(A);
|
|
128
|
+
* // eigenvalues: [3, 2]
|
|
129
|
+
* // eigenvectors: matrix where each column corresponds to an eigenvalue
|
|
130
|
+
* // Verify eigenvalue equation: A × v = λ × v
|
|
131
|
+
* const v = Matrix_GetColumn(eigenvectors, 0); // First eigenvector
|
|
132
|
+
* const Av = MatrixMultiplyVector(A, v);
|
|
133
|
+
* const lambdaV = Matrix_ScaleVector(v, eigenvalues[0]);
|
|
134
|
+
* // Av should approximately equal lambdaV
|
|
135
|
+
* ```
|
|
136
|
+
* @complexity O(n³) time for an n×n matrix
|
|
137
|
+
* @see {@link MatrixEigenQRIteration} For the iterative algorithm used for larger matrices
|
|
138
|
+
* ```
|
|
139
139
|
*/
|
|
140
140
|
export function MatrixEigen(matrix) {
|
|
141
141
|
AssertMatrix(matrix, { square: true });
|
|
@@ -338,19 +338,19 @@ export function MatrixEigenQRIteration(matrix, iterations = 50) {
|
|
|
338
338
|
* @throws {MatrixError} If matrix is not square, singular (zero pivot), or contains invalid values
|
|
339
339
|
*
|
|
340
340
|
* @example
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
341
|
+
* ```typescript
|
|
342
|
+
* ```ts
|
|
343
|
+
* const A = [[2, 1], [1, 1]];
|
|
344
|
+
* const { L, U, P } = MatrixLU(A);
|
|
345
|
+
* // L = [[1, 0], [0.5, 1]], U = [[2, 1], [0, 0.5]], P = [0, 1]
|
|
346
|
+
* // Verify: L × U should equal P-permuted A
|
|
347
|
+
* const product = MatrixMultiply(L, U);
|
|
348
|
+
* // product ≈ [[2, 1], [1, 1]]
|
|
349
|
+
* ```
|
|
350
|
+
* @complexity Time: O(n³/3), Space: O(n²)
|
|
351
|
+
* @see {@link MatrixCholesky} For symmetric positive definite matrices (more efficient)
|
|
352
|
+
* @see {@link MatrixSolve} For solving Ax = b directly
|
|
353
|
+
* ```
|
|
354
354
|
*/
|
|
355
355
|
export function MatrixLU(matrix) {
|
|
356
356
|
AssertMatrix(matrix, { square: true });
|
|
@@ -438,23 +438,22 @@ export function MatrixLU(matrix) {
|
|
|
438
438
|
* @throws {Error} If matrix has more columns than rows or columns are linearly dependent
|
|
439
439
|
*
|
|
440
440
|
* @example
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
* @see {@link MatrixGramSchmidt} {@link MatrixLU} {@link MatrixEigenQRIteration}
|
|
441
|
+
* ```typescript
|
|
442
|
+
* ```ts
|
|
443
|
+
* const A = [[1, 1], [1, 0], [0, 1]]; // 3×2 matrix
|
|
444
|
+
* const { Q, R } = MatrixQR(A);
|
|
445
|
+
* // Q: 3×2 orthogonal matrix with Q^T × Q = I₂
|
|
446
|
+
* // R: 2×2 upper triangular matrix
|
|
447
|
+
* // Verify: Q × R should equal A
|
|
448
|
+
* const reconstructed = MatrixMultiply(Q, R);
|
|
449
|
+
* // reconstructed ≈ A
|
|
450
|
+
* // Check orthogonality: Q^T × Q should be identity
|
|
451
|
+
* const QT = MatrixTranspose(Q);
|
|
452
|
+
* const identity = MatrixMultiply(QT, Q);
|
|
453
|
+
* ```
|
|
454
|
+
* @complexity Time: O(mn²), Space: O(mn) where m ≥ n
|
|
455
|
+
* @see {@link MatrixGramSchmidt} {@link MatrixLU} {@link MatrixEigenQRIteration}
|
|
456
|
+
* ```
|
|
458
457
|
*/
|
|
459
458
|
export function MatrixQR(matrix, allowDependentColumns = false) {
|
|
460
459
|
AssertMatrix(matrix);
|
|
@@ -602,26 +601,24 @@ export function MatrixQR(matrix, allowDependentColumns = false) {
|
|
|
602
601
|
* @throws {Error} If matrix contains invalid values (NaN, Infinity)
|
|
603
602
|
*
|
|
604
603
|
* @example
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
* @complexity Time: O(min(m²n, mn²)), Space: O(m² + n²)
|
|
624
|
-
* @see {@link MatrixQR} {@link MatrixEigenQRIteration} {@link Matrix_PseudoInverse}
|
|
604
|
+
* ```typescript
|
|
605
|
+
* ```ts
|
|
606
|
+
* const A = [[1, 2], [3, 4], [5, 6]]; // 3×2 matrix
|
|
607
|
+
* const { U, S, VT } = MatrixSVD(A);
|
|
608
|
+
* // U: 3×2 matrix with orthonormal columns
|
|
609
|
+
* // S: [σ₁, σ₂] singular values in descending order
|
|
610
|
+
* // VT: 2×2 orthogonal matrix (V transposed)
|
|
611
|
+
* // Verify reconstruction: U × diag(S) × VT ≈ A
|
|
612
|
+
* const Sigma = Matrix_Diagonal(S);
|
|
613
|
+
* const reconstructed = MatrixMultiply(MatrixMultiply(U, Sigma), VT);
|
|
614
|
+
* // Matrix rank from singular values (count non-zero values)
|
|
615
|
+
* const rank = S.filter(s => s > 1e-10).length;
|
|
616
|
+
* // Condition number for stability analysis
|
|
617
|
+
* const conditionNumber = S[0] / S[S.length - 1];
|
|
618
|
+
* ```
|
|
619
|
+
* @complexity Time: O(min(m²n, mn²)), Space: O(m² + n²)
|
|
620
|
+
* @see {@link MatrixQR} {@link MatrixEigenQRIteration} {@link Matrix_PseudoInverse}
|
|
621
|
+
* ```
|
|
625
622
|
*/
|
|
626
623
|
export function MatrixSVD(matrix) {
|
|
627
624
|
AssertMatrix(matrix);
|
|
@@ -701,15 +698,16 @@ export function MatrixSVD(matrix) {
|
|
|
701
698
|
* @throws {MatrixError} If A is not square, singular, or b has the wrong length
|
|
702
699
|
*
|
|
703
700
|
* @example
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
701
|
+
* ```typescript
|
|
702
|
+
* // 2x + y = 8
|
|
703
|
+
* // 5x + 3y = 20
|
|
704
|
+
* MatrixSolve([[2, 1], [5, 3]], [8, 20]); // [4, 0]
|
|
705
|
+
* @example
|
|
706
|
+
* // Solve a 3×3 system
|
|
707
|
+
* const A = [[1, 2, -1], [2, 1, 1], [3, -1, 2]];
|
|
708
|
+
* const b = [4, 7, 2];
|
|
709
|
+
* MatrixSolve(A, b); // solution vector x
|
|
710
|
+
* ```
|
|
713
711
|
*/
|
|
714
712
|
export function MatrixSolve(a, b) {
|
|
715
713
|
AssertMatrix(a, { square: true });
|