numpy-ts 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.
- package/README.md +199 -293
- package/dist/numpy-ts.browser.js +2 -2
- package/dist/numpy-ts.esm.js +2 -2
- package/dist/numpy-ts.node-io.cjs +3 -0
- package/dist/numpy-ts.node-io.cjs.map +7 -0
- package/dist/numpy-ts.node-io.mjs +3 -0
- package/dist/numpy-ts.node-io.mjs.map +7 -0
- package/dist/numpy-ts.node.cjs +2 -1
- package/dist/numpy-ts.node.cjs.map +7 -0
- package/dist/types/core/ndarray.d.ts +383 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/io/index.d.ts +17 -0
- package/dist/types/io/npy/format.d.ts +91 -0
- package/dist/types/io/npy/index.d.ts +7 -0
- package/dist/types/io/npy/parser.d.ts +28 -0
- package/dist/types/io/npy/serializer.d.ts +17 -0
- package/dist/types/io/npz/index.d.ts +6 -0
- package/dist/types/io/npz/parser.d.ts +57 -0
- package/dist/types/io/npz/serializer.d.ts +49 -0
- package/dist/types/io/zip/index.d.ts +7 -0
- package/dist/types/io/zip/reader.d.ts +22 -0
- package/dist/types/io/zip/types.d.ts +59 -0
- package/dist/types/io/zip/writer.d.ts +29 -0
- package/dist/types/node.d.ts +154 -0
- package/dist/types/ops/advanced.d.ts +34 -0
- package/dist/types/ops/hyperbolic.d.ts +59 -0
- package/dist/types/ops/shape.d.ts +61 -0
- package/dist/types/ops/trig.d.ts +94 -0
- package/package.json +18 -6
package/README.md
CHANGED
|
@@ -1,368 +1,274 @@
|
|
|
1
1
|
# numpy-ts
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/numpy-ts)
|
|
5
|
+

|
|
6
6
|

