@ac-kit/math-linear 0.4.0 → 0.5.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +57 -4
  3. package/dist/_euclidean-norm.d.ts +18 -0
  4. package/dist/_euclidean-norm.d.ts.map +1 -0
  5. package/dist/_euclidean-norm.js +73 -0
  6. package/dist/_euclidean-norm.js.map +1 -0
  7. package/dist/_matrix-shape.d.ts +4 -0
  8. package/dist/_matrix-shape.d.ts.map +1 -0
  9. package/dist/_matrix-shape.js +7 -0
  10. package/dist/_matrix-shape.js.map +1 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +5 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/lu-decomposition.d.ts +142 -0
  16. package/dist/lu-decomposition.d.ts.map +1 -0
  17. package/dist/lu-decomposition.js +416 -0
  18. package/dist/lu-decomposition.js.map +1 -0
  19. package/dist/matrix.d.ts +136 -0
  20. package/dist/matrix.d.ts.map +1 -0
  21. package/dist/matrix.js +308 -0
  22. package/dist/matrix.js.map +1 -0
  23. package/dist/qr-decomposition.d.ts +283 -0
  24. package/dist/qr-decomposition.d.ts.map +1 -0
  25. package/dist/qr-decomposition.js +1071 -0
  26. package/dist/qr-decomposition.js.map +1 -0
  27. package/dist/singular-value-decomposition.d.ts +154 -0
  28. package/dist/singular-value-decomposition.d.ts.map +1 -0
  29. package/dist/singular-value-decomposition.js +474 -0
  30. package/dist/singular-value-decomposition.js.map +1 -0
  31. package/dist/symmetric-eigen-decomposition.d.ts +92 -0
  32. package/dist/symmetric-eigen-decomposition.d.ts.map +1 -0
  33. package/dist/symmetric-eigen-decomposition.js +289 -0
  34. package/dist/symmetric-eigen-decomposition.js.map +1 -0
  35. package/dist/vec2-array.d.ts +16 -0
  36. package/dist/vec2-array.d.ts.map +1 -1
  37. package/dist/vec2-array.js +42 -0
  38. package/dist/vec2-array.js.map +1 -1
  39. package/package.json +11 -10
