math-extensive
v1.0.3
Published
An npm package with non-common math functions: Pascal's triangle/pyramid (binomial expansion), linear programming (simplex method), and extended trigonometry.
Downloads
170
Maintainers
Readme
math-extensive
An npm package with non-common math functions:
- Pascal's triangle & pyramid – binomial expansion coefficients and trinomial (Pascal pyramid) coefficients
- Linear Programming – simplex method for maximisation (
simplex) and two-phase simplex for minimisation (minimise) - Trigonometry – reciprocal, inverse, hyperbolic, versed/haversine functions, triangle laws, and angle utilities
Installation
npm install math-extensiveUsage
const math = require('math-extensive');
// Or import individual modules:
const { pascalRow, pascalTriangle, pascalPyramid, binomialCoefficient } = require('math-extensive/src/pascal');
const { simplex, minimise } = require('math-extensive/src/linearProgramming');
const trig = require('math-extensive/src/trigonometry');Pascal's Triangle & Pyramid
pascalRow(rowIndex)
Returns a single row of Pascal's triangle (zero-based).
math.pascalRow(5); // [1, 5, 10, 10, 5, 1]pascalTriangle(numRows)
Returns the full triangle as a 2-D array.
math.pascalTriangle(4);
// [[1], [1,1], [1,2,1], [1,3,3,1]]binomialCoefficient(n, k)
Computes C(n, k).
math.binomialCoefficient(10, 3); // 120binomialExpansionCoefficients(n)
Returns all coefficients of (a + b)^n.
math.binomialExpansionCoefficients(4); // [1, 4, 6, 4, 1]pascalPyramid(depth)
Generates Pascal's pyramid (trinomial coefficients) up to the given depth.
Layer d contains all coefficients of (a + b + c)^d.
const pyramid = math.pascalPyramid(2);
// pyramid[2] = [[1,2,1],[2,2],[1]] (coefficients of (a+b+c)^2)Linear Programming
simplex(c, A, b) – Maximisation
Maximises c · x subject to A · x ≤ b, x ≥ 0, b ≥ 0.
// max 3x + 5y s.t. x <= 4, 2y <= 12, 3x + 5y <= 25
const { status, value, solution } = math.simplex(
[3, 5],
[[1, 0], [0, 2], [3, 5]],
[4, 12, 25]
);
// status: 'optimal', value: 25, solution: [x, y]Returns { status, value, solution } where status is one of:
'optimal'– optimal solution found'unbounded'– objective is unbounded'infeasible'– no feasible solution (via phase-1 check)
minimise(c, A, b) – Minimisation
Minimises c · x subject to A · x ≥ b, x ≥ 0, b ≥ 0.
Uses two-phase simplex internally.
// min x1 + x2 s.t. x1 + x2 >= 2, x1 >= 1
const { status, value, solution } = math.minimise(
[1, 1],
[[1, 1], [1, 0]],
[2, 1]
);
// status: 'optimal', value: 2Trigonometry
Conversion
math.toRadians(180); // Math.PI
math.toDegrees(Math.PI); // 180Standard (degree variants)
math.sinDeg(90); // 1
math.cosDeg(0); // 1
math.tanDeg(45); // 1Reciprocal functions
| Function | Description |
|----------|-------------|
| csc(rad) / cscDeg(deg) | Cosecant |
| sec(rad) / secDeg(deg) | Secant |
| cot(rad) / cotDeg(deg) | Cotangent |
Inverse reciprocal functions
math.acsc(1); // π/2
math.asec(1); // 0
math.acot(1); // π/4Hyperbolic reciprocal & inverse hyperbolic
math.sech(0); // 1
math.csch(1); // 1/sinh(1)
math.coth(2); // cosh(2)/sinh(2)
math.asech(0.5); // inverse hyperbolic secant
math.acsch(2); // inverse hyperbolic cosecant
math.acoth(2); // inverse hyperbolic cotangentVersed / haversine
math.versin(Math.PI); // 2
math.haversin(Math.PI); // 1
// Great-circle distance between London and Paris (km)
math.haversineDistance(51.5074, -0.1278, 48.8566, 2.3522); // ~340 kmTriangle helpers
// Law of cosines – find side c given sides a, b and angle C (degrees)
math.lawOfCosinesSide(3, 4, 90); // 5 (Pythagoras)
// Law of cosines – find angle C given all three sides
math.lawOfCosinesAngle(1, 1, 1); // 60° (equilateral)
// Law of sines – find side b given side a, angle A, angle B
math.lawOfSinesSide(1, 60, 60); // 1Angle normalisation
math.normalizeAngleDeg(370); // 10
math.normalizeAngleDeg(-90); // 270
math.normalizeAngle(-Math.PI); // Math.PITesting
npm test