deepbox 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/LICENSE +21 -0
- package/README.md +344 -0
- package/dist/CSRMatrix-CwGwQRea.d.cts +219 -0
- package/dist/CSRMatrix-KzNt6QpS.d.ts +219 -0
- package/dist/Tensor-BQLk1ltW.d.cts +147 -0
- package/dist/Tensor-g8mUClel.d.ts +147 -0
- package/dist/chunk-4S73VUBD.js +677 -0
- package/dist/chunk-4S73VUBD.js.map +1 -0
- package/dist/chunk-5R4S63PF.js +2925 -0
- package/dist/chunk-5R4S63PF.js.map +1 -0
- package/dist/chunk-6AE5FKKQ.cjs +9264 -0
- package/dist/chunk-6AE5FKKQ.cjs.map +1 -0
- package/dist/chunk-AD436M45.js +3854 -0
- package/dist/chunk-AD436M45.js.map +1 -0
- package/dist/chunk-ALS7ETWZ.cjs +4263 -0
- package/dist/chunk-ALS7ETWZ.cjs.map +1 -0
- package/dist/chunk-AU7XHGKJ.js +2092 -0
- package/dist/chunk-AU7XHGKJ.js.map +1 -0
- package/dist/chunk-B5TNKUEY.js +1481 -0
- package/dist/chunk-B5TNKUEY.js.map +1 -0
- package/dist/chunk-BCR7G3A6.js +9136 -0
- package/dist/chunk-BCR7G3A6.js.map +1 -0
- package/dist/chunk-C4PKXY74.cjs +1917 -0
- package/dist/chunk-C4PKXY74.cjs.map +1 -0
- package/dist/chunk-DWZY6PIP.cjs +6400 -0
- package/dist/chunk-DWZY6PIP.cjs.map +1 -0
- package/dist/chunk-E3EU5FZO.cjs +2113 -0
- package/dist/chunk-E3EU5FZO.cjs.map +1 -0
- package/dist/chunk-F3JWBINJ.js +1054 -0
- package/dist/chunk-F3JWBINJ.js.map +1 -0
- package/dist/chunk-FJYLIGJX.js +1940 -0
- package/dist/chunk-FJYLIGJX.js.map +1 -0
- package/dist/chunk-JSCDE774.cjs +729 -0
- package/dist/chunk-JSCDE774.cjs.map +1 -0
- package/dist/chunk-LWECRCW2.cjs +2412 -0
- package/dist/chunk-LWECRCW2.cjs.map +1 -0
- package/dist/chunk-MLBMYKCG.js +6379 -0
- package/dist/chunk-MLBMYKCG.js.map +1 -0
- package/dist/chunk-OX6QXFMV.cjs +3874 -0
- package/dist/chunk-OX6QXFMV.cjs.map +1 -0
- package/dist/chunk-PHV2DKRS.cjs +1072 -0
- package/dist/chunk-PHV2DKRS.cjs.map +1 -0
- package/dist/chunk-PL7TAYKI.js +4056 -0
- package/dist/chunk-PL7TAYKI.js.map +1 -0
- package/dist/chunk-PR647I7R.js +1898 -0
- package/dist/chunk-PR647I7R.js.map +1 -0
- package/dist/chunk-QERHVCHC.cjs +2960 -0
- package/dist/chunk-QERHVCHC.cjs.map +1 -0
- package/dist/chunk-XEG44RF6.cjs +1514 -0
- package/dist/chunk-XEG44RF6.cjs.map +1 -0
- package/dist/chunk-XMWVME2W.js +2377 -0
- package/dist/chunk-XMWVME2W.js.map +1 -0
- package/dist/chunk-ZB75FESB.cjs +1979 -0
- package/dist/chunk-ZB75FESB.cjs.map +1 -0
- package/dist/chunk-ZLW62TJG.cjs +4061 -0
- package/dist/chunk-ZLW62TJG.cjs.map +1 -0
- package/dist/chunk-ZXKBDFP3.js +4235 -0
- package/dist/chunk-ZXKBDFP3.js.map +1 -0
- package/dist/core/index.cjs +204 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +2 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -0
- package/dist/dataframe/index.cjs +22 -0
- package/dist/dataframe/index.cjs.map +1 -0
- package/dist/dataframe/index.d.cts +3 -0
- package/dist/dataframe/index.d.ts +3 -0
- package/dist/dataframe/index.js +5 -0
- package/dist/dataframe/index.js.map +1 -0
- package/dist/datasets/index.cjs +134 -0
- package/dist/datasets/index.cjs.map +1 -0
- package/dist/datasets/index.d.cts +3 -0
- package/dist/datasets/index.d.ts +3 -0
- package/dist/datasets/index.js +5 -0
- package/dist/datasets/index.js.map +1 -0
- package/dist/index-74AB8Cyh.d.cts +1126 -0
- package/dist/index-9oQx1HgV.d.cts +1180 -0
- package/dist/index-BJY2SI4i.d.ts +483 -0
- package/dist/index-BWGhrDlr.d.ts +733 -0
- package/dist/index-B_DK4FKY.d.cts +242 -0
- package/dist/index-BbA2Gxfl.d.ts +456 -0
- package/dist/index-BgHYAoSS.d.cts +837 -0
- package/dist/index-BndMbqsM.d.ts +1439 -0
- package/dist/index-C1mfVYoo.d.ts +2517 -0
- package/dist/index-CCvlwAmL.d.cts +809 -0
- package/dist/index-CDw5CnOU.d.ts +785 -0
- package/dist/index-Cn3SdB0O.d.ts +1126 -0
- package/dist/index-CrqLlS-a.d.ts +776 -0
- package/dist/index-D61yaSMY.d.cts +483 -0
- package/dist/index-D9Loo1_A.d.cts +2517 -0
- package/dist/index-DIT_OO9C.d.cts +785 -0
- package/dist/index-DIp_RrRt.d.ts +242 -0
- package/dist/index-DbultU6X.d.cts +1427 -0
- package/dist/index-DmEg_LCm.d.cts +776 -0
- package/dist/index-DoPWVxPo.d.cts +1439 -0
- package/dist/index-DuCxd-8d.d.ts +837 -0
- package/dist/index-Dx42TZaY.d.ts +809 -0
- package/dist/index-DyZ4QQf5.d.cts +456 -0
- package/dist/index-GFAVyOWO.d.ts +1427 -0
- package/dist/index-WHQLn0e8.d.cts +733 -0
- package/dist/index-ZtI1Iy4L.d.ts +1180 -0
- package/dist/index-eJgeni9c.d.cts +1911 -0
- package/dist/index-tk4lSYod.d.ts +1911 -0
- package/dist/index.cjs +72 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/linalg/index.cjs +86 -0
- package/dist/linalg/index.cjs.map +1 -0
- package/dist/linalg/index.d.cts +3 -0
- package/dist/linalg/index.d.ts +3 -0
- package/dist/linalg/index.js +5 -0
- package/dist/linalg/index.js.map +1 -0
- package/dist/metrics/index.cjs +158 -0
- package/dist/metrics/index.cjs.map +1 -0
- package/dist/metrics/index.d.cts +3 -0
- package/dist/metrics/index.d.ts +3 -0
- package/dist/metrics/index.js +5 -0
- package/dist/metrics/index.js.map +1 -0
- package/dist/ml/index.cjs +87 -0
- package/dist/ml/index.cjs.map +1 -0
- package/dist/ml/index.d.cts +3 -0
- package/dist/ml/index.d.ts +3 -0
- package/dist/ml/index.js +6 -0
- package/dist/ml/index.js.map +1 -0
- package/dist/ndarray/index.cjs +501 -0
- package/dist/ndarray/index.cjs.map +1 -0
- package/dist/ndarray/index.d.cts +5 -0
- package/dist/ndarray/index.d.ts +5 -0
- package/dist/ndarray/index.js +4 -0
- package/dist/ndarray/index.js.map +1 -0
- package/dist/nn/index.cjs +142 -0
- package/dist/nn/index.cjs.map +1 -0
- package/dist/nn/index.d.cts +6 -0
- package/dist/nn/index.d.ts +6 -0
- package/dist/nn/index.js +5 -0
- package/dist/nn/index.js.map +1 -0
- package/dist/optim/index.cjs +77 -0
- package/dist/optim/index.cjs.map +1 -0
- package/dist/optim/index.d.cts +4 -0
- package/dist/optim/index.d.ts +4 -0
- package/dist/optim/index.js +4 -0
- package/dist/optim/index.js.map +1 -0
- package/dist/plot/index.cjs +114 -0
- package/dist/plot/index.cjs.map +1 -0
- package/dist/plot/index.d.cts +6 -0
- package/dist/plot/index.d.ts +6 -0
- package/dist/plot/index.js +5 -0
- package/dist/plot/index.js.map +1 -0
- package/dist/preprocess/index.cjs +82 -0
- package/dist/preprocess/index.cjs.map +1 -0
- package/dist/preprocess/index.d.cts +4 -0
- package/dist/preprocess/index.d.ts +4 -0
- package/dist/preprocess/index.js +5 -0
- package/dist/preprocess/index.js.map +1 -0
- package/dist/random/index.cjs +74 -0
- package/dist/random/index.cjs.map +1 -0
- package/dist/random/index.d.cts +3 -0
- package/dist/random/index.d.ts +3 -0
- package/dist/random/index.js +5 -0
- package/dist/random/index.js.map +1 -0
- package/dist/stats/index.cjs +142 -0
- package/dist/stats/index.cjs.map +1 -0
- package/dist/stats/index.d.cts +3 -0
- package/dist/stats/index.d.ts +3 -0
- package/dist/stats/index.js +5 -0
- package/dist/stats/index.js.map +1 -0
- package/dist/tensor-B96jjJLQ.d.cts +205 -0
- package/dist/tensor-B96jjJLQ.d.ts +205 -0
- package/package.json +226 -0
|
@@ -0,0 +1,809 @@
|
|
|
1
|
+
import { T as Tensor } from './Tensor-g8mUClel.js';
|
|
2
|
+
import { A as Axis } from './tensor-B96jjJLQ.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Cholesky decomposition.
|
|
6
|
+
*
|
|
7
|
+
* Factorizes symmetric positive definite matrix A into L * L^T where L is lower triangular.
|
|
8
|
+
*
|
|
9
|
+
* **Algorithm**: Cholesky-Banachiewicz algorithm
|
|
10
|
+
* **Time Complexity**: O(N³/3) - approximately half the cost of LU decomposition
|
|
11
|
+
* **Space Complexity**: O(N²) for output matrix
|
|
12
|
+
*
|
|
13
|
+
* **Mathematical Background**:
|
|
14
|
+
* For a symmetric positive definite matrix A, the Cholesky decomposition is:
|
|
15
|
+
* A = L * L^T
|
|
16
|
+
* where L is lower triangular with positive diagonal elements.
|
|
17
|
+
*
|
|
18
|
+
* **Numerical Stability**:
|
|
19
|
+
* - Only works for positive definite matrices (all eigenvalues > 0)
|
|
20
|
+
* - More stable than LU for this class of matrices
|
|
21
|
+
* - No pivoting required due to positive definiteness
|
|
22
|
+
*
|
|
23
|
+
* **Parameters**:
|
|
24
|
+
* @param a - Symmetric positive definite matrix of shape (N, N)
|
|
25
|
+
*
|
|
26
|
+
* **Returns**: L - Lower triangular matrix where A = L * L^T
|
|
27
|
+
*
|
|
28
|
+
* **Requirements**:
|
|
29
|
+
* - Input must be 2D square matrix
|
|
30
|
+
* - Matrix must be symmetric: A = A^T
|
|
31
|
+
* - Matrix must be positive definite: x^T * A * x > 0 for all x ≠ 0
|
|
32
|
+
*
|
|
33
|
+
* **Properties**:
|
|
34
|
+
* - A = L @ L^T
|
|
35
|
+
* - L is lower triangular (L[i,j] = 0 for j > i)
|
|
36
|
+
* - L has positive diagonal elements
|
|
37
|
+
* - Much faster than LU decomposition for positive definite matrices
|
|
38
|
+
* - Determinant: det(A) = (product of L[i,i])²
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { cholesky } from 'deepbox/linalg';
|
|
43
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
44
|
+
*
|
|
45
|
+
* // Positive definite matrix
|
|
46
|
+
* const A = tensor([[4, 12, -16], [12, 37, -43], [-16, -43, 98]]);
|
|
47
|
+
* const L = cholesky(A);
|
|
48
|
+
*
|
|
49
|
+
* // Verify: A ≈ L @ L^T
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @throws {ShapeError} If input is not 2D square matrix
|
|
53
|
+
* @throws {DTypeError} If input has string dtype
|
|
54
|
+
* @throws {DataValidationError} If matrix is not symmetric
|
|
55
|
+
* @throws {DataValidationError} If matrix is not positive definite
|
|
56
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
57
|
+
*
|
|
58
|
+
* @see {@link https://en.wikipedia.org/wiki/Cholesky_decomposition | Wikipedia: Cholesky decomposition}
|
|
59
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 4.2.1
|
|
60
|
+
*/
|
|
61
|
+
declare function cholesky(a: Tensor): Tensor;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Compute eigenvalues and eigenvectors of a square matrix.
|
|
65
|
+
*
|
|
66
|
+
* Solves A * v = λ * v where λ are eigenvalues and v are eigenvectors.
|
|
67
|
+
*
|
|
68
|
+
* **Algorithm**:
|
|
69
|
+
* - Symmetric matrices: Jacobi iteration (stable, accurate)
|
|
70
|
+
* - General matrices: QR iteration with Hessenberg reduction
|
|
71
|
+
*
|
|
72
|
+
* **Limitations**:
|
|
73
|
+
* - Only real eigenvalues are supported. Non-symmetric matrices whose
|
|
74
|
+
* spectrum includes complex eigenvalues will cause an
|
|
75
|
+
* {@link InvalidParameterError} to be thrown.
|
|
76
|
+
* - For symmetric/Hermitian matrices, use `eigh()` for better performance
|
|
77
|
+
* - May not converge for some matrices (bounded QR iterations; see options)
|
|
78
|
+
*
|
|
79
|
+
* **Parameters**:
|
|
80
|
+
* @param a - Square matrix of shape (N, N)
|
|
81
|
+
* @param options - Optional configuration overrides (see {@link EigOptions})
|
|
82
|
+
* @param options.maxIter - Maximum QR iterations (default: 300)
|
|
83
|
+
* @param options.tol - Convergence tolerance for subdiagonal norm (default: 1e-10)
|
|
84
|
+
*
|
|
85
|
+
* **Returns**: [eigenvalues, eigenvectors]
|
|
86
|
+
* - eigenvalues: Real values of shape (N,)
|
|
87
|
+
* - eigenvectors: Column vectors of shape (N, N) where eigenvectors[:,i] corresponds to eigenvalues[i]
|
|
88
|
+
*
|
|
89
|
+
* **Requirements**:
|
|
90
|
+
* - Input must be square matrix
|
|
91
|
+
* - Matrix must have only real eigenvalues
|
|
92
|
+
* - For symmetric matrices, use eigh() for better performance
|
|
93
|
+
*
|
|
94
|
+
* **Properties**:
|
|
95
|
+
* - A @ v[:,i] = λ[i] * v[:,i]
|
|
96
|
+
* - Eigenvectors are normalized
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { eig } from 'deepbox/linalg';
|
|
101
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
102
|
+
*
|
|
103
|
+
* const A = tensor([[1, 2], [2, 1]]);
|
|
104
|
+
* const [eigenvalues, eigenvectors] = eig(A);
|
|
105
|
+
*
|
|
106
|
+
* // Verify: A @ eigenvectors[:,i] ≈ eigenvalues[i] * eigenvectors[:,i]
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @throws {ShapeError} If input is not square matrix
|
|
110
|
+
* @throws {DTypeError} If input has string dtype
|
|
111
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
112
|
+
* @throws {InvalidParameterError} If matrix has complex eigenvalues
|
|
113
|
+
*
|
|
114
|
+
* @see {@link https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix | Wikipedia: Eigendecomposition}
|
|
115
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 7.5.2
|
|
116
|
+
*/
|
|
117
|
+
type EigOptions = {
|
|
118
|
+
readonly maxIter?: number;
|
|
119
|
+
readonly tol?: number;
|
|
120
|
+
};
|
|
121
|
+
declare function eig(a: Tensor, options?: EigOptions): [Tensor, Tensor];
|
|
122
|
+
/**
|
|
123
|
+
* Compute eigenvalues only (faster than eig).
|
|
124
|
+
*
|
|
125
|
+
* **Parameters**:
|
|
126
|
+
* @param a - Square matrix of shape (N, N)
|
|
127
|
+
*
|
|
128
|
+
* **Returns**: eigenvalues - Array of real eigenvalues
|
|
129
|
+
*
|
|
130
|
+
* **Limitations**:
|
|
131
|
+
* - Matrices with complex eigenvalues are not supported and will throw
|
|
132
|
+
* {@link InvalidParameterError}
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { eigvals } from 'deepbox/linalg';
|
|
137
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
138
|
+
*
|
|
139
|
+
* const A = tensor([[1, 2], [2, 1]]);
|
|
140
|
+
* const eigenvalues = eigvals(A);
|
|
141
|
+
* console.log(eigenvalues); // [3, -1]
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
declare function eigvals(a: Tensor, options?: EigOptions): Tensor;
|
|
145
|
+
/**
|
|
146
|
+
* Compute eigenvalues only of symmetric matrix (faster than eigh).
|
|
147
|
+
*
|
|
148
|
+
* **Parameters**:
|
|
149
|
+
* @param a - Symmetric square matrix of shape (N, N)
|
|
150
|
+
*
|
|
151
|
+
* **Returns**: eigenvalues - Array of real eigenvalues
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { eigvalsh } from 'deepbox/linalg';
|
|
156
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
157
|
+
*
|
|
158
|
+
* const A = tensor([[1, 2], [2, 1]]);
|
|
159
|
+
* const eigenvalues = eigvalsh(A);
|
|
160
|
+
* console.log(eigenvalues); // [-1, 3]
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @throws {ShapeError} If input is not square matrix
|
|
164
|
+
* @throws {DTypeError} If input has string dtype
|
|
165
|
+
* @throws {DataValidationError} If input is not symmetric
|
|
166
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
167
|
+
*/
|
|
168
|
+
declare function eigvalsh(a: Tensor): Tensor;
|
|
169
|
+
/**
|
|
170
|
+
* Compute eigenvalues and eigenvectors of a symmetric/Hermitian matrix.
|
|
171
|
+
*
|
|
172
|
+
* More efficient than eig() for symmetric matrices.
|
|
173
|
+
*
|
|
174
|
+
* **Parameters**:
|
|
175
|
+
* @param a - Symmetric matrix of shape (N, N)
|
|
176
|
+
*
|
|
177
|
+
* **Returns**: [eigenvalues, eigenvectors]
|
|
178
|
+
* - All eigenvalues are real
|
|
179
|
+
* - Eigenvectors are orthonormal
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { eigh } from 'deepbox/linalg';
|
|
184
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
185
|
+
*
|
|
186
|
+
* const A = tensor([[1, 2], [2, 1]]); // Symmetric
|
|
187
|
+
* const [eigenvalues, eigenvectors] = eigh(A);
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* @throws {ShapeError} If input is not square matrix
|
|
191
|
+
* @throws {DTypeError} If input has string dtype
|
|
192
|
+
* @throws {DataValidationError} If input is not symmetric
|
|
193
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
194
|
+
*/
|
|
195
|
+
declare function eigh(a: Tensor): [Tensor, Tensor];
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* LU decomposition with partial pivoting.
|
|
199
|
+
*
|
|
200
|
+
* Factorizes matrix A into P * A = L * U where:
|
|
201
|
+
* - P is a permutation matrix
|
|
202
|
+
* - L is lower triangular with unit diagonal
|
|
203
|
+
* - U is upper triangular
|
|
204
|
+
*
|
|
205
|
+
* **Algorithm**: Gaussian elimination with partial pivoting
|
|
206
|
+
*
|
|
207
|
+
* **Parameters**:
|
|
208
|
+
* @param a - Input matrix of shape (M, N)
|
|
209
|
+
*
|
|
210
|
+
* **Returns**: [P, L, U]
|
|
211
|
+
* - P: Permutation matrix of shape (M, M)
|
|
212
|
+
* - L: Lower triangular matrix of shape (M, K) where K = min(M, N)
|
|
213
|
+
* - U: Upper triangular matrix of shape (K, N)
|
|
214
|
+
*
|
|
215
|
+
* **Requirements**:
|
|
216
|
+
* - Input must be 2D matrix
|
|
217
|
+
*
|
|
218
|
+
* **Properties**:
|
|
219
|
+
* - P @ A = L @ U
|
|
220
|
+
* - L has unit diagonal (L[i,i] = 1)
|
|
221
|
+
* - Partial pivoting ensures numerical stability
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* import { lu } from 'deepbox/linalg';
|
|
226
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
227
|
+
*
|
|
228
|
+
* const A = tensor([[2, 1, 1], [4, 3, 3], [8, 7, 9]]);
|
|
229
|
+
* const [P, L, U] = lu(A);
|
|
230
|
+
*
|
|
231
|
+
* // Verify: P @ A ≈ L @ U
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @throws {ShapeError} If input is not 2D
|
|
235
|
+
* @throws {DTypeError} If input has string dtype
|
|
236
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
237
|
+
*
|
|
238
|
+
* **Note**: Rank-deficient matrices (with zero pivots) are handled gracefully.
|
|
239
|
+
* The factorization will still produce valid P, L, U factors.
|
|
240
|
+
*
|
|
241
|
+
* @see {@link https://en.wikipedia.org/wiki/LU_decomposition | Wikipedia: LU decomposition}
|
|
242
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 3.4.1
|
|
243
|
+
*/
|
|
244
|
+
declare function lu(a: Tensor): [Tensor, Tensor, Tensor];
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* QR decomposition.
|
|
248
|
+
*
|
|
249
|
+
* Factorizes matrix A into Q * R where Q is orthogonal and R is upper triangular.
|
|
250
|
+
*
|
|
251
|
+
* **Algorithm**: Householder reflections
|
|
252
|
+
*
|
|
253
|
+
* **Parameters**:
|
|
254
|
+
* @param a - Input matrix of shape (M, N)
|
|
255
|
+
* @param mode - Decomposition mode:
|
|
256
|
+
* - 'reduced': Q has shape (M, K), R has shape (K, N) where K = min(M, N)
|
|
257
|
+
* - 'complete': Q has shape (M, M), R has shape (M, N)
|
|
258
|
+
*
|
|
259
|
+
* **Returns**: [Q, R]
|
|
260
|
+
* - Q: Orthogonal matrix (Q^T * Q = I)
|
|
261
|
+
* - R: Upper triangular matrix
|
|
262
|
+
*
|
|
263
|
+
* **Requirements**:
|
|
264
|
+
* - Input must be 2D matrix
|
|
265
|
+
*
|
|
266
|
+
* **Properties**:
|
|
267
|
+
* - A = Q @ R
|
|
268
|
+
* - Q is orthogonal: Q^T @ Q = I
|
|
269
|
+
* - R is upper triangular
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* import { qr } from 'deepbox/linalg';
|
|
274
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
275
|
+
*
|
|
276
|
+
* const A = tensor([[12, -51, 4], [6, 167, -68], [-4, 24, -41]]);
|
|
277
|
+
* const [Q, R] = qr(A);
|
|
278
|
+
*
|
|
279
|
+
* // Verify: A ≈ Q @ R
|
|
280
|
+
* // Verify: Q is orthogonal (Q^T @ Q ≈ I)
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* @throws {ShapeError} If input is not 2D
|
|
284
|
+
* @throws {DTypeError} If input has string dtype
|
|
285
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
286
|
+
*
|
|
287
|
+
* @see {@link https://en.wikipedia.org/wiki/QR_decomposition | Wikipedia: QR decomposition}
|
|
288
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 5.2.1
|
|
289
|
+
*/
|
|
290
|
+
declare function qr(a: Tensor, mode?: "reduced" | "complete"): [Tensor, Tensor];
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Singular Value Decomposition.
|
|
294
|
+
*
|
|
295
|
+
* Factorizes matrix A into three matrices: A = U * Σ * V^T
|
|
296
|
+
*
|
|
297
|
+
* **Algorithm**: One-sided Jacobi (Hestenes) SVD
|
|
298
|
+
*
|
|
299
|
+
* **Numerical Stability**: Uses orthogonal rotations on A's columns to
|
|
300
|
+
* directly compute singular values and right singular vectors without forming
|
|
301
|
+
* A^T A. This is substantially more stable for ill-conditioned matrices than
|
|
302
|
+
* the normal-equations approach.
|
|
303
|
+
*
|
|
304
|
+
* **Implementation Details**:
|
|
305
|
+
* 1. Apply cyclic Jacobi rotations to orthogonalize columns of A
|
|
306
|
+
* 2. Column norms converge to singular values
|
|
307
|
+
* 3. Right singular vectors are accumulated as the product of rotations
|
|
308
|
+
* 4. Left singular vectors are normalized columns of the rotated matrix
|
|
309
|
+
*
|
|
310
|
+
* **Parameters**:
|
|
311
|
+
* @param a - Input matrix of shape (M, N)
|
|
312
|
+
* @param fullMatrices - If true, U has shape (M, M) and V has shape (N, N).
|
|
313
|
+
* If false, U has shape (M, K) and V has shape (N, K) where K = min(M, N)
|
|
314
|
+
*
|
|
315
|
+
* **Returns**: [U, s, Vt]
|
|
316
|
+
* - U: Left singular vectors of shape (M, M) or (M, K)
|
|
317
|
+
* - s: Singular values of shape (K,) in descending order
|
|
318
|
+
* - Vt: Right singular vectors transposed of shape (N, N) or (K, N)
|
|
319
|
+
*
|
|
320
|
+
* **Requirements**:
|
|
321
|
+
* - Input must be 2D matrix
|
|
322
|
+
* - Works with any M x N matrix
|
|
323
|
+
*
|
|
324
|
+
* **Properties**:
|
|
325
|
+
* - A = U @ diag(s) @ Vt
|
|
326
|
+
* - U and V are orthogonal matrices
|
|
327
|
+
* - Singular values are non-negative and sorted in descending order
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```ts
|
|
331
|
+
* import { svd } from 'deepbox/linalg';
|
|
332
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
333
|
+
*
|
|
334
|
+
* const A = tensor([[1, 2], [3, 4], [5, 6]]);
|
|
335
|
+
* const [U, s, Vt] = svd(A);
|
|
336
|
+
*
|
|
337
|
+
* console.log(U.shape); // [3, 3] if fullMatrices=true, [3, 2] if false
|
|
338
|
+
* console.log(s.shape); // [2]
|
|
339
|
+
* console.log(Vt.shape); // [2, 2] if fullMatrices=true, [2, 2] if false
|
|
340
|
+
*
|
|
341
|
+
* // Reconstruction: A ≈ U @ diag(s) @ Vt
|
|
342
|
+
* ```
|
|
343
|
+
*
|
|
344
|
+
* @throws {ShapeError} If input is not 2D
|
|
345
|
+
* @throws {DTypeError} If input has string dtype
|
|
346
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
347
|
+
*
|
|
348
|
+
* @see {@link https://en.wikipedia.org/wiki/Singular_value_decomposition | Wikipedia: SVD}
|
|
349
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 8.6.2
|
|
350
|
+
*/
|
|
351
|
+
declare function svd(a: Tensor, fullMatrices?: boolean): [Tensor, Tensor, Tensor];
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Compute the inverse of a matrix.
|
|
355
|
+
*
|
|
356
|
+
* Finds matrix A^(-1) such that A * A^(-1) = I.
|
|
357
|
+
*
|
|
358
|
+
* **Parameters**:
|
|
359
|
+
* @param a - Square matrix of shape (N, N)
|
|
360
|
+
*
|
|
361
|
+
* **Returns**: Inverse matrix
|
|
362
|
+
*
|
|
363
|
+
* **Requirements**:
|
|
364
|
+
* - Matrix must be square
|
|
365
|
+
* - Matrix must be non-singular (det(A) ≠ 0)
|
|
366
|
+
*
|
|
367
|
+
* **Properties**:
|
|
368
|
+
* - A * inv(A) = I
|
|
369
|
+
* - inv(inv(A)) = A
|
|
370
|
+
* - inv(AB) = inv(B) * inv(A)
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```ts
|
|
374
|
+
* import { inv } from 'deepbox/linalg';
|
|
375
|
+
* import { tensor, matmul } from 'deepbox/ndarray';
|
|
376
|
+
*
|
|
377
|
+
* const A = tensor([[1, 2], [3, 4]]);
|
|
378
|
+
* const Ainv = inv(A);
|
|
379
|
+
*
|
|
380
|
+
* // Verify: A * A^(-1) ≈ I
|
|
381
|
+
* const I = matmul(A, Ainv);
|
|
382
|
+
* ```
|
|
383
|
+
*
|
|
384
|
+
* @throws {ShapeError} If matrix is not square or not 2D
|
|
385
|
+
* @throws {DTypeError} If input has string dtype
|
|
386
|
+
* @throws {DataValidationError} If matrix is singular or contains non-finite values
|
|
387
|
+
*
|
|
388
|
+
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Wikipedia: Matrix inverse}
|
|
389
|
+
*/
|
|
390
|
+
declare function inv(a: Tensor): Tensor;
|
|
391
|
+
/**
|
|
392
|
+
* Compute the Moore-Penrose pseudo-inverse.
|
|
393
|
+
*
|
|
394
|
+
* Generalization of matrix inverse for non-square or singular matrices.
|
|
395
|
+
*
|
|
396
|
+
* **Parameters**:
|
|
397
|
+
* @param a - Input matrix of shape (M, N)
|
|
398
|
+
* @param rcond - Cutoff for small singular values
|
|
399
|
+
*
|
|
400
|
+
* **Returns**: Pseudo-inverse of shape (N, M)
|
|
401
|
+
*
|
|
402
|
+
* **Properties**:
|
|
403
|
+
* - A * pinv(A) * A = A
|
|
404
|
+
* - pinv(A) * A * pinv(A) = pinv(A)
|
|
405
|
+
* - For full rank square matrix: pinv(A) = inv(A)
|
|
406
|
+
* - For overdetermined system: pinv(A) gives least squares solution
|
|
407
|
+
*
|
|
408
|
+
* **Algorithm**: Using SVD
|
|
409
|
+
* A = U * Σ * V^T
|
|
410
|
+
* pinv(A) = V * Σ^+ * U^T
|
|
411
|
+
* where Σ^+ is pseudo-inverse of Σ (1/s_i for s_i > rcond, else 0)
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```ts
|
|
415
|
+
* import { pinv } from 'deepbox/linalg';
|
|
416
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
417
|
+
*
|
|
418
|
+
* // Non-square matrix
|
|
419
|
+
* const A = tensor([[1, 2], [3, 4], [5, 6]]);
|
|
420
|
+
* const Apinv = pinv(A);
|
|
421
|
+
*
|
|
422
|
+
* console.log(Apinv.shape); // [2, 3]
|
|
423
|
+
* ```
|
|
424
|
+
*
|
|
425
|
+
* @throws {ShapeError} If input is not 2D matrix
|
|
426
|
+
* @throws {DTypeError} If input has string dtype
|
|
427
|
+
* @throws {InvalidParameterError} If rcond is negative or non-finite
|
|
428
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
429
|
+
*
|
|
430
|
+
* @see {@link https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse | Wikipedia: Pseudo-inverse}
|
|
431
|
+
*/
|
|
432
|
+
declare function pinv(a: Tensor, rcond?: number): Tensor;
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Matrix or vector norm.
|
|
436
|
+
*
|
|
437
|
+
* Computes various matrix and vector norms.
|
|
438
|
+
*
|
|
439
|
+
* **Parameters**:
|
|
440
|
+
* @param x - Input array
|
|
441
|
+
* @param ord - Order of the norm:
|
|
442
|
+
* For vectors:
|
|
443
|
+
* - undefined or 2: L2 norm (Euclidean)
|
|
444
|
+
* - 1: L1 norm (Manhattan)
|
|
445
|
+
* - Infinity: Max norm
|
|
446
|
+
* - -Infinity: Min norm
|
|
447
|
+
* - 0: L0 "norm" (number of non-zero elements)
|
|
448
|
+
* - p: Lp norm (p > 0). Negative p (except -Infinity) is invalid.
|
|
449
|
+
* For matrices:
|
|
450
|
+
* - 'fro': Frobenius norm
|
|
451
|
+
* - 'nuc': Nuclear norm (sum of singular values)
|
|
452
|
+
* - 1: Max column sum
|
|
453
|
+
* - -1: Min column sum
|
|
454
|
+
* - 2: Largest singular value
|
|
455
|
+
* - -2: Smallest singular value
|
|
456
|
+
* - Infinity: Max row sum
|
|
457
|
+
* - -Infinity: Min row sum
|
|
458
|
+
* @param axis - Axis along which to compute norm
|
|
459
|
+
* @param keepdims - Keep reduced dimensions
|
|
460
|
+
*
|
|
461
|
+
* **Returns**: Norm value (scalar or tensor)
|
|
462
|
+
*
|
|
463
|
+
* @throws {DTypeError} If input has string dtype
|
|
464
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
465
|
+
* @throws {InvalidParameterError} If norm order or axis values are invalid
|
|
466
|
+
* @throws {ShapeError} If axis configuration is incompatible with input
|
|
467
|
+
*
|
|
468
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_norm | Wikipedia: Matrix norm}
|
|
469
|
+
*/
|
|
470
|
+
declare function norm(x: Tensor, ord?: number | "fro" | "nuc", axis?: Axis | Axis[], keepdims?: boolean): Tensor | number;
|
|
471
|
+
/**
|
|
472
|
+
* Condition number of a matrix.
|
|
473
|
+
*
|
|
474
|
+
* Measures how sensitive the solution of A*x=b is to changes in b.
|
|
475
|
+
* Large condition number indicates ill-conditioned matrix.
|
|
476
|
+
*
|
|
477
|
+
* **Formula**: cond(A) = ||A|| * ||A^(-1)||
|
|
478
|
+
*
|
|
479
|
+
* **Parameters**:
|
|
480
|
+
* @param a - Input matrix
|
|
481
|
+
* @param p - Norm order (same as norm())
|
|
482
|
+
*
|
|
483
|
+
* **Returns**: Condition number (>= 1, infinity for singular matrices)
|
|
484
|
+
*
|
|
485
|
+
* @throws {ShapeError} If input is not a 2D matrix
|
|
486
|
+
* @throws {DTypeError} If input has string dtype
|
|
487
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
488
|
+
* @throws {InvalidParameterError} If p is unsupported
|
|
489
|
+
*/
|
|
490
|
+
declare function cond(a: Tensor, _p?: number | "fro"): number;
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Compute the determinant of a matrix.
|
|
494
|
+
*
|
|
495
|
+
* Uses LU decomposition with partial pivoting for numerical stability.
|
|
496
|
+
* The determinant is computed as: det(A) = pivSign * product(diag(U))
|
|
497
|
+
*
|
|
498
|
+
* **Algorithm**: LU decomposition
|
|
499
|
+
* **Time Complexity**: O(N³) for N×N matrix
|
|
500
|
+
* **Space Complexity**: O(N²) for LU factorization
|
|
501
|
+
*
|
|
502
|
+
* **Parameters**:
|
|
503
|
+
* @param a - Square matrix of shape (N, N)
|
|
504
|
+
*
|
|
505
|
+
* **Returns**: Determinant value (scalar)
|
|
506
|
+
*
|
|
507
|
+
* **Properties**:
|
|
508
|
+
* - det(A) = 0 if and only if A is singular (non-invertible)
|
|
509
|
+
* - det(AB) = det(A) * det(B)
|
|
510
|
+
* - det(A^T) = det(A)
|
|
511
|
+
* - det(cA) = c^N * det(A) for scalar c and N×N matrix A
|
|
512
|
+
* - det(I) = 1 for identity matrix
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* ```ts
|
|
516
|
+
* import { det } from 'deepbox/linalg';
|
|
517
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
518
|
+
*
|
|
519
|
+
* const A = tensor([[1, 2], [3, 4]]);
|
|
520
|
+
* console.log(det(A)); // -2
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @throws {ShapeError} If input is not a 2D square matrix
|
|
524
|
+
* @throws {DTypeError} If input has string dtype
|
|
525
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
526
|
+
*
|
|
527
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Wikipedia: Determinant}
|
|
528
|
+
*/
|
|
529
|
+
declare function det(a: Tensor): number;
|
|
530
|
+
/**
|
|
531
|
+
* Compute sign and natural logarithm of the determinant.
|
|
532
|
+
*
|
|
533
|
+
* More numerically stable than det() for large matrices or matrices with
|
|
534
|
+
* very large/small determinants that would overflow/underflow.
|
|
535
|
+
*
|
|
536
|
+
* **Algorithm**: LU decomposition
|
|
537
|
+
* **Time Complexity**: O(N³)
|
|
538
|
+
* **Space Complexity**: O(N²)
|
|
539
|
+
*
|
|
540
|
+
* **Parameters**:
|
|
541
|
+
* @param a - Square matrix
|
|
542
|
+
*
|
|
543
|
+
* **Returns**: [sign, logdet]
|
|
544
|
+
* - sign: +1, -1, or 0 (as 0D tensor)
|
|
545
|
+
* - logdet: Natural log of |det(A)| (as 0D tensor), -Infinity if singular
|
|
546
|
+
*
|
|
547
|
+
* **Mathematical Relation**:
|
|
548
|
+
* det(A) = sign * exp(logdet)
|
|
549
|
+
*
|
|
550
|
+
* **Advantages over det()**:
|
|
551
|
+
* - Avoids overflow for large determinants
|
|
552
|
+
* - Avoids underflow for small determinants
|
|
553
|
+
* - More numerically stable for ill-conditioned matrices
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* ```ts
|
|
557
|
+
* import { slogdet } from 'deepbox/linalg';
|
|
558
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
559
|
+
*
|
|
560
|
+
* const A = tensor([[1, 2], [3, 4]]);
|
|
561
|
+
* const [sign, logdet] = slogdet(A);
|
|
562
|
+
* // det(A) = sign * exp(logdet)
|
|
563
|
+
* ```
|
|
564
|
+
*
|
|
565
|
+
* @throws {ShapeError} If input is not a 2D square matrix
|
|
566
|
+
* @throws {DTypeError} If input has string dtype
|
|
567
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
568
|
+
*/
|
|
569
|
+
declare function slogdet(a: Tensor): [Tensor, Tensor];
|
|
570
|
+
/**
|
|
571
|
+
* Compute the trace of a matrix.
|
|
572
|
+
*
|
|
573
|
+
* Sum of diagonal elements. Supports offset diagonals.
|
|
574
|
+
*
|
|
575
|
+
* **Algorithm**: Direct summation
|
|
576
|
+
* **Time Complexity**: O(min(M, N)) where M×N is matrix size
|
|
577
|
+
* **Space Complexity**: O(1)
|
|
578
|
+
*
|
|
579
|
+
* **Parameters**:
|
|
580
|
+
* @param a - Input matrix (at least 2D)
|
|
581
|
+
* @param offset - Integer offset from main diagonal:
|
|
582
|
+
* - 0: main diagonal (default)
|
|
583
|
+
* - >0: upper diagonal (k-th diagonal above main)
|
|
584
|
+
* - <0: lower diagonal (k-th diagonal below main)
|
|
585
|
+
* @param axis1 - First axis to take the diagonal from (default: 0)
|
|
586
|
+
* @param axis2 - Second axis to take the diagonal from (default: 1)
|
|
587
|
+
*
|
|
588
|
+
* **Returns**: Trace values as tensor (one per slice if input is batched)
|
|
589
|
+
*
|
|
590
|
+
* **Properties**:
|
|
591
|
+
* - trace(A) = sum of eigenvalues (for square matrices)
|
|
592
|
+
* - trace(AB) = trace(BA) (cyclic property)
|
|
593
|
+
* - trace(A + B) = trace(A) + trace(B) (linearity)
|
|
594
|
+
* - trace(cA) = c * trace(A) for scalar c
|
|
595
|
+
* - trace(A^T) = trace(A)
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```ts
|
|
599
|
+
* import { trace } from 'deepbox/linalg';
|
|
600
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
601
|
+
*
|
|
602
|
+
* const A = tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
|
|
603
|
+
* console.log(trace(A)); // 1 + 5 + 9 = 15
|
|
604
|
+
* console.log(trace(A, 1)); // 2 + 6 = 8 (upper diagonal)
|
|
605
|
+
* console.log(trace(A, -1)); // 4 + 8 = 12 (lower diagonal)
|
|
606
|
+
* ```
|
|
607
|
+
*
|
|
608
|
+
* @throws {ShapeError} If input is not at least 2D
|
|
609
|
+
* @throws {InvalidParameterError} If axis values are invalid/identical or offset is non-integer
|
|
610
|
+
* @throws {DTypeError} If input has string dtype
|
|
611
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
612
|
+
*/
|
|
613
|
+
declare function trace(a: Tensor, offset?: number, axis1?: Axis, axis2?: Axis): Tensor;
|
|
614
|
+
/**
|
|
615
|
+
* Compute the rank of a matrix.
|
|
616
|
+
*
|
|
617
|
+
* Number of linearly independent rows/columns.
|
|
618
|
+
* Uses SVD to count singular values above a threshold.
|
|
619
|
+
*
|
|
620
|
+
* **Algorithm**: SVD-based rank computation
|
|
621
|
+
* **Time Complexity**: O(min(M,N) * M * N) for M×N matrix
|
|
622
|
+
* **Space Complexity**: O(M*N) for SVD computation
|
|
623
|
+
*
|
|
624
|
+
* **Parameters**:
|
|
625
|
+
* @param a - Input matrix of shape (M, N)
|
|
626
|
+
* @param tol - Threshold for small singular values (optional)
|
|
627
|
+
* - Default: max(M,N) * largest_singular_value * machine_epsilon
|
|
628
|
+
* - Singular values > tol are counted as non-zero
|
|
629
|
+
*
|
|
630
|
+
* **Returns**: Rank (integer between 0 and min(M, N))
|
|
631
|
+
*
|
|
632
|
+
* **Properties**:
|
|
633
|
+
* - 0 ≤ rank(A) ≤ min(M, N)
|
|
634
|
+
* - rank(A) = rank(A^T)
|
|
635
|
+
* - rank(A) = number of non-zero singular values
|
|
636
|
+
* - Full rank: rank(A) = min(M, N)
|
|
637
|
+
* - Rank deficient: rank(A) < min(M, N)
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* ```ts
|
|
641
|
+
* import { matrixRank } from 'deepbox/linalg';
|
|
642
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
643
|
+
*
|
|
644
|
+
* const A = tensor([[1, 2], [2, 4]]); // Rank 1 (linearly dependent rows)
|
|
645
|
+
* console.log(matrixRank(A)); // 1
|
|
646
|
+
*
|
|
647
|
+
* const B = tensor([[1, 0], [0, 1]]); // Full rank
|
|
648
|
+
* console.log(matrixRank(B)); // 2
|
|
649
|
+
* ```
|
|
650
|
+
*
|
|
651
|
+
* @throws {ShapeError} If input is not a 2D matrix
|
|
652
|
+
* @throws {DTypeError} If input has string dtype
|
|
653
|
+
* @throws {InvalidParameterError} If tol is negative or non-finite
|
|
654
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
655
|
+
*/
|
|
656
|
+
declare function matrixRank(a: Tensor, tol?: number): number;
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Least squares solution to A * x = b.
|
|
660
|
+
*
|
|
661
|
+
* Finds x that minimizes ||A*x - b||^2 (Euclidean norm).
|
|
662
|
+
* Works for overdetermined (M > N), underdetermined (M < N), and square systems.
|
|
663
|
+
*
|
|
664
|
+
* **Algorithm**: SVD-based least squares
|
|
665
|
+
*
|
|
666
|
+
* **Parameters**:
|
|
667
|
+
* @param a - Coefficient matrix of shape (M, N)
|
|
668
|
+
* @param b - Target values of shape (M,) or (M, K)
|
|
669
|
+
* @param rcond - Cutoff for small singular values (default: machine epsilon * max(M,N))
|
|
670
|
+
*
|
|
671
|
+
* **Returns**: Object with:
|
|
672
|
+
* - x: Least squares solution of shape (N,) or (N, K)
|
|
673
|
+
* - residuals: Sum of squared residuals ||b - A*x||^2
|
|
674
|
+
* - rank: Effective rank of A
|
|
675
|
+
* - s: Singular values of A
|
|
676
|
+
*
|
|
677
|
+
* **Requirements**:
|
|
678
|
+
* - A can be any M x N matrix
|
|
679
|
+
* - First dimension of A must match first dimension of b
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* ```ts
|
|
683
|
+
* import { lstsq } from 'deepbox/linalg';
|
|
684
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
685
|
+
*
|
|
686
|
+
* // Overdetermined system (more equations than unknowns)
|
|
687
|
+
* const A = tensor([[1, 1], [1, 2], [1, 3]]);
|
|
688
|
+
* const b = tensor([2, 3, 5]);
|
|
689
|
+
* const result = lstsq(A, b);
|
|
690
|
+
*
|
|
691
|
+
* console.log(result.x); // Best fit solution
|
|
692
|
+
* console.log(result.residuals); // Residual error
|
|
693
|
+
* ```
|
|
694
|
+
*
|
|
695
|
+
* @throws {ShapeError} If A is not 2D matrix
|
|
696
|
+
* @throws {ShapeError} If b is not 1D or 2D tensor
|
|
697
|
+
* @throws {ShapeError} If dimensions don't match
|
|
698
|
+
* @throws {DTypeError} If input has string dtype
|
|
699
|
+
* @throws {InvalidParameterError} If rcond is negative or non-finite
|
|
700
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
701
|
+
*
|
|
702
|
+
* @see {@link https://en.wikipedia.org/wiki/Least_squares | Wikipedia: Least squares}
|
|
703
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 5.5.4
|
|
704
|
+
*/
|
|
705
|
+
declare function lstsq(a: Tensor, b: Tensor, rcond?: number): {
|
|
706
|
+
readonly x: Tensor;
|
|
707
|
+
readonly residuals: Tensor;
|
|
708
|
+
readonly rank: number;
|
|
709
|
+
readonly s: Tensor;
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Solve linear system A * x = b.
|
|
714
|
+
*
|
|
715
|
+
* Finds vector x that satisfies the equation A * x = b.
|
|
716
|
+
*
|
|
717
|
+
* **Algorithm**: LU decomposition with partial pivoting
|
|
718
|
+
*
|
|
719
|
+
* **Parameters**:
|
|
720
|
+
* @param a - Coefficient matrix of shape (N, N)
|
|
721
|
+
* @param b - Right-hand side of shape (N,) or (N, K) for multiple RHS
|
|
722
|
+
*
|
|
723
|
+
* **Returns**: Solution x of same shape as b
|
|
724
|
+
*
|
|
725
|
+
* **Requirements**:
|
|
726
|
+
* - A must be square matrix
|
|
727
|
+
* - A must be non-singular (invertible)
|
|
728
|
+
* - Number of rows in A must equal length of b
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```ts
|
|
732
|
+
* import { solve } from 'deepbox/linalg';
|
|
733
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
734
|
+
*
|
|
735
|
+
* const A = tensor([[3, 1], [1, 2]]);
|
|
736
|
+
* const b = tensor([9, 8]);
|
|
737
|
+
* const x = solve(A, b);
|
|
738
|
+
*
|
|
739
|
+
* // Verify: A @ x ≈ b
|
|
740
|
+
* console.log(x); // [2, 3]
|
|
741
|
+
* ```
|
|
742
|
+
*
|
|
743
|
+
* @throws {ShapeError} If A is not square
|
|
744
|
+
* @throws {ShapeError} If dimensions don't match
|
|
745
|
+
* @throws {DTypeError} If input has string dtype
|
|
746
|
+
* @throws {DataValidationError} If A is singular
|
|
747
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
748
|
+
*
|
|
749
|
+
* @see {@link https://en.wikipedia.org/wiki/System_of_linear_equations | Wikipedia: Linear systems}
|
|
750
|
+
* @see Golub & Van Loan, "Matrix Computations", Algorithm 3.4.1
|
|
751
|
+
*/
|
|
752
|
+
declare function solve(a: Tensor, b: Tensor): Tensor;
|
|
753
|
+
/**
|
|
754
|
+
* Solve triangular system.
|
|
755
|
+
*
|
|
756
|
+
* More efficient than solve() when A is already triangular.
|
|
757
|
+
*
|
|
758
|
+
* **Parameters**:
|
|
759
|
+
* @param a - Triangular matrix of shape (N, N)
|
|
760
|
+
* @param b - Right-hand side of shape (N,) or (N, K)
|
|
761
|
+
* @param lower - If true, A is lower triangular; if false, upper triangular
|
|
762
|
+
*
|
|
763
|
+
* **Returns**: Solution x
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```ts
|
|
767
|
+
* import { solveTriangular } from 'deepbox/linalg';
|
|
768
|
+
* import { tensor } from 'deepbox/ndarray';
|
|
769
|
+
*
|
|
770
|
+
* const L = tensor([[2, 0], [3, 4]]); // Lower triangular
|
|
771
|
+
* const b = tensor([6, 18]);
|
|
772
|
+
* const x = solveTriangular(L, b, true);
|
|
773
|
+
* ```
|
|
774
|
+
*
|
|
775
|
+
* @throws {ShapeError} If A is not square
|
|
776
|
+
* @throws {ShapeError} If dimensions don't match
|
|
777
|
+
* @throws {DTypeError} If input has string dtype
|
|
778
|
+
* @throws {DataValidationError} If A is singular (zero diagonal)
|
|
779
|
+
* @throws {DataValidationError} If input contains non-finite values (NaN, Infinity)
|
|
780
|
+
*
|
|
781
|
+
* @see {@link https://en.wikipedia.org/wiki/Triangular_matrix | Wikipedia: Triangular matrix}
|
|
782
|
+
*/
|
|
783
|
+
declare function solveTriangular(a: Tensor, b: Tensor, lower?: boolean): Tensor;
|
|
784
|
+
|
|
785
|
+
type index_EigOptions = EigOptions;
|
|
786
|
+
declare const index_cholesky: typeof cholesky;
|
|
787
|
+
declare const index_cond: typeof cond;
|
|
788
|
+
declare const index_det: typeof det;
|
|
789
|
+
declare const index_eig: typeof eig;
|
|
790
|
+
declare const index_eigh: typeof eigh;
|
|
791
|
+
declare const index_eigvals: typeof eigvals;
|
|
792
|
+
declare const index_eigvalsh: typeof eigvalsh;
|
|
793
|
+
declare const index_inv: typeof inv;
|
|
794
|
+
declare const index_lstsq: typeof lstsq;
|
|
795
|
+
declare const index_lu: typeof lu;
|
|
796
|
+
declare const index_matrixRank: typeof matrixRank;
|
|
797
|
+
declare const index_norm: typeof norm;
|
|
798
|
+
declare const index_pinv: typeof pinv;
|
|
799
|
+
declare const index_qr: typeof qr;
|
|
800
|
+
declare const index_slogdet: typeof slogdet;
|
|
801
|
+
declare const index_solve: typeof solve;
|
|
802
|
+
declare const index_solveTriangular: typeof solveTriangular;
|
|
803
|
+
declare const index_svd: typeof svd;
|
|
804
|
+
declare const index_trace: typeof trace;
|
|
805
|
+
declare namespace index {
|
|
806
|
+
export { type index_EigOptions as EigOptions, index_cholesky as cholesky, index_cond as cond, index_det as det, index_eig as eig, index_eigh as eigh, index_eigvals as eigvals, index_eigvalsh as eigvalsh, index_inv as inv, index_lstsq as lstsq, index_lu as lu, index_matrixRank as matrixRank, index_norm as norm, index_pinv as pinv, index_qr as qr, index_slogdet as slogdet, index_solve as solve, index_solveTriangular as solveTriangular, index_svd as svd, index_trace as trace };
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
export { type EigOptions as E, eigh as a, eigvals as b, cholesky as c, eigvalsh as d, eig as e, inv as f, cond as g, det as h, index as i, slogdet as j, lstsq as k, lu as l, matrixRank as m, norm as n, solve as o, pinv as p, qr as q, solveTriangular as r, svd as s, trace as t };
|