@tangent.to/lina 0.1.3 → 0.1.5
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/README.md +7 -4
- package/dist/index.js +11 -1
- package/dist/index.js.map +4 -4
- package/llms-full.txt +604 -0
- package/llms.txt +131 -0
- package/package.json +13 -6
- package/src/help.js +76 -0
- package/src/index.js +3 -0
- package/src/version.js +2 -0
- package/types/_mat.d.ts +63 -0
- package/types/cholesky.d.ts +75 -0
- package/types/eigsym.d.ts +76 -0
- package/types/help.d.ts +17 -0
- package/types/index.d.ts +61 -0
- package/types/lu.d.ts +71 -0
- package/types/ops.d.ts +51 -0
- package/types/qr.d.ts +31 -0
- package/types/svd.d.ts +51 -0
- package/types/version.d.ts +2 -0
package/llms.txt
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @tangent.to/lina 0.1.5
|
|
2
|
+
|
|
3
|
+
> Linear algebra for JavaScript (ESM): LU, QR, Cholesky, SVD, symmetric eigen, least squares. scipy.linalg-validated.
|
|
4
|
+
|
|
5
|
+
Linear algebra for JavaScript in browsers, Node.js and Deno. This
|
|
6
|
+
dependency-free package provides the matrix solvers used by Tangent's
|
|
7
|
+
statistics, autodiff and differential-equation packages.
|
|
8
|
+
|
|
9
|
+
Matrices are plain nested row-major arrays (`number[][]`) at the API
|
|
10
|
+
boundary; computation runs on flat `Float64Array` storage internally.
|
|
11
|
+
|
|
12
|
+
## What's in it
|
|
13
|
+
|
|
14
|
+
- **Factorizations**: `lu` (partial pivoting), `qr` (Householder,
|
|
15
|
+
reduced/full), `cholesky`, `svd` (one-sided Jacobi — high relative
|
|
16
|
+
accuracy), `eigSym` (cyclic Jacobi, symmetric matrices)
|
|
17
|
+
- **Generalized eigenproblem**: `eigSymGeneralized` (`A x = λ B x` by
|
|
18
|
+
Cholesky reduction, with a truncated fallback when `B` is singular),
|
|
19
|
+
`invSqrtSym` (truncated inverse square root)
|
|
20
|
+
- **Solvers**: `solve` (vector or multi-RHS), `choleskySolve` (vector or
|
|
21
|
+
multi-RHS), `lstsq` (QR-based), `pinvSolve` (minimum-norm, any rank)
|
|
22
|
+
- **SVD-derived**: `pinv`, `rank`, `cond`
|
|
23
|
+
- **Utilities**: `matmul`, `transpose`, `identity`, `diag`, `norm`
|
|
24
|
+
(fro/1/inf), `trace`, `det`, `inv`, `isPositiveDefinite`
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @tangent.to/lina # npm
|
|
30
|
+
deno add jsr:@tangent/lina # Deno / JSR
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The npm package includes TypeScript declarations, `llms.txt` and
|
|
34
|
+
`llms-full.txt`. In a notebook, `await help('solve')` shows the argument
|
|
35
|
+
and return types.
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
import { cholesky, eigSym, eigSymGeneralized, lstsq, solve, svd } from '@tangent.to/lina';
|
|
41
|
+
|
|
42
|
+
solve([[2, 1], [1, 3]], [3, 5]); // [0.8, 1.4]
|
|
43
|
+
|
|
44
|
+
const { values, vectors } = eigSym(covarianceMatrix); // PCA in two lines
|
|
45
|
+
const { U, s, V } = svd(dataMatrix);
|
|
46
|
+
|
|
47
|
+
const { x } = lstsq(designMatrix, y); // OLS coefficients
|
|
48
|
+
const L = cholesky(spdMatrix); // throws if not positive definite
|
|
49
|
+
|
|
50
|
+
// Discriminant axes: Sb x = λ Sw x, scipy's eigh(Sb, Sw) without the
|
|
51
|
+
// hard failure when Sw is rank-deficient
|
|
52
|
+
const { values: ratios, vectors: axes, definite } = eigSymGeneralized(Sb, Sw);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Validation against numpy/scipy
|
|
56
|
+
|
|
57
|
+
`tests_compare-to-scipy/` checks every operation against
|
|
58
|
+
`numpy.linalg`/`scipy.linalg` on seeded random matrices — solve/det/inv,
|
|
59
|
+
Cholesky (entrywise vs numpy), QR and SVD invariants, singular values and
|
|
60
|
+
symmetric eigenvalues vs numpy, the generalized symmetric eigenproblem vs
|
|
61
|
+
`scipy.linalg.eigh(A, B)` (eigenvalues, residual and B-orthonormality),
|
|
62
|
+
`invSqrtSym` vs `fractional_matrix_power(A, -1/2)`, lstsq, pinv (including
|
|
63
|
+
rank-deficient), rank and cond. Agreement is at machine precision (~1e-15).
|
|
64
|
+
Requires
|
|
65
|
+
[uv](https://docs.astral.sh/uv/) and Node:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm run test:scipy
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Scope
|
|
72
|
+
|
|
73
|
+
Dense, double-precision, textbook-modern algorithms sized for the suite's
|
|
74
|
+
workloads (covariance algebra, regression, ordination — n up to a few
|
|
75
|
+
hundred). Deliberately out of scope: sparse matrices, complex numbers,
|
|
76
|
+
general nonsymmetric eigenproblems (until a consumer needs them), and
|
|
77
|
+
BLAS-style micro-optimization — wasm kernels belong in tangent/nd when it
|
|
78
|
+
lands.
|
|
79
|
+
|
|
80
|
+
## Performance
|
|
81
|
+
|
|
82
|
+
lina favors simple, unconditionally-stable algorithms (one-sided Jacobi
|
|
83
|
+
SVD, cyclic Jacobi eigensolver) that hit machine precision on the suite's
|
|
84
|
+
target sizes. On `solve`/`inv` it is competitive with the mature
|
|
85
|
+
`ml-matrix`; on `matmul` and `svd` it is roughly 1.5–2× slower, and on
|
|
86
|
+
symmetric eigendecomposition about 5× slower, because those Jacobi methods
|
|
87
|
+
trade speed for simplicity. If a consumer ever profiles past this, the fix
|
|
88
|
+
is targeted, not a rewrite: swap the symmetric eigensolver for tridiagonal
|
|
89
|
+
QL and the SVD for Golub–Reinsch (the JAMA algorithms), and improve
|
|
90
|
+
`matmul` cache locality. Until then, correctness and a zero-dependency
|
|
91
|
+
footprint win. lina is not a general-purpose matrix library — for the rich
|
|
92
|
+
`Matrix` API (elementwise arithmetic, broadcasting, views, nonsymmetric
|
|
93
|
+
eigen), use `ml-matrix`; lina is the focused linear-algebra leaf the
|
|
94
|
+
tangent suite's own packages build on.
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT.
|
|
99
|
+
|
|
100
|
+
## Exports
|
|
101
|
+
|
|
102
|
+
- `help`: This package's documentation, for where the README is out of reach, such as a notebook, and for coding assistants.
|
|
103
|
+
- `version`: This package's version.
|
|
104
|
+
- `det`: Determinant via LU factorization (permutation sign times product of pivots).
|
|
105
|
+
- `inv`: Matrix inverse via solve(A, I).
|
|
106
|
+
- `lu`: LU factorization with partial pivoting: P A = L U.
|
|
107
|
+
- `luFactor`: LU factorization in packed flat storage, for callers that back-substitute many right-hand sides against one factorization on a hot path.
|
|
108
|
+
- `luFactorSolve`: Solve A x = b from a packed factorization returned by luFactor().
|
|
109
|
+
- `solve`: Solve A x = b via LU factorization with partial pivoting.
|
|
110
|
+
- `cholesky`: Cholesky factorization of a symmetric positive definite matrix.
|
|
111
|
+
- `choleskySolve`: Solve A x = b (or A X = B) given the Cholesky factor L of A (A = L L^T), by forward substitution (L y = b) then back substitution (L^T x = y).
|
|
112
|
+
- `isPositiveDefinite`: Test positive definiteness by attempting a Cholesky factorization.
|
|
113
|
+
- `lstsq`: Least-squares solution of A x ≈ b via reduced QR.
|
|
114
|
+
- `qr`: QR decomposition A = Q R via Householder reflections.
|
|
115
|
+
- `cond`: Condition number (2-norm): s_max / s_min.
|
|
116
|
+
- `pinv`: Moore-Penrose pseudoinverse via SVD, with numpy's default cutoff.
|
|
117
|
+
- `pinvSolve`: Minimum-norm least squares via the pseudoinverse (works for any rank).
|
|
118
|
+
- `rank`: Numerical rank via SVD.
|
|
119
|
+
- `svd`: Thin SVD: A = U diag(s) V^T with U m×k, s length k, V n×k, k = min(m, n).
|
|
120
|
+
- `eigSym`: Eigendecomposition of a symmetric matrix: A = V diag(values) V^T.
|
|
121
|
+
- `eigSymGeneralized`: Generalized symmetric eigenproblem A x = lambda B x, for symmetric A and symmetric positive (semi)definite B.
|
|
122
|
+
- `invSqrtSym`: Inverse square root of a symmetric positive semidefinite matrix: the symmetric W with W A W = I on A's range, and W = 0 on its null space.
|
|
123
|
+
- `diag`: Build a diagonal matrix from a vector, or extract the diagonal of a matrix.
|
|
124
|
+
- `identity`: Identity matrix of size n.
|
|
125
|
+
- `matmul`: Matrix product A B, or matrix-vector product A b.
|
|
126
|
+
- `norm`: Matrix or vector norm.
|
|
127
|
+
- `trace`: Sum of the diagonal of a square matrix.
|
|
128
|
+
- `transpose`: Matrix transpose.
|
|
129
|
+
|
|
130
|
+
Each export in full, with its types and options: https://cdn.jsdelivr.net/npm/@tangent.to/lina@0.1.5/llms-full.txt, or `await help('name')`
|
|
131
|
+
from the package itself.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangent.to/lina",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Linear algebra for JavaScript (ESM): LU, QR, Cholesky, SVD, symmetric eigen, least squares. scipy.linalg-validated.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"browser": "./dist/index.js",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
11
12
|
"import": "./dist/index.js",
|
|
12
13
|
"default": "./dist/index.js"
|
|
13
14
|
}
|
|
@@ -16,10 +17,13 @@
|
|
|
16
17
|
"src",
|
|
17
18
|
"dist",
|
|
18
19
|
"README.md",
|
|
19
|
-
"LICENSE"
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"types",
|
|
22
|
+
"llms.txt",
|
|
23
|
+
"llms-full.txt"
|
|
20
24
|
],
|
|
21
25
|
"scripts": {
|
|
22
|
-
"prepare": "npm run build:browser",
|
|
26
|
+
"prepare": "npm run build:browser && npm run build:docs",
|
|
23
27
|
"test": "vitest",
|
|
24
28
|
"test:run": "vitest run",
|
|
25
29
|
"test:scipy": "uv run --with scipy python3 tests_compare-to-scipy/compare_with_scipy.py",
|
|
@@ -27,7 +31,8 @@
|
|
|
27
31
|
"jsr:publish": "deno publish --allow-dirty",
|
|
28
32
|
"jsr:publish:dry-run": "deno publish --dry-run --allow-dirty",
|
|
29
33
|
"deno:check": "deno check src/index.js",
|
|
30
|
-
"version": "node scripts/sync-version.mjs && git add deno.json"
|
|
34
|
+
"version": "node scripts/sync-version.mjs && git add deno.json src/version.js",
|
|
35
|
+
"build:docs": "node scripts/docs.mjs"
|
|
31
36
|
},
|
|
32
37
|
"keywords": [
|
|
33
38
|
"linalg",
|
|
@@ -51,6 +56,8 @@
|
|
|
51
56
|
},
|
|
52
57
|
"devDependencies": {
|
|
53
58
|
"esbuild": "^0.25.11",
|
|
54
|
-
"vitest": "^1.6.0"
|
|
55
|
-
|
|
59
|
+
"vitest": "^1.6.0",
|
|
60
|
+
"typescript": "^6.0.3"
|
|
61
|
+
},
|
|
62
|
+
"types": "./types/index.d.ts"
|
|
56
63
|
}
|
package/src/help.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { version } from './version.js';
|
|
2
|
+
|
|
3
|
+
const PACKAGE = '@tangent.to/lina';
|
|
4
|
+
const REMOTE = `https://cdn.jsdelivr.net/npm/${PACKAGE}@${version}/llms-full.txt`;
|
|
5
|
+
|
|
6
|
+
let loading;
|
|
7
|
+
|
|
8
|
+
/** llms-full.txt: beside the package when it is on disk, else from the CDN at this version. */
|
|
9
|
+
async function read() {
|
|
10
|
+
// Not `new URL('…', import.meta.url)`, which bundlers would copy into the app.
|
|
11
|
+
const here = import.meta.url;
|
|
12
|
+
if (here.startsWith('file:')) {
|
|
13
|
+
const local = new URL('../llms-full.txt', here);
|
|
14
|
+
try {
|
|
15
|
+
if (globalThis.Deno) return await globalThis.Deno.readTextFile(local);
|
|
16
|
+
if (globalThis.process?.versions?.node) {
|
|
17
|
+
const moduleName = 'node:fs';
|
|
18
|
+
const fs = globalThis.process.getBuiltinModule?.(moduleName) ?? await import(moduleName);
|
|
19
|
+
return fs.readFileSync(local, 'utf8');
|
|
20
|
+
}
|
|
21
|
+
} catch {
|
|
22
|
+
// not built here: the published copy
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const res = await fetch(REMOTE);
|
|
26
|
+
if (!res.ok) throw new Error(`help: ${REMOTE} answered ${res.status}`);
|
|
27
|
+
return res.text();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The entry for `name` in an llms-full.txt, or the overview without a name.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
export function lookup(text, name) {
|
|
35
|
+
const at = text.indexOf('\n## Reference\n');
|
|
36
|
+
const overview = at < 0 ? text : text.slice(0, at);
|
|
37
|
+
if (name === undefined) return overview;
|
|
38
|
+
const entries = (at < 0 ? '' : text.slice(at)).split('\n### ').slice(1).map((s) => ({
|
|
39
|
+
name: s.slice(0, s.indexOf('\n')).trim(),
|
|
40
|
+
body: `### ${s.trimEnd()}`,
|
|
41
|
+
}));
|
|
42
|
+
const short = PACKAGE.split('/')[1];
|
|
43
|
+
const key = String(name).replace(new RegExp(`^${short}\\.`), '');
|
|
44
|
+
const exact = entries.filter((e) => e.name === key);
|
|
45
|
+
if (exact.length) return exact.map((e) => e.body).join('\n\n');
|
|
46
|
+
const k = key.toLowerCase();
|
|
47
|
+
const named = entries.filter((e) => e.name.toLowerCase() === k || e.name.toLowerCase().endsWith(`.${k}`));
|
|
48
|
+
if (named.length) return named.map((e) => e.body).join('\n\n');
|
|
49
|
+
// A method: the classes that declare it.
|
|
50
|
+
const method = new RegExp(`\\n\\s+(?:static\\s+|async\\s+|get\\s+)*${key.replace(/[^\w$]/g, '')}\\s*[(<]`);
|
|
51
|
+
const owners = key.match(/^[\w$]+$/) ? entries.filter((e) => method.test(e.body)) : [];
|
|
52
|
+
if (owners.length && owners.length <= 3) return owners.map((e) => e.body).join('\n\n');
|
|
53
|
+
if (owners.length) return `"${name}" is a method of ${owners.map((e) => e.name).join(', ')}: help('one of them').`;
|
|
54
|
+
const close = entries.filter((e) => e.name.toLowerCase().includes(k)).map((e) => e.name);
|
|
55
|
+
return `${PACKAGE} exports nothing named "${name}".`
|
|
56
|
+
+ (close.length ? ` Close: ${close.join(', ')}.` : ' help() lists every export.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* This package's documentation, for where the README is out of reach, such
|
|
61
|
+
* as a notebook, and for coding assistants.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} [name] - an export, such as `'solve'`
|
|
64
|
+
* @returns {Promise<string>} its declaration, options and examples; without
|
|
65
|
+
* `name`, the overview and a line per export
|
|
66
|
+
* @example
|
|
67
|
+
* await help(); // overview and index
|
|
68
|
+
* await help('solve'); // one export in full
|
|
69
|
+
*/
|
|
70
|
+
export async function help(name) {
|
|
71
|
+
loading ??= read().catch((e) => {
|
|
72
|
+
loading = undefined;
|
|
73
|
+
throw e;
|
|
74
|
+
});
|
|
75
|
+
return lookup(await loading, name);
|
|
76
|
+
}
|
package/src/index.js
CHANGED
package/src/version.js
ADDED
package/types/_mat.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal matrix representation.
|
|
3
|
+
*
|
|
4
|
+
* Public lina APIs accept and return row-major nested arrays
|
|
5
|
+
* (Array<Array<number>>) — the tangent suite's boundary currency.
|
|
6
|
+
* Internally, algorithms run on flat Float64Array storage for speed.
|
|
7
|
+
* This module is the only place that converts between the two.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Convert a nested row-major matrix to flat storage.
|
|
11
|
+
* Validates rectangularity and finite dimensions.
|
|
12
|
+
*
|
|
13
|
+
* @param {Array<Array<number>>} A - Nested matrix
|
|
14
|
+
* @param {string} [name='A'] - Argument name for error messages
|
|
15
|
+
* @returns {{data: Float64Array, m: number, n: number}}
|
|
16
|
+
*/
|
|
17
|
+
export function fromNested(A: Array<Array<number>>, name?: string): {
|
|
18
|
+
data: Float64Array;
|
|
19
|
+
m: number;
|
|
20
|
+
n: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Validate a nested matrix's shape without copying it, for kernels that read
|
|
24
|
+
* the caller's rows directly instead of converting to flat storage.
|
|
25
|
+
*
|
|
26
|
+
* @param {Array<Array<number>>} A - Nested matrix
|
|
27
|
+
* @param {string} [name='A'] - Argument name for error messages
|
|
28
|
+
* @returns {{m: number, n: number}}
|
|
29
|
+
*/
|
|
30
|
+
export function shapeOfNested(A: Array<Array<number>>, name?: string): {
|
|
31
|
+
m: number;
|
|
32
|
+
n: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Convert flat storage back to a nested row-major matrix.
|
|
36
|
+
*
|
|
37
|
+
* @param {Float64Array|Array<number>} data - Flat row-major values
|
|
38
|
+
* @param {number} m - Rows
|
|
39
|
+
* @param {number} n - Columns
|
|
40
|
+
* @returns {Array<Array<number>>}
|
|
41
|
+
*/
|
|
42
|
+
export function toNested(data: Float64Array | Array<number>, m: number, n: number): Array<Array<number>>;
|
|
43
|
+
/**
|
|
44
|
+
* Validate a vector argument and copy it to a Float64Array.
|
|
45
|
+
*
|
|
46
|
+
* @param {Array<number>} b - Vector
|
|
47
|
+
* @param {number} expected - Required length
|
|
48
|
+
* @param {string} [name='b'] - Argument name for error messages
|
|
49
|
+
* @returns {Float64Array}
|
|
50
|
+
*/
|
|
51
|
+
export function vecFrom(b: Array<number>, expected: number, name?: string): Float64Array;
|
|
52
|
+
/**
|
|
53
|
+
* Check symmetry to a tolerance; throws when violated.
|
|
54
|
+
*
|
|
55
|
+
* @param {{data: Float64Array, m: number, n: number}} M - Flat matrix
|
|
56
|
+
* @param {string} caller - Function name for the error message
|
|
57
|
+
* @param {number} [tol=1e-10] - Relative tolerance
|
|
58
|
+
*/
|
|
59
|
+
export function assertSymmetric(M: {
|
|
60
|
+
data: Float64Array;
|
|
61
|
+
m: number;
|
|
62
|
+
n: number;
|
|
63
|
+
}, caller: string, tol?: number): void;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cholesky factorization of a symmetric positive definite matrix.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array<Array<number>>} A - Symmetric positive definite nested matrix
|
|
5
|
+
* @returns {Array<Array<number>>} Lower triangular L with A = L L^T
|
|
6
|
+
* @throws {Error} When A is not symmetric, or a diagonal pivot is <= 0
|
|
7
|
+
* (not positive definite)
|
|
8
|
+
*/
|
|
9
|
+
export function cholesky(A: Array<Array<number>>): Array<Array<number>>;
|
|
10
|
+
/**
|
|
11
|
+
* Cholesky factorization on flat storage, for callers that already hold a
|
|
12
|
+
* validated square matrix and want to avoid the nested round trip.
|
|
13
|
+
*
|
|
14
|
+
* @param {{data: Float64Array, n: number}} M - Square matrix from fromNested()
|
|
15
|
+
* @param {string} caller - Function name for error messages
|
|
16
|
+
* @returns {Float64Array} Row-major n*n lower triangular L with A = L L^T
|
|
17
|
+
* @throws {Error} When a diagonal pivot is <= 0 (not positive definite)
|
|
18
|
+
*/
|
|
19
|
+
export function choleskyFlat(M: {
|
|
20
|
+
data: Float64Array;
|
|
21
|
+
n: number;
|
|
22
|
+
}, caller?: string): Float64Array;
|
|
23
|
+
/**
|
|
24
|
+
* Forward substitution L X = B for lower triangular L, on flat storage.
|
|
25
|
+
*
|
|
26
|
+
* @param {Float64Array} L - Row-major n*n lower triangular factor
|
|
27
|
+
* @param {number} n - Dimension of L
|
|
28
|
+
* @param {Float64Array} B - Row-major n*k right-hand side (not modified)
|
|
29
|
+
* @param {number} k - Number of right-hand side columns
|
|
30
|
+
* @returns {Float64Array} Row-major n*k solution X
|
|
31
|
+
*/
|
|
32
|
+
export function solveLowerFlat(L: Float64Array, n: number, B: Float64Array, k: number): Float64Array;
|
|
33
|
+
/**
|
|
34
|
+
* Back substitution L^T X = B for lower triangular L, on flat storage.
|
|
35
|
+
*
|
|
36
|
+
* @param {Float64Array} L - Row-major n*n lower triangular factor
|
|
37
|
+
* @param {number} n - Dimension of L
|
|
38
|
+
* @param {Float64Array} B - Row-major n*k right-hand side (not modified)
|
|
39
|
+
* @param {number} k - Number of right-hand side columns
|
|
40
|
+
* @returns {Float64Array} Row-major n*k solution X
|
|
41
|
+
*/
|
|
42
|
+
export function solveLowerTransposeFlat(L: Float64Array, n: number, B: Float64Array, k: number): Float64Array;
|
|
43
|
+
/**
|
|
44
|
+
* Solve A x = b (or A X = B) given the Cholesky factor L of A (A = L L^T), by
|
|
45
|
+
* forward substitution (L y = b) then back substitution (L^T x = y).
|
|
46
|
+
*
|
|
47
|
+
* Accepts either a single right-hand side vector or a matrix of them. Passing
|
|
48
|
+
* the whole set at once matters: the alternative — calling this once per
|
|
49
|
+
* column — repeats the triangular walk's setup per column, which turns
|
|
50
|
+
* building an inverse into markedly more work than it needs. Measured on a
|
|
51
|
+
* 340x340 factor, one call with 340 right-hand sides against 340 single-vector
|
|
52
|
+
* calls: 76 ms against 371 ms.
|
|
53
|
+
*
|
|
54
|
+
* Reads the factor's nested rows directly rather than copying it to flat
|
|
55
|
+
* storage, and divides rather than multiplying by a reciprocal so the scaling
|
|
56
|
+
* step stays exact. The two paths accumulate in different orders — a scalar
|
|
57
|
+
* per element for one right-hand side, an axpy across the row for many — so
|
|
58
|
+
* they agree to roundoff (~1e-19 relative on the sizes measured) rather than
|
|
59
|
+
* bit for bit.
|
|
60
|
+
*
|
|
61
|
+
* @param {Array<Array<number>>} L - Lower triangular factor from cholesky()
|
|
62
|
+
* @param {Array<number>|Array<Array<number>>} b - Right-hand side vector, or
|
|
63
|
+
* an n x k matrix of right-hand sides
|
|
64
|
+
* @returns {Array<number>|Array<Array<number>>} Solution, matching the shape
|
|
65
|
+
* of `b`
|
|
66
|
+
*/
|
|
67
|
+
export function choleskySolve(L: Array<Array<number>>, b: Array<number> | Array<Array<number>>): Array<number> | Array<Array<number>>;
|
|
68
|
+
/**
|
|
69
|
+
* Test positive definiteness by attempting a Cholesky factorization.
|
|
70
|
+
* Never throws; non-symmetric or malformed input returns false.
|
|
71
|
+
*
|
|
72
|
+
* @param {Array<Array<number>>} A - Nested matrix
|
|
73
|
+
* @returns {boolean}
|
|
74
|
+
*/
|
|
75
|
+
export function isPositiveDefinite(A: Array<Array<number>>): boolean;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Eigendecomposition of a symmetric matrix: A = V diag(values) V^T.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array<Array<number>>} A - Symmetric matrix (validated to 1e-10)
|
|
5
|
+
* @param {Object} [options]
|
|
6
|
+
* @param {number} [options.maxSweeps=60] - Maximum Jacobi sweeps
|
|
7
|
+
* @param {number} [options.tol=1e-14] - Off-diagonal convergence tolerance,
|
|
8
|
+
* relative to the Frobenius norm of the diagonal
|
|
9
|
+
* @returns {{values: Array<number>, vectors: Array<Array<number>>}}
|
|
10
|
+
* values[i] descending; vectors' column i is the eigenvector for values[i]
|
|
11
|
+
*/
|
|
12
|
+
export function eigSym(A: Array<Array<number>>, options?: {
|
|
13
|
+
maxSweeps?: number | undefined;
|
|
14
|
+
tol?: number | undefined;
|
|
15
|
+
}): {
|
|
16
|
+
values: Array<number>;
|
|
17
|
+
vectors: Array<Array<number>>;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Inverse square root of a symmetric positive semidefinite matrix:
|
|
21
|
+
* the symmetric W with W A W = I on A's range, and W = 0 on its null space.
|
|
22
|
+
*
|
|
23
|
+
* @param {Array<Array<number>>} A - Symmetric positive semidefinite matrix
|
|
24
|
+
* @param {Object} [options]
|
|
25
|
+
* @param {number} [options.rcond] - Relative eigenvalue cutoff below which a
|
|
26
|
+
* direction is treated as null; default n * eps
|
|
27
|
+
* @returns {Array<Array<number>>} Symmetric n x n inverse square root
|
|
28
|
+
* @throws {Error} When A is not symmetric or has a clearly negative eigenvalue
|
|
29
|
+
*/
|
|
30
|
+
export function invSqrtSym(A: Array<Array<number>>, options?: {
|
|
31
|
+
rcond?: number | undefined;
|
|
32
|
+
}): Array<Array<number>>;
|
|
33
|
+
/**
|
|
34
|
+
* Generalized symmetric eigenproblem A x = lambda B x, for symmetric A and
|
|
35
|
+
* symmetric positive (semi)definite B.
|
|
36
|
+
*
|
|
37
|
+
* When B is positive definite this is the textbook Cholesky reduction:
|
|
38
|
+
* B = L L^T, eigendecompose C = L^-1 A L^-T, then x = L^-T y. The returned
|
|
39
|
+
* eigenvectors are B-orthonormal (x^T B x = 1), matching LAPACK/scipy's
|
|
40
|
+
* `eigh(A, B)`.
|
|
41
|
+
*
|
|
42
|
+
* When B is only semidefinite the Cholesky factorization does not exist —
|
|
43
|
+
* scipy raises here — so the reduction falls back to B's truncated inverse
|
|
44
|
+
* square root (see invSqrtSym). Note what this solves: with P the orthogonal
|
|
45
|
+
* projector onto range(B), the returned pairs satisfy
|
|
46
|
+
*
|
|
47
|
+
* P A x = lambda B x
|
|
48
|
+
*
|
|
49
|
+
* the eigenproblem of A restricted to range(B), which is the most that is
|
|
50
|
+
* defined when B is singular — off that range A x = lambda B x generally has
|
|
51
|
+
* no solution at all. Null directions come back as zero eigenvectors with
|
|
52
|
+
* zero eigenvalues, and the remaining vectors are scaled to unit euclidean
|
|
53
|
+
* length, since B-orthonormality is undefined once B is singular. `definite`
|
|
54
|
+
* reports which route ran, so callers needing scipy's strictness can check it
|
|
55
|
+
* (or pass `strict`).
|
|
56
|
+
*
|
|
57
|
+
* @param {Array<Array<number>>} A - Symmetric matrix
|
|
58
|
+
* @param {Array<Array<number>>} B - Symmetric positive (semi)definite matrix
|
|
59
|
+
* @param {Object} [options]
|
|
60
|
+
* @param {boolean} [options.strict=false] - Throw instead of falling back when
|
|
61
|
+
* B is not positive definite
|
|
62
|
+
* @param {number} [options.rcond] - Relative eigenvalue cutoff for the
|
|
63
|
+
* semidefinite fallback; default n * eps
|
|
64
|
+
* @returns {{values: Array<number>, vectors: Array<Array<number>>, definite: boolean}}
|
|
65
|
+
* values[i] descending; vectors' column i is the eigenvector for values[i]
|
|
66
|
+
* @throws {Error} When A or B is not symmetric, sizes disagree, or `strict`
|
|
67
|
+
* is set and B is not positive definite
|
|
68
|
+
*/
|
|
69
|
+
export function eigSymGeneralized(A: Array<Array<number>>, B: Array<Array<number>>, options?: {
|
|
70
|
+
strict?: boolean | undefined;
|
|
71
|
+
rcond?: number | undefined;
|
|
72
|
+
}): {
|
|
73
|
+
values: Array<number>;
|
|
74
|
+
vectors: Array<Array<number>>;
|
|
75
|
+
definite: boolean;
|
|
76
|
+
};
|
package/types/help.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry for `name` in an llms-full.txt, or the overview without a name.
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export function lookup(text: any, name: any): any;
|
|
6
|
+
/**
|
|
7
|
+
* This package's documentation, for where the README is out of reach, such
|
|
8
|
+
* as a notebook, and for coding assistants.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} [name] - an export, such as `'solve'`
|
|
11
|
+
* @returns {Promise<string>} its declaration, options and examples; without
|
|
12
|
+
* `name`, the overview and a line per export
|
|
13
|
+
* @example
|
|
14
|
+
* await help(); // overview and index
|
|
15
|
+
* await help('solve'); // one export in full
|
|
16
|
+
*/
|
|
17
|
+
export function help(name?: string): Promise<string>;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
declare namespace _default {
|
|
2
|
+
export { lu };
|
|
3
|
+
export { luFactor };
|
|
4
|
+
export { luFactorSolve };
|
|
5
|
+
export { solve };
|
|
6
|
+
export { det };
|
|
7
|
+
export { inv };
|
|
8
|
+
export { cholesky };
|
|
9
|
+
export { choleskySolve };
|
|
10
|
+
export { isPositiveDefinite };
|
|
11
|
+
export { qr };
|
|
12
|
+
export { lstsq };
|
|
13
|
+
export { svd };
|
|
14
|
+
export { pinv };
|
|
15
|
+
export { pinvSolve };
|
|
16
|
+
export { rank };
|
|
17
|
+
export { cond };
|
|
18
|
+
export { eigSym };
|
|
19
|
+
export { eigSymGeneralized };
|
|
20
|
+
export { invSqrtSym };
|
|
21
|
+
export { matmul };
|
|
22
|
+
export { transpose };
|
|
23
|
+
export { identity };
|
|
24
|
+
export { diag };
|
|
25
|
+
export { norm };
|
|
26
|
+
export { trace };
|
|
27
|
+
}
|
|
28
|
+
export default _default;
|
|
29
|
+
export { help } from "./help.js";
|
|
30
|
+
export { version } from "./version.js";
|
|
31
|
+
import { lu } from './lu.js';
|
|
32
|
+
import { luFactor } from './lu.js';
|
|
33
|
+
import { luFactorSolve } from './lu.js';
|
|
34
|
+
import { solve } from './lu.js';
|
|
35
|
+
import { det } from './lu.js';
|
|
36
|
+
import { inv } from './lu.js';
|
|
37
|
+
import { cholesky } from './cholesky.js';
|
|
38
|
+
import { choleskySolve } from './cholesky.js';
|
|
39
|
+
import { isPositiveDefinite } from './cholesky.js';
|
|
40
|
+
import { qr } from './qr.js';
|
|
41
|
+
import { lstsq } from './qr.js';
|
|
42
|
+
import { svd } from './svd.js';
|
|
43
|
+
import { pinv } from './svd.js';
|
|
44
|
+
import { pinvSolve } from './svd.js';
|
|
45
|
+
import { rank } from './svd.js';
|
|
46
|
+
import { cond } from './svd.js';
|
|
47
|
+
import { eigSym } from './eigsym.js';
|
|
48
|
+
import { eigSymGeneralized } from './eigsym.js';
|
|
49
|
+
import { invSqrtSym } from './eigsym.js';
|
|
50
|
+
import { matmul } from './ops.js';
|
|
51
|
+
import { transpose } from './ops.js';
|
|
52
|
+
import { identity } from './ops.js';
|
|
53
|
+
import { diag } from './ops.js';
|
|
54
|
+
import { norm } from './ops.js';
|
|
55
|
+
import { trace } from './ops.js';
|
|
56
|
+
export { det, inv, lu, luFactor, luFactorSolve, solve } from "./lu.js";
|
|
57
|
+
export { cholesky, choleskySolve, isPositiveDefinite } from "./cholesky.js";
|
|
58
|
+
export { lstsq, qr } from "./qr.js";
|
|
59
|
+
export { cond, pinv, pinvSolve, rank, svd } from "./svd.js";
|
|
60
|
+
export { eigSym, eigSymGeneralized, invSqrtSym } from "./eigsym.js";
|
|
61
|
+
export { diag, identity, matmul, norm, trace, transpose } from "./ops.js";
|
package/types/lu.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LU factorization with partial pivoting: P A = L U.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array<Array<number>>} A - Square nested matrix
|
|
5
|
+
* @returns {{L: Array<Array<number>>, U: Array<Array<number>>, P: Array<Array<number>>}}
|
|
6
|
+
* L unit lower triangular, U upper triangular, P a permutation matrix.
|
|
7
|
+
*/
|
|
8
|
+
export function lu(A: Array<Array<number>>): {
|
|
9
|
+
L: Array<Array<number>>;
|
|
10
|
+
U: Array<Array<number>>;
|
|
11
|
+
P: Array<Array<number>>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* LU factorization in packed flat storage, for callers that back-substitute
|
|
15
|
+
* many right-hand sides against one factorization on a hot path.
|
|
16
|
+
*
|
|
17
|
+
* Unlike lu(), this skips building the nested L, U and dense permutation
|
|
18
|
+
* matrix P: it returns the combined LU array directly (unit-diagonal L
|
|
19
|
+
* strictly below the diagonal, U on and above, in row-major n*n storage)
|
|
20
|
+
* together with the permutation vector, so no nested round-trip or
|
|
21
|
+
* permutation-matrix scan is needed. Pair it with luFactorSolve().
|
|
22
|
+
*
|
|
23
|
+
* @param {Array<Array<number>>} A - Square nested matrix
|
|
24
|
+
* @returns {{lu: Float64Array, perm: Int32Array, n: number, sign: number, singular: boolean}}
|
|
25
|
+
* `lu` is the combined LU storage (row-major n*n); `perm` maps factored row k
|
|
26
|
+
* to input row perm[k]; `sign` is the permutation sign; `singular` flags a
|
|
27
|
+
* pivot at or below 1e-13 * maxAbs(A).
|
|
28
|
+
*/
|
|
29
|
+
export function luFactor(A: Array<Array<number>>): {
|
|
30
|
+
lu: Float64Array;
|
|
31
|
+
perm: Int32Array;
|
|
32
|
+
n: number;
|
|
33
|
+
sign: number;
|
|
34
|
+
singular: boolean;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Solve A x = b from a packed factorization returned by luFactor().
|
|
38
|
+
*
|
|
39
|
+
* @param {{lu: Float64Array, perm: Int32Array, n: number}} fac - luFactor() result
|
|
40
|
+
* @param {Array<number>|Float64Array} b - Right-hand side (length n, not modified)
|
|
41
|
+
* @returns {Float64Array} Solution vector x
|
|
42
|
+
*/
|
|
43
|
+
export function luFactorSolve(fac: {
|
|
44
|
+
lu: Float64Array;
|
|
45
|
+
perm: Int32Array;
|
|
46
|
+
n: number;
|
|
47
|
+
}, b: Array<number> | Float64Array): Float64Array;
|
|
48
|
+
/**
|
|
49
|
+
* Solve A x = b via LU factorization with partial pivoting.
|
|
50
|
+
*
|
|
51
|
+
* @param {Array<Array<number>>} A - Square nested matrix
|
|
52
|
+
* @param {Array<number>|Array<Array<number>>} b - Right-hand side vector (length n)
|
|
53
|
+
* or nested matrix of right-hand sides (n x k, solved column by column)
|
|
54
|
+
* @returns {Array<number>|Array<Array<number>>} Solution vector, or nested n x k matrix
|
|
55
|
+
*/
|
|
56
|
+
export function solve(A: Array<Array<number>>, b: Array<number> | Array<Array<number>>): Array<number> | Array<Array<number>>;
|
|
57
|
+
/**
|
|
58
|
+
* Determinant via LU factorization (permutation sign times product of pivots).
|
|
59
|
+
* Returns 0 for singular matrices instead of throwing.
|
|
60
|
+
*
|
|
61
|
+
* @param {Array<Array<number>>} A - Square nested matrix
|
|
62
|
+
* @returns {number}
|
|
63
|
+
*/
|
|
64
|
+
export function det(A: Array<Array<number>>): number;
|
|
65
|
+
/**
|
|
66
|
+
* Matrix inverse via solve(A, I).
|
|
67
|
+
*
|
|
68
|
+
* @param {Array<Array<number>>} A - Square nested matrix
|
|
69
|
+
* @returns {Array<Array<number>>}
|
|
70
|
+
*/
|
|
71
|
+
export function inv(A: Array<Array<number>>): Array<Array<number>>;
|