Licence
MIT
Version
0.0.5
Deps
4
Size
109 kB
Vulns
0
Weekly
0
@johnhenry/math-plus-adapter-math
The bridge between @johnhenry/math (Vector/Matrix, Symbolic CAS — the
science side) and @johnhenry/math-plus-tensor-core (Tensor — the
engineering side). Three jobs live here: Matrix/Vector Tensor conversion,
compiling Symbolic expressions into the tensor and dataframe worlds, and
the tensor-native numerics (linalg, stats, FFT) that sit on top of the
conversion layer.
Install
npm install @johnhenry/math-plus-adapter-math
Depends on @johnhenry/math, tensor-core, tensor-compile, and
frame-arrow (all @johnhenry/math-plus-*).
Quick start
import { fromMatrix, toMatrix, compileExpr, compileFrameExpr, linalg } from "@johnhenry/math-plus-adapter-math";
import { Symbolic } from "@johnhenry/math";
import { Tensor } from "@johnhenry/math-plus-tensor-core";
// Matrix/Vector <-> Tensor — plain arrays or @johnhenry/math values
const t = fromMatrix([[1, 2, 3], [4, 5, 6]]); // 2x3 f32 Tensor
toMatrix(t.transpose()); // [[1,4],[2,5],[3,6]] — views handled
// Compile a CAS expression to run over tensor batches
const expr = Symbolic.parse("sin(x) * y + sqrt(x^2 + 1)");
const compiled = compileExpr(expr, { variables: ["x", "y"] });
compiled.forward(Tensor.from([0.1, 1.7]), Tensor.from([1.0, 0.3]));
const { localGrads } = compiled.forwardWithGrad(Tensor.from([0.3]), Tensor.from([1]));
// gradients — tested against Symbolic.differentiate as an independent oracle
// Same Symbolic expression as a frame-arrow computed column
const formula = Symbolic.parse("sin(x) * x");
const derivative = Symbolic.differentiate(formula, "x");
frame.withColumns({
y: compileFrameExpr(formula), // free variables become col() refs
dy_dx: compileFrameExpr(derivative), // differentiate symbolically, run columnar
});
// Tensor-native linear algebra
linalg.solve(a, b);
linalg.svd(a);
API surface
- Conversion:
fromMatrix/fromVector(plain arrays or@johnhenry/mathMatrix/Vector; optional{ dtype }),toMatrix/toVector(correct through non-contiguous views). Ragged input throwsRangeError. compileExpr(expr, opts?):Symbolic→ tensor-compile IR. Returns aCompiledFnwithforward(...tensors)andforwardWithGrad(...); default variable order isSymbolic.freeVariables(alphabetical) — passvariablesto pin positions.piecewisecompiles to select chains. Unsupported nodes throwUnsupportedExprError.compileFrameExpr(expr):Symbolic→ frame-arrowExpr. Function names map 1:1 (frame-arrow'sfn.*was spelled to matchSymbolic); unknown columns surface as frame-arrow's own error.linalg:lu/solve/det/inv/rref/rank/nullSpace,qr,cholesky,eigSymmetric(Jacobi) andeigGeneral(complex eigenvalues, returned as@johnhenry/mathComplexNumbers),powerIteration,svd,leastSquares,pseudoInverse, norms andconditionNumber.- Stats:
mean/median/percentile/variance/standardDeviation(+ population variants),correlation,linearRegression,Distributions,HypothesisTests,SpecialFunctions. - FFT:
fft/ifft/fftPadded/convolve/realSignaloverFloat64Arraypairs. - Graphs:
toCSR/toDensefor@johnhenry/mathgraph values. ./test-utils:dualGrad/dualGradN— theDualNumberforward-mode gradient oracle, exported so other packages can check their gradients against an independent implementation.
Traps
compileExprrenamesln→loginternally to match tensor-compile's IR;compileFrameExprneeds no rename table — if you extend one bridge, don't assume the other spells functions the same way.- Variable order is alphabetical unless you pass
variables.Symbolic.parse("b - a")compiled without options takes(a, b)— in that order. - Conversion defaults to
f32. Round-tripping f64 data throughfromMatrixwithout{ dtype: "f64" }quietly drops precision.
Part of the math-plus family — docs at opensource.johnhenry.me/math/.