@danielsimonjr/mathts-matrix 0.1.6 → 0.1.7

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.js CHANGED
@@ -128,61 +128,69 @@ var init_Matrix = __esm({
128
128
  });
129
129
 
130
130
  // src/types/dense/arithmetic.ts
131
- function add(a, b) {
132
- const result = new Float64Array(a.rows * a.cols);
133
- for (let i = 0; i < a.rows; i++) {
134
- for (let j = 0; j < a.cols; j++) {
135
- result[i * a.cols + j] = a.get(i, j) + b.get(i, j);
136
- }
131
+ function flat(m) {
132
+ const maybe = m;
133
+ if (typeof maybe.toFloat64Array === "function") return maybe.toFloat64Array();
134
+ const out = new Float64Array(m.rows * m.cols);
135
+ for (let i = 0; i < m.rows; i++) {
136
+ for (let j = 0; j < m.cols; j++) out[i * m.cols + j] = m.get(i, j);
137
137
  }
138
+ return out;
139
+ }
140
+ function add(a, b) {
141
+ const ad = flat(a);
142
+ const bd = flat(b);
143
+ const result = new Float64Array(ad.length);
144
+ for (let i = 0; i < ad.length; i++) result[i] = ad[i] + bd[i];
138
145
  return result;
139
146
  }
140
147
  function subtract(a, b) {
141
- const result = new Float64Array(a.rows * a.cols);
142
- for (let i = 0; i < a.rows; i++) {
143
- for (let j = 0; j < a.cols; j++) {
144
- result[i * a.cols + j] = a.get(i, j) - b.get(i, j);
145
- }
146
- }
148
+ const ad = flat(a);
149
+ const bd = flat(b);
150
+ const result = new Float64Array(ad.length);
151
+ for (let i = 0; i < ad.length; i++) result[i] = ad[i] - bd[i];
147
152
  return result;
148
153
  }
149
154
  function multiplyElementwise(a, b) {
150
- const result = new Float64Array(a.rows * a.cols);
151
- for (let i = 0; i < a.rows; i++) {
152
- for (let j = 0; j < a.cols; j++) {
153
- result[i * a.cols + j] = a.get(i, j) * b.get(i, j);
154
- }
155
- }
155
+ const ad = flat(a);
156
+ const bd = flat(b);
157
+ const result = new Float64Array(ad.length);
158
+ for (let i = 0; i < ad.length; i++) result[i] = ad[i] * bd[i];
156
159
  return result;
157
160
  }
158
161
  function multiply(a, b) {
159
- const result = new Float64Array(a.rows * b.cols);
160
- for (let i = 0; i < a.rows; i++) {
161
- for (let j = 0; j < b.cols; j++) {
162
- let sum3 = 0;
163
- for (let k = 0; k < a.cols; k++) {
164
- sum3 += a.get(i, k) * b.get(k, j);
165
- }
166
- result[i * b.cols + j] = sum3;
162
+ const n = a.rows;
163
+ const p = a.cols;
164
+ const m = b.cols;
165
+ const ad = flat(a);
166
+ const bd = flat(b);
167
+ const result = new Float64Array(n * m);
168
+ for (let i = 0; i < n; i++) {
169
+ const ai = i * p;
170
+ const ri = i * m;
171
+ for (let k = 0; k < p; k++) {
172
+ const aik = ad[ai + k];
173
+ if (aik === 0) continue;
174
+ const bk = k * m;
175
+ for (let j = 0; j < m; j++) result[ri + j] += aik * bd[bk + j];
167
176
  }
168
177
  }
169
178
  return result;
170
179
  }
171
180
  function scale(a, scalar) {
172
- const result = new Float64Array(a.rows * a.cols);
173
- for (let i = 0; i < a.rows; i++) {
174
- for (let j = 0; j < a.cols; j++) {
175
- result[i * a.cols + j] = a.get(i, j) * scalar;
176
- }
177
- }
181
+ const ad = flat(a);
182
+ const result = new Float64Array(ad.length);
183
+ for (let i = 0; i < ad.length; i++) result[i] = ad[i] * scalar;
178
184
  return result;
179
185
  }
180
186
  function transpose(a) {
181
- const result = new Float64Array(a.cols * a.rows);
182
- for (let i = 0; i < a.rows; i++) {
183
- for (let j = 0; j < a.cols; j++) {
184
- result[j * a.rows + i] = a.get(i, j);
185
- }
187
+ const ad = flat(a);
188
+ const rows = a.rows;
189
+ const cols = a.cols;
190
+ const result = new Float64Array(rows * cols);
191
+ for (let i = 0; i < rows; i++) {
192
+ const ai = i * cols;
193
+ for (let j = 0; j < cols; j++) result[j * rows + i] = ad[ai + j];
186
194
  }
187
195
  return result;
188
196
  }
@@ -1049,11 +1057,11 @@ var init_DenseMatrix = __esm({
1049
1057
  }
1050
1058
  }
1051
1059
  } else {
1052
- const flat = data;
1053
- if (flat.length !== rows * cols) {
1054
- throw new Error(`Data length ${flat.length} does not match dimensions ${rows}\xD7${cols}`);
1060
+ const flat2 = data;
1061
+ if (flat2.length !== rows * cols) {
1062
+ throw new Error(`Data length ${flat2.length} does not match dimensions ${rows}\xD7${cols}`);
1055
1063
  }
1056
- this.data = new Float64Array(flat);
1064
+ this.data = new Float64Array(flat2);
1057
1065
  }
1058
1066
  }
1059
1067
  }
@@ -2485,13 +2493,15 @@ var WASMBackend = class {
2485
2493
  * platform-correct conversion.
2486
2494
  */
2487
2495
  async resolveAsWasmPath() {
2488
- const url = new URL(`../../../lib/wasm/mathts-as.wasm`, import.meta.url);
2489
2496
  const isNode = typeof process !== "undefined" && process.versions?.node !== void 0;
2490
2497
  if (isNode) {
2498
+ const { resolvePackagedWasm } = await import("./resolve-JRHDDNVQ.js");
2499
+ const found = await resolvePackagedWasm(import.meta.url, "mathts-as.wasm");
2500
+ if (found) return found;
2491
2501
  const { fileURLToPath } = await import("url");
2492
- return fileURLToPath(url);
2502
+ return fileURLToPath(new URL(`../../../lib/wasm/mathts-as.wasm`, import.meta.url));
2493
2503
  }
2494
- return url.href;
2504
+ return new URL(`../../../lib/wasm/mathts-as.wasm`, import.meta.url).href;
2495
2505
  }
2496
2506
  /**
2497
2507
  * Compile + instantiate the AssemblyScript WASM artifact. Each
@@ -6270,12 +6280,14 @@ var WasmLoader = class _WasmLoader {
6270
6280
  async getDefaultWasmPath() {
6271
6281
  const useAS = typeof process !== "undefined" && process.env?.MATHTS_WASM_BACKEND === "assemblyscript";
6272
6282
  const wasmFile = useAS ? "mathts-as.wasm" : "mathts.wasm";
6273
- const resolvedUrl = new URL(`../../../lib/wasm/${wasmFile}`, import.meta.url);
6274
6283
  if (this.isNode) {
6284
+ const { resolvePackagedWasm } = await import("./resolve-JRHDDNVQ.js");
6285
+ const found = await resolvePackagedWasm(import.meta.url, wasmFile);
6286
+ if (found) return found;
6275
6287
  const { fileURLToPath } = await import("url");
6276
- return fileURLToPath(resolvedUrl);
6288
+ return fileURLToPath(new URL(`../../../lib/wasm/${wasmFile}`, import.meta.url));
6277
6289
  }
6278
- return resolvedUrl.href;
6290
+ return new URL(`../../../lib/wasm/${wasmFile}`, import.meta.url).href;
6279
6291
  }
6280
6292
  async loadNodeWasm(path, totalStart) {
6281
6293
  const fs = await import("fs");
@@ -7058,13 +7070,13 @@ function isSymmetric(matrix2, tolerance = 1e-10) {
7058
7070
  }
7059
7071
  function flattenMatrix(matrix2) {
7060
7072
  const n = matrix2.length;
7061
- const flat = new Float64Array(n * n);
7073
+ const flat2 = new Float64Array(n * n);
7062
7074
  for (let i = 0; i < n; i++) {
7063
7075
  for (let j = 0; j < n; j++) {
7064
- flat[i * n + j] = matrix2[i][j];
7076
+ flat2[i * n + j] = matrix2[i][j];
7065
7077
  }
7066
7078
  }
7067
- return flat;
7079
+ return flat2;
7068
7080
  }
7069
7081
  async function eigWasm(matrix2, options) {
7070
7082
  const n = matrix2.length;
@@ -7213,11 +7225,11 @@ async function svdWasm(matrix2, options) {
7213
7225
  }
7214
7226
  try {
7215
7227
  loader.resetAllocator();
7216
- const flat = new Float64Array(m * n);
7228
+ const flat2 = new Float64Array(m * n);
7217
7229
  for (let i = 0; i < m; i++) {
7218
- for (let j = 0; j < n; j++) flat[i * n + j] = matrix2[i][j];
7230
+ for (let j = 0; j < n; j++) flat2[i * n + j] = matrix2[i][j];
7219
7231
  }
7220
- const aPtr = loader.writeF64(flat);
7232
+ const aPtr = loader.writeF64(flat2);
7221
7233
  const uPtr = loader.allocF64(m * k);
7222
7234
  const sPtr = loader.allocF64(k);
7223
7235
  const vPtr = loader.allocF64(n * k);
@@ -7382,8 +7394,8 @@ function lu(A) {
7382
7394
  if (A.cols !== n) {
7383
7395
  throw new Error(`lu: matrix must be square (got ${A.rows}\xD7${A.cols})`);
7384
7396
  }
7385
- const flat = A.toFloat64Array();
7386
- const W = new Float64Array(flat);
7397
+ const flat2 = A.toFloat64Array();
7398
+ const W = new Float64Array(flat2);
7387
7399
  const perm = new Array(n);
7388
7400
  for (let i = 0; i < n; i++) perm[i] = i;
7389
7401
  for (let k = 0; k < n; k++) {
@@ -0,0 +1,24 @@
1
+ import "./chunk-4VMDO6W2.js";
2
+
3
+ // src/backends/wasm/resolve.ts
4
+ async function resolvePackagedWasm(metaUrl, wasmFile) {
5
+ const isNode = typeof process !== "undefined" && process.versions?.node !== void 0;
6
+ if (!isNode) return null;
7
+ const { fileURLToPath } = await import("url");
8
+ const { dirname, join } = await import("path");
9
+ const { existsSync } = await import("fs");
10
+ let dir = dirname(fileURLToPath(metaUrl));
11
+ for (let depth = 0; depth < 8; depth++) {
12
+ const candidates = [join(dir, "wasm", wasmFile), join(dir, "dist", "wasm", wasmFile)];
13
+ for (const candidate of candidates) {
14
+ if (existsSync(candidate)) return candidate;
15
+ }
16
+ const parent = dirname(dir);
17
+ if (parent === dir) break;
18
+ dir = parent;
19
+ }
20
+ return null;
21
+ }
22
+ export {
23
+ resolvePackagedWasm
24
+ };
Binary file
@@ -0,0 +1,3 @@
1
+ {
2
+ "mathts-as.wasm": "sha384-q/fiU1qnpj7vLiYnprGBDdO7on0S/Gnd5Pyql20PZxGfl9Jx0jdWPUGQe0FM91wo"
3
+ }
package/package.json CHANGED
@@ -1,60 +1,61 @@
1
- {
2
- "name": "@danielsimonjr/mathts-matrix",
3
- "version": "0.1.6",
4
- "description": "Matrix operations for MathTS with WASM/WebGPU backend support",
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-parallel": "^0.2.2"
36
- },
37
- "devDependencies": {
38
- "@types/node": "^25.5.2",
39
- "@webgpu/types": "^0.1.67",
40
- "tsup": "^8.0.0",
41
- "typescript": "^5.3.0",
42
- "vitest": "^4.1.5"
43
- },
44
- "publishConfig": {
45
- "access": "public"
46
- },
47
- "repository": {
48
- "type": "git",
49
- "url": "https://github.com/danielsimonjr/mathts",
50
- "directory": "matrix"
51
- },
52
- "keywords": [
53
- "math",
54
- "matrix",
55
- "linear-algebra",
56
- "typescript",
57
- "wasm",
58
- "webgpu"
59
- ]
60
- }
1
+ {
2
+ "name": "@danielsimonjr/mathts-matrix",
3
+ "version": "0.1.7",
4
+ "description": "Matrix operations for MathTS with WASM/WebGPU backend support",
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 && node scripts/copy-wasm.mjs",
23
+ "copy:wasm": "node scripts/copy-wasm.mjs",
24
+ "dev": "tsup src/index.ts --format esm --dts --watch",
25
+ "test": "vitest run",
26
+ "test:watch": "vitest",
27
+ "test:coverage": "vitest run --coverage",
28
+ "typecheck": "tsc --noEmit",
29
+ "lint": "eslint src --ext .ts",
30
+ "lint:fix": "eslint src --ext .ts --fix",
31
+ "clean": "rm -rf dist",
32
+ "build:prod": "tsup src/index.ts --format esm --dts --clean --minify --treeshake && node scripts/copy-wasm.mjs"
33
+ },
34
+ "dependencies": {
35
+ "@danielsimonjr/mathts-core": "^0.1.4",
36
+ "@danielsimonjr/mathts-parallel": "^0.2.2"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^25.5.2",
40
+ "@webgpu/types": "^0.1.67",
41
+ "tsup": "^8.0.0",
42
+ "typescript": "^5.3.0",
43
+ "vitest": "^4.1.5"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "https://github.com/danielsimonjr/mathts",
51
+ "directory": "matrix"
52
+ },
53
+ "keywords": [
54
+ "math",
55
+ "matrix",
56
+ "linear-algebra",
57
+ "typescript",
58
+ "wasm",
59
+ "webgpu"
60
+ ]
61
+ }