DEV Community

Avinash
Avinash

Posted on

mathfuse: TypeScript Math Utilities (Statistics, Vectors, Matrices) Zero Dependencies

Tired of pulling in huge math libraries just to compute a mean or dot product? mathfuse gives you a lean, tree-shakeable TypeScript math toolkit with zero dependencies.

What is mathfuse?

A collection of typed math utilities covering statistics, vectors, matrices, and more.

npm install mathfuse
bun add mathfuse
Enter fullscreen mode Exit fullscreen mode

Statistics

import { mean, median, stddev, percentile } from 'mathfuse';

const data = [2, 4, 4, 4, 5, 5, 7, 9];
console.log(mean(data));           // 5
console.log(median(data));         // 4.5
console.log(stddev(data));         // 2
console.log(percentile(data, 75)); // 6
Enter fullscreen mode Exit fullscreen mode

Vectors

import { dot, magnitude, normalize } from 'mathfuse';

const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
console.log(dot(v1, v2));      // 32
console.log(magnitude(v1));    // 3.74
console.log(normalize(v1));    // [0.27, 0.53, 0.80]
Enter fullscreen mode Exit fullscreen mode

Matrices

import { matmul, transpose, determinant } from 'mathfuse';

const A = [[1, 2], [3, 4]];
console.log(matmul(A, [[5,6],[7,8]])); // [[19,22],[43,50]]
console.log(determinant(A));           // -2
Enter fullscreen mode Exit fullscreen mode

Why mathfuse?

  • Zero dependencies
  • Full TypeScript generics
  • Tree-shakeable ESM/CJS
  • Works everywhere (Node.js, Bun, Deno, browsers)

GitHub: https://github.com/Avinashvelu03/mathfuse

What math functions do you wish were easier in TypeScript? Let me know!

Top comments (0)