// Node.js. Compare unnormalized terminal-word weights, exactly as BigInts. function subsetFormula(weights) { const size = 1 << weights.length; const ans = Array(size).fill(0n); for (let set = 1; set < size; set++) { let sum = 0n, count = 0; for (let i = 0; i < weights.length; i++) { if (set & (1 << i)) { sum += weights[i]; count++; } } ans[set] = sum ** BigInt(2 * count); for (let sub = (set - 1) & set; sub; sub = (sub - 1) & set) { let missing = 0; for (let i = 0; i < weights.length; i++) if ((set ^ sub) & (1 << i)) missing++; ans[set] -= ans[sub] * sum ** BigInt(2 * missing); } } return ans; } function stoppedWords(weights) { const ans = Array(1 << weights.length).fill(0n); function visit(seen, balance, weight) { for (let i = 0; i < weights.length; i++) { const bit = 1 << i; const after = balance + ((seen & bit) ? -1 : 1); const nextWeight = weight * weights[i]; if (after === 0) ans[seen | bit] += nextWeight; else visit(seen | bit, after, nextWeight); } } visit(0, 0, 1n); return ans; } for (const weights of [[1n], [1n, 1n], [1n, 2n, 6n], [1n, 1n, 1n, 1n], [7n, 4n, 2n, 1n]]) { const a = subsetFormula(weights), b = stoppedWords(weights); if (a.some((x,i) => x !== b[i])) throw Error("Terminal weights disagree"); } console.log("All terminal subsets agree for five test vectors.");