numpy-ts 0.3.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 -297
- package/dist/numpy-ts.browser.js +2 -397
- package/dist/numpy-ts.esm.js +2 -397
- 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 -412
- package/dist/numpy-ts.node.cjs.map +7 -0
- package/dist/types/core/ndarray.d.ts +477 -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/linalg.d.ts +79 -0
- package/dist/types/ops/shape.d.ts +61 -0
- package/dist/types/ops/trig.d.ts +94 -0
- package/package.json +18 -10
package/README.md
CHANGED
|
@@ -1,372 +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.
|
|
40
|
+
// Array creation with dtype support
|
|
41
|
+
const A = np.array([[1, 2], [3, 4]], 'float32');
|
|
42
|
+
const B = np.ones([2, 2], 'int32');
|
|
36
43
|
|
|
37
|
-
//
|
|
38
|
-
const
|
|
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
|
|
44
|
+
// Broadcasting and chained operations
|
|
45
|
+
const result = A.add(5).multiply(2);
|
|
42
46
|
|
|
43
|
-
//
|
|
44
|
-
const result = intArr.add(5); // Stays int32
|
|
45
|
-
const promoted = intArr.add(floatArr); // Promotes to float32
|
|
46
|
-
|
|
47
|
-
// Matrix operations
|
|
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
|
|
106
|
-
|
|
107
|
-
Pure TypeScript implementation with three layers:
|
|
51
|
+
// Reductions with axis support
|
|
52
|
+
const colMeans = A.mean(0); // [2.0, 3.0]
|
|
108
53
|
|
|
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:]
|
|
109
57
|
```
|
|
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
|
-
└────────────────────────────────┘
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
Built from scratch for correctness and NumPy compatibility.
|
|
126
58
|
|
|
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
|
|
187
|
-
|
|
188
|
-
```bash
|
|
189
|
-
npm install numpy-ts
|
|
190
|
-
```
|
|
163
|
+
Pure TypeScript implementation built from scratch for correctness and NumPy compatibility.
|
|
191
164
|
|
|
192
|
-
|
|
193
|
-
import * as np from 'numpy-ts';
|
|
194
|
-
```
|
|
165
|
+
## Performance
|
|
195
166
|
|
|
196
|
-
|
|
167
|
+

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

|
|
338
|
-
|
|
339
|
-
See [benchmarks/README.md](https://github.com/dupontcyborg/numpy-ts/blob/main/benchmarks/README.md) for detailed benchmarking guide.
|
|
340
|
-
|
|
341
|
-
---
|
|
249
|
+
## Contributing
|
|
342
250
|
|
|
343
|
-
|
|
251
|
+
Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed instructions on:
|
|
344
252
|
|
|
345
|
-
|
|
346
|
-
-
|
|
347
|
-
-
|
|
253
|
+
- Setting up the development environment
|
|
254
|
+
- Adding new functions with tests
|
|
255
|
+
- Running benchmarks
|
|
256
|
+
- Submitting pull requests
|
|
348
257
|
|
|
349
|
-
|
|
350
|
-
- WASM for compute-intensive operations
|
|
351
|
-
- SIMD for vectorized operations
|
|
352
|
-
- Target: 2-20x slower than NumPy
|
|
258
|
+
## Documentation
|
|
353
259
|
|
|
354
|
-
|
|
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
|
|
355
265
|
|
|
356
|
-
---
|
|
357
266
|
|
|
358
267
|
## License
|
|
359
268
|
|
|
360
|
-
[MIT License](
|
|
361
|
-
|
|
362
|
-
---
|
|
363
|
-
|
|
364
|
-
## Links
|
|
365
|
-
|
|
366
|
-
- **NumPy**: https://numpy.org/
|
|
367
|
-
- **@stdlib**: https://stdlib.io/
|
|
368
|
-
- **Issues**: https://github.com/dupontcyborg/numpy-ts/issues
|
|
269
|
+
[MIT License](LICENSE) — Copyright (c) 2025 Nicolas Dupont
|
|
369
270
|
|
|
370
271
|
---
|
|
371
272
|
|
|
372
|
-
**
|
|
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/)
|