@cooljapan/scirs2 0.3.4
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 +372 -0
- package/package.json +33 -0
- package/scirs2_wasm.d.ts +1155 -0
- package/scirs2_wasm.js +9 -0
- package/scirs2_wasm_bg.js +2490 -0
- package/scirs2_wasm_bg.wasm +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# SciRS2-WASM: Scientific Computing in WebAssembly
|
|
2
|
+
|
|
3
|
+
High-performance scientific computing for JavaScript and TypeScript environments, powered by Rust compiled to WebAssembly. Part of the [SciRS2](https://github.com/cool-japan/scirs) ecosystem.
|
|
4
|
+
|
|
5
|
+
[](../LICENSE)
|
|
6
|
+
[]()
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
`scirs2-wasm` brings the full power of SciRS2's scientific computing capabilities to the browser and Node.js through WebAssembly. It exposes a `wasm-bindgen`-based interface with TypeScript type definitions, SIMD-accelerated operations (where supported by the runtime), and utilities for linear algebra, signal processing, statistics, machine learning, and streaming data processing.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Pure Rust**: 100% safe Rust code compiled to `wasm32-unknown-unknown`
|
|
15
|
+
- **wasm-bindgen Interface**: Direct JS/TS interop without glue code overhead
|
|
16
|
+
- **TypeScript Definitions**: Full type definitions in `ts-src/scirs2.ts` for strong IDE support
|
|
17
|
+
- **SIMD Acceleration**: Optional `wasm32-simd128` acceleration for vectorized operations
|
|
18
|
+
- **WasmMatrix Type**: First-class 2D matrix type for linear algebra in JS/TS
|
|
19
|
+
- **Async Operations**: Non-blocking computations with JavaScript Promises
|
|
20
|
+
- **WebWorker Support**: Offload heavy computations to worker threads
|
|
21
|
+
- **Streaming Processing**: Process large datasets incrementally without exhausting memory
|
|
22
|
+
- **Signal Processing**: FFT, filtering, spectrogram generation
|
|
23
|
+
- **Linear Algebra**: Matrix operations, SVD, eigenvalue decomposition
|
|
24
|
+
- **Statistics**: Descriptive statistics, distributions, hypothesis tests, regression
|
|
25
|
+
- **ML Utilities**: Forward pass helpers, activation functions, loss computation
|
|
26
|
+
- **Advanced Stats**: Time series primitives, regression diagnostics
|
|
27
|
+
- **Memory Efficient**: Optimized allocations for browser memory constraints
|
|
28
|
+
- **Zero JS Dependencies**: No external JavaScript runtime dependencies required
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
### NPM
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install scirs2-wasm
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Yarn
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
yarn add scirs2-wasm
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### PNPM
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pnpm add scirs2-wasm
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
### Browser (ES Modules)
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
import init, * as scirs2 from 'scirs2-wasm';
|
|
56
|
+
|
|
57
|
+
async function main() {
|
|
58
|
+
await init();
|
|
59
|
+
|
|
60
|
+
const a = new scirs2.WasmArray([1, 2, 3, 4]);
|
|
61
|
+
const b = new scirs2.WasmArray([5, 6, 7, 8]);
|
|
62
|
+
|
|
63
|
+
const sum = scirs2.add(a, b);
|
|
64
|
+
const mean = scirs2.mean(a);
|
|
65
|
+
const std = scirs2.std(a);
|
|
66
|
+
|
|
67
|
+
console.log('Sum:', sum.to_array());
|
|
68
|
+
console.log('Mean:', mean, 'Std:', std);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
main();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### TypeScript
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import init, * as scirs2 from 'scirs2-wasm';
|
|
78
|
+
|
|
79
|
+
async function main(): Promise<void> {
|
|
80
|
+
await init();
|
|
81
|
+
|
|
82
|
+
// WasmMatrix for 2D linear algebra
|
|
83
|
+
const mat: scirs2.WasmMatrix = scirs2.WasmMatrix.from_rows([
|
|
84
|
+
[1.0, 2.0, 3.0],
|
|
85
|
+
[4.0, 5.0, 6.0],
|
|
86
|
+
[7.0, 8.0, 9.0],
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const svd = scirs2.svd(mat);
|
|
90
|
+
console.log('Singular values:', svd.s.to_array());
|
|
91
|
+
|
|
92
|
+
// Check SIMD availability
|
|
93
|
+
console.log('SIMD support:', scirs2.has_simd_support());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
main();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Node.js
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
const scirs2 = require('scirs2-wasm');
|
|
103
|
+
|
|
104
|
+
async function main() {
|
|
105
|
+
const matrix = scirs2.WasmArray.from_shape([2, 2], [1, 2, 3, 4]);
|
|
106
|
+
|
|
107
|
+
const det = scirs2.det(matrix);
|
|
108
|
+
const trace = scirs2.trace(matrix);
|
|
109
|
+
|
|
110
|
+
console.log('Determinant:', det);
|
|
111
|
+
console.log('Trace:', trace);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
main();
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## API Reference
|
|
118
|
+
|
|
119
|
+
### Array Creation
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
const arr = new scirs2.WasmArray([1, 2, 3, 4]);
|
|
123
|
+
const typed = new scirs2.WasmArray(new Float64Array([1, 2, 3, 4]));
|
|
124
|
+
const matrix = scirs2.WasmArray.from_shape([2, 2], [1, 2, 3, 4]);
|
|
125
|
+
const zeros = scirs2.WasmArray.zeros([3, 3]);
|
|
126
|
+
const ones = scirs2.WasmArray.ones([5]);
|
|
127
|
+
const linspace = scirs2.WasmArray.linspace(0, 1, 50);
|
|
128
|
+
const arange = scirs2.WasmArray.arange(0, 10, 0.5);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Linear Algebra (WasmMatrix)
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
const A = scirs2.WasmMatrix.from_rows([[1,2],[3,4]]);
|
|
135
|
+
const B = scirs2.WasmMatrix.from_rows([[5,6],[7,8]]);
|
|
136
|
+
|
|
137
|
+
const C = scirs2.matmul(A, B); // Matrix multiply
|
|
138
|
+
const inv = scirs2.inv(A); // Inverse
|
|
139
|
+
const det = scirs2.det(A); // Determinant
|
|
140
|
+
const trace = scirs2.trace(A); // Trace
|
|
141
|
+
const svd_res = scirs2.svd(A); // SVD: {U, s, Vt}
|
|
142
|
+
const eig_res = scirs2.eig(A); // Eigenvalues/vectors
|
|
143
|
+
const norm = scirs2.norm_frobenius(A); // Frobenius norm
|
|
144
|
+
const rank = scirs2.matrix_rank(A); // Rank
|
|
145
|
+
const x = scirs2.solve(A, b_vec); // Ax = b
|
|
146
|
+
const transpose = A.transpose();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Signal Processing
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
const signal = new scirs2.WasmArray([/* samples */]);
|
|
153
|
+
|
|
154
|
+
// FFT
|
|
155
|
+
const spectrum = scirs2.fft(signal);
|
|
156
|
+
const power = scirs2.periodogram(signal, 1024);
|
|
157
|
+
const spectrogram = scirs2.spectrogram(signal, { nperseg: 256, noverlap: 128 });
|
|
158
|
+
|
|
159
|
+
// Filtering
|
|
160
|
+
const filtered_lp = scirs2.lowpass_filter(signal, 0.2);
|
|
161
|
+
const filtered_bp = scirs2.bandpass_filter(signal, 0.1, 0.4);
|
|
162
|
+
const filtered_hp = scirs2.highpass_filter(signal, 0.3);
|
|
163
|
+
|
|
164
|
+
// Convolution
|
|
165
|
+
const kernel = new scirs2.WasmArray([0.25, 0.5, 0.25]);
|
|
166
|
+
const convolved = scirs2.convolve(signal, kernel);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Statistics
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
const data = new scirs2.WasmArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
173
|
+
|
|
174
|
+
// Descriptive
|
|
175
|
+
const mean = scirs2.mean(data);
|
|
176
|
+
const std = scirs2.std(data);
|
|
177
|
+
const variance = scirs2.variance(data);
|
|
178
|
+
const median = scirs2.median(data);
|
|
179
|
+
const p25 = scirs2.percentile(data, 25);
|
|
180
|
+
const p75 = scirs2.percentile(data, 75);
|
|
181
|
+
const skew = scirs2.skewness(data);
|
|
182
|
+
const kurt = scirs2.kurtosis(data);
|
|
183
|
+
|
|
184
|
+
// Correlation
|
|
185
|
+
const x = new scirs2.WasmArray([1, 2, 3, 4, 5]);
|
|
186
|
+
const y = new scirs2.WasmArray([2, 4, 6, 8, 10]);
|
|
187
|
+
const corr = scirs2.corrcoef(x, y);
|
|
188
|
+
|
|
189
|
+
// Advanced: regression diagnostics
|
|
190
|
+
const reg_result = scirs2.linear_regression(x, y);
|
|
191
|
+
console.log('Slope:', reg_result.slope, 'R2:', reg_result.r2);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### ML Utilities
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
// Activation functions
|
|
198
|
+
const relu = scirs2.relu(data);
|
|
199
|
+
const sigmoid = scirs2.sigmoid(data);
|
|
200
|
+
const softmax = scirs2.softmax(data);
|
|
201
|
+
const tanh = scirs2.tanh_activation(data);
|
|
202
|
+
|
|
203
|
+
// Loss functions
|
|
204
|
+
const mse_loss = scirs2.mse_loss(predictions, targets);
|
|
205
|
+
const ce_loss = scirs2.cross_entropy_loss(logits, labels);
|
|
206
|
+
|
|
207
|
+
// Normalization
|
|
208
|
+
const normalized = scirs2.layer_norm(data);
|
|
209
|
+
const batch_normed = scirs2.batch_norm(data, mean_vec, var_vec);
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Streaming Data Processing
|
|
213
|
+
|
|
214
|
+
```javascript
|
|
215
|
+
// Process large datasets in chunks
|
|
216
|
+
const processor = new scirs2.StreamingProcessor({ window_size: 1024 });
|
|
217
|
+
|
|
218
|
+
for (const chunk of dataSource) {
|
|
219
|
+
processor.push(chunk);
|
|
220
|
+
const current_stats = processor.current_stats();
|
|
221
|
+
console.log('Running mean:', current_stats.mean);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const final_result = processor.finalize();
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### WebWorker Support
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
// In main thread
|
|
231
|
+
const worker = new Worker('scirs2-worker.js');
|
|
232
|
+
worker.postMessage({ op: 'fft', data: signal_data });
|
|
233
|
+
worker.onmessage = (e) => console.log('FFT result:', e.data.result);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Building from Source
|
|
237
|
+
|
|
238
|
+
### Prerequisites
|
|
239
|
+
|
|
240
|
+
- Rust 1.75+ with `wasm32-unknown-unknown` target
|
|
241
|
+
- `wasm-pack` 0.12+
|
|
242
|
+
- Node.js 18+
|
|
243
|
+
|
|
244
|
+
### Build Steps
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# Install Rust WASM target
|
|
248
|
+
rustup target add wasm32-unknown-unknown
|
|
249
|
+
|
|
250
|
+
# Install wasm-pack
|
|
251
|
+
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
|
252
|
+
|
|
253
|
+
# Build for bundlers (webpack, rollup, Vite)
|
|
254
|
+
npm run build
|
|
255
|
+
|
|
256
|
+
# Build for web (plain ES modules, no bundler needed)
|
|
257
|
+
npm run build:web
|
|
258
|
+
|
|
259
|
+
# Build for Node.js (CommonJS)
|
|
260
|
+
npm run build:nodejs
|
|
261
|
+
|
|
262
|
+
# Build with SIMD acceleration
|
|
263
|
+
npm run build:simd
|
|
264
|
+
|
|
265
|
+
# Optimize binary with wasm-opt
|
|
266
|
+
npm run optimize
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Testing
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
# Run tests in headless Firefox
|
|
273
|
+
npm test
|
|
274
|
+
|
|
275
|
+
# Run tests in headless Chrome
|
|
276
|
+
npm run test:chrome
|
|
277
|
+
|
|
278
|
+
# Run tests in Node.js
|
|
279
|
+
npm run test:node
|
|
280
|
+
|
|
281
|
+
# Run Rust unit tests (non-WASM)
|
|
282
|
+
cargo test --lib
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Performance
|
|
286
|
+
|
|
287
|
+
SciRS2-WASM delivers near-native performance for scientific workloads:
|
|
288
|
+
|
|
289
|
+
| Operation | Performance vs Native | Notes |
|
|
290
|
+
|-----------|----------------------|-------|
|
|
291
|
+
| Array arithmetic | 80-95% | Scalar fallback without SIMD |
|
|
292
|
+
| Matrix multiply | Up to 90% | With `wasm32-simd128` |
|
|
293
|
+
| Statistical functions | 85-95% | Parallel not available in WASM |
|
|
294
|
+
| FFT | 75-90% | Depends on transform size |
|
|
295
|
+
| Random number generation | 70-85% | WASM RNG overhead |
|
|
296
|
+
|
|
297
|
+
### SIMD Acceleration
|
|
298
|
+
|
|
299
|
+
Build with `--features simd` for `wasm32-simd128` vectorization:
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
RUSTFLAGS="-C target-feature=+simd128" wasm-pack build --release
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
SIMD is supported in Chrome 91+, Firefox 89+, Safari 16.4+, Edge 91+, and Node.js 20+.
|
|
306
|
+
|
|
307
|
+
## Browser Compatibility
|
|
308
|
+
|
|
309
|
+
| Browser | Baseline | With SIMD |
|
|
310
|
+
|---------|----------|-----------|
|
|
311
|
+
| Chrome | 91+ | 91+ |
|
|
312
|
+
| Firefox | 89+ | 89+ |
|
|
313
|
+
| Safari | 16.4+ | Limited |
|
|
314
|
+
| Edge | 91+ | 91+ |
|
|
315
|
+
| Node.js | 18+ | 20+ |
|
|
316
|
+
|
|
317
|
+
## TypeScript Type Definitions
|
|
318
|
+
|
|
319
|
+
Full TypeScript definitions are provided in `ts-src/scirs2.ts`. The `WasmMatrix` and `WasmArray` types are strongly typed for ergonomic IDE support:
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import type { WasmMatrix, WasmArray, SvdResult, EigResult } from 'scirs2-wasm';
|
|
323
|
+
|
|
324
|
+
function process(m: WasmMatrix): SvdResult {
|
|
325
|
+
return scirs2.svd(m);
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Memory Management
|
|
330
|
+
|
|
331
|
+
WASM uses a linear memory model. Best practices:
|
|
332
|
+
|
|
333
|
+
1. **Reuse arrays** when possible to reduce allocation overhead
|
|
334
|
+
2. **Process in chunks** for datasets larger than a few hundred MB
|
|
335
|
+
3. **Monitor memory** with browser DevTools Performance panel
|
|
336
|
+
4. **Dispose large temporaries** explicitly when done
|
|
337
|
+
|
|
338
|
+
## Module Architecture
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
scirs2-wasm/
|
|
342
|
+
├── src/
|
|
343
|
+
│ ├── lib.rs - WASM entry point, init, version
|
|
344
|
+
│ ├── array.rs - WasmArray type and array operations
|
|
345
|
+
│ ├── linalg.rs - WasmMatrix, decompositions, solvers
|
|
346
|
+
│ ├── stats.rs - Descriptive stats, distributions, tests
|
|
347
|
+
│ ├── fft.rs - FFT, spectrogram, periodogram
|
|
348
|
+
│ ├── signal.rs - Filters, convolution, denoising
|
|
349
|
+
│ ├── optimize.rs - Optimization algorithms
|
|
350
|
+
│ ├── integrate.rs - Numerical integration
|
|
351
|
+
│ ├── interpolate.rs - Interpolation methods
|
|
352
|
+
│ ├── random.rs - RNG and distributions
|
|
353
|
+
│ ├── utils.rs - Performance timer, capabilities
|
|
354
|
+
│ └── error.rs - WASM-friendly error types
|
|
355
|
+
├── ts-src/
|
|
356
|
+
│ └── scirs2.ts - TypeScript definitions
|
|
357
|
+
└── Cargo.toml
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Related Projects
|
|
361
|
+
|
|
362
|
+
- [SciRS2](https://github.com/cool-japan/scirs) - Core Rust library
|
|
363
|
+
- [scirs2-python](../scirs2-python/) - Python/PyO3 bindings
|
|
364
|
+
- [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) - WASM-JS interop framework
|
|
365
|
+
|
|
366
|
+
## License
|
|
367
|
+
|
|
368
|
+
Licensed under the Apache License 2.0. See [LICENSE](../LICENSE) for details.
|
|
369
|
+
|
|
370
|
+
## Authors
|
|
371
|
+
|
|
372
|
+
COOLJAPAN OU (Team KitaSan)
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cooljapan/scirs2",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"COOLJAPAN OU (Team KitaSan)"
|
|
6
|
+
],
|
|
7
|
+
"description": "WebAssembly (WASM) bindings for SciRS2 - JavaScript/TypeScript interop for scientific computing",
|
|
8
|
+
"version": "0.3.4",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/cool-japan/scirs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"scirs2_wasm_bg.wasm",
|
|
16
|
+
"scirs2_wasm.js",
|
|
17
|
+
"scirs2_wasm_bg.js",
|
|
18
|
+
"scirs2_wasm.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"main": "scirs2_wasm.js",
|
|
21
|
+
"types": "scirs2_wasm.d.ts",
|
|
22
|
+
"sideEffects": [
|
|
23
|
+
"./scirs2_wasm.js",
|
|
24
|
+
"./snippets/*"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"scientific",
|
|
28
|
+
"wasm",
|
|
29
|
+
"webassembly",
|
|
30
|
+
"javascript",
|
|
31
|
+
"typescript"
|
|
32
|
+
]
|
|
33
|
+
}
|