package/CHANGELOG.md CHANGED
@@ -1,5 +1,72 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5c203e7: Dense matrices of any size and their least-squares decomposition.
8
+
9
+ - `Matrix`: `{ data, rows, columns }`, `rows · columns` entries in one
10
+ `Float64Array`, column-major like the fixed sizes (row `r`, column `c` at
11
+ `rows · c + r`). `matrixCreate`, `matrixView` (over existing storage, no
12
+ copy), `matrixOfRows`, `matrixGet`, `matrixSet`, `matrixCopy`,
13
+ `matrixIdentity`, `matrixTranspose`, `matrixMultiply`,
14
+ `matrixMultiplyVector`, `matrixTransposeMultiplyVector`, `matrixEquals`,
15
+ `matrixIsClose`, `matrixIsFinite`. `out` first, no allocation; a
16
+ `RangeError` for mismatched shapes, a `data` of the wrong length, an index
17
+ outside the matrix, or an `out` sharing storage with an input.
18
+ - `QrDecomposition(rows, columns)`: Householder QR with column pivoting
19
+ (LAPACK `xGEQP3`), reused across matrices of one shape and allocating
20
+ nothing after its constructor. `decompose(matrix, { weights, rankTolerance })`
21
+ factorises `√W·A` and decides the numerical `rank` by incremental condition
22
+ estimation (`xGELSY`), reporting `pivots` (independent columns first) and
23
+ `reciprocalCondition`; `false`, rank `0` and `NaN` results for a non-finite
24
+ entry, any finite magnitude scaled exactly by powers of two.
25
+ `solve(out, b, "basic" | "minimum-norm")` gives the weighted least-squares
26
+ coefficients — aliased ones `0`, or the minimum-norm answer by a complete
27
+ orthogonal decomposition — and `residualSumOfSquares`;
28
+ `covariance(out, { scale, solution })` gives `scale · (AᵀWA)⁻¹` (`NaN` on
29
+ aliased columns) or `scale · (AᵀWA)⁺`; `leverages(out)` the hat-matrix
30
+ diagonal; `applyQ`, `applyQTranspose`, `q`, `r` the factors. O(m·n·min(m, n))
31
+ to decompose, O(m·min(m, n) + rank²) to solve.
32
+ - 5c203e7: `vec2ArrayFindNearestXyAffine2(points, transform, x, y, maxDistance?)` finds
33
+ the point whose image under an affine transform is nearest to `(x, y)`, in one
34
+ pass and without transforming the buffer: the same answer, tie for tie, as
35
+ `vec2ArrayFindNearestXy` over the transformed points. A hit test of data-space
36
+ points under a view that just moved.
37
+ - 5c203e7: - `LuDecomposition(size)`: LU with partial pivoting as a reusable plan —
38
+ `decompose`, `solve`, `determinant`, `singular`, `pivots` and
39
+ `reciprocalCondition` (the 1-norm estimate of LAPACK `xGECON`).
40
+ - `matrixSolve(matrix, right)` solves a square system and returns the solution
41
+ with its reciprocal condition estimate.
42
+ - `CholeskyDecomposition(size)`: `A = L·Lᵀ` for symmetric positive-definite
43
+ matrices — `decompose` (`false` when not positive definite), `solve`,
44
+ `factor` and `logDeterminant`.
45
+ - 5c203e7: `SingularValueDecomposition(rows, columns)`: the thin SVD `A = U·diag(σ)·Vᵀ`
46
+ of any `Matrix` shape by one-sided Jacobi, as a plan allocating nothing after
47
+ construction. `decompose` (`false` on a non-finite entry), `singularValues`
48
+ (descending, small ones to high relative accuracy for column-graded matrices),
49
+ `u` and `v` with orthonormal columns (completed for zero singular values),
50
+ `rank`, `conditionNumber`, `solve` (minimum-norm least squares) and
51
+ `pseudoInverse`, truncated at `rcond · σ₀` (default `max(rows, columns) · ε`,
52
+ NumPy's).
53
+
54
+ ### Patch Changes
55
+
56
+ - Updated dependencies [5c203e7]
57
+ - Updated dependencies [5c203e7]
58
+ - Updated dependencies [5c203e7]
59
+ - Updated dependencies [5c203e7]
60
+ - Updated dependencies [5c203e7]
61
+ - Updated dependencies [5c203e7]
62
+ - Updated dependencies [5c203e7]
63
+ - Updated dependencies [5c203e7]
64
+ - Updated dependencies [5c203e7]
65
+ - Updated dependencies [5c203e7]
66
+ - @ac-kit/core@0.14.0
67
+ - @ac-kit/math-complex@0.3.0
68
+ - @ac-kit/math-scalar@0.5.0
69
+
3
70
  ## 0.4.0
4
71
 
5
72
  ### Minor Changes
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @ac-kit/math-linear
2
2
 
3
3
  Linear algebra over IEEE 754 binary64: vectors, matrices and quaternions of
4
- fixed, small dimension.
4
+ fixed, small dimension, and dense matrices of any size with their
5
+ decompositions.
5
6
 
6
7
  ## Scope
7
8
 
@@ -11,8 +12,11 @@ The fixed-dimension types that 2D and 3D work needs — `Vec2`/`Vec3`/`Vec4`,
11
12
  type-level brand, and operated on by free functions.
12
13
 
13
14
  Dimension is part of the type, not a runtime property. That is what makes the
14
- operations inlineable and allocation-predictable, and it is why an arbitrary
15
- `Matrix<n, m>` is a different design rather than a generalisation of this one.
15
+ operations inlineable and allocation-predictable, and it is why the dense
16
+ `Matrix` of any size is a different design rather than a generalisation of
17
+ this one: `{ data, rows, columns }` over one column-major `Float64Array`, the
18
+ type that least squares, regression and the decompositions work on (see
19
+ [Dense matrices and least squares](#dense-matrices-and-least-squares)).
16
20
 
17
21
  ## Contents
18
22
 
@@ -27,9 +31,13 @@ operations inlineable and allocation-predictable, and it is why an arbitrary
27
31
  | `projection` | `mat4x4Perspective`, `Frustum`, `Orthographic` (three depth ranges, an infinite far plane), `mat4x4ConvertDepthRange`, `mat4x4LookAt`, `affine3TargetTo` |
28
32
  | `view-basis` | `mat4x4ViewFromBasis`: a view matrix from a camera's right, up and back axes and its eye (exact looking straight up or down) |
29
33
  | `viewport` | Pixels (top left, y down) and normalised device coordinates (y up): `vec3ProjectToViewport`, `vec3UnprojectFromViewport`, a bulk kernel |
30
- | `vec2-array`, `vec3-array`, `vec4-array` | Buffers of vectors (a `Float64Array` of `count × N`): fused kernels (arithmetic, lerp, normalise, means, transforms by matrices and affine forms), strided kernels, narrowing to and widening from interleaved `Float32Array`, nearest point |
34
+ | `vec2-array`, `vec3-array`, `vec4-array` | Buffers of vectors (a `Float64Array` of `count × N`): fused kernels (arithmetic, lerp, normalise, means, transforms by matrices and affine forms), strided kernels, narrowing to and widening from interleaved `Float32Array`, nearest point (also under an affine transform, without transforming the buffer) |
31
35
  | `mat4x4-array`, `affine2-array`, `affine3-array`, `quaternion-array` | Instance buffers: `…ArrayCreate`, `…Load`, `…Store`, `…LoadStrided`, `…StoreStrided`, `…ArrayIsFinite` (also for `mat2x2` and `mat3x3`) |
32
36
  | `tridiagonal` | `tridiagonalSolve(out, lower, diagonal, upper, right, scratch)`: the Thomas algorithm in O(n), `false` on a zero pivot; stable for diagonally dominant or symmetric positive definite systems |
37
+ | `matrix` | `Matrix` of any size, column-major over a `Float64Array`: `matrixCreate`, `matrixView`, `matrixOfRows`, get and set, copy, identity, transpose, products with matrices and vectors, equality |
38
+ | `qr-decomposition` | `QrDecomposition`: Householder QR with column pivoting, rank by condition estimation, weighted least squares (basic or minimum-norm), covariances, leverages; 0 B after construction |
39
+ | `lu-decomposition` | `LuDecomposition` (partial pivoting, determinant, Hager–Higham reciprocal condition estimate), `matrixSolve(matrix, right)` with that estimate, and `CholeskyDecomposition` (positive-definite test, solve, log-determinant); plans, 0 B after construction |
40
+ | `singular-value-decomposition` | `SingularValueDecomposition`: thin SVD by one-sided Jacobi (high relative accuracy of small singular values), rank, condition number, minimum-norm least squares and pseudo-inverse with an `rcond` truncation; a plan, 0 B after construction |
33
41
  | `upload` | Unrolled `…StoreFloat32` of matrices and quaternions, and the padded `…StoreStd140Float32` of uniform blocks |
34
42
  | `checked` | The `@ac-kit/math-linear/checked` subpath: `assertFiniteValue`, `assertFiniteBuffer`, throwing assertions for the boundaries of a program |
35
43
  | `kl-activations` | `KlActivations`: KL-divergence NMF activations of a sparse fixed dictionary, with per-column L1 penalties, iterated across calls |
@@ -232,6 +240,51 @@ and `assertFiniteBuffer(name, array, elementSize, count?)`, which throw a
232
240
  `RangeError` naming the argument and the first bad component. Call them after
233
241
  parsing untrusted data, before an upload in a development build, and in tests.
234
242
 
243
+ ## Dense matrices and least squares
244
+
245
+ A `Matrix` is `{ data, rows, columns }`: `rows · columns` entries in one
246
+ `Float64Array`, column-major like the fixed sizes (row `r`, column `c` at
247
+ `rows · c + r`, LAPACK's layout). `matrixView(data, rows, columns)` wraps
248
+ existing storage without a copy, `matrixOfRows([[1, 2], [3, 4]])` copies rows
249
+ as written. The operations take `out` first and allocate nothing, but at a
250
+ runtime size an alias-safe product would need scratch of that size, so an
251
+ `out` sharing storage with an input is a `RangeError` rather than a silent
252
+ wrong answer.
253
+
254
+ `QrDecomposition` is a plan for one shape: its constructor allocates the
255
+ factors and every working array, and `decompose`, `solve`, `covariance`,
256
+ `leverages` and the `Q` products allocate nothing after it. It factorises `A·P
257
+ = Q·R` by Householder reflections with column pivoting (LAPACK's `xGEQP3`), and
258
+ decides the **rank** by incremental condition estimation (`xGELSY`): the
259
+ leading triangle keeps a pivot column only while its estimated reciprocal
260
+ condition stays above `rankTolerance` — `max(rows, columns) · ε` by default,
261
+ `1e-7` for R's `lm` behaviour. `pivots` names the independent columns, the
262
+ others are aliased, and `reciprocalCondition` tells how many digits an answer
263
+ can have lost.
264
+
265
+ ```ts
266
+ import { matrixCreate, matrixOfRows, QrDecomposition } from "@ac-kit/math-linear";
267
+
268
+ // Weighted fit of y ≈ β₀ + β₁·t; the last observation has weight zero.
269
+ const design = matrixOfRows([[1, 0], [1, 1], [1, 2], [1, 3]]);
270
+ const qr = new QrDecomposition(4, 2);
271
+ qr.decompose(design, { weights: Float64Array.of(1, 2, 1, 0) });
272
+ const beta = qr.solve(new Float64Array(2), Float64Array.of(1, 3, 5, 100));
273
+ const sigma2 = qr.residualSumOfSquares / (3 - qr.rank);
274
+ const covariance = qr.covariance(matrixCreate(2, 2), { scale: sigma2 });
275
+ ```
276
+
277
+ A rank-deficient design gets either the **basic** solution (aliased
278
+ coefficients `0`, their covariance rows `NaN`, as a regression reports them)
279
+ or the **minimum-norm** one (`solve(out, b, "minimum-norm")`, the
280
+ pseudo-inverse answer through a complete orthogonal decomposition), with the
281
+ matching covariance `(AᵀWA)⁻¹` or `(AᵀWA)⁺`. Weights make it the inner step
282
+ of iteratively reweighted least squares; `leverages` gives the hat-matrix
283
+ diagonal for studentised residuals and robust weights. A non-finite entry
284
+ makes `decompose` return `false` and every result `NaN`; finite matrices of
285
+ any magnitude are scaled by powers of two first, so entries near `1e±300`
286
+ factorise exactly as entries near `1` do.
287
+
235
288
  ## Usage
236
289
 
237
290
  ```ts
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Writes the Euclidean norm of `count` entries of `data`, from `start`,
3
+ * `stride` apart, to `out[outIndex]`, right over the whole binary64 range: the
4
+ * plain `√Σx²` when its sum lies in `[2⁻⁹⁰⁰, ∞)`, and a second pass scaled by
5
+ * the largest magnitude otherwise (an overflowing or underflowing sum), so the
6
+ * scaled pass runs only for entries outside about `[1e−135, 1e150]`. `NaN` when
7
+ * an entry is `NaN`.
8
+ *
9
+ * The result goes through an array, never a return value: a double returned
10
+ * from a call the engine does not inline is boxed.
11
+ */
12
+ export declare function euclideanNorm_(out: Float64Array, outIndex: number, data: Float64Array, start: number, count: number, stride: number): void;
13
+ /**
14
+ * `√(a² + b²)` without overflow or underflow, never `Math.hypot`. Small enough
15
+ * for the engine to inline at its call sites, which a double return needs.
16
+ */
17
+ export declare function pairNorm_(a: number, b: number): number;
18
+ //# sourceMappingURL=_euclidean-norm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_euclidean-norm.d.ts","sourceRoot":"","sources":["../src/_euclidean-norm.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC7B,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACZ,IAAI,CAsDN;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAatD"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Below this, a plain sum of squares may hold squares that underflowed to
3
+ * subnormals; their total error is then at most `count · 2⁻¹⁰⁷⁵`, which against
4
+ * a sum of at least `2⁻⁹⁰⁰` is below `count · 2⁻¹⁷⁵` relative.
5
+ */
6
+ const PLAIN_SUM_MINIMUM_ = 2 ** -900;
7
+ /**
8
+ * Writes the Euclidean norm of `count` entries of `data`, from `start`,
9
+ * `stride` apart, to `out[outIndex]`, right over the whole binary64 range: the
10
+ * plain `√Σx²` when its sum lies in `[2⁻⁹⁰⁰, ∞)`, and a second pass scaled by
11
+ * the largest magnitude otherwise (an overflowing or underflowing sum), so the
12
+ * scaled pass runs only for entries outside about `[1e−135, 1e150]`. `NaN` when
13
+ * an entry is `NaN`.
14
+ *
15
+ * The result goes through an array, never a return value: a double returned
16
+ * from a call the engine does not inline is boxed.
17
+ */
18
+ export function euclideanNorm_(out, outIndex, data, start, count, stride) {
19
+ let sum = 0;
20
+ let index = start;
21
+ for (let step = 0; step < count; step++) {
22
+ const value = data[index];
23
+ sum += value * value;
24
+ index += stride;
25
+ }
26
+ if (sum >= PLAIN_SUM_MINIMUM_ && sum < Number.POSITIVE_INFINITY) {
27
+ out[outIndex] = Math.sqrt(sum);
28
+ return;
29
+ }
30
+ if (sum !== sum) {
31
+ out[outIndex] = Number.NaN;
32
+ return;
33
+ }
34
+ let largest = 0;
35
+ index = start;
36
+ for (let step = 0; step < count; step++) {
37
+ const magnitude = Math.abs(data[index]);
38
+ if (magnitude > largest) {
39
+ largest = magnitude;
40
+ }
41
+ index += stride;
42
+ }
43
+ if (largest === 0 || largest === Number.POSITIVE_INFINITY) {
44
+ out[outIndex] = largest;
45
+ return;
46
+ }
47
+ // Divided, not multiplied by a reciprocal: the reciprocal of a subnormal
48
+ // largest entry overflows.
49
+ let scaled = 0;
50
+ index = start;
51
+ for (let step = 0; step < count; step++) {
52
+ const value = data[index] / largest;
53
+ scaled += value * value;
54
+ index += stride;
55
+ }
56
+ out[outIndex] = largest * Math.sqrt(scaled);
57
+ }
58
+ /**
59
+ * `√(a² + b²)` without overflow or underflow, never `Math.hypot`. Small enough
60
+ * for the engine to inline at its call sites, which a double return needs.
61
+ */
62
+ export function pairNorm_(a, b) {
63
+ const x = Math.abs(a);
64
+ const y = Math.abs(b);
65
+ const larger = x > y ? x : y;
66
+ const smaller = x > y ? y : x;
67
+ if (larger === 0 || larger === Number.POSITIVE_INFINITY) {
68
+ return larger;
69
+ }
70
+ const ratio = smaller / larger;
71
+ return larger * Math.sqrt(1 + ratio * ratio);
72
+ }
73
+ //# sourceMappingURL=_euclidean-norm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_euclidean-norm.js","sourceRoot":"","sources":["../src/_euclidean-norm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,kBAAkB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;AAErC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,cAAc,CAC7B,GAAiB,EACjB,QAAgB,EAChB,IAAkB,EAClB,KAAa,EACb,KAAa,EACb,MAAc;IAEd,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,IAAI,KAAK,GAAG,KAAK,CAAC;IAElB,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC;QACzC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAE,CAAC;QAE3B,GAAG,IAAI,KAAK,GAAG,KAAK,CAAC;QACrB,KAAK,IAAI,MAAM,CAAC;IACjB,CAAC;IAED,IAAI,GAAG,IAAI,kBAAkB,IAAI,GAAG,GAAG,MAAM,CAAC,iBAAiB,EAAE,CAAC;QACjE,GAAG,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC/B,OAAO;IACR,CAAC;IAED,IAAI,GAAG,KAAK,GAAG,EAAE,CAAC;QACjB,GAAG,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;QAC3B,OAAO;IACR,CAAC;IAED,IAAI,OAAO,GAAG,CAAC,CAAC;IAEhB,KAAK,GAAG,KAAK,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CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9B,IAAI,MAAM,KAAK,CAAC,IAAI,MAAM,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;QACzD,OAAO,MAAM,CAAC;IACf,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC;IAE/B,OAAO,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Matrix } from "./matrix.js";
2
+ /** Throws unless `m`'s data holds exactly `rows · columns` entries. */
3
+ export declare function assertMatrixShape_(m: Matrix, name: string): void;
4
+ //# sourceMappingURL=_matrix-shape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_matrix-shape.d.ts","sourceRoot":"","sources":["../src/_matrix-shape.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,uEAAuE;AACvE,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAMhE"}
@@ -0,0 +1,7 @@
1
+ /** Throws unless `m`'s data holds exactly `rows · columns` entries. */
2
+ export function assertMatrixShape_(m, name) {
3
+ if (m.data.length !== m.rows * m.columns) {
4
+ throw new RangeError(`${name}: data holds ${m.data.length} entries, not rows × columns = ${m.rows} × ${m.columns}`);
5
+ }
6
+ }
7
+ //# sourceMappingURL=_matrix-shape.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_matrix-shape.js","sourceRoot":"","sources":["../src/_matrix-shape.ts"],"names":[],"mappings":"AAEA,uEAAuE;AACvE,MAAM,UAAU,kBAAkB,CAAC,CAAS,EAAE,IAAY;IACzD,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC;QAC1C,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,gBAAgB,CAAC,CAAC,IAAI,CAAC,MAAM,kCAAkC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,OAAO,EAAE,CAC7F,CAAC;IACH,CAAC;AACF,CAAC"}
package/dist/index.d.ts CHANGED
@@ -15,9 +15,14 @@ export * from "./mat3x3.js";
15
15
  export * from "./mat4x4-array.js";
16
16
  export * from "./mat4x4-componentwise.js";
17
17
  export * from "./mat4x4.js";
18
+ export * from "./lu-decomposition.js";
19
+ export * from "./matrix.js";
18
20
  export * from "./projection.js";
21
+ export * from "./qr-decomposition.js";
22
+ export * from "./singular-value-decomposition.js";
19
23
  export * from "./quaternion-array.js";
20
24
  export * from "./quaternion.js";
25
+ export * from "./symmetric-eigen-decomposition.js";
21
26
  export * from "./transform.js";
22
27
  export * from "./tridiagonal.js";
23
28
  export * from "./upload.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,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,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -15,9 +15,14 @@ export * from "./mat3x3.js";
15
15
  export * from "./mat4x4-array.js";
16
16
  export * from "./mat4x4-componentwise.js";
17
17
  export * from "./mat4x4.js";
18
+ export * from "./lu-decomposition.js";
19
+ export * from "./matrix.js";
18
20
  export * from "./projection.js";
21
+ export * from "./qr-decomposition.js";
22
+ export * from "./singular-value-decomposition.js";
19
23
  export * from "./quaternion-array.js";
20
24
  export * from "./quaternion.js";
25
+ export * from "./symmetric-eigen-decomposition.js";
21
26
  export * from "./transform.js";
22
27
  export * from "./tridiagonal.js";
23
28
  export * from "./upload.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;A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c,iBAAiB,CAAC;AAChC,cAAc,oCAAoC,CAAC;AACnD,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC"}
@@ -0,0 +1,142 @@
1
+ import type { Matrix } from "./matrix.js";
2
+ /**
3
+ * An LU decomposition with partial pivoting, `P·A = L·U` (LAPACK `xGETRF`), as
4
+ * a plan for one size: the factors and scratch are allocated once by the
5
+ * constructor, and `decompose`, `solve`, `determinant` and
6
+ * `reciprocalCondition` allocate nothing afterwards.
7
+ *
8
+ * The general solver for a square system: about `⅔n³` operations, half of a
9
+ * QR's. Partial pivoting is backward stable in practice (Wilkinson's growth
10
+ * factor is rarely large); for symmetric positive-definite matrices
11
+ * {@link CholeskyDecomposition} is twice as fast again, and for least squares or
12
+ * rank-deficient matrices `QrDecomposition` is the right tool.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const lu = new LuDecomposition(2);
17
+ * lu.decompose(matrixOfRows([[4, 3], [6, 3]])); // true
18
+ * lu.solve(new Float64Array(2), Float64Array.of(10, 12)); // [1, 2]
19
+ * lu.determinant; // -6
20
+ * ```;
21
+ */
22
+ export declare class LuDecomposition {
23
+ /** The order of the matrices this decomposition takes. */
24
+ readonly size: number;
25
+ private readonly factors_;
26
+ private readonly pivots_;
27
+ private readonly work_;
28
+ private readonly work2_;
29
+ private sign_;
30
+ private singular_;
31
+ private norm1_;
32
+ /** @throws {RangeError} When `size` is not a non-negative safe integer. */
33
+ constructor(size: number);
34
+ /**
35
+ * The row interchanges: row `i` of `U` came from row `pivots[i]` after the
36
+ * earlier interchanges (LAPACK's `ipiv`, 0-based). Read, never write.
37
+ */
38
+ get pivots(): Int32Array;
39
+ /** `det A`, the product of `U`'s diagonal with the permutation's sign. */
40
+ get determinant(): number;
41
+ /** Whether `U` has an exactly zero pivot (or a non-finite entry). */
42
+ get singular(): boolean;
43
+ /**
44
+ * Factorises `matrix`. Returns `false` when a pivot is exactly zero or an
45
+ * entry not finite; the factors are then incomplete and `solve` answers
46
+ * `NaN`.
47
+ *
48
+ * @throws {RangeError} When `matrix` is not `size × size`.
49
+ */
50
+ decompose(matrix: Matrix): boolean;
51
+ /**
52
+ * Solves `A·x = b` for the last decomposed `A` into `out`, which may be
53
+ * `right`. All `NaN` after a singular matrix.
54
+ *
55
+ * @throws {RangeError} When `out` or `right` does not hold `size` numbers.
56
+ */
57
+ solve(out: Float64Array, right: Float64Array): Float64Array;
58
+ /** `A·x = b` (or `Aᵀ·x = b`) in place on `x`. */
59
+ private solveInPlace_;
60
+ /**
61
+ * An estimate of `1 / (‖A‖₁ ‖A⁻¹‖₁)`, the reciprocal condition number in the
62
+ * 1-norm (LAPACK `xGECON`, Hager's estimator as refined by Higham): near 1
63
+ * for a well-conditioned matrix, near `ε` when the solution has no correct
64
+ * digit, `0` for a singular one. A lower bound on `‖A⁻¹‖₁` in at most five
65
+ * pairs of solves, almost always within a factor of three.
66
+ */
67
+ get reciprocalCondition(): number;
68
+ }
69
+ /** The result of {@link matrixSolve}. */
70
+ export type LinearSolution = {
71
+ /** `x` with `A·x = b`; all `NaN` for a singular matrix. */
72
+ readonly solution: Float64Array;
73
+ /**
74
+ * The estimated reciprocal 1-norm condition number of `A`: the solution has
75
+ * about `−log₁₀(reciprocalCondition · 2⁵³)` fewer correct digits than
76
+ * binary64 carries, and none when it is near `ε`; `0` when singular.
77
+ */
78
+ readonly reciprocalCondition: number;
79
+ };
80
+ /**
81
+ * Solves the square system `A·x = b` by LU decomposition with partial pivoting,
82
+ * and estimates the reciprocal condition number so a caller can tell an answer
83
+ * that is right from one that only looks right. The one-shot form of
84
+ * {@link LuDecomposition}: it allocates the plan.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * matrixSolve(matrixOfRows([[2, 1], [1, 3]]), Float64Array.of(3, 5)); // { solution: [0.8, 1.4], reciprocalCondition: ≈ 0.4 }
89
+ * ```;
90
+ *
91
+ * @throws {RangeError} When `matrix` is not square or `right` has another
92
+ * length.
93
+ */
94
+ export declare function matrixSolve(matrix: Matrix, right: Float64Array): LinearSolution;
95
+ /**
96
+ * A Cholesky decomposition `A = L·Lᵀ` of a symmetric positive-definite matrix
97
+ * (LAPACK `xPOTRF`, lower triangle), as a plan for one size: half the work of
98
+ * LU, no pivoting, and the decomposition itself is the test of positive
99
+ * definiteness — what a covariance matrix, a Gram matrix or the normal
100
+ * equations of a well-posed fit need.
101
+ *
102
+ * Only the lower triangle of the matrix is read.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * const cholesky = new CholeskyDecomposition(2);
107
+ * cholesky.decompose(matrixOfRows([[4, 2], [2, 3]])); // true
108
+ * cholesky.solve(new Float64Array(2), Float64Array.of(2, 1)); // [0.5, 0]
109
+ * ```;
110
+ */
111
+ export declare class CholeskyDecomposition {
112
+ /** The order of the matrices this decomposition takes. */
113
+ readonly size: number;
114
+ private readonly factor_;
115
+ private positive_;
116
+ /** @throws {RangeError} When `size` is not a non-negative safe integer. */
117
+ constructor(size: number);
118
+ /** Whether the last decomposed matrix was positive definite. */
119
+ get positiveDefinite(): boolean;
120
+ /**
121
+ * `L`, column-major `size × size` with zeros above the diagonal. Read, never
122
+ * write.
123
+ */
124
+ get factor(): Float64Array;
125
+ /** `ln det A = 2 Σ ln Lₖₖ`, without the overflow of the determinant. */
126
+ get logDeterminant(): number;
127
+ /**
128
+ * Factorises the lower triangle of `matrix`. Returns `false` when it is not
129
+ * positive definite (a pivot is not positive) or not finite.
130
+ *
131
+ * @throws {RangeError} When `matrix` is not `size × size`.
132
+ */
133
+ decompose(matrix: Matrix): boolean;
134
+ /**
135
+ * Solves `A·x = b` for the last decomposed `A` into `out`, which may be
136
+ * `right`; all `NaN` when it was not positive definite.
137
+ *
138
+ * @throws {RangeError} When `out` or `right` does not hold `size` numbers.
139
+ */
140
+ solve(out: Float64Array, right: Float64Array): Float64Array;
141
+ }
142
+ //# sourceMappingURL=lu-decomposition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lu-decomposition.d.ts","sourceRoot":"","sources":["../src/lu-decomposition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAc1C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,eAAe;IAC3B,0DAA0D;IAC1D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAa;IACrC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,MAAM,CAAK;IAEnB,2EAA2E;IAC3E,YAAY,IAAI,EAAE,MAAM,EAevB;IAED;;;OAGG;IACH,IAAI,MAAM,IAAI,UAAU,CAEvB;IAED,0EAA0E;IAC1E,IAAI,WAAW,IAAI,MAAM,CAOxB;IAED,qEAAqE;IACrE,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CA6DjC;IAED;;;;;OAKG;IACH,KAAK,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,YAAY,GAAG,YAAY,CAe1D;IAED,i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