Mathematical expressions, curves, exact-value helpers and interchangeable numeric backends for JavaScript and TypeScript. Use native Number, BigInt, Neutrium Decimal or a configured adapter supplied by your application. Engineering units and domain rules belong to the consuming package.
npm install @neutrium/math
This documentation describes the initial release being prepared. Use the install command after publication.
The package is ESM and requires Node.js 24 or newer for Node usage. It includes TypeScript declarations and supports browser bundling. Decimal is an installed dependency, but Number, BigInt, syntax and backend-independent helper imports do not load its runtime.
Import the feature first, then its backend. The package root is a convenience entry for curves, not a barrel of every feature.
| Task | Start here | Guide |
|---|---|---|
| Evaluate formulas or build a calculator | /expressions |
Expressions |
| Parse expression text without evaluation | /expressions/syntax |
Syntax |
| Evaluate, interpolate or fit curves | /curves |
Curves |
| Validate or store curve specifications | /curves/definitions |
Persistence |
| Work with exact terms and bounded values | /numeric/rational |
Rational values |
| Calculate exact fractions, roots and powers | /numeric/fractions, /numeric/roots, /numeric/powers |
Fractions and powers |
| Cancel integer products before expansion | /numeric/factors |
Factors |
| Write algorithms for different scalar types | /adapters/contracts and /adapters/* |
Adapters |
| Convert an existing scalar between adapters | /numeric/conversion |
Conversion |
| Implement and test a custom backend | /adapters/testing |
Creating a custom adapter |
Paths in the table are relative to @neutrium/math. See the complete entry-point reference for exports and aliases.
import { Expression } from '@neutrium/math/expressions';
Expression.evaluate('sqrt(3^2 + 4^2)'); // 5
const formula = Expression.compile('a*x + b');
formula.evaluate({ a: 2, x: 3, b: 1 }); // 7
Expression.tryEvaluate('2 +').status; // 'incomplete'
const suggestions = Expression.functions.filter(fn => fn.available);
Compile once for repeated evaluation with fresh bindings. Factories expose frozen function and constant metadata for autocomplete and help. Syntax, bindings and numeric failures have structured errors and source spans. Evaluation uses a mathematical grammar without eval or generated JavaScript.
For Decimal expressions, import Expression or createDecimalExpressionFactory from /expressions/decimal. To reuse an existing backend, use createExpressionFactory(adapter, options?) from /expressions/core.
import { Curve } from '@neutrium/math/curves';
const curve = Curve.polynomial({
coefficients: [40, -3, -0.5],
domain: [0, 6]
});
curve.evaluate(2); // 32
curve.derivative(2); // -5
const fitted = Curve.fit({
points: [[0, 40], [2, 32], [4, 20], [6, 4]],
model: 'polynomial', degree: 2,
});
const restored = Curve.fromJSON(JSON.stringify(fitted.curve));
Curves support polynomial and power-law equations, linear and PCHIP interpolation, piecewise combinations, and regression with diagnostics. Definitions are detached, immutable data with a versioned JSON format. Decimal factories are available from /curves/decimal; adapter-based construction reuses an existing backend.
import { DecimalArithmeticAdapter } from '@neutrium/math/adapters/decimal/arithmetic';
import { parseFraction } from '@neutrium/math/numeric/fractions';
import { powRational } from '@neutrium/math/numeric/powers';
const numeric = new DecimalArithmeticAdapter({ precision: 40, rounding: 'half-even' });
const third = parseFraction('1/3');
const result = powRational(numeric, numeric.from(-8), third);
numeric.toString(result); // '-2'
Use strings when input digits must survive JavaScript Number rounding. Choose the smallest adapter contract your algorithm needs: core, arithmetic or scientific. Rational extraction, construction and comparison are independent optional capabilities; their composite remains RationalArithmeticAdapter<T>.
The /numeric barrel collects backend-independent contracts and helpers. It exports no concrete adapters. Rational helpers offer validation boundaries, structured errors and a reusable RationalComputationOptions limit policy. Backend rounding and range still govern final scalar results.
This project is licensed under the MIT License, see the LICENSE file for details.
You are free to:
Under the following conditions:
This plugin is provided "as is", without warranty of any kind. Use at your own risk.