@danielsimonjr/mathts-tensor 0.1.3 → 0.2.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/dist/index.d.ts +17 -17
- package/dist/index.js +38 -22
- package/package.json +57 -57
package/dist/index.d.ts
CHANGED
|
@@ -289,17 +289,16 @@ interface TensorSvdResult {
|
|
|
289
289
|
truncationError: number;
|
|
290
290
|
}
|
|
291
291
|
/**
|
|
292
|
-
*
|
|
293
|
-
* axes into "row" and "col" groups.
|
|
294
|
-
*
|
|
295
|
-
* @param t - Input tensor of any rank ≥ 1.
|
|
296
|
-
* @param rowAxes - Axis indices (integers) whose dimensions form the row
|
|
297
|
-
* dimension of the effective 2-D matrix. The remaining axes
|
|
298
|
-
* form the column dimension. The order of `rowAxes` controls
|
|
299
|
-
* the layout of the U output tensor.
|
|
300
|
-
* @param opts - Optional truncation / naming settings.
|
|
292
|
+
* Truncated tensor SVD using the pure-JS DenseMatrix SVD primitive (synchronous).
|
|
301
293
|
*/
|
|
302
294
|
declare function tensorSvd(t: Tensor, rowAxes: ReadonlyArray<number>, opts?: TensorSvdOpts): TensorSvdResult;
|
|
295
|
+
/**
|
|
296
|
+
* Truncated tensor SVD routed through the AssemblyScript WASM SVD kernel
|
|
297
|
+
* (`svdWasm`) — the same result as {@link tensorSvd} but accelerated for large
|
|
298
|
+
* matricisations. Async because WASM instantiation is async; falls back to JS
|
|
299
|
+
* inside `svdWasm` when no binary is available.
|
|
300
|
+
*/
|
|
301
|
+
declare function tensorSvdWasm(t: Tensor, rowAxes: ReadonlyArray<number>, opts?: TensorSvdOpts): Promise<TensorSvdResult>;
|
|
303
302
|
|
|
304
303
|
/**
|
|
305
304
|
* randomTensor — construct a Tensor filled with pseudo-random values.
|
|
@@ -637,15 +636,16 @@ interface TensorEigResult {
|
|
|
637
636
|
eigenvaluesImaginary?: Tensor;
|
|
638
637
|
}
|
|
639
638
|
/**
|
|
640
|
-
*
|
|
641
|
-
* into "row" and "col" groups. The reshaped 2-D matrix must be square.
|
|
642
|
-
*
|
|
643
|
-
* @param t - Input tensor of any rank ≥ 1.
|
|
644
|
-
* @param rowAxes - Axis indices forming the row dimension of the effective
|
|
645
|
-
* 2-D matrix. The remaining axes form the column dimension.
|
|
646
|
-
* @param opts - Optional symmetric hint + computeVectors flag.
|
|
639
|
+
* Tensor eigendecomposition using the pure-JS matrix eig primitive (synchronous).
|
|
647
640
|
*/
|
|
648
641
|
declare function tensorEig(t: Tensor, rowAxes: ReadonlyArray<number>, opts?: TensorEigOpts): TensorEigResult;
|
|
642
|
+
/**
|
|
643
|
+
* Tensor eigendecomposition routed through the AssemblyScript WASM eig kernel
|
|
644
|
+
* (`eigWasm`) — same result as {@link tensorEig}, accelerated for large
|
|
645
|
+
* matricisations. Async (WASM instantiation); `eigWasm` falls back to JS when no
|
|
646
|
+
* binary is available.
|
|
647
|
+
*/
|
|
648
|
+
declare function tensorEigWasm(t: Tensor, rowAxes: ReadonlyArray<number>, opts?: TensorEigOpts): Promise<TensorEigResult>;
|
|
649
649
|
|
|
650
650
|
/**
|
|
651
651
|
* tensorPinv — Moore-Penrose pseudoinverse for rank-N tensors.
|
|
@@ -1119,4 +1119,4 @@ declare function roll(t: Tensor, shifts: number | readonly number[], axes?: read
|
|
|
1119
1119
|
*/
|
|
1120
1120
|
declare function flip(t: Tensor, axes: readonly number[]): Tensor;
|
|
1121
1121
|
|
|
1122
|
-
export { type ContractNetworkOpts, type ContractNetworkResult, type EinsumSpec, Index, type IndexOpts, type NestedArray, type PadOptions, type RandomTensorOpts, type ScatterOpts, type SliceRange, type StackOpts, Tensor, type TensorCholeskyOpts, type TensorCholeskyResult, type TensorEigOpts, type TensorEigResult, type TensorKronOpts, type TensorLUOpts, type TensorLUResult, type TensorPinvOpts, type TensorQrOpts, type TensorQrResult, type TensorSolveOpts, type TensorSolveResult, type TensorSvdOpts, type TensorSvdResult, concatenate, contractNetwork, flip, gather, idx, pad, randomTensor, roll, scatter, slice, stack, tensorCholesky, tensorEig, tensorKron, tensorLU, tensorPinv, tensorQr, tensorSolve, tensorSvd };
|
|
1122
|
+
export { type ContractNetworkOpts, type ContractNetworkResult, type EinsumSpec, Index, type IndexOpts, type NestedArray, type PadOptions, type RandomTensorOpts, type ScatterOpts, type SliceRange, type StackOpts, Tensor, type TensorCholeskyOpts, type TensorCholeskyResult, type TensorEigOpts, type TensorEigResult, type TensorKronOpts, type TensorLUOpts, type TensorLUResult, type TensorPinvOpts, type TensorQrOpts, type TensorQrResult, type TensorSolveOpts, type TensorSolveResult, type TensorSvdOpts, type TensorSvdResult, concatenate, contractNetwork, flip, gather, idx, pad, randomTensor, roll, scatter, slice, stack, tensorCholesky, tensorEig, tensorEigWasm, tensorKron, tensorLU, tensorPinv, tensorQr, tensorSolve, tensorSvd, tensorSvdWasm };
|
package/dist/index.js
CHANGED
|
@@ -813,8 +813,8 @@ function idx(dim, name, opts) {
|
|
|
813
813
|
}
|
|
814
814
|
|
|
815
815
|
// src/operations/svd.ts
|
|
816
|
-
import { svd } from "@danielsimonjr/mathts-matrix";
|
|
817
|
-
function
|
|
816
|
+
import { svd, svdWasm } from "@danielsimonjr/mathts-matrix";
|
|
817
|
+
function prepareSvdMatrix(t, rowAxes) {
|
|
818
818
|
const rank = t.shape.length;
|
|
819
819
|
for (const ax of rowAxes) {
|
|
820
820
|
if (!Number.isInteger(ax) || ax < 0 || ax >= rank) {
|
|
@@ -844,7 +844,10 @@ function tensorSvd(t, rowAxes, opts) {
|
|
|
844
844
|
{ length: numRows },
|
|
845
845
|
(_, i) => Array.from({ length: numCols }, (_2, j) => permuted.data[i * numCols + j])
|
|
846
846
|
);
|
|
847
|
-
|
|
847
|
+
return { matrix, rowDims, colDims, numRows, numCols };
|
|
848
|
+
}
|
|
849
|
+
function finishSvd(result, prep, opts) {
|
|
850
|
+
const { rowDims, colDims, numRows, numCols } = prep;
|
|
848
851
|
const { U: fullU, S: allS, V: fullV } = result;
|
|
849
852
|
const cutoff = opts?.cutoff ?? 0;
|
|
850
853
|
const maxdim = opts?.maxdim;
|
|
@@ -885,6 +888,14 @@ function tensorSvd(t, rowAxes, opts) {
|
|
|
885
888
|
truncationError
|
|
886
889
|
};
|
|
887
890
|
}
|
|
891
|
+
function tensorSvd(t, rowAxes, opts) {
|
|
892
|
+
const prep = prepareSvdMatrix(t, rowAxes);
|
|
893
|
+
return finishSvd(svd(prep.matrix, { tolerance: 1e-12 }), prep, opts);
|
|
894
|
+
}
|
|
895
|
+
async function tensorSvdWasm(t, rowAxes, opts) {
|
|
896
|
+
const prep = prepareSvdMatrix(t, rowAxes);
|
|
897
|
+
return finishSvd(await svdWasm(prep.matrix, { tolerance: 1e-12 }), prep, opts);
|
|
898
|
+
}
|
|
888
899
|
|
|
889
900
|
// src/operations/random.ts
|
|
890
901
|
import { DenseMatrix as DenseMatrix2, qr as matrixQr } from "@danielsimonjr/mathts-matrix";
|
|
@@ -1644,11 +1655,9 @@ function tensorCholesky(t, rowAxes, opts) {
|
|
|
1644
1655
|
}
|
|
1645
1656
|
|
|
1646
1657
|
// src/operations/eig.ts
|
|
1647
|
-
import { eig as matrixEig } from "@danielsimonjr/mathts-matrix";
|
|
1648
|
-
function
|
|
1658
|
+
import { eig as matrixEig, eigWasm } from "@danielsimonjr/mathts-matrix";
|
|
1659
|
+
function prepareEigMatrix(t, rowAxes, symmetricHint) {
|
|
1649
1660
|
const rank = t.shape.length;
|
|
1650
|
-
const computeVectors = opts?.computeVectors ?? true;
|
|
1651
|
-
const symmetricHint = opts?.symmetric ?? false;
|
|
1652
1661
|
for (const ax of rowAxes) {
|
|
1653
1662
|
if (!Number.isInteger(ax) || ax < 0 || ax >= rank) {
|
|
1654
1663
|
throw new RangeError(
|
|
@@ -1693,7 +1702,10 @@ function tensorEig(t, rowAxes, opts) {
|
|
|
1693
1702
|
}
|
|
1694
1703
|
}
|
|
1695
1704
|
}
|
|
1696
|
-
|
|
1705
|
+
return { matrix2D, n };
|
|
1706
|
+
}
|
|
1707
|
+
function finishEig(eigResult, n, computeVectors, symmetricHint) {
|
|
1708
|
+
const { values, vectors } = eigResult;
|
|
1697
1709
|
const reData = new Float64Array(n);
|
|
1698
1710
|
const imData = new Float64Array(n);
|
|
1699
1711
|
let anyImaginary = false;
|
|
@@ -1719,6 +1731,18 @@ function tensorEig(t, rowAxes, opts) {
|
|
|
1719
1731
|
}
|
|
1720
1732
|
return result;
|
|
1721
1733
|
}
|
|
1734
|
+
function tensorEig(t, rowAxes, opts) {
|
|
1735
|
+
const computeVectors = opts?.computeVectors ?? true;
|
|
1736
|
+
const symmetricHint = opts?.symmetric ?? false;
|
|
1737
|
+
const { matrix2D, n } = prepareEigMatrix(t, rowAxes, symmetricHint);
|
|
1738
|
+
return finishEig(matrixEig(matrix2D, { computeVectors }), n, computeVectors, symmetricHint);
|
|
1739
|
+
}
|
|
1740
|
+
async function tensorEigWasm(t, rowAxes, opts) {
|
|
1741
|
+
const computeVectors = opts?.computeVectors ?? true;
|
|
1742
|
+
const symmetricHint = opts?.symmetric ?? false;
|
|
1743
|
+
const { matrix2D, n } = prepareEigMatrix(t, rowAxes, symmetricHint);
|
|
1744
|
+
return finishEig(await eigWasm(matrix2D, { computeVectors }), n, computeVectors, symmetricHint);
|
|
1745
|
+
}
|
|
1722
1746
|
|
|
1723
1747
|
// src/operations/pinv.ts
|
|
1724
1748
|
import { svd as svd2 } from "@danielsimonjr/mathts-matrix";
|
|
@@ -1992,11 +2016,11 @@ function tensorKron(a, b, opts) {
|
|
|
1992
2016
|
}
|
|
1993
2017
|
const outShape = aShape.map((d, k) => d * bShape[k]);
|
|
1994
2018
|
const outSize = outShape.reduce((acc, d) => acc * d, 1);
|
|
1995
|
-
const outStrides = rowMajorStrides(outShape);
|
|
2019
|
+
const outStrides = Tensor.rowMajorStrides(outShape);
|
|
1996
2020
|
const aData = a.data;
|
|
1997
2021
|
const bData = b.data;
|
|
1998
|
-
const aOrigStrides = rowMajorStrides(aShape.slice(outRank - rankA));
|
|
1999
|
-
const bOrigStrides = rowMajorStrides(bShape.slice(outRank - rankB));
|
|
2022
|
+
const aOrigStrides = Tensor.rowMajorStrides(aShape.slice(outRank - rankA));
|
|
2023
|
+
const bOrigStrides = Tensor.rowMajorStrides(bShape.slice(outRank - rankB));
|
|
2000
2024
|
const result = new Float64Array(outSize);
|
|
2001
2025
|
const outIdx = new Array(outRank).fill(0);
|
|
2002
2026
|
for (let flatOut = 0; flatOut < outSize; flatOut++) {
|
|
@@ -2035,16 +2059,6 @@ function tensorKron(a, b, opts) {
|
|
|
2035
2059
|
}
|
|
2036
2060
|
return new Tensor(outShape, result, outLabels);
|
|
2037
2061
|
}
|
|
2038
|
-
function rowMajorStrides(shape) {
|
|
2039
|
-
const rank = shape.length;
|
|
2040
|
-
const strides = new Array(rank);
|
|
2041
|
-
let acc = 1;
|
|
2042
|
-
for (let k = rank - 1; k >= 0; k--) {
|
|
2043
|
-
strides[k] = acc;
|
|
2044
|
-
acc *= shape[k];
|
|
2045
|
-
}
|
|
2046
|
-
return strides;
|
|
2047
|
-
}
|
|
2048
2062
|
|
|
2049
2063
|
// src/operations/slice.ts
|
|
2050
2064
|
function slice(t, ranges) {
|
|
@@ -2542,10 +2556,12 @@ export {
|
|
|
2542
2556
|
stack,
|
|
2543
2557
|
tensorCholesky,
|
|
2544
2558
|
tensorEig,
|
|
2559
|
+
tensorEigWasm,
|
|
2545
2560
|
tensorKron,
|
|
2546
2561
|
tensorLU,
|
|
2547
2562
|
tensorPinv,
|
|
2548
2563
|
tensorQr,
|
|
2549
2564
|
tensorSolve,
|
|
2550
|
-
tensorSvd
|
|
2565
|
+
tensorSvd,
|
|
2566
|
+
tensorSvdWasm
|
|
2551
2567
|
};
|
package/package.json
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@danielsimonjr/mathts-tensor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Rank-N dense Tensor type for MathTS with Float64Array-backed storage",
|
|
5
|
-
"author": "Daniel Simon Jr.",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"main": "./dist/index.js",
|
|
9
|
-
"module": "./dist/index.js",
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"import": "./dist/index.js",
|
|
14
|
-
"types": "./dist/index.d.ts"
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist",
|
|
19
|
-
"README.md"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
23
|
-
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
24
|
-
"test": "vitest run",
|
|
25
|
-
"test:watch": "vitest",
|
|
26
|
-
"test:coverage": "vitest run --coverage",
|
|
27
|
-
"typecheck": "tsc --noEmit",
|
|
28
|
-
"lint": "eslint src --ext .ts",
|
|
29
|
-
"lint:fix": "eslint src --ext .ts --fix",
|
|
30
|
-
"clean": "rm -rf dist",
|
|
31
|
-
"build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"@danielsimonjr/mathts-core": "^0.1.4",
|
|
35
|
-
"@danielsimonjr/mathts-matrix": "^0.1.
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@types/node": "^25.5.2",
|
|
39
|
-
"tsup": "^8.0.0",
|
|
40
|
-
"typescript": "^5.3.0",
|
|
41
|
-
"vitest": "^4.1.5"
|
|
42
|
-
},
|
|
43
|
-
"publishConfig": {
|
|
44
|
-
"access": "public"
|
|
45
|
-
},
|
|
46
|
-
"repository": {
|
|
47
|
-
"type": "git",
|
|
48
|
-
"url": "https://github.com/danielsimonjr/mathts",
|
|
49
|
-
"directory": "tensor"
|
|
50
|
-
},
|
|
51
|
-
"keywords": [
|
|
52
|
-
"math",
|
|
53
|
-
"tensor",
|
|
54
|
-
"linear-algebra",
|
|
55
|
-
"typescript"
|
|
56
|
-
]
|
|
57
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@danielsimonjr/mathts-tensor",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Rank-N dense Tensor type for MathTS with Float64Array-backed storage",
|
|
5
|
+
"author": "Daniel Simon Jr.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
23
|
+
"dev": "tsup src/index.ts --format esm --dts --watch",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"test:coverage": "vitest run --coverage",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"lint": "eslint src --ext .ts",
|
|
29
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
30
|
+
"clean": "rm -rf dist",
|
|
31
|
+
"build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@danielsimonjr/mathts-core": "^0.1.4",
|
|
35
|
+
"@danielsimonjr/mathts-matrix": "^0.1.10"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^25.5.2",
|
|
39
|
+
"tsup": "^8.0.0",
|
|
40
|
+
"typescript": "^5.3.0",
|
|
41
|
+
"vitest": "^4.1.5"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "https://github.com/danielsimonjr/mathts",
|
|
49
|
+
"directory": "tensor"
|
|
50
|
+
},
|
|
51
|
+
"keywords": [
|
|
52
|
+
"math",
|
|
53
|
+
"tensor",
|
|
54
|
+
"linear-algebra",
|
|
55
|
+
"typescript"
|
|
56
|
+
]
|
|
57
|
+
}
|