ββββ ββββββ βββββββ βββββββββββ βββ βββ βββββββββββββββββ
βββββ ββββββ ββββββββ βββββββββββββββββ ββββ βββββββββββββββββ
ββββββ ββββββ ββββββββββββββββββββββ ββββββββββββββββ ββββββββ
βββββββββββββ βββββββββββββββββββββ βββββ βββββββββ ββββββββ
βββ ββββββββββββββββββ βββ ββββββ βββ βββ ββββββββ
βββ βββββ βββββββ βββ ββββββ βββ βββ ββββββββ
Complete NumPy implementation for TypeScript and JavaScript
npm install numpy-ts- π Extensive API β 336 of 507 NumPy functions (66.3% coverage)
- β NumPy-validated β 3000+ test cases cross-validated against Python NumPy
- π Type-safe β Full TypeScript support with shape and dtype inference
- π Universal β Works in Node.js and browsers with .npy/.npz file support
- π― Zero dependencies β Pure TypeScript, no heavy external libraries
import * as np from 'numpy-ts';
// Array creation with dtype support
const A = np.array([[1, 2], [3, 4]], 'float32');
const B = np.ones([2, 2], 'int32');
// Broadcasting and chained operations
const result = A.add(5).multiply(2);
// Linear algebra
const C = A.matmul(B);
const trace = A.trace();
// Reductions with axis support
const colMeans = A.mean(0); // [2.0, 3.0]
// NumPy-style slicing with strings
const row = A.slice('0', ':'); // A[0, :]
const submatrix = A.slice('0:2', '1:'); // A[0:2, 1:]Progress toward complete NumPy API compatibility:
| Category | Complete | Total | Status |
|---|---|---|---|
| Arithmetic | 29/29 | 100% | β |
| Broadcasting | 3/3 | 100% | β |
| Comparison | 10/10 | 100% | β |
| Exponential | 9/9 | 100% | β |
| Gradient | 4/4 | 100% | β |
| Hyperbolic | 9/9 | 100% | β |
| I/O | 8/8 | 100% | β |
| Indexing | 21/21 | 100% | β |
| Logic | 24/24 | 100% | β |
| Rounding | 7/7 | 100% | β |
| Searching | 7/7 | 100% | β |
| Sorting | 6/6 | 100% | β |
| Trigonometric | 16/16 | 100% | β |
| Reductions | 34/36 | 94% | π‘ |
| Array Creation | 33/35 | 94% | π‘ |
| Array Manipulation | 37/46 | 80% | π‘ |
| Statistics | 9/12 | 75% | π‘ |
| Bit Operations | 9/13 | 69% | π‘ |
| NDArray Methods | 34/53 | 64% | π‘ |
| Linear Algebra (linalg) | 19/31 | 61% | π‘ |
| Linear Algebra | 9/15 | 60% | π‘ |
| Set Operations | 7/12 | 58% | π‘ |
| Other Math | 5/15 | 33% | π΄ |
| Random | 17/53 | 32% | π΄ |
| Utilities | 4/16 | 25% | π΄ |
| FFT | 0/18 | 0% | π΄ |
| Polynomials | 0/10 | 0% | π΄ |
| String/Formatting | 0/10 | 0% | π΄ |
| Type Checking | 0/7 | 0% | π΄ |
| Unplanned | 0/25 | 0% | π΄ |
Overall: 336/507 functions (66.3% complete)
See the complete API Reference for detailed function list.
NumPy-compatible type system with automatic promotion:
| DType | NumPy | numpy-ts | Notes |
|---|---|---|---|
| Floating Point | |||
float64 |
β | β | Default dtype |
float32 |
β | β | |
float16 |
β | Planned (waiting for this) | |
| Signed Integers | |||
int64 |
β | β | Uses BigInt |
int32 |
β | β | |
int16 |
β | β | |
int8 |
β | β | |
| Unsigned Integers | |||
uint64 |
β | β | Uses BigInt |
uint32 |
β | β | |
uint16 |
β | β | |
uint8 |
β | β | |
| Other Numeric | |||
bool |
β | β | Stored as uint8 |
complex64 |
β | β | |
complex128 |
β | β | |
| Non-Numeric | |||
str_ |
β | β | Not planned |
bytes_ |
β | β | Not planned |
object_ |
β | β | Not planned |
datetime64 |
β | β | Not planned |
timedelta64 |
β | β | Not planned |
Supported: 13/20 numeric dtypes
numpy-ts focuses on numeric array computing. The following NumPy features are not planned:
| Feature | Why Not Included |
|---|---|
Datetime/Timedelta (datetime64, timedelta64) |
JS has native Date; libraries like date-fns handle time math better |
| F-order memory layout | Exists in NumPy for Fortran/BLAS interop, which doesn't exist in JS |
Object dtype (object_) |
Defeats the purpose of typed arrays; use regular JS arrays instead |
String/Bytes dtypes (str_, bytes_, U, S) |
JS strings are first-class; no need for fixed-width string arrays |
These omissions keep the library focused and the bundle small. For string manipulation, datetime math, or heterogeneous data, use native JS/TS constructs alongside numpy-ts.
- View tracking β
baseattribute andOWNDATAflag - Strided arrays β C/F contiguous flags for memory layout
- Zero-copy ops β Views for slicing, transpose, reshape (when possible)
const arr = np.ones([4, 4]);
const view = arr.slice('0:2', '0:2');
console.log(view.base === arr); // true - view tracks base
console.log(view.flags.OWNDATA); // false - doesn't own data
console.log(arr.flags.C_CONTIGUOUS); // true - row-major layoutβββββββββββββββββββββββββββββββββββ
β NumPy-Compatible API β
β Broadcasting, DType Promotion β
βββββββββββββββββ¬ββββββββββββββββββ
β
βββββββββββββββββ΄ββββββββββββββββββ
β NDArray (Views & Memory Mgmt) β
β Strided Arrays, Base Tracking β
βββββββββββββββββ¬ββββββββββββββββββ
β- - - - - - - - - - - - - - - - - - β
βββββββββββββββββ΄ββββββββββββββββββ β β β β β β β β β΄ β β β β β β β ββ
β TypeScript / JavaScript Core β β WASM Compute Engine (Future) β
β Computational Engine β β Optimized BLAS / arithmetic β
βββββββββββββββββββββββββββββββββββ β β β β β β β β β β β β β β β β ββ
Pure TypeScript implementation built from scratch for correctness and NumPy compatibility.
See benchmarks/README.md for detailed performance analysis.
Read and write .npy and .npz files with Node.js or browsers.
import { load, save, savez, savez_compressed } from 'numpy-ts/node';
save('array.npy', arr);
const arr = load('array.npy');
savez('arrays.npz', { a: arr1, b: arr2 });
const { a, b } = load('arrays.npz');import * as np from 'numpy-ts';
// Parse fetched .npy file
const response = await fetch('array.npy');
const arr = np.parseNpy(await response.arrayBuffer());
// Serialize for download
const bytes = np.serializeNpy(arr);Why separate imports? The /node entry includes Node.js fs usage. Keeping it separate ensures browser bundles stay clean.
const matrix = np.ones([3, 4]); // (3, 4)
const row = np.arange(4); // (4,)
const result = matrix.add(row); // (3, 4) - row broadcast to each row
const col = np.array([[1], [2], [3]]); // (3, 1)
const grid = col.multiply(row); // (3, 4) - outer product via broadcastingTypeScript doesn't support Python's arr[0:5, :], so we use strings:
arr.slice('0:5', '1:3'); // arr[0:5, 1:3]
arr.slice(':', '-1'); // arr[:, -1]
arr.slice('::2'); // arr[::2]
// Convenience helpers
arr.row(0); // arr[0, :]
arr.col(2); // arr[:, 2]const arr = np.zeros([3, 4]); // Type: NDArray<Float64>
arr.shape; // Type: readonly [3, 4]
arr.sum(); // Type: number| Feature | numpy-ts | numjs | ndarray | TensorFlow.js |
|---|---|---|---|---|
| NumPy API Coverage | 336/507 (66%) | ~20% | Different | ML-focused |
| TypeScript Native | β Full | Partial | β No | β Yes |
| NumPy Validated | β 1365+ tests | Mostly | β No | β No |
| .npy/.npz Files | β v1/v2/v3 | β No | β No | β No |
| Broadcasting | β Full | Limited | Limited | β Full |
| Bundle Size | <50kb | ~60kb | ~5kb | >100kb |
Contributions welcome! See CONTRIBUTING.md for detailed instructions on:
- Setting up the development environment
- Adding new functions with tests
- Running benchmarks
- Submitting pull requests
- API Reference β Complete function checklist (120+ functions)
- Feature Details β Broadcasting, dtypes, views, slicing
- Contributing Guide β How to contribute
- Testing Guide β Testing strategy and examples
- Architecture β System design and internals
MIT License β Copyright (c) 2025 Nicolas Dupont
Bring NumPy to TypeScript! β GitHub β’ Issues β’ NumPy Docs
