Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# numpst
# numpst

A small TypeScript port of common NumPy-style vector operations.

## API

- `array(iterable)`
- `zeros(length)`
- `ones(length)`
- `arange(start, stop?, step?)`
- `add(a, b)`
- `subtract(a, b)`
- `multiply(a, b)`
- `divide(a, b)`
- `sum(values)`
- `mean(values)`

## Development

```bash
npm install
npm test
```
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "numpst",
"version": "1.0.0",
"description": "TypeScript port of NumPy",
"main": "dist/index.js",
"scripts": {
"test": "npm run build && node --test test",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^6.0.3"
},
"types": "dist/index.d.ts"
}
77 changes: 77 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export type VectorLike = number[];

const assertSameLength = (a: VectorLike, b: VectorLike): void => {
if (a.length !== b.length) {
throw new Error(`Shape mismatch: ${a.length} !== ${b.length}`);
}
};

export const array = (values: Iterable<number>): number[] => [...values];

export const zeros = (length: number): number[] => {
if (!Number.isInteger(length) || length < 0) {
throw new Error("length must be a non-negative integer");
}
return new Array<number>(length).fill(0);
};

export const ones = (length: number): number[] => {
if (!Number.isInteger(length) || length < 0) {
throw new Error("length must be a non-negative integer");
}
return new Array<number>(length).fill(1);
};

export const arange = (start: number, stop?: number, step = 1): number[] => {
if (step === 0) {
throw new Error("step must not be zero");
}

const from = stop === undefined ? 0 : start;
const to = stop === undefined ? start : stop;
const out: number[] = [];

if (step > 0) {
for (let i = from; i < to; i += step) {
out.push(i);
}
} else {
for (let i = from; i > to; i += step) {
out.push(i);
}
}

return out;
};

const elementwise = (
a: VectorLike,
b: VectorLike,
op: (left: number, right: number) => number,
): number[] => {
assertSameLength(a, b);
return a.map((value, idx) => op(value, b[idx]!));
};

export const add = (a: VectorLike, b: VectorLike): number[] =>
elementwise(a, b, (left, right) => left + right);

export const subtract = (a: VectorLike, b: VectorLike): number[] =>
elementwise(a, b, (left, right) => left - right);

export const multiply = (a: VectorLike, b: VectorLike): number[] =>
elementwise(a, b, (left, right) => left * right);

export const divide = (a: VectorLike, b: VectorLike): number[] =>
elementwise(a, b, (left, right) => left / right);

export const sum = (values: VectorLike): number =>
values.reduce((acc, current) => acc + current, 0);

export const mean = (values: VectorLike): number => {
if (values.length === 0) {
throw new Error("cannot compute mean of empty array");
}

return sum(values) / values.length;
};
25 changes: 25 additions & 0 deletions test/numpst.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const np = require('../dist/index.js');

test('array construction helpers', () => {
assert.deepEqual(np.array([1, 2, 3]), [1, 2, 3]);
assert.deepEqual(np.zeros(3), [0, 0, 0]);
assert.deepEqual(np.ones(2), [1, 1]);
assert.deepEqual(np.arange(4), [0, 1, 2, 3]);
assert.deepEqual(np.arange(1, 6, 2), [1, 3, 5]);
});

test('vector arithmetic', () => {
const a = [1, 2, 3];
const b = [4, 5, 6];
assert.deepEqual(np.add(a, b), [5, 7, 9]);
assert.deepEqual(np.subtract(b, a), [3, 3, 3]);
assert.deepEqual(np.multiply(a, b), [4, 10, 18]);
assert.deepEqual(np.divide(b, a), [4, 2.5, 2]);
});

test('reductions', () => {
assert.equal(np.sum([1, 2, 3, 4]), 10);
assert.equal(np.mean([1, 2, 3, 4]), 2.5);
});
46 changes: 46 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Visit https://aka.ms/tsconfig to read more about this file
"compilerOptions": {
// File Layout
"rootDir": "src",
"outDir": "dist",

// Environment Settings
// See also https://aka.ms/tsconfig/module
"module": "commonjs",
"target": "es2020",
"types": [],
// For nodejs:
// "lib": ["esnext"],
// "types": ["node"],
// and npm install -D @types/node

// Other Outputs
"sourceMap": true,
"declaration": true,
"declarationMap": true,

// Stricter Typechecking Options
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,

// Style Options
// "noImplicitReturns": true,
// "noImplicitOverride": true,
// "noUnusedLocals": true,
// "noUnusedParameters": true,
// "noFallthroughCasesInSwitch": true,
// "noPropertyAccessFromIndexSignature": true,

// Recommended Options
"strict": true,
"jsx": "react-jsx",
"verbatimModuleSyntax": false,
"isolatedModules": true,
"noUncheckedSideEffectImports": true,
"moduleDetection": "force",
"skipLibCheck": true,

"esModuleInterop": true,
}
}