diff --git a/scaling-check/README.md b/scaling-check/README.md new file mode 100644 index 0000000..3976a9b --- /dev/null +++ b/scaling-check/README.md @@ -0,0 +1,111 @@ +# Scaling exponents without logarithms + +A check for the sentence "the exponent between two scales lies between p/q and r/s", made on the numbers a table prints. +Exploration, not peer reviewed, no new result claimed. `node scaling-check/table.mjs` prints the verdicts below in an +instant. `node scaling-check/table.mjs verify` (about 45 s) runs the two scripts that print the series and finds every +number again. `npm test` (`tests/scaling-check.mjs`) checks the comparison itself and the verdicts. Integers only, no +dependencies, Node 18 or later (it uses BigInt). + +## What it does + +A series is a list of scales x and values y, such as box sizes L and a magnetisation at each. The slope between two +scales a < b is the exponent in y ~ x^slope, taken over that stretch: ln(y_b / y_a) divided by ln(x_b / x_a). + +For positive rationals and a rational p/q with q > 0: + +> the slope between a and b is at least p/q exactly when (y_b / y_a)^q >= (x_b / x_a)^p. + +Take natural logarithms of both sides and divide by the positive ln(x_b / x_a) to see it. Each side is a fraction raised +to an integer power, so the test is a comparison of two integers. No logarithm is taken, no float is made, nothing is +rounded, and a slope that equals p/q exactly counts as at least p/q. (For negative p the powers move to the other side.) +That powers can stand in for logarithms in a comparison of this kind is elementary. The same move is stated for a +bound on an entropy in Krohn-Grimberghe (2026, arXiv 2608.17376, section 6): "a claimed bound m/n on a rational power is +an integer inequality". Two web searches found no tool that applies it to scaling brackets. That is all this study +claims about prior work. + +`check.mjs` asks whether the slope lies in a closed bracket [p1/q1, p2/q2], in three modes: + +| Mode | What must lie in the bracket | +|---|---| +| window | the slope from the first scale to the last | +| steps | every slope between neighbouring scales | +| all pairs | every slope between any two scales | + +Window is the loosest. Steps and all pairs are stricter, so they reject series that a fit over the whole range accepts: +they ask whether the exponent is steady along the range, not whether its average is right. + +On exact values steps and all pairs always give the same verdict. The slope over a longer stretch is an average of the +step slopes inside it, weighted by ln(x_b / x_a), so it stays in any bracket that holds every step. All pairs is kept as +a cross-check of steps. + +With error bars they can differ. Every step can fit the bracket while a longer stretch does not. Give each value a box +[L, U] with 0 < L <= U, and take a bracket [p1/q1, p2/q2] with q1, q2 > 0 and p1/q1 <= p2/q2. The question is whether +values inside the boxes exist with every step slope in the bracket. + +The interval form of all pairs decides it exactly: for every i < j, (U_j / L_i)^q1 >= (x_j / x_i)^p1 and +(L_j / U_i)^q2 <= (x_j / x_i)^p2. This is the consistency test for a chain of difference constraints, the simple temporal +problem of Dechter, Meiri and Pearl (1991). Foundation-0/fractals draft pull request 60 states it and applies it. + +The interval form is not a mode of `check.mjs`, which takes point values. `tests/scaling-check.mjs` pins a smallest case. +It has three scales, the fewest that can differ, since with two scales the only pair is the one step. The scales are 1, +2, 4, the bracket is [1, 1] and the boxes are [1, 1], [3/2, 5/2] and [9/2, 5]. Each step alone admits slope 1, and the +pair of the first and last scales does not. + +`exponentInLog2` is the same test for a printed column that already holds base-2 logarithms of y at x = 2^n, such as +log2 <|u_n|^2> against shell n. The test then reads q (L_b - L_a) >= p (n_b - n_a), a comparison of decimals. + +## What it certifies, and what it does not + +It certifies a statement about the printed numbers: that the slopes the mode looks at, computed from exactly the +digits printed, lie in the bracket. The printed digits are rounded, and the verdict is about them as printed. + +It says nothing about measurement error. A table with an error bar of 0.5% on each value gives a slope uncertain by about +as much, and the check does not know that. A bracket chosen narrower than that uncertainty can reject a correct +exponent, which is why steps and all pairs reject some series here. It says nothing about the physics: whether the +exponent should be there, whether the range is long enough, or whether a model is right. + +## The series, and the verdict on each + +Every number is printed by a script in this repository, and `series.mjs` pins the commit and the place. The brackets are +the published exponent plus and minus 1/100, 3/100 and 1/10. The last line of each block is the smallest half-width, +on a grid of 0.001, that each mode accepts. `node scaling-check/table.mjs` prints all of this, and `npm test` checks +the verdicts. + +| Series (source: `node critical/table.mjs`, "At T_c", commit 48b923b) | Published | window | steps | all pairs | +|---|---|---|---|---| +| 2D Ising magnetisation, L = 8 to 128 | slope -1/8 (exact) | accepts at 0.001 | rejects at 1/100, accepts at 3/100 (0.023) | same as steps (0.023) | +| 2D Ising susceptibility, L = 8 to 128 | slope 7/4 (exact) | accepts at 0.001 | rejects at 1/100 and 3/100, accepts at 1/10 (0.032) | same as steps (0.032) | +| 2D Ising correlation G(L/2), L = 8 to 128 | slope -1/4 (exact) | accepts at 0.003 | rejects at 1/100 and 3/100, accepts at 1/10 (0.039) | same as steps (0.039) | +| Site percolation, spanning-cluster mass, L = 32 to 256, seed 401 (`node critical/table.mjs`, percolation table) | 91/48 | **rejects at 1/100**, accepts at 3/100 (0.024) | rejects at 3/100, accepts at 1/10 (0.039) | same as steps (0.039) | +| Sabra shell model, plain moment <\|u_n\|^2> against k_n = 2^n, shells 3 to 8 (`node turbulence/table.mjs quick`, last column, printed as log2) | zeta_2 = 0.720 | **rejects at 1/100 and 3/100**, accepts at 1/10 (slope 0.756, 0.036) | rejects at 1/10 (0.370) | same as steps (0.370) | + +How to read the rows that reject: + +- **Percolation, one seed.** The printed masses 398, 1442, 5308, 19546 give a window slope of 1.8727, below 91/48 = 1.8958 + by 0.023. (The percolation table's own least-squares fit over the same four sizes prints 1.8735.) Two things bear on + it. This is the study seed alone, and the series starts at L = 32. `critical/README.md` quotes the nine-seed mean, + 1.880 ± 0.002, 0.8% below 91/48, and explains it as a finite-size shortfall. From L = 64 on, this seed's window slope + is 1.8804, still 0.015 below 91/48 and still outside plus and minus 1/100. The check does not test the finite-size + explanation. And a bracket of plus and minus 1/100 is narrower than the shortfall the README already reports. +- **Sabra, plain moment.** The printed column gives a window slope of 0.756 over shells 3 to 8. It lies outside + 0.720 ± 0.008 and outside the Kolmogorov value 2/3 ± 1/30. `turbulence/README.md` says the plain moments ripple from + shell to shell and are "printed but not used": its exponents come from the flux-based moments against the third one + (extended self-similarity), and its own table prints the plain-moment ζ₂ as 0.760. So the rejection restates a + difference the turbulence study already prints. L'vov et al.'s Table I gives ζ₂ = 0.720 for the plain moments + ⟨|u_n|^q⟩ and 0.719 for the flux-based ones. They fitted both with a power law that bends into the dissipative range + (their eq. 44), over 34 shells, not with a straight slope over a few shells. So this row measures how far the straight + slope of the printed column is from 0.720. It is not a test of the model against their table. +- **Ising, steps and all pairs.** Each value is a Monte Carlo average with an error of 0.4% to 1.4%, and slopes between + neighbouring sizes (a factor of 2) move by a few hundredths. Rejection at 1/100 by these two modes is expected and + says the bracket is narrower than the noise. + +## What is checked and what is not + +- Checked by `npm test`: the comparison against 80-digit logarithms on 3,000 random cases, exact ties, the printed-log2 + form against the ratio form, four must-fail controls, the verdict and the smallest half-width in the table for every + series, and that `scaling-check/table.mjs` runs and prints the same half-widths. +- Also checked by `npm test`: the three-scale case with error bars where steps pass and all pairs fail, its widened + twin, and two brackets that guard the interval inequality itself. +- Checked by `node scaling-check/table.mjs verify`, not by `npm test` (it runs the Monte Carlo): that every pinned number + is in what the two scripts print in the current checkout. At the pinned commit every one of them is. +- Not checked: that a different seed or a longer run gives the same verdicts. diff --git a/scaling-check/check.mjs b/scaling-check/check.mjs new file mode 100644 index 0000000..51a4220 --- /dev/null +++ b/scaling-check/check.mjs @@ -0,0 +1,108 @@ +// Exponent brackets without logarithms or floats. For positive rationals, the slope between scales a < b is at least +// p/q exactly when (y_b / y_a)^q >= (x_b / x_a)^p (q > 0, p any integer). Both sides are rationals raised to integer +// powers, so the comparison is a comparison of BigInt integers: no rounding, and a tie is a tie. +// A rational is a pair [num, den] of positive integers (BigInt, or Number holding a safe integer). +// Used by scaling-check/table.mjs and tests/scaling-check.mjs. No dependencies. + +const big = v => { + if (typeof v === 'bigint') return v; + if (typeof v === 'number' && Number.isSafeInteger(v)) return BigInt(v); + throw new TypeError(`expected an integer, got ${String(v)}`); +}; +const rat = r => { + const n = big(r[0]), d = big(r[1]); + if (n <= 0n || d <= 0n) throw new RangeError('positive rationals only'); + return [n, d]; +}; + +// A decimal string such as "0.7819" or "-12.71" as [num, den] (num may be negative; den > 0). No float is made. +export function dec(s) { + const m = /^(-?)(\d+)(?:\.(\d+))?$/.exec(String(s).trim()); + if (!m) throw new SyntaxError(`not a decimal: ${s}`); + const frac = m[3] || ''; + const num = BigInt(m[2] + frac) * (m[1] ? -1n : 1n); + return [num, 10n ** BigInt(frac.length)]; +} + +// Sign (+1, 0, -1) of (y_b/y_a)^q - (x_b/x_a)^p, in integers only. +export function cmpPower(xa, xb, ya, yb, p, q) { + const Q = big(q), P = big(p); + if (Q <= 0n) throw new RangeError('q must be positive'); + const [xan, xad] = rat(xa), [xbn, xbd] = rat(xb), [yan, yad] = rat(ya), [ybn, ybd] = rat(yb); + const ryn = ybn * yad, ryd = ybd * yan; // y_b / y_a + const rxn = xbn * xad, rxd = xbd * xan; // x_b / x_a + const lhs = P >= 0n ? ryn ** Q * rxd ** P : ryn ** Q * rxn ** -P; + const rhs = P >= 0n ? rxn ** P * ryd ** Q : rxd ** -P * ryd ** Q; + return lhs > rhs ? 1 : lhs < rhs ? -1 : 0; +} + +// The pairs of points a mode looks at: 'window' the first and last, 'steps' each consecutive pair, 'pairs' every pair. +export function pairsFor(n, mode) { + if (mode === 'window') return [[0, n - 1]]; + const out = []; + if (mode === 'steps') { for (let i = 0; i + 1 < n; i++) out.push([i, i + 1]); return out; } + if (mode === 'pairs') { for (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) out.push([i, j]); return out; } + throw new RangeError(`unknown mode: ${mode}`); +} + +// True when every slope the mode looks at lies in the closed bracket [lo, hi]; lo and hi are [p, q] with q > 0. +export function exponentIn(xs, ys, lo, hi, mode = 'window') { + if (xs.length !== ys.length || xs.length < 2) throw new RangeError('need two or more points, xs and ys the same length'); + for (const [i, j] of pairsFor(xs.length, mode)) { + if (cmpPower(xs[i], xs[j], ys[i], ys[j], lo[0], lo[1]) < 0) return false; + if (cmpPower(xs[i], xs[j], ys[i], ys[j], hi[0], hi[1]) > 0) return false; + } + return true; +} + +// Same test when the printed values are base-2 logarithms of y at x = 2^n (a column such as log2 <|u_n|^2> against shell +// n). Then (y_b/y_a)^q >= (x_b/x_a)^p reads 2^(q dL) >= 2^(p dn), that is q dL >= p dn, with dL a difference of decimals. +// ns are integers, ls are [num, den] decimals (den > 0, num any sign). Integers only. +export function exponentInLog2(ns, ls, lo, hi, mode = 'window') { + if (ns.length !== ls.length || ns.length < 2) throw new RangeError('need two or more points, ns and ls the same length'); + for (const [i, j] of pairsFor(ns.length, mode)) { + const dn = big(ns[j]) - big(ns[i]); + const [an, ad] = ls[i], [bn, bd] = ls[j]; + const dLn = bn * ad - an * bd, dLd = ad * bd; // L_b - L_a = dLn / dLd, dLd > 0 + // slope = dLn / (dLd dn); compare q * dLn against p * dLd * dn (dn > 0) + if (big(lo[1]) * dLn < big(lo[0]) * dLd * dn) return false; + if (big(hi[1]) * dLn > big(hi[0]) * dLd * dn) return false; + } + return true; +} + +// Smallest half-width w = k/step, k = 0, 1, 2, ..., kMax, for which accepts(lo, hi, mode) holds on the bracket +// [e - w, e + w], or null if none up to kMax/step. e is [p, q]. accepts is exponentIn or exponentInLog2 with its data bound. +// Each trial is exact; the grid is the only coarseness. +export function tightestHalfWidth(accepts, e, mode, step = 1000, kMax = 1000) { + const p = big(e[0]), q = big(e[1]), st = BigInt(step); + for (let k = 0; k <= kMax; k++) { + const den = q * st; + if (accepts([p * st - BigInt(k) * q, den], [p * st + BigInt(k) * q, den], mode)) return [k, step]; + } + return null; +} + +// ---- reference used by the tests: natural logarithm of a positive rational to about 70 decimal digits, BigInt only. +const SCALE_DIGITS = 80n, S = 10n ** SCALE_DIGITS; +function atanhFixed(zn, zd) { // atanh(zn/zd) as fixed point, |zn/zd| < 1/5 + const z = zn * S / zd, z2 = z * z / S; + let term = z, sum = 0n, k = 1n; + while (term !== 0n) { sum += term / k; term = term * z2 / S; k += 2n; } + return sum; +} +const LN2 = 2n * atanhFixed(1n, 3n); +export function lnFixed(num, den) { // ln(num/den) times 10^80, truncated + let n = big(num), d = big(den), k = 0n; + if (n <= 0n || d <= 0n) throw new RangeError('positive rationals only'); + while (n * n > 2n * d * d) { d *= 2n; k += 1n; } + while (2n * n * n < d * d) { n *= 2n; k -= 1n; } + return k * LN2 + 2n * atanhFixed(n - d, n + d); +} +// Sign of q ln(y_b/y_a) - p ln(x_b/x_a), or 0 when its size is under 10^(-60) (a tie or too close to call). +export function cmpPowerByLogs(xa, xb, ya, yb, p, q) { + const [xan, xad] = rat(xa), [xbn, xbd] = rat(xb), [yan, yad] = rat(ya), [ybn, ybd] = rat(yb); + const v = big(q) * lnFixed(ybn * yad, ybd * yan) - big(p) * lnFixed(xbn * xad, xbd * xan); + const tiny = 10n ** (SCALE_DIGITS - 60n); + return v > tiny ? 1 : v < -tiny ? -1 : 0; +} diff --git a/scaling-check/series.mjs b/scaling-check/series.mjs new file mode 100644 index 0000000..1ee248f --- /dev/null +++ b/scaling-check/series.mjs @@ -0,0 +1,41 @@ +// The series the checker is applied to: numbers printed by this repository's own scripts, copied here as the strings the +// scripts print (rounded, as printed). Each series says which script prints it, at which commit, and where in the output, +// so `node scaling-check/table.mjs verify` can run the script and find every number again. +// Source commit for every number below: 48b923bd25dbcfef7fa8e8399d4144592145f0ef (main when this study was added). +import { dec, exponentIn, exponentInLog2 } from './check.mjs'; +export const SOURCE_COMMIT = '48b923bd25dbcfef7fa8e8399d4144592145f0ef'; + +// 'critical' prints the Ising table "At T_c" (columns <|m|>, chi, G(L/2)) and the percolation table (spanning cluster). +// 'turbulence' is `node turbulence/table.mjs quick`, the cascade table's last column, log2 <|u_n|^2>. +export const SERIES = [ + { id: 'ising-m', kind: 'ratio', what: '2D Ising at T_c, magnetisation <|m|> against box size L', command: 'critical/table.mjs', + after: /^At T_c \(runs at/, col: 2, rows: [8, 16, 32, 64, 128], + x: [8, 16, 32, 64, 128], y: ['0.7819', '0.7060', '0.6537', '0.5975', '0.5520'], + published: [-1, 8], publishedLabel: 'beta/nu = 1/8, so slope -1/8 (Onsager, Yang: exact)' }, + { id: 'ising-chi', kind: 'ratio', what: '2D Ising at T_c, susceptibility chi = N against L', command: 'critical/table.mjs', + after: /^At T_c \(runs at/, col: 5, rows: [8, 16, 32, 64, 128], + x: [8, 16, 32, 64, 128], y: ['41.8', '137.6', '469.2', '1572', '5337'], + published: [7, 4], publishedLabel: 'gamma/nu = 7/4 (exact)' }, + { id: 'ising-g', kind: 'ratio', what: '2D Ising at T_c, correlation G(L/2) against L', command: 'critical/table.mjs', + after: /^At T_c \(runs at/, col: 8, rows: [8, 16, 32, 64, 128], + x: [8, 16, 32, 64, 128], y: ['0.6324', '0.5177', '0.4421', '0.3706', '0.3139'], + published: [-1, 4], publishedLabel: 'eta = 1/4, so slope -1/4 (exact)' }, + { id: 'perc-mass', kind: 'ratio', what: 'site percolation at p_c, spanning-cluster mass against L (study seed 401, one seed)', command: 'critical/table.mjs', + after: /^Site percolation, open edges, spanning top to bottom/, col: 8, rows: [32, 64, 128, 256], + x: [32, 64, 128, 256], y: ['398', '1442', '5308', '19546'], + published: [91, 48], publishedLabel: 'D = 91/48 (den Nijs, Nienhuis et al.; proved for the triangular lattice)' }, + { id: 'sabra-u2', kind: 'log2', what: 'Sabra shell model (CI run), plain moment <|u_n|^2> against k_n = 2^n, shells 3 to 8, printed as log2', command: 'turbulence/table.mjs quick', + after: /^ {2}n {3}Pi_n\/eps/, col: 4, rows: [3, 4, 5, 6, 7, 8], + x: [3, 4, 5, 6, 7, 8], y: ['-8.93', '-9.43', '-10.52', '-10.94', '-11.85', '-12.71'], + published: [-18, 25], publishedLabel: "zeta_2 = 0.720 +- 0.008 (L'vov et al. 1998, moments of u); slope -0.720" }, +]; + +// The check bound to a series: (lo, hi, mode) -> true when every slope the mode looks at lies in [lo, hi], lo and hi [p, q]. +export function acceptor(s) { + if (s.kind === 'log2') { + const ls = s.y.map(dec); + return (lo, hi, mode) => exponentInLog2(s.x, ls, lo, hi, mode); + } + const xs = s.x.map(v => [BigInt(v), 1n]), ys = s.y.map(v => { const [n, d] = dec(v); return [n, d]; }); + return (lo, hi, mode) => exponentIn(xs, ys, lo, hi, mode); +} diff --git a/scaling-check/table.mjs b/scaling-check/table.mjs new file mode 100644 index 0000000..5929823 --- /dev/null +++ b/scaling-check/table.mjs @@ -0,0 +1,61 @@ +// Applies the no-logarithm exponent check (scaling-check/check.mjs) to series printed by this repository's own scripts and +// prints a verdict for each, in three modes. Usage: node scaling-check/table.mjs (instant, integers only) +// node scaling-check/table.mjs verify (runs the two source scripts, about 45 s, +// and finds every number in their output) +// A verdict is about the printed numbers and nothing else: no error bar, no physics. See scaling-check/README.md. +import { spawnSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; +import { tightestHalfWidth } from './check.mjs'; +import { SERIES, SOURCE_COMMIT, acceptor } from './series.mjs'; + +const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); +const MODES = ['window', 'steps', 'pairs']; +const TOLS = [[1n, 100n], [3n, 100n], [1n, 10n]]; + +// Closed bracket [e - tol, e + tol] as two [p, q] pairs. +const bracket = (e, [tn, td]) => { + const [p, q] = [BigInt(e[0]), BigInt(e[1])]; + return [[p * td - tn * q, q * td], [p * td + tn * q, q * td]]; +}; +const word = ok => (ok ? 'accepts' : 'rejects'); +const grid = ([k, step]) => `${(k / step).toFixed(3)}`; + +function verdicts() { + for (const s of SERIES) { + const acc = acceptor(s); + console.log(`\n${s.what}\n published: ${s.publishedLabel}\n points: ${s.x.join(', ')} -> ${s.y.join(', ')}`); + for (const t of TOLS) { + const [lo, hi] = bracket(s.published, t); + console.log(` bracket +-${t[0]}/${t[1]}: ` + MODES.map(m => `${m} ${word(acc(lo, hi, m))}`).join(', ')); + } + console.log(' smallest half-width that is accepted (grid 0.001): ' + + MODES.map(m => { const w = tightestHalfWidth(acc, s.published, m); return `${m} ${w ? grid(w) : 'over 1.000'}`; }).join(', ')); + } +} + +function verify() { + const outputs = {}; + for (const cmd of [...new Set(SERIES.map(s => s.command))]) { + const [script, ...args] = cmd.split(' '); + const r = spawnSync(process.execPath, [path.join(root, script), ...args], { encoding: 'utf8', maxBuffer: 1 << 26, timeout: 600000 }); + if (r.status !== 0) { console.log(`FAIL ${cmd} exited with ${r.status}`); process.exit(1); } + outputs[cmd] = r.stdout.split('\n'); + } + let bad = 0; + for (const s of SERIES) { + const lines = outputs[s.command], start = lines.findIndex(l => s.after.test(l)); + if (start < 0) { console.log(`FAIL ${s.id}: section not found in ${s.command}`); bad++; continue; } + s.rows.forEach((row, i) => { + const line = lines.slice(start + 1).find(l => l.trim().split(/\s+/)[0] === String(row)); + const tok = line ? line.trim().split(/\s+/)[s.col] : undefined; + const ok = tok === s.y[i]; + if (!ok) bad++; + console.log(`${ok ? 'PASS' : 'FAIL'} ${s.id} row ${row}: pinned ${s.y[i]}, printed ${tok}`); + }); + } + console.log(bad ? `\n${bad} number(s) differ from the printed output` : `\nevery pinned number is in the output of the scripts (source commit ${SOURCE_COMMIT.slice(0, 7)})`); + process.exit(bad ? 1 : 0); +} + +if (process.argv[2] === 'verify') verify(); else verdicts(); diff --git a/tests/scaling-check.mjs b/tests/scaling-check.mjs new file mode 100644 index 0000000..7c627b0 --- /dev/null +++ b/tests/scaling-check.mjs @@ -0,0 +1,253 @@ +// Checks the no-logarithm exponent check (scaling-check/check.mjs): known answers, exact ties, 3000 random cases against +// 80-digit natural logarithms (BigInt fixed point, no float), the same test on printed base-2 logarithms against the ratio +// form, four must-fail controls (a wrong exponent, a dropped tie, a dropped sign, a perturbed point: each is run through a +// deliberately broken version and the test requires the broken version to give the wrong answer), and the verdicts on the +// series in scaling-check/series.mjs, and a three-scale case with error bars where steps pass and all pairs fail. +// Run: npm test (or node tests/scaling-check.mjs). Integers only: nothing here can flake, and no check is an assert +// that a flag could strip. Each line states what it guards. +import { spawnSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { cmpPower, cmpPowerByLogs, dec, exponentIn, exponentInLog2, pairsFor, tightestHalfWidth } from '../scaling-check/check.mjs'; +import { SERIES, acceptor } from '../scaling-check/series.mjs'; + +let fails = 0; +const check = (name, ok, detail = '') => { console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail ? `\n ${detail}` : ''}`); if (!ok) fails++; }; +const R = (n, d = 1) => [BigInt(n), BigInt(d)]; +const throws = f => { try { f(); return false; } catch { return true; } }; + +// 1. Known answers for one pair of scales. +{ + // y = x^(3/2) between x = 1 and 4: y goes 1 -> 8, slope exactly 3/2. + const c = (p, q) => cmpPower(R(1), R(4), R(1), R(8), p, q); + check('Known answer: slope 8 over 4 is exactly 3/2 (tie, 0), above 1 (+1), below 2 (-1)', c(3, 2) === 0 && c(1, 1) === 1 && c(2, 1) === -1); + check('Known answer: unreduced fractions give the same slope (6/4, 9/6)', c(6, 4) === 0 && c(9, 6) === 0); + // falling: y 8 -> 1 over x 1 -> 4: slope -3/2. + const f = (p, q) => cmpPower(R(1), R(4), R(8), R(1), p, q); + check('Known answer: slope -3/2 for a falling series; above -2, below -1, tie at -3/2', f(-2, 1) === 1 && f(-1, 1) === -1 && f(-3, 2) === 0); + check('Known answer: slope 0 for a flat series; tie at 0/1, below 1/100', cmpPower(R(2), R(10), R(5), R(5), 0, 1) === 0 && cmpPower(R(2), R(10), R(5), R(5), 1, 100) === -1); + check('Known answer: fractions as scales and values (x 1/2 -> 2, y 3/4 -> 12: ratio 16 over 4, slope 2)', cmpPower(R(1, 2), R(2), R(3, 4), R(12), 2, 1) === 0); + check('Refuses q <= 0, zero or negative values, non-integers', throws(() => cmpPower(R(1), R(2), R(1), R(2), 1, 0)) && + throws(() => cmpPower(R(1), R(2), [0n, 1n], R(2), 1, 1)) && throws(() => cmpPower(R(1), R(2), R(1), [-2n, 1n], 1, 1)) && + throws(() => cmpPower(R(1), R(2), R(1), R(2), 0.5, 1))); + check('Decimal strings parse to exact fractions, no float', dec('0.7819')[0] === 7819n && dec('0.7819')[1] === 10000n && dec('-12.71')[0] === -1271n && throws(() => dec('1e-3'))); +} + +// 2. Exact ties: y = x^(p/q) at points where both ratios are integers. +{ + let n = 0, bad = 0; + for (const [p, q] of [[3, 2], [-1, 3], [0, 1], [5, 4], [-7, 2], [2, 1], [91, 48], [-18, 25]]) { + for (const r of [2, 3, 4, 9]) { + const xr = BigInt(r) ** BigInt(q), ypow = BigInt(r) ** BigInt(Math.abs(p)); // x ratio r^q, y ratio r^p + const yb = p >= 0 ? [ypow, 1n] : [1n, ypow]; + n++; + if (cmpPower(R(1), [xr, 1n], R(1), yb, p, q) !== 0) bad++; + if (cmpPower(R(1), [xr, 1n], R(1), [yb[0] * 2n, yb[1]], p, q) !== 1) bad++; // doubling y moves it strictly above + if (cmpPower(R(1), [xr, 1n], R(1), [yb[0], yb[1] * 2n], p, q) !== -1) bad++; // halving moves it strictly below + } + } + check('Exact ties are ties, and doubling or halving y leaves the tie on the right side', bad === 0, `${n} ties, ${bad} wrong answers`); +} + +// 3. Against 80-digit logarithms on random cases (fixed seed, integer generator). +{ + let s = 20261120; + const rnd = (lo, hi) => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return lo + (s >>> 8) % (hi - lo + 1); }; + const N = 3000; + let bad = 0, compared = 0; + for (let i = 0; i < N; i++) { + const xa = R(rnd(1, 50), rnd(1, 9)), xb = [xa[0] * BigInt(rnd(2, 9)), xa[1]]; + const ya = R(rnd(1, 99), rnd(1, 99)), yb = R(rnd(1, 99), rnd(1, 99)); + const p = rnd(-9, 9), q = rnd(1, 6); + const ref = cmpPowerByLogs(xa, xb, ya, yb, p, q); + if (ref === 0) continue; // a tie or closer than 1e-60: not decided by the reference + compared++; + if (cmpPower(xa, xb, ya, yb, p, q) !== ref) bad++; + } + check('3000 random cases agree with 80-digit natural logarithms', bad === 0 && compared > 2900, `${compared} compared, ${bad} disagree`); + check('The log reference is not vacuous: slope 3/2 on 4 -> 8 is above 1.4999, below 1.5001, and a tie at 3/2', + cmpPowerByLogs(R(1), R(4), R(1), R(8), 14999, 10000) === 1 && cmpPowerByLogs(R(1), R(4), R(1), R(8), 15001, 10000) === -1 && cmpPowerByLogs(R(1), R(4), R(1), R(8), 3, 2) === 0); +} + +// 4. Brackets over a series, three modes. +{ + const xs = [R(1), R(4), R(16), R(64)], ys = [R(1), R(8), R(64), R(512)]; // y = x^(3/2) exactly + check('Series known answer: y = x^(3/2) is inside [3/2, 3/2] in all three modes', ['window', 'steps', 'pairs'].every(m => exponentIn(xs, ys, [3, 2], [3, 2], m))); + check('Series known answer: and outside [1, 1] and [2, 2]', !exponentIn(xs, ys, [1, 1], [1, 1]) && !exponentIn(xs, ys, [2, 2], [2, 2]) && !exponentIn(xs, ys, [3, 2], [1, 1])); + // window passes where steps and pairs fail: the middle point is off the line. + const ys2 = [R(1), R(8), R(80), R(512)]; + check('Modes differ: a bump in the middle leaves the window tie but fails steps and pairs', exponentIn(xs, ys2, [3, 2], [3, 2], 'window') && !exponentIn(xs, ys2, [3, 2], [3, 2], 'steps') && !exponentIn(xs, ys2, [3, 2], [3, 2], 'pairs')); + check('Modes differ: steps and pairs look at 2 and 3 slopes of a 3-point series', pairsFor(3, 'steps').length === 2 && pairsFor(3, 'pairs').length === 3 && pairsFor(3, 'window').length === 1); + check('Refuses an unknown mode, one point, mismatched lengths', throws(() => exponentIn(xs, ys, [1, 1], [2, 1], 'all')) && throws(() => exponentIn([R(1)], [R(1)], [1, 1], [2, 1])) && throws(() => exponentIn(xs, ys.slice(1), [1, 1], [2, 1]))); +} + +// 5. Printed base-2 logarithms: exponentInLog2 against the ratio form where both apply (x = 2^n, y = 2^L, L an integer). +{ + let s = 99, bad = 0, n = 0; + const rnd = (lo, hi) => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return lo + (s >>> 8) % (hi - lo + 1); }; + for (let i = 0; i < 400; i++) { + const m = rnd(2, 5), ns = [], Ls = [], n0 = rnd(0, 6); + let L = rnd(-5, 5); + for (let j = 0; j < m; j++) { ns.push(n0 + j); L += rnd(-4, 4); Ls.push(L); } + const lo = [rnd(-6, 2), rnd(1, 4)], hi = [lo[0] + rnd(0, 8), lo[1]]; + for (const mode of ['window', 'steps', 'pairs']) { + n++; + const ratio = exponentIn(ns.map(v => [2n ** BigInt(v), 1n]), Ls.map(v => (v >= 0 ? [2n ** BigInt(v), 1n] : [1n, 2n ** BigInt(-v)])), lo, hi, mode); + const logs = exponentInLog2(ns, Ls.map(v => [BigInt(v), 1n]), lo, hi, mode); + if (ratio !== logs) bad++; + } + } + check('Printed-log2 form agrees with the ratio form on 1200 integer cases', bad === 0, `${n} compared, ${bad} disagree`); + // Steps and all pairs give the same verdict (a longer slope is a weighted average of the step slopes), and window is + // never stricter than steps. Checked on random ratio series with uneven scale steps. + let sameBad = 0, winBad = 0, differ = 0; + for (let i = 0; i < 400; i++) { + const m = rnd(3, 6), xs = [], ys = []; + let x = rnd(1, 5); + for (let j = 0; j < m; j++) { xs.push(R(x)); ys.push(R(rnd(1, 400), rnd(1, 9))); x *= rnd(2, 4); } + const lo = [rnd(-30, 10), 10], hi = [lo[0] + rnd(0, 30), 10]; + const st = exponentIn(xs, ys, lo, hi, 'steps'), pr = exponentIn(xs, ys, lo, hi, 'pairs'), wi = exponentIn(xs, ys, lo, hi, 'window'); + if (st !== pr) sameBad++; + if (st && !wi) winBad++; + if (wi && !st) differ++; + } + check('Steps and all pairs agree on 400 random series, window accepts whenever steps does, and window is looser on some', + sameBad === 0 && winBad === 0 && differ > 0, `${sameBad} steps/pairs disagreements, ${winBad} window rejections under accepted steps, ${differ} looser windows`); + const ls = ['0', '1.5', '3', '4.5'].map(dec); + check('Printed-log2 known answer: slope exactly 3/2 is accepted by [3/2, 3/2] and not by [1, 7/5] or [8/5, 2]', + exponentInLog2([0, 1, 2, 3], ls, [3, 2], [3, 2]) && !exponentInLog2([0, 1, 2, 3], ls, [1, 1], [7, 5]) && !exponentInLog2([0, 1, 2, 3], ls, [8, 5], [2, 1])); +} + +// 6. Must-fail controls. Each control runs a deliberately broken version and requires it to give the wrong answer where +// the real checker gives the right one, so the test goes red if the checker loses the property. +{ + const inWindow = (cmp, xs, ys, lo, hi) => { const j = xs.length - 1; return cmp(xs[0], xs[j], ys[0], ys[j], lo[0], lo[1]) >= 0 && cmp(xs[0], xs[j], ys[0], ys[j], hi[0], hi[1]) <= 0; }; + const strictCmp = (xa, xb, ya, yb, p, q) => (cmpPower(xa, xb, ya, yb, p, q) > 0 ? 1 : -1); // drops the tie: 0 becomes -1 + const unsignedCmp = (xa, xb, ya, yb, p, q) => cmpPower(xa, xb, ya, yb, Math.abs(p), q); // drops the sign of p + const real = (xs, ys, lo, hi) => inWindow(cmpPower, xs, ys, lo, hi); + + // (a) a wrong exponent is rejected: Ising susceptibility against the mean-field 2 +- 1/10, and the right 7/4 +- 1/10 is accepted. + const chi = SERIES.find(s => s.id === 'ising-chi'), acc = acceptor(chi); + check('Control, wrong exponent: susceptibility against 2 +- 1/10 is rejected; against 7/4 +- 1/10 accepted', !acc([19, 10], [21, 10], 'window') && acc([33, 20], [37, 20], 'window')); + const m = SERIES.find(s => s.id === 'ising-m'), accM = acceptor(m); + check('Control, wrong exponent: magnetisation against -1/2 +- 1/10 is rejected', !accM([-6, 10], [-4, 10], 'window')); + // (b) a dropped tie: slope exactly 3/2 must be inside [3/2, 3/2]; a strict comparison rejects it. + const xs = [R(1), R(4)], ys = [R(1), R(8)]; + check('Control, dropped tie: the real check accepts [3/2, 3/2], the strict-comparison version rejects it', real(xs, ys, [3, 2], [3, 2]) && !inWindow(strictCmp, xs, ys, [3, 2], [3, 2])); + // (c) a dropped sign: falling series, slope -3/2. Real accepts [-3/2, -3/2] and rejects [0, 1]; the unsigned version gets one wrong. + const xf = [R(1), R(4)], yf = [R(8), R(1)]; + const realOk = real(xf, yf, [-3, 2], [-3, 2]) && !real(xf, yf, [0, 1], [1, 1]); + const mutOk = inWindow(unsignedCmp, xf, yf, [-3, 2], [-3, 2]) && !inWindow(unsignedCmp, xf, yf, [0, 1], [1, 1]); + check('Control, dropped sign: the real check is right on a falling series, the unsigned version is wrong on it', realOk && !mutOk); + // (d) a perturbed point: y 9 for 8 leaves [3/2, 3/2]; so does 7. And one printed digit changed in a real series leaves a tight bracket. + check('Control, perturbed point: y = 9 or 7 for 8 leaves [3/2, 3/2]', !real(xs, [R(1), R(9)], [3, 2], [3, 2]) && !real(xs, [R(1), R(7)], [3, 2], [3, 2]) && real(xs, [R(1), R(8)], [3, 2], [3, 2])); + const ysPert = chi.y.slice(); ysPert[2] = '569.2'; // 469.2 -> 569.2 in the middle point + const xsR = chi.x.map(v => [BigInt(v), 1n]); + check('Control, perturbed point: changing the middle susceptibility 469.2 to 569.2 breaks steps at +-1/10 that held', + acceptor(chi)([33, 20], [37, 20], 'steps') && !exponentIn(xsR, ysPert.map(dec), [33, 20], [37, 20], 'steps')); +} + +// 7. The series in scaling-check/series.mjs: what the check says, recorded. window / steps / pairs at +-1/100, +-3/100, +-1/10. +{ + const EXPECT = { + 'ising-m': ['ynn', 'yyy', 'yyy'], 'ising-chi': ['ynn', 'ynn', 'yyy'], 'ising-g': ['ynn', 'ynn', 'yyy'], + 'perc-mass': ['nnn', 'ynn', 'yyy'], 'sabra-u2': ['nnn', 'nnn', 'ynn'], + }; + const TOLS = [[1n, 100n], [3n, 100n], [1n, 10n]]; + for (const s of SERIES) { + const acc = acceptor(s); + const got = TOLS.map(([tn, td]) => { + const [p, q] = [BigInt(s.published[0]), BigInt(s.published[1])]; + return ['window', 'steps', 'pairs'].map(mode => (acc([p * td - tn * q, q * td], [p * td + tn * q, q * td], mode) ? 'y' : 'n')).join(''); + }); + // EXPECT is indexed by mode order window, steps, pairs for each tolerance: store as one string per mode across tolerances. + const want = [0, 1, 2].map(i => EXPECT[s.id].map(t => t[i]).join('')); // want[i] = verdicts of mode i at the three tolerances + const have = [0, 1, 2].map(i => got.map(t => t[i]).join('')); + check(`Series ${s.id}: verdicts at +-1/100, +-3/100, +-1/10 (window / steps / pairs): ${have.join(' / ')}`, want.join() === have.join(), `expected ${want.join(' / ')}`); + } + // Rejections where the repository compares: stated plainly. + const perc = acceptor(SERIES.find(x => x.id === 'perc-mass')), sab = acceptor(SERIES.find(x => x.id === 'sabra-u2')); + // 91/48 +- 1/100 is [9052/4800, 9148/4800]. The window slope 1.8727 is 0.0232 below 91/48: outside +- 23/1000, inside +- 24/1000. + check('Rejected: the one-seed percolation mass window slope (1.8727) is outside 91/48 +- 1/100 and +- 23/1000, inside +- 24/1000', + !perc([9052, 4800], [9148, 4800], 'window') && !perc([91000 - 23 * 48, 48000], [91000 + 23 * 48, 48000], 'window') && + perc([91000 - 24 * 48, 48000], [91000 + 24 * 48, 48000], 'window')); + // From L = 64 on (the README's second reading), the window slope is 1.8804: 0.0155 below 91/48. + const p64 = SERIES.find(x => x.id === 'perc-mass'), x64 = p64.x.slice(1).map(v => [BigInt(v), 1n]), y64 = p64.y.slice(1).map(dec); + check('Rejected: from L = 64 on, the percolation window slope (1.8804) is still outside 91/48 +- 1/100 and +- 15/1000, inside +- 16/1000', + !exponentIn(x64, y64, [9052, 4800], [9148, 4800]) && !exponentIn(x64, y64, [91000 - 15 * 48, 48000], [91000 + 15 * 48, 48000]) && + exponentIn(x64, y64, [91000 - 16 * 48, 48000], [91000 + 16 * 48, 48000])); + check("Rejected: the plain moment's printed log2 slope is outside 0.720 +- 0.008 (slope 0.756 over shells 3 to 8)", !sab([-728, 1000], [-712, 1000], 'window')); + check('Rejected: and outside K41 2/3 +- 1/30; the window slope is exactly -0.756, an exact tie at [-756/1000, -756/1000]', + !sab([-21, 30], [-19, 30], 'window') && sab([-756, 1000], [-756, 1000], 'window') && !sab([-755, 1000], [-755, 1000], 'window')); +} + +// 8. The smallest half-widths that table.mjs prints and README.md quotes, recomputed outside this repository with 60-digit +// decimal logarithms of the same printed numbers. A changed digit, a wrong mode or a broken grid search turns these red. +{ + const WANT = { // window, steps, pairs, in thousandths + 'ising-m': [1, 23, 23], 'ising-chi': [1, 32, 32], 'ising-g': [3, 39, 39], 'perc-mass': [24, 39, 39], 'sabra-u2': [36, 370, 370], + }; + for (const s of SERIES) { + const acc = acceptor(s); + const have = ['window', 'steps', 'pairs'].map(m => { const w = tightestHalfWidth(acc, s.published, m); return w ? w[0] : null; }); + check(`Series ${s.id}: smallest accepted half-width on a 0.001 grid (window, steps, pairs) is ${have.join(', ')} thousandths`, + have.join() === WANT[s.id].join(), `expected ${WANT[s.id].join(', ')}`); + } + // Known answer for the grid search itself: slope exactly 3/2; 1.4 and 1.6 need half-width 0.1 exactly (a tie at the edge). + const xs = [R(1), R(4)], ys = [R(1), R(8)]; + const accT = (lo, hi, mode) => exponentIn(xs, ys, lo, hi, mode); + const w0 = tightestHalfWidth(accT, [3, 2], 'window'), w1 = tightestHalfWidth(accT, [7, 5], 'window'), w2 = tightestHalfWidth(accT, [8, 5], 'window'); + const wNone = tightestHalfWidth(accT, [3, 2], 'window', 1000, -1); + check('Grid search known answer: 0 around 3/2, 100 thousandths around 7/5 and 8/5, null when no step is tried', + w0 && w0[0] === 0 && w1 && w1[0] === 100 && w2 && w2[0] === 100 && wNone === null); +} + +// 9. The verdict script itself runs and prints the same half-widths (it is not in CI on its own). +{ + const r = spawnSync(process.execPath, [fileURLToPath(new URL('../scaling-check/table.mjs', import.meta.url))], { encoding: 'utf8', timeout: 60000 }); + const lines = (r.stdout || '').split('\n').filter(l => l.includes('smallest half-width')); + check('scaling-check/table.mjs exits 0 and prints the five half-width lines above', + r.status === 0 && lines.length === 5 && lines[3].endsWith('window 0.024, steps 0.039, pairs 0.039') && lines[4].endsWith('window 0.036, steps 0.370, pairs 0.370'), + `exit ${r.status}, ${lines.length} half-width lines`); +} + +// 10. Error bars: steps and all pairs can differ. The interval form of all pairs, (U_j/L_i)^q1 >= (x_j/x_i)^p1 and +// (L_j/U_i)^q2 <= (x_j/x_i)^p2 for every i < j, decides whether values inside the boxes exist with every step slope in the +// bracket (a chain of difference constraints, Dechter, Meiri and Pearl 1991; Foundation-0/fractals draft pull request 60). +// It is written here in exact BigInt fractions on purpose and is not a mode of check.mjs. The smallest disagreement: scales +// 1, 2, 4, bracket [1, 1], boxes [1,1], [3/2,5/2], [9/2,5]. +{ + const mul = (a, b) => [a[0] * b[0], a[1] * b[1]]; + const rpow = (r, e) => (e >= 0n ? [r[0] ** e, r[1] ** e] : [r[1] ** -e, r[0] ** -e]); // r positive + const geq = (a, b) => a[0] * b[1] >= b[0] * a[1]; // denominators positive + const slopeOk = (x, box, i, j, lo, hi) => { // the interval inequalities for the pair (i, j) + const rx = [x[j][0] * x[i][1], x[j][1] * x[i][0]]; + const up = mul(box[j][1], [box[i][0][1], box[i][0][0]]); // U_j / L_i + const dn = mul(box[j][0], [box[i][1][1], box[i][1][0]]); // L_j / U_i + return geq(rpow(up, BigInt(lo[1])), rpow(rx, BigInt(lo[0]))) && geq(rpow(rx, BigInt(hi[0])), rpow(dn, BigInt(hi[1]))); + }; + const allOk = (x, box, lo, hi, pairs) => pairs.every(([i, j]) => slopeOk(x, box, i, j, lo, hi)); + const x = [R(1), R(2), R(4)], one = [1, 1]; + const boxes = [[R(1), R(1)], [R(3, 2), R(5, 2)], [R(9, 2), R(5)]]; + const stepsPairs = pairsFor(3, 'steps'), allPairs = pairsFor(3, 'pairs'); + check('Boxes: each step of the smallest disagreement example admits slope 1, so the step test passes', + allOk(x, boxes, one, one, stepsPairs)); + check('Boxes: the pair of the first and last scales does not (L_2/U_0 = 9/2 is above 4), so the all-pairs test fails', + !slopeOk(x, boxes, 0, 2, one, one) && !allOk(x, boxes, one, one, allPairs)); + // Must-fail twin: widen the last box to [4, 5]. Now the pair test passes, and values y = 1, 2, 4 inside the boxes have slope 1. + const wide = [boxes[0], boxes[1], [R(4), R(5)]]; + const inBox = (v, b) => geq(v, b[0]) && geq(b[1], v); + check('Control, widened last box [4, 5]: the pair test now passes, and the witness 1, 2, 4 sits in the boxes with slope 1 on every pair', + allOk(x, wide, one, one, allPairs) && [R(1), R(2), R(4)].every((v, k) => inBox(v, wide[k])) && + allPairs.every(([i, j]) => cmpPower(x[i], x[j], [R(1), R(2), R(4)][i], [R(1), R(2), R(4)][j], 1, 1) === 0)); + // The step test is blind to the widening: it gave the same verdict before, so it cannot be what tells the two cases apart. + check('Control: the step test gives the same verdict (pass) on the original and the widened boxes, only the pair test differs', + allOk(x, wide, one, one, stepsPairs) && allOk(x, wide, one, one, allPairs) !== allOk(x, boxes, one, one, allPairs)); + // The inequality is not vacuous: a corrupted lower bracket end breaks a case that passed. + check('Control, corrupted bracket: the widened boxes fail the pair test against [2, 2]', !allOk(x, wide, [2, 1], [2, 1], allPairs)); + // A bracket whose ends are not whole numbers, so that p and q swapped in either inequality makes this line fail. + check('Control, bracket [1/2, 2]: the widened boxes pass the pair test (the witness 1, 2, 4 has slope 1)', + allOk(x, wide, [1, 2], [2, 1], allPairs)); +} + +console.log(fails ? `\n${fails} check(s) failed` : '\nall scaling-check checks pass'); +process.exit(fails ? 1 : 0);