|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
```
|
|
9
|
+
_
|
|
10
|
+
_ __ _ _ _ __ ___ _ __ _ _ ____| |_ ___
|
|
11
|
+
| '_ \| | | | '_ ` _ \| '_ \| | | |____| __/ __)
|
|
12
|
+
| | | | |_| | | | | | | |_) | |_| | | |_\__ \
|
|
13
|
+
|_| |_|\__,_|_| |_| |_| .__/ \__, | \__(___/
|
|
14
|
+
|_| (___/
|
|
15
|
+
```
|
|
9
16
|
|
|
10
|
-
|
|
17
|
+
Complete NumPy implementation for TypeScript and JavaScript
|
|
11
18
|
|
|
12
|
-
|
|
19
|
+
> **⚠️ Under active development** — API may change before v1.0
|
|
13
20
|
|
|
14
|
-
A
|
|
21
|
+
A faithful NumPy 2.0+ implementation for TypeScript/JavaScript, validated against Python NumPy. **128 of 333 NumPy functions (38.4% complete)** covering array creation, manipulation, linear algebra, reductions, and more.
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- **Correctness** - Cross-validated against Python NumPy
|
|
20
|
-
- **Cross-Platform** - Node.js and browsers
|
|
23
|
+
```bash
|
|
24
|
+
npm install numpy-ts
|
|
25
|
+
```
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
- Matching NumPy's C-optimized performance (initially)
|
|
24
|
-
- C API compatibility
|
|
27
|
+
## Why numpy-ts?
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
- **📊 Extensive API** — 120+ NumPy functions with 1365+ tests validated against Python NumPy
|
|
30
|
+
- **✅ NumPy-validated** — 1365+ test cases cross-validated against Python NumPy
|
|
31
|
+
- **🔒 Type-safe** — Full TypeScript support with shape and dtype inference
|
|
32
|
+
- **🌐 Universal** — Works in Node.js and browsers with .npy/.npz file support
|
|
33
|
+
- **🎯 Zero dependencies** — Pure TypeScript, no heavy external libraries
|
|
27
34
|
|
|
28
|
-
## Quick
|
|
35
|
+
## Quick Start
|
|
29
36
|
|
|
30
37
|
```typescript
|
|
31
38
|
import * as np from 'numpy-ts';
|
|
32
39
|
|
|
33
|
-
//
|
|
34
|
-
const A = np.array([[1, 2], [3, 4]]);
|
|
35
|
-
const B = np.
|
|
36
|
-
|
|
37
|
-
// Create arrays with specific dtypes (11 types supported)
|
|
38
|
-
const intArr = np.ones([3, 3], 'int32');
|
|
39
|
-
const floatArr = np.arange(0, 10, 1, 'float32');
|
|
40
|
-
const boolArr = np.array([1, 0, 1], 'bool');
|
|
41
|
-
const bigIntArr = np.array([1n, 2n, 3n], 'int64'); // BigInt support
|
|
40
|
+
// Array creation with dtype support
|
|
41
|
+
const A = np.array([[1, 2], [3, 4]], 'float32');
|
|
42
|
+
const B = np.ones([2, 2], 'int32');
|
|
42
43
|
|
|
43
|
-
//
|
|
44
|
-
const result =
|
|
45
|
-
const promoted = intArr.add(floatArr); // Promotes to float32
|
|
44
|
+
// Broadcasting and chained operations
|
|
45
|
+
const result = A.add(5).multiply(2);
|
|
46
46
|
|
|
47
|
-
//
|
|
47
|
+
// Linear algebra
|
|
48
48
|
const C = A.matmul(B);
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
// Slicing (string-based syntax)
|
|
52
|
-
const row = A.slice('0', ':'); // First row
|
|
53
|
-
const col = A.col(1); // Second column
|
|
54
|
-
|
|
55
|
-
// Broadcasting (fully implemented!)
|
|
56
|
-
const scaled = A.add(5).multiply(2);
|
|
57
|
-
|
|
58
|
-
// Advanced broadcasting examples:
|
|
59
|
-
const row = np.array([1, 2, 3, 4]); // (4,)
|
|
60
|
-
const col = np.array([[1], [2], [3]]); // (3, 1)
|
|
61
|
-
const result = col.multiply(row); // (3, 4) via broadcasting!
|
|
62
|
-
|
|
63
|
-
// Reductions
|
|
64
|
-
const total = A.sum(); // Sum all elements
|
|
65
|
-
const columnMeans = A.mean(0); // Mean along axis 0
|
|
66
|
-
const rowMaxs = A.max(1, true); // Max along axis 1, keep dims
|
|
67
|
-
|
|
68
|
-
// Comparisons (return boolean arrays as uint8)
|
|
69
|
-
const mask = A.greater(5); // Element-wise A > 5
|
|
70
|
-
const equal = A.equal(B); // Element-wise A == B
|
|
71
|
-
const inRange = A.greater_equal(0); // A >= 0
|
|
72
|
-
|
|
73
|
-
// Tolerance comparisons (for floating point)
|
|
74
|
-
const close = A.isclose(B); // Element-wise closeness
|
|
75
|
-
const allClose = A.allclose(B); // True if all elements close
|
|
76
|
-
|
|
77
|
-
// Reshape operations (view vs copy semantics)
|
|
78
|
-
const reshaped = A.reshape(4, 1); // View if C-contiguous, copy otherwise
|
|
79
|
-
const flat = A.flatten(); // Always returns a copy
|
|
80
|
-
const ravel = A.ravel(); // View if C-contiguous, copy otherwise
|
|
81
|
-
const transposed = A.transpose(); // Always returns a view
|
|
82
|
-
const squeezed = A.squeeze(); // Always returns a view
|
|
83
|
-
const expanded = A.expand_dims(0); // Always returns a view
|
|
84
|
-
|
|
85
|
-
// View tracking (NumPy-compatible)
|
|
86
|
-
const view = A.slice('0:2', '0:2');
|
|
87
|
-
console.log(view.base === A); // true - view tracks base array
|
|
88
|
-
console.log(view.flags.OWNDATA); // false - doesn't own data
|
|
89
|
-
console.log(A.flags.OWNDATA); // true - owns data
|
|
90
|
-
|
|
91
|
-
// Memory layout flags
|
|
92
|
-
console.log(A.flags.C_CONTIGUOUS); // true - C-order (row-major)
|
|
93
|
-
console.log(A.flags.F_CONTIGUOUS); // false - not Fortran-order
|
|
94
|
-
|
|
95
|
-
// Random
|
|
96
|
-
const random = np.random.randn([100, 100]);
|
|
97
|
-
|
|
98
|
-
// I/O (Node.js)
|
|
99
|
-
np.save('matrix.npy', A);
|
|
100
|
-
const loaded = np.load('matrix.npy');
|
|
101
|
-
```
|
|
49
|
+
const trace = A.trace();
|
|
102
50
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
## Architecture
|
|
51
|
+
// Reductions with axis support
|
|
52
|
+
const colMeans = A.mean(0); // [2.0, 3.0]
|
|
106
53
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
┌────────────────────────────────┐
|
|
111
|
-
│ NumPy-Compatible API │
|
|
112
|
-
└────────────┬───────────────────┘
|
|
113
|
-
│
|
|
114
|
-
┌────────────┴───────────────────┐
|
|
115
|
-
│ NDArray (Memory & Views) │
|
|
116
|
-
│ Broadcasting, Slicing, DTypes │
|
|
117
|
-
└────────────┬───────────────────┘
|
|
118
|
-
│
|
|
119
|
-
┌────────────┴───────────────────┐
|
|
120
|
-
│ TypeScript/JavaScript Core │
|
|
121
|
-
│ Computational Engine │
|
|
122
|
-
└────────────────────────────────┘
|
|
54
|
+
// NumPy-style slicing with strings
|
|
55
|
+
const row = A.slice('0', ':'); // A[0, :]
|
|
56
|
+
const submatrix = A.slice('0:2', '1:'); // A[0:2, 1:]
|
|
123
57
|
```
|
|
124
58
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
59
|
+
## Features
|
|
60
|
+
|
|
61
|
+
### API Coverage
|
|
62
|
+
|
|
63
|
+
Progress toward complete NumPy API compatibility:
|
|
64
|
+
|
|
65
|
+
| Category | Complete | Total | Status |
|
|
66
|
+
|----------|----------|-------|--------|
|
|
67
|
+
| **Hyperbolic** | 6/6 | 100% | ✅ |
|
|
68
|
+
| **Comparison** | 9/10 | 90% | 🟡 |
|
|
69
|
+
| **Trigonometric** | 10/12 | 83% | 🟡 |
|
|
70
|
+
| **Arithmetic** | 13/19 | 68% | 🟡 |
|
|
71
|
+
| **Broadcasting** | 2/3 | 67% | 🟡 |
|
|
72
|
+
| **Linear Algebra** | 6/9 | 67% | 🟡 |
|
|
73
|
+
| **Array Creation** | 17/32 | 53% | 🟡 |
|
|
74
|
+
| **Array Manipulation** | 18/35 | 51% | 🟡 |
|
|
75
|
+
| **Reductions** | 11/30 | 37% | 🔴 |
|
|
76
|
+
| **Indexing** | 3/20 | 15% | 🔴 |
|
|
77
|
+
| **Bit Operations** | 0/9 | 0% | 🔴 |
|
|
78
|
+
| **Exponential** | 0/9 | 0% | 🔴 |
|
|
79
|
+
| **FFT** | 0/18 | 0% | 🔴 |
|
|
80
|
+
| **Gradient** | 0/4 | 0% | 🔴 |
|
|
81
|
+
| **I/O** | 0/8 | 0% | 🔴 |
|
|
82
|
+
| **Linear Algebra (linalg)** | 0/19 | 0% | 🔴 |
|
|
83
|
+
| **Logic** | 0/12 | 0% | 🔴 |
|
|
84
|
+
| **Other Math** | 0/11 | 0% | 🔴 |
|
|
85
|
+
| **Random** | 0/17 | 0% | 🔴 |
|
|
86
|
+
| **Rounding** | 0/7 | 0% | 🔴 |
|
|
87
|
+
| **Searching** | 0/6 | 0% | 🔴 |
|
|
88
|
+
| **Set Operations** | 0/7 | 0% | 🔴 |
|
|
89
|
+
| **Sorting** | 0/6 | 0% | 🔴 |
|
|
90
|
+
| **Statistics** | 0/9 | 0% | 🔴 |
|
|
91
|
+
|
|
92
|
+
**Overall: 128/333 functions (38.4% complete)**
|
|
93
|
+
|
|
94
|
+
See the complete [API Reference](docs/API-REFERENCE.md) for detailed function list.
|
|
95
|
+
|
|
96
|
+
### Data Types (dtypes)
|
|
97
|
+
|
|
98
|
+
NumPy-compatible type system with automatic promotion:
|
|
99
|
+
|
|
100
|
+
| DType | NumPy | numpy-ts | Notes |
|
|
101
|
+
|-------|-------|----------|-------|
|
|
102
|
+
| **Floating Point** ||||
|
|
103
|
+
| `float64` | ✅ | ✅ | Default dtype |
|
|
104
|
+
| `float32` | ✅ | ✅ | |
|
|
105
|
+
| `float16` | ✅ | ⚠️ | Planned (half-precision) |
|
|
106
|
+
| **Signed Integers** ||||
|
|
107
|
+
| `int64` | ✅ | ✅ | Uses BigInt |
|
|
108
|
+
| `int32` | ✅ | ✅ | |
|
|
109
|
+
| `int16` | ✅ | ✅ | |
|
|
110
|
+
| `int8` | ✅ | ✅ | |
|
|
111
|
+
| **Unsigned Integers** ||||
|
|
112
|
+
| `uint64` | ✅ | ✅ | Uses BigInt |
|
|
113
|
+
| `uint32` | ✅ | ✅ | |
|
|
114
|
+
| `uint16` | ✅ | ✅ | |
|
|
115
|
+
| `uint8` | ✅ | ✅ | |
|
|
116
|
+
| **Other Numeric** ||||
|
|
117
|
+
| `bool` | ✅ | ✅ | Stored as uint8 |
|
|
118
|
+
| `complex64` | ✅ | ❌ | Not yet supported |
|
|
119
|
+
| `complex128` | ✅ | ❌ | Not yet supported |
|
|
120
|
+
| **Non-Numeric** ||||
|
|
121
|
+
| `str_` | ✅ | ❌ | Not planned |
|
|
122
|
+
| `bytes_` | ✅ | ❌ | Not planned |
|
|
123
|
+
| `object_` | ✅ | ❌ | Not planned |
|
|
124
|
+
| `datetime64` | ✅ | ❌ | Future consideration |
|
|
125
|
+
| `timedelta64` | ✅ | ❌ | Future consideration |
|
|
126
|
+
|
|
127
|
+
**Supported: 11/20 numeric dtypes** • Complex and temporal types planned for future releases
|
|
128
|
+
|
|
129
|
+
### NumPy Memory Model
|
|
130
|
+
|
|
131
|
+
- **View tracking** — `base` attribute and `OWNDATA` flag
|
|
132
|
+
- **Strided arrays** — C/F contiguous flags for memory layout
|
|
133
|
+
- **Zero-copy ops** — Views for slicing, transpose, reshape (when possible)
|
|
128
134
|
|
|
129
|
-
## Key Features
|
|
130
|
-
|
|
131
|
-
### Comprehensive NumPy API
|
|
132
|
-
- **Array creation**: `zeros`, `ones`, `arange`, `linspace`, `eye` (all support dtype parameter)
|
|
133
|
-
- **Arithmetic operations**: `add`, `subtract`, `multiply`, `divide` with broadcasting
|
|
134
|
-
- **Linear algebra**: `matmul`, `dot`, `transpose`
|
|
135
|
-
- **Reductions**: `sum`, `mean`, `std`, `min`, `max` with axis support
|
|
136
|
-
- **DTypes**: 11 types supported (float32/64, int8/16/32/64, uint8/16/32/64, bool)
|
|
137
|
-
- Full dtype preservation across operations
|
|
138
|
-
- NumPy-compatible type promotion
|
|
139
|
-
- BigInt support for int64/uint64
|
|
140
|
-
- **View tracking**: `base` attribute tracks view relationships
|
|
141
|
-
- **Memory flags**: `C_CONTIGUOUS`, `F_CONTIGUOUS`, `OWNDATA`
|
|
142
|
-
- **Comparisons**: `greater`, `less`, `equal`, `isclose`, `allclose`
|
|
143
|
-
- **Reshaping**: `reshape`, `flatten`, `ravel`, `transpose`, `squeeze`, `expand_dims`
|
|
144
|
-
|
|
145
|
-
### TypeScript Native
|
|
146
135
|
```typescript
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
arr.shape; // Type: readonly [3, 4]
|
|
150
|
-
arr.sum(); // Type: number
|
|
136
|
+
const arr = np.ones([4, 4]);
|
|
137
|
+
const view = arr.slice('0:2', '0:2');
|
|
151
138
|
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
arr.
|
|
139
|
+
console.log(view.base === arr); // true - view tracks base
|
|
140
|
+
console.log(view.flags.OWNDATA); // false - doesn't own data
|
|
141
|
+
console.log(arr.flags.C_CONTIGUOUS); // true - row-major layout
|
|
155
142
|
```
|
|
156
143
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
Since TypeScript doesn't support Python's `arr[0:5, :]` syntax, we use strings:
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
// String-based (primary)
|
|
163
|
-
arr.slice('0:5', '1:3'); // arr[0:5, 1:3]
|
|
164
|
-
arr.slice(':', '-1'); // arr[:, -1]
|
|
165
|
-
arr.slice('::2'); // arr[::2]
|
|
144
|
+
## Architecture
|
|
166
145
|
|
|
167
|
-
// Convenience helpers
|
|
168
|
-
arr.row(0); // arr[0, :]
|
|
169
|
-
arr.col(2); // arr[:, 2]
|
|
170
|
-
arr.rows(0, 5); // arr[0:5, :]
|
|
171
|
-
arr.cols(1, 3); // arr[:, 1:3]
|
|
172
146
|
```
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
147
|
+
┌───────────────────────────────────┐
|
|
148
|
+
│ NumPy-Compatible API │
|
|
149
|
+
│ Broadcasting, DType Promotion │
|
|
150
|
+
└─────────────────┬─────────────────┘
|
|
151
|
+
│
|
|
152
|
+
┌─────────────────┴─────────────────┐
|
|
153
|
+
│ NDArray (Views & Memory Mgmt) │
|
|
154
|
+
│ Strided Arrays, Base Tracking │
|
|
155
|
+
└─────────────────┬─────────────────┘
|
|
156
|
+
│- - - - - - - - - - - - - - - - - - - ┐
|
|
157
|
+
┌─────────────────┴─────────────────┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┐
|
|
158
|
+
│ TypeScript / JavaScript Core │ │ WASM Compute Engine (Future) │
|
|
159
|
+
│ Computational Engine │ │ Optimized BLAS / arithmetic │
|
|
160
|
+
└───────────────────────────────────┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
|
|
182
161
|
```
|
|
183
162
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
## Installation
|
|
163
|
+
Pure TypeScript implementation built from scratch for correctness and NumPy compatibility.
|
|
187
164
|
|
|
188
|
-
|
|
189
|
-
npm install numpy-ts
|
|
190
|
-
```
|
|
165
|
+
## Performance
|
|
191
166
|
|
|
192
|
-
|
|
193
|
-
import * as np from 'numpy-ts';
|
|
194
|
-
```
|
|
167
|
+

|
|
195
168
|
|
|
196
|
-
|
|
169
|
+
See [benchmarks/README.md](benchmarks/README.md) for detailed performance analysis.
|
|
197
170
|
|
|
198
|
-
##
|
|
171
|
+
## File I/O
|
|
199
172
|
|
|
200
|
-
|
|
201
|
-
- [API-REFERENCE.md](https://github.com/dupontcyborg/numpy-ts/blob/main/docs/API-REFERENCE.md) - Complete API checklist
|
|
173
|
+
Read and write `.npy` and `.npz` files with Node.js or browsers.
|
|
202
174
|
|
|
203
|
-
###
|
|
204
|
-
- [TESTING-GUIDE.md](https://github.com/dupontcyborg/numpy-ts/blob/main/docs/TESTING-GUIDE.md) - How to add tests (unit, validation, benchmarks)
|
|
205
|
-
- [benchmarks/README.md](https://github.com/dupontcyborg/numpy-ts/blob/main/benchmarks/README.md) - Performance benchmarking guide
|
|
175
|
+
### Node.js
|
|
206
176
|
|
|
207
|
-
|
|
177
|
+
```typescript
|
|
178
|
+
import { load, save, savez, savez_compressed } from 'numpy-ts/node';
|
|
208
179
|
|
|
209
|
-
|
|
180
|
+
save('array.npy', arr);
|
|
181
|
+
const arr = load('array.npy');
|
|
210
182
|
|
|
211
|
-
|
|
183
|
+
savez('arrays.npz', { a: arr1, b: arr2 });
|
|
184
|
+
const { a, b } = load('arrays.npz');
|
|
185
|
+
```
|
|
212
186
|
|
|
213
|
-
|
|
214
|
-
2. **Python Comparison** - Validate against NumPy
|
|
187
|
+
### Browser
|
|
215
188
|
|
|
216
189
|
```typescript
|
|
217
|
-
|
|
218
|
-
it('creates 2D array of zeros', () => {
|
|
219
|
-
const arr = np.zeros([2, 3]);
|
|
220
|
-
expect(arr.shape).toEqual([2, 3]);
|
|
221
|
-
expect(arr.sum()).toBe(0);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
// NumPy validation (cross-checked against Python)
|
|
225
|
-
it('matmul matches NumPy', () => {
|
|
226
|
-
const A = np.array([[1, 2], [3, 4]]);
|
|
227
|
-
const B = np.array([[5, 6], [7, 8]]);
|
|
228
|
-
const result = A.matmul(B);
|
|
229
|
-
|
|
230
|
-
const npResult = runNumPy(`
|
|
231
|
-
A = np.array([[1, 2], [3, 4]])
|
|
232
|
-
B = np.array([[5, 6], [7, 8]])
|
|
233
|
-
result = A @ B
|
|
234
|
-
`);
|
|
235
|
-
|
|
236
|
-
expect(result.toArray()).toEqual(npResult);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
// Edge case validation
|
|
240
|
-
it('int8 overflow wraps like NumPy', () => {
|
|
241
|
-
const arr = np.array([127], 'int8');
|
|
242
|
-
const result = arr.add(1);
|
|
243
|
-
expect(result.get([0])).toBe(-128); // Wraps like NumPy
|
|
244
|
-
});
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
---
|
|
190
|
+
import * as np from 'numpy-ts';
|
|
248
191
|
|
|
249
|
-
|
|
192
|
+
// Parse fetched .npy file
|
|
193
|
+
const response = await fetch('array.npy');
|
|
194
|
+
const arr = np.parseNpy(await response.arrayBuffer());
|
|
250
195
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
196
|
+
// Serialize for download
|
|
197
|
+
const bytes = np.serializeNpy(arr);
|
|
198
|
+
```
|
|
254
199
|
|
|
255
|
-
**
|
|
256
|
-
- Exact representation over convenience
|
|
257
|
-
- No precision loss (different type but fully accurate)
|
|
200
|
+
> **Why separate imports?** The `/node` entry includes Node.js `fs` usage. Keeping it separate ensures browser bundles stay clean.
|
|
258
201
|
|
|
259
|
-
|
|
260
|
-
- `arr.slice('0:5', ':')` instead of `arr[0:5, :]`
|
|
261
|
-
- TypeScript limitation, Pythonic compromise
|
|
202
|
+
## Examples
|
|
262
203
|
|
|
263
|
-
|
|
264
|
-
- Track base array with `base` attribute
|
|
265
|
-
- Zero-copy optimizations where possible
|
|
266
|
-
- Matches NumPy semantics
|
|
204
|
+
### Broadcasting
|
|
267
205
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
206
|
+
```typescript
|
|
207
|
+
const matrix = np.ones([3, 4]); // (3, 4)
|
|
208
|
+
const row = np.arange(4); // (4,)
|
|
209
|
+
const result = matrix.add(row); // (3, 4) - row broadcast to each row
|
|
271
210
|
|
|
272
|
-
|
|
211
|
+
const col = np.array([[1], [2], [3]]); // (3, 1)
|
|
212
|
+
const grid = col.multiply(row); // (3, 4) - outer product via broadcasting
|
|
213
|
+
```
|
|
273
214
|
|
|
274
|
-
|
|
215
|
+
### Slicing
|
|
275
216
|
|
|
276
|
-
|
|
217
|
+
TypeScript doesn't support Python's `arr[0:5, :]`, so we use strings:
|
|
277
218
|
|
|
278
|
-
|
|
219
|
+
```typescript
|
|
220
|
+
arr.slice('0:5', '1:3'); // arr[0:5, 1:3]
|
|
221
|
+
arr.slice(':', '-1'); // arr[:, -1]
|
|
222
|
+
arr.slice('::2'); // arr[::2]
|
|
279
223
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
npm install
|
|
284
|
-
npm test
|
|
224
|
+
// Convenience helpers
|
|
225
|
+
arr.row(0); // arr[0, :]
|
|
226
|
+
arr.col(2); // arr[:, 2]
|
|
285
227
|
```
|
|
286
228
|
|
|
287
|
-
###
|
|
288
|
-
|
|
289
|
-
1. Pick a function from [API-REFERENCE.md](https://github.com/dupontcyborg/numpy-ts/blob/main/docs/API-REFERENCE.md)
|
|
290
|
-
2. Follow the [TESTING-GUIDE.md](https://github.com/dupontcyborg/numpy-ts/blob/main/docs/TESTING-GUIDE.md) to add:
|
|
291
|
-
- Implementation in `src/`
|
|
292
|
-
- Unit tests in `tests/unit/`
|
|
293
|
-
- NumPy validation tests in `tests/validation/`
|
|
294
|
-
- Performance benchmarks in `benchmarks/`
|
|
295
|
-
3. Ensure all tests pass: `npm test`
|
|
296
|
-
4. Run benchmarks: `npm run bench:quick`
|
|
297
|
-
5. Submit a pull request
|
|
229
|
+
### Type Safety
|
|
298
230
|
|
|
299
|
-
|
|
231
|
+
```typescript
|
|
232
|
+
const arr = np.zeros([3, 4]); // Type: NDArray<Float64>
|
|
233
|
+
arr.shape; // Type: readonly [3, 4]
|
|
234
|
+
arr.sum(); // Type: number
|
|
235
|
+
```
|
|
300
236
|
|
|
301
|
-
---
|
|
302
237
|
|
|
303
238
|
## Comparison with Alternatives
|
|
304
239
|
|
|
305
240
|
| Feature | numpy-ts | numjs | ndarray | TensorFlow.js |
|
|
306
241
|
|---------|----------|-------|---------|---------------|
|
|
307
|
-
| API Coverage |
|
|
308
|
-
| TypeScript |
|
|
309
|
-
|
|
|
310
|
-
|
|
|
311
|
-
|
|
|
312
|
-
|
|
313
|
-
---
|
|
314
|
-
|
|
315
|
-
## Benchmarking
|
|
316
|
-
|
|
317
|
-
Compare numpy-ts performance against Python NumPy with auto-calibrated benchmarks:
|
|
318
|
-
|
|
319
|
-
```bash
|
|
320
|
-
# Quick benchmarks (~2-3 min) - 1 sample, 50ms/sample
|
|
321
|
-
source ~/.zshrc && conda activate py313 && npm run bench:quick
|
|
322
|
-
|
|
323
|
-
# Standard benchmarks (~5-10 min) - 5 samples, 100ms/sample (default)
|
|
324
|
-
source ~/.zshrc && conda activate py313 && npm run bench
|
|
325
|
-
|
|
326
|
-
# View interactive HTML report with ops/sec comparison
|
|
327
|
-
npm run bench:view
|
|
328
|
-
```
|
|
242
|
+
| NumPy API Coverage | 128/333 (38%) | ~20% | Different | ML-focused |
|
|
243
|
+
| TypeScript Native | ✅ Full | Partial | ❌ No | ✅ Yes |
|
|
244
|
+
| NumPy Validated | ✅ 1365+ tests | Mostly | ❌ No | ❌ No |
|
|
245
|
+
| .npy/.npz Files | ✅ v1/v2/v3 | ❌ No | ❌ No | ❌ No |
|
|
246
|
+
| Broadcasting | ✅ Full | Limited | Limited | ✅ Full |
|
|
247
|
+
| Bundle Size | ~50kb | ~20kb | ~5kb | ~500kb |
|
|
329
248
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
### Performance Overview
|
|
333
|
-
|
|
334
|
-

|
|
335
|
-
|
|
336
|
-
See [benchmarks/README.md](https://github.com/dupontcyborg/numpy-ts/blob/main/benchmarks/README.md) for detailed benchmarking guide.
|
|
337
|
-
|
|
338
|
-
---
|
|
249
|
+
## Contributing
|
|
339
250
|
|
|
340
|
-
|
|
251
|
+
Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed instructions on:
|
|
341
252
|
|
|
342
|
-
|
|
343
|
-
-
|
|
344
|
-
-
|
|
253
|
+
- Setting up the development environment
|
|
254
|
+
- Adding new functions with tests
|
|
255
|
+
- Running benchmarks
|
|
256
|
+
- Submitting pull requests
|
|
345
257
|
|
|
346
|
-
|
|
347
|
-
- WASM for compute-intensive operations
|
|
348
|
-
- SIMD for vectorized operations
|
|
349
|
-
- Target: 2-20x slower than NumPy
|
|
258
|
+
## Documentation
|
|
350
259
|
|
|
351
|
-
|
|
260
|
+
- **[API Reference](docs/API-REFERENCE.md)** — Complete function checklist (120+ functions)
|
|
261
|
+
- **[Feature Details](docs/FEATURES.md)** — Broadcasting, dtypes, views, slicing
|
|
262
|
+
- **[Contributing Guide](CONTRIBUTING.md)** — How to contribute
|
|
263
|
+
- **[Testing Guide](docs/TESTING-GUIDE.md)** — Testing strategy and examples
|
|
264
|
+
- **[Architecture](docs/ARCHITECTURE.md)** — System design and internals
|
|
352
265
|
|
|
353
|
-
---
|
|
354
266
|
|
|
355
267
|
## License
|
|
356
268
|
|
|
357
|
-
[MIT License](
|
|
358
|
-
|
|
359
|
-
---
|
|
360
|
-
|
|
361
|
-
## Links
|
|
362
|
-
|
|
363
|
-
- **NumPy**: https://numpy.org/
|
|
364
|
-
- **Issues**: https://github.com/dupontcyborg/numpy-ts/issues
|
|
269
|
+
[MIT License](LICENSE) — Copyright (c) 2025 Nicolas Dupont
|
|
365
270
|
|
|
366
271
|
---
|
|
367
272
|
|
|
368
|
-
**
|
|
273
|
+
**Bring NumPy to TypeScript!** ⭐
|
|
274
|
+
[GitHub](https://github.com/dupontcyborg/numpy-ts) • [Issues](https://github.com/dupontcyborg/numpy-ts/issues) • [NumPy Docs](https://numpy.org/)
|