Initial commit
Test / gate (push) Failing after 19m0s

This commit is contained in:
alexm committed 2026-11-12 22:23:54 -05:00
commit 401084bab3
265 files changed
+69224

No files matched your search

+186
View File
@@ -0,0 +1,186 @@
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// matho (equations, written in LaTeX) → pseudo: formulas from statistics, probability, physics, chemistry
// and linear algebra, each run through the translation and checked against a value worked out by hand; plus
// the refusals, which have to say what finite procedure to write instead.
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseMatho, runMatho, lowerMatho, defaultCallMatho, splitEquations, tokenizeMatho } from '../dist/services/matho.js';
import { studyOf } from '../dist/services/pseudo.js';
const value = (src, call) => {
const r = parseMatho(src);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.ok(r.ok, errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ') + '\n' + r.program.pseudo);
const tr = runMatho(r, { call, maxSteps: 200000 });
assert.equal(tr.error, null, tr.error?.message);
const m = /returned (.*)$/.exec(tr.output[tr.output.length - 1]);
return { v: Number(m[1]), r, tr };
};
const close = (a, b, eps = 1e-9) => assert.ok(Math.abs(a - b) < eps, `${a} is not within ${eps} of ${b}`);
test('matho: mean and variance, the second referring to the first and hoisted out of the loop', () => {
const src = '\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n\\sigma^2 = \\frac{1}{n - 1} \\sum_{i=0}^{n-1} (x_i - \\bar{x})^2';
const { v, r } = value(src, 'SIGMA_SQUARED(4, [2.0, 4.0, 4.0, 6.0])');
close(v, 8 / 3);
assert.match(r.program.pseudo, /FUNCTION X_BAR\(N : INTEGER, X : LIST OF FLOAT\) RETURNS FLOAT/);
assert.match(r.program.pseudo, /XBar <- X_BAR\(N, X\)\n\s+Total <- 0\.0\n\s+FOR I <- 0 TO N/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-hoist'));
assert.ok(r.diagnostics.some((d) => d.id === 'matho-list' && /X is indexed/.test(d.message)));
assert.equal(value(src, 'X_BAR(4, [2.0, 4.0, 4.0, 6.0])').v, 4);
});
test('matho: a sum from 1 shifts the subscript to a 0-based position, and says so', () => {
const { v, r } = value('\\bar{x} = \\frac{1}{n} \\sum_{i=1}^{n} x_i', 'X_BAR(4, [1.0, 2.0, 3.0, 4.0])');
assert.equal(v, 2.5);
assert.match(r.program.pseudo, /FOR I <- 1 TO N \+ 1\n\s+Total <- Total \+ X\[I - 1\]/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-one-based'));
});
test('matho: a sum with no bounds runs over the whole list', () => {
const { v, r } = value('S = \\sum_i x_i^2', 'S([1.0, 2.0, 3.0])');
assert.equal(v, 14);
assert.match(r.program.pseudo, /FOR I <- 0 TO LENGTH\(X\)/);
});
test('matho: binomial probability, with the extra names becoming parameters and p renamed away from P', () => {
const { v, r } = value('P(k) = \\binom{n}{k} p^k (1 - p)^{n - k}', 'P(2, 4, 0.5)');
close(v, 0.375);
assert.match(r.program.pseudo, /FUNCTION P\(K : INTEGER, N : INTEGER, P2 : FLOAT\)/);
assert.ok(r.diagnostics.some((d) => d.id === 'matho-case'));
assert.ok(r.diagnostics.some((d) => d.id === 'matho-extra-params'));
});
test('matho: physics formulas with subscripted constants and Greek letters', () => {
close(value('h(t) = v_0 t - \\frac{1}{2} g t^2', 'H(2.0, 10.0, 9.81)').v, 20 - 0.5 * 9.81 * 4);
close(value('\\mathrm{KE} = \\frac{1}{2} m v^2', 'KE(3.0, 4.0)').v, 24);
close(value('F = \\frac{G m_1 m_2}{r^2}', 'F(2.0, 3.0, 4.0, 2.0)').v, 6);
const { r } = value('F = \\frac{G m_1 m_2}{r^2}', 'F(2.0, 3.0, 4.0, 2.0)');
assert.match(r.program.pseudo, /FUNCTION F\(G : FLOAT, M1 : FLOAT, M2 : FLOAT, R : FLOAT\)/);
close(value('\\omega = 2 \\pi f', 'OMEGA(0.5)').v, Math.PI);
});
test('matho: the normal density, with e^{...} as EXP and \\sqrt', () => {
const { v } = value('f(x) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}', 'F(0.0, 1.0, 0.0)');
close(v, 1 / Math.sqrt(2 * Math.PI));
});
test('matho: chemistry: pH from a concentration, log bases', () => {
close(value('\\text{pH} = -\\log_{10} [H]', 'PH(0.001)').v, 3);
close(value('b = \\log_2 n', 'B(8.0)').v, 3);
close(value('y = \\ln x + \\log x', 'Y(10.0)').v, Math.log(10) + 1);
});
test('matho: factorial as a product loop, both as n! and as \\prod', () => {
assert.equal(value('n! = \\prod_{k=1}^{n} k', 'FACTORIAL(5)').v, 120);
const { v, r } = value('c = \\frac{n!}{k! (n-k)!}', 'C(5, 2)');
assert.equal(v, 10);
assert.match(r.program.pseudo, /Factorial <- 1\.0/);
assert.match(r.program.pseudo, /Factorial2 <- 1\.0/);
});
test('matho: vectors: dot product, norm, the cosine of the angle', () => {
close(value('\\cos\\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{\\|\\vec{a}\\| \\, \\|\\vec{b}\\|}', 'COS_THETA([1.0, 0.0], [0.0, 1.0])').v, 0);
const { r } = value('\\cos\\theta = \\frac{\\vec{a} \\cdot \\vec{b}}{\\|\\vec{a}\\| \\, \\|\\vec{b}\\|}', 'COS_THETA([1.0, 0.0], [1.0, 0.0])');
assert.match(r.program.pseudo, /RETURN DOT\(A, B\) \/ \(NORM_OF\(A\) \* NORM_OF\(B\)\)/);
close(value('d = \\sqrt{\\sum_{i=0}^{n-1} (p_i - q_i)^2}', 'D(2, [0.0, 0.0], [3.0, 4.0])').v, 5);
});
test('matho: a matrix row times a vector, from a two-letter subscript', () => {
const { v, r } = value('y_i = \\sum_{j=0}^{n-1} A_{ij} x_j', 'Y(1, 2, [[1.0, 2.0], [3.0, 4.0]], [1.0, 1.0])');
assert.equal(v, 7);
assert.match(r.program.pseudo, /FUNCTION Y\(I : INTEGER, N : INTEGER, A : LIST OF LIST OF FLOAT, X : LIST OF FLOAT\)/);
assert.match(r.program.pseudo, /A\[I\]\[J\] \* X\[J\]/);
});
test('matho: nested sums', () => {
const { v } = value('s = \\sum_{i=0}^{n-1} \\sum_{j=0}^{m-1} a_{ij}', 'S(2, 2, [[1.0, 2.0], [3.0, 4.0]])');
assert.equal(v, 10);
});
test('matho: piecewise definitions become IF / ELSE IF / ELSE', () => {
const src = '\\mathrm{relu}(x) = \\begin{cases} 0 & \\text{if } x < 0 \\\\ x & \\text{otherwise} \\end{cases}';
assert.equal(value(src, 'RELU(-2.0)').v, 0);
assert.equal(value(src, 'RELU(3.0)').v, 3);
const tri = 'f(x) = \\begin{cases} 0 & x < 0 \\\\ x & 0 \\le x < 1 \\\\ 2 - x & 1 \\le x \\le 2 \\\\ 0 & \\text{otherwise} \\end{cases}';
assert.equal(value(tri, 'F(0.5)').v, 0.5);
assert.equal(value(tri, 'F(1.5)').v, 0.5);
assert.equal(value(tri, 'F(5.0)').v, 0);
assert.match(parseMatho(tri).program.pseudo, /ELSEIF 0 <= X AND X < 1 THEN/);
});
test('matho: a defined function is called by name later, with explicit and implicit arguments', () => {
const src = 'g(x) = x^2 + 1\nh(x) = 2 g(x) + c';
const { v, r } = value(src, 'H(3.0, 1.0)');
assert.equal(v, 21);
assert.match(r.program.pseudo, /RETURN 2 \* G\(X\) \+ C/);
});
test('matho: a sum over a set uses FOR EACH', () => {
const { v, r } = value('m = \\sum_{x \\in S} x', 'M([1.0, 2.0, 3.5])');
assert.equal(v, 6.5);
assert.match(r.program.pseudo, /FOR EACH X IN S/);
});
test('matho: floors, ceilings, absolute values, max and min, mod', () => {
assert.equal(value('h = \\lfloor n / 2 \\rfloor', 'H(7)').v, 3);
assert.equal(value('h = \\lceil n / 2 \\rceil', 'H(7)').v, 4);
assert.equal(value('d = |a - b|', 'D(2.0, 5.0)').v, 3);
assert.equal(value('m = \\max(a, b) - \\min(a, b)', 'M(2.0, 5.0)').v, 3);
assert.equal(value('r = a \\bmod b', 'R(7, 3)').v, 1);
});
test('matho: the refusals say what to write instead, and the good equations still translate', () => {
const r = parseMatho('a = b^2\n\\int_0^1 f(x) dx\n\\sum_{i=0}^{\\infty} \\frac{1}{2^i}\n\\lim_{x \\to 0} \\frac{\\sin x}{x}\n\\frac{\\partial f}{\\partial x}');
assert.equal(r.ok, false);
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.deepEqual(errs.map((d) => d.line), [2, 3, 4, 5]);
assert.ok(errs.every((d) => d.id === 'matho-refused'));
assert.match(errs[0].message, /sum it approximates/);
assert.match(errs[1].message, /upper bound/);
assert.match(errs[2].message, /not a procedure/);
assert.match(r.program.pseudo, /FUNCTION A\(B : FLOAT\)/);
});
test('matho: syntax problems are reported on the right line, with a sentence', () => {
const r = parseMatho('a = \\frac{1}{\nb = \\foo{x}\nc = x \\pm y\nd = \\begin{cases} 1 & x < 0 \\end{cases}');
const errs = r.diagnostics.filter((d) => d.severity === 'error');
assert.deepEqual(errs.map((d) => [d.line, d.id]), [[1, 'matho-syntax'], [2, 'matho-command'], [3, 'matho-pm'], [4, 'matho-cases-otherwise']]);
assert.match(errs[1].message, /\\foo is not a command this reader knows/);
});
test('matho: a chain of equalities and a bare comparison are refused as statements', () => {
const r = parseMatho('a = b = c\nx < 0');
assert.deepEqual(r.diagnostics.filter((d) => d.severity === 'error').map((d) => d.id), ['matho-chain', 'matho-condition']);
});
test('matho: equations split on lines, continue on operators, and ignore document wrappers', () => {
const parts = splitEquations('\\begin{align}\na &= b\n + c \\\\\nd &= e\n\\end{align}\n\nf = \\begin{cases}\n1 & x < 0 \\\\\n0 & \\text{otherwise}\n\\end{cases}\n% a comment\n$$ g = h $$');
assert.deepEqual(parts.map((p) => p.line), [2, 4, 7, 12]);
assert.match(parts[0].text, /a\s*= b\n\s*\+ c/);
assert.match(parts[2].text, /begin\{cases\}[\s\S]*end\{cases\}/);
});
test('matho: the pseudo it produces reads cleanly and lowers with a line map back to the equations', () => {
const r = parseMatho('\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n\n\\sigma^2 = \\frac{1}{n-1} \\sum_{i=0}^{n-1} (x_i - \\bar{x})^2');
const st = studyOf(r.program.pseudo);
assert.ok(st.ok, st.diagnostics.map((d) => d.message).join(' | '));
const low = lowerMatho(r, 'pseudo');
assert.equal(low.target, 'pseudo');
const from = new Set(low.map.map((m) => m.from));
assert.deepEqual([...from], [1, 3]);
assert.equal(defaultCallMatho(r), 'SIGMA_SQUARED(4, [1.0, 2.0, 3.0, 4.0])');
const tr = runMatho(r, {});
assert.equal(tr.lang, 'matho');
assert.ok(tr.hits.get(3) > 0, 'the trace is mapped back to equation lines');
});
test('matho: the tokenizer marks commands, refused commands, numbers and names', () => {
const toks = tokenizeMatho('\\frac{1}{n} \\sum_{i=0}^{n-1} x_i \\int % note');
const cls = (t) => toks.find((x) => x.text === t)?.cls;
assert.equal(cls('\\frac'), 'tok-keyword');
assert.equal(cls('\\int'), 'tok-error');
assert.equal(cls('1'), 'tok-number');
assert.equal(cls('x'), 'tok-name');
assert.equal(cls('% note'), 'tok-comment');
});