broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        ks.append(str(k))
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['regression: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['repair check: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}]], [['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['regression: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['repair check: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}]], [['regression: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['regression: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 21', ['1/2', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['regression: random denominator 23', ['1', '1/2', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 22', ['2', '1/8', '3/4'], {'stable': True, 'reflection': ['3/8', '1/22']}], ['control: random denominator 24', ['2', '1/2'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 25', ['1', '3/4'], {'stable': True, 'reflection': ['3/4']}], ['control: random denominator 26', ['1', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14']}]], [['regression: random denominator 32', ['1/2', '-1/2'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 33', ['1', '5/4', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['repair check: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['control: random denominator 35', ['2', '1/8', '1/2', '1/3'], {'stable': True, 'reflection': ['1/6', '69/280', '6/349']}], ['control: random denominator 37', ['1', '1/2', '3/4'], {'stable': True, 'reflection': ['3/4', '2/7']}], ['control: random denominator 44', ['1', '1/2'], {'stable': True, 'reflection': ['1/2']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(abs(k)))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['regression: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['repair check: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}]], [['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['regression: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['repair check: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}]], [['regression: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['regression: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 21', ['1/2', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['regression: random denominator 23', ['1', '1/2', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 22', ['2', '1/8', '3/4'], {'stable': True, 'reflection': ['3/8', '1/22']}], ['control: random denominator 24', ['2', '1/2'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 25', ['1', '3/4'], {'stable': True, 'reflection': ['3/4']}], ['control: random denominator 26', ['1', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14']}]], [['regression: random denominator 32', ['1/2', '-1/2'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 33', ['1', '5/4', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['repair check: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['control: random denominator 35', ['2', '1/8', '1/2', '1/3'], {'stable': True, 'reflection': ['1/6', '69/280', '6/349']}], ['control: random denominator 37', ['1', '1/2', '3/4'], {'stable': True, 'reflection': ['3/4', '2/7']}], ['control: random denominator 44', ['1', '1/2'], {'stable': True, 'reflection': ['1/2']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An unstable first-order denominator reports an empty reflection list, hiding which stage failed.

Input the denominator [a0, a1, ..., an] as rational strings. Normalize by a0 ("bad-a0" if missing or 0), drop trailing zero coefficients, then step down: k = last coefficient, unstable if |k| >= 1, else a_i <- (a_i - k a_{p-i}) / (1 - k^2) for i < p. Return {"stable": bool, "reflection": [k strings so far]}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['repair check: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}]], [['regression: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['regression: random denominator 31', ['1', '1/8', '-1', '-1/4'], {'stable': False, 'reflection': ['-1/4', '-31/30']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 38', ['-1', '1/2', '5/4', '1/3'], {'stable': False, 'reflection': ['-1/3', '-51/32']}], ['regression: random denominator 40', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['repair check: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 49', ['1', '1/2', '-1/2', '1/2'], {'stable': False, 'reflection': ['1/2', '-1']}], ['regression: random denominator 50', ['1', '1/3', '3/4', '1/8'], {'stable': True, 'reflection': ['1/8', '136/189', '46/325']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['regression: random denominator 27', ['2', '1/3', '-1/2', '1/8'], {'stable': True, 'reflection': ['1/16', '-40/153', '28/113']}], ['repair check: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 16', ['1/2', '-1', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 19', ['-1', '3/4'], {'stable': True, 'reflection': ['-3/4']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - 1 - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['repair check: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}]], [['regression: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['regression: random denominator 31', ['1', '1/8', '-1', '-1/4'], {'stable': False, 'reflection': ['-1/4', '-31/30']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 38', ['-1', '1/2', '5/4', '1/3'], {'stable': False, 'reflection': ['-1/3', '-51/32']}], ['regression: random denominator 40', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['repair check: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 49', ['1', '1/2', '-1/2', '1/2'], {'stable': False, 'reflection': ['1/2', '-1']}], ['regression: random denominator 50', ['1', '1/3', '3/4', '1/8'], {'stable': True, 'reflection': ['1/8', '136/189', '46/325']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['regression: random denominator 27', ['2', '1/3', '-1/2', '1/8'], {'stable': True, 'reflection': ['1/16', '-40/153', '28/113']}], ['repair check: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 16', ['1/2', '-1', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 19', ['-1', '3/4'], {'stable': True, 'reflection': ['-3/4']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Higher-order denominators get wrong reflection coefficients after the first step.

Input the denominator [a0, a1, ..., an] as rational strings. Normalize by a0 ("bad-a0" if missing or 0), drop trailing zero coefficients, then step down: k = last coefficient, unstable if |k| >= 1, else a_i <- (a_i - k a_{p-i}) / (1 - k^2) for i < p. Return {"stable": bool, "reflection": [k strings so far]}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}]], [['regression: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}], ['regression: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['regression: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['regression: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 22', ['2', '1/8', '3/4'], {'stable': True, 'reflection': ['3/8', '1/22']}], ['regression: random denominator 26', ['1', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}]], [['regression: random denominator 29', ['1', '-1/2', '3/4'], {'stable': True, 'reflection': ['3/4', '-2/7']}], ['regression: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['regression: random denominator 20', ['1', '1/2', '-1/4'], {'stable': True, 'reflection': ['-1/4', '2/3']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 16', ['1/2', '-1', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 + k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}]], [['regression: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}], ['regression: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['regression: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['regression: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['regression: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]], [['regression: random denominator 22', ['2', '1/8', '3/4'], {'stable': True, 'reflection': ['3/8', '1/22']}], ['regression: random denominator 26', ['1', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14']}], ['regression: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}]], [['regression: random denominator 29', ['1', '-1/2', '3/4'], {'stable': True, 'reflection': ['3/4', '-2/7']}], ['regression: random denominator 30', ['1', '1/3', '1/2', '1/3'], {'stable': True, 'reflection': ['1/3', '7/16', '3/23']}], ['regression: random denominator 20', ['1', '1/2', '-1/4'], {'stable': True, 'reflection': ['-1/4', '2/3']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 16', ['1/2', '-1', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['control: random denominator 17', ['1', '1/8'], {'stable': True, 'reflection': ['1/8']}], ['control: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Reflection coefficients after the first step are mis-scaled and stability verdicts flip.

Input the denominator [a0, a1, ..., an] as rational strings. Normalize by a0 ("bad-a0" if missing or 0), drop trailing zero coefficients, then step down: k = last coefficient, unstable if |k| >= 1, else a_i <- (a_i - k a_{p-i}) / (1 - k^2) for i < p. Return {"stable": bool, "reflection": [k strings so far]}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}]], [['regression: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['regression: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}]], [['regression: random denominator 19', ['-1', '3/4'], {'stable': True, 'reflection': ['-3/4']}], ['regression: random denominator 21', ['1/2', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['regression: random denominator 28', ['-1', '1/8'], {'stable': True, 'reflection': ['-1/8']}], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}]], [['regression: random denominator 24', ['2', '1/2'], {'stable': True, 'reflection': ['1/4']}], ['regression: random denominator 27', ['2', '1/3', '-1/2', '1/8'], {'stable': True, 'reflection': ['1/16', '-40/153', '28/113']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}]], [['regression: random denominator 32', ['1/2', '-1/2'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: random denominator 52', ['-1', '3/4', '5/4', '-1'], {'stable': False, 'reflection': ['1']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / abs(a[0]) for v in a]
    while len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['regression: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['control: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}]], [['regression: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['regression: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['regression: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}]], [['regression: random denominator 19', ['-1', '3/4'], {'stable': True, 'reflection': ['-3/4']}], ['regression: random denominator 21', ['1/2', '3/4'], {'stable': False, 'reflection': ['3/2']}], ['regression: random denominator 28', ['-1', '1/8'], {'stable': True, 'reflection': ['-1/8']}], ['control: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}]], [['regression: random denominator 24', ['2', '1/2'], {'stable': True, 'reflection': ['1/4']}], ['regression: random denominator 27', ['2', '1/3', '-1/2', '1/8'], {'stable': True, 'reflection': ['1/16', '-40/153', '28/113']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}]], [['regression: random denominator 32', ['1/2', '-1/2'], {'stable': False, 'reflection': ['-1']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: random denominator 52', ['-1', '3/4', '5/4', '-1'], {'stable': False, 'reflection': ['1']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['control: random denominator 14', ['1', '1/8', '5/4'], {'stable': False, 'reflection': ['5/4']}], ['control: random denominator 15', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A denominator with a0 = 2 reports reflection coefficients twice too large and false instability.

Input the denominator [a0, a1, ..., an] as rational strings. Normalize by a0 ("bad-a0" if missing or 0), drop trailing zero coefficients, then step down: k = last coefficient, unstable if |k| >= 1, else a_i <- (a_i - k a_{p-i}) / (1 - k^2) for i < p. Return {"stable": bool, "reflection": [k strings so far]}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    pass
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['regression: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}]], [['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}]], [['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}], ['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}]], [['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['regression: random denominator 51', ['1', '0'], {'stable': True, 'reflection': []}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(x):
    a = [Fraction(v) for v in x]
    if not a or a[0] == 0:
        return 'bad-a0'
    a = [v / a[0] for v in a]
    if len(a) > 1 and a[-1] == 0:
        a.pop()
    ks = []
    while len(a) > 1:
        p = len(a) - 1
        k = a[p]
        ks.append(str(k))
        if abs(k) >= 1:
            return {'stable': False, 'reflection': ks}
        a = [(a[i] - k * a[p - i]) / (1 - k * k) for i in range(p)]
    return {'stable': True, 'reflection': ks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: marginal pole on unit circle', ['1', '0', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: stable second order', ['1', '-1/2', '1/4'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: negative leading coefficient', ['-2', '1', '-1/2'], {'stable': True, 'reflection': ['1/4', '-2/5']}], ['control: pole at -1', ['1', '1'], {'stable': False, 'reflection': ['1']}], ['regression: random denominator 0', ['-1', '0'], {'stable': True, 'reflection': []}]], [['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: unstable first order', ['1', '-3/2'], {'stable': False, 'reflection': ['-3/2']}], ['control: bad a0', ['0', '1'], 'bad-a0'], ['control: third order', ['1', '-1/2', '1/3', '-1/5'], {'stable': True, 'reflection': ['-1/5', '35/144', '-65/179']}], ['control: random denominator 1', ['1', '1/8', '1/8'], {'stable': True, 'reflection': ['1/8', '1/9']}]], [['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 2', ['1', '1/3', '1/3'], {'stable': True, 'reflection': ['1/3', '1/4']}], ['control: random denominator 3', ['-1', '-1/2'], {'stable': True, 'reflection': ['1/2']}], ['control: random denominator 4', ['-1', '0', '5/4'], {'stable': False, 'reflection': ['-5/4']}], ['control: random denominator 5', ['1', '1/8', '1/3'], {'stable': True, 'reflection': ['1/3', '3/32']}], ['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}]], [['regression: random denominator 18', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: random denominator 34', ['-1', '1/2', '-1', '0'], {'stable': False, 'reflection': ['1']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 6', ['1/2', '1/8'], {'stable': True, 'reflection': ['1/4']}], ['control: random denominator 7', ['1', '3/4', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 8', ['1', '-1/4'], {'stable': True, 'reflection': ['-1/4']}], ['control: random denominator 9', ['1', '-1', '-1'], {'stable': False, 'reflection': ['-1']}]], [['regression: trailing zero coefficient', ['1', '1/2', '0'], {'stable': True, 'reflection': ['1/2']}], ['regression: two trailing zeros', ['1', '-1/3', '0', '0'], {'stable': True, 'reflection': ['-1/3']}], ['control: random denominator 10', ['1', '-1/4', '1/8', '-1/2'], {'stable': True, 'reflection': ['-1/2', '0', '-1/4']}], ['control: random denominator 11', ['-1', '0', '-1/2'], {'stable': True, 'reflection': ['1/2', '0']}], ['control: random denominator 12', ['1', '5/4', '-1', '-1'], {'stable': False, 'reflection': ['-1']}], ['control: random denominator 13', ['1', '1/8', '1/8', '3/4'], {'stable': True, 'reflection': ['3/4', '1/14', '1/15']}], ['regression: random denominator 51', ['1', '0'], {'stable': True, 'reflection': []}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A padded denominator [1, 1/2, 0] reports an extra reflection coefficient 0.

Input the denominator [a0, a1, ..., an] as rational strings. Normalize by a0 ("bad-a0" if missing or 0), drop trailing zero coefficients, then step down: k = last coefficient, unstable if |k| >= 1, else a_i <- (a_i - k a_{p-i}) / (1 - k^2) for i < p. Return {"stable": bool, "reflection": [k strings so far]}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = round(v)
        else:
            q = math.floor(v)
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}], ['control: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['control: random quantize 10', [['-1.232', '-0.29', '-0.82', '0.594'], 4, 5, 'truncate'], {'codes': [-16, -5, -14, 9], 'saturated': 1, 'max_error': '29/125'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(v + Fraction(1, 2))
        else:
            q = math.floor(v)
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}], ['control: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}]], [['regression: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['control: random quantize 10', [['-1.232', '-0.29', '-0.82', '0.594'], 4, 5, 'truncate'], {'codes': [-16, -5, -14, 9], 'saturated': 1, 'max_error': '29/125'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
0.125 at two fractional bits quantizes to code 0 instead of 1 (half away from zero).

Input [coeffs, f, B, mode]: quantize each decimal coefficient to a B-bit two's-complement code with f fractional bits; "nearest" rounds half away from zero, "truncate" floors. Codes saturate to [-2^(B-1), 2^(B-1)-1]. Return {"codes", "saturated": count of clipped codes, "max_error": max |code/2^f - c| after saturation as a fraction string}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = int(v)
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['regression: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}]], [['regression: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}], ['regression: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}]], [['regression: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['regression: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}]], [['regression: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['regression: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 20', [['-1.201', '-0.66', '-1.739', '-0.86'], 2, 5, 'nearest'], {'codes': [-5, -3, -7, -3], 'saturated': 0, 'max_error': '11/100'}], ['control: random quantize 21', [['1.81'], 6, 7, 'truncate'], {'codes': [63], 'saturated': 1, 'max_error': '1321/1600'}]], [['regression: random quantize 24', [['-1.135', '-1.03'], 4, 7, 'truncate'], {'codes': [-19, -17], 'saturated': 0, 'max_error': '21/400'}], ['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 22', [['-1.31', '1.54', '-0.187'], 5, 7, 'nearest'], {'codes': [-42, 49, -6], 'saturated': 0, 'max_error': '7/800'}], ['control: random quantize 23', [['-0.61', '1.01'], 6, 8, 'nearest'], {'codes': [-39, 65], 'saturated': 0, 'max_error': '9/1600'}], ['control: random quantize 25', [['0.186', '0.87', '-2.12', '1.06'], 6, 9, 'nearest'], {'codes': [12, 56, -136, 68], 'saturated': 0, 'max_error': '1/200'}], ['control: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = math.ceil(v) - 1
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['regression: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['control: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}]], [['regression: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}], ['regression: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}]], [['regression: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['regression: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}]], [['regression: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['regression: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 20', [['-1.201', '-0.66', '-1.739', '-0.86'], 2, 5, 'nearest'], {'codes': [-5, -3, -7, -3], 'saturated': 0, 'max_error': '11/100'}], ['control: random quantize 21', [['1.81'], 6, 7, 'truncate'], {'codes': [63], 'saturated': 1, 'max_error': '1321/1600'}]], [['regression: random quantize 24', [['-1.135', '-1.03'], 4, 7, 'truncate'], {'codes': [-19, -17], 'saturated': 0, 'max_error': '21/400'}], ['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['repair check: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 22', [['-1.31', '1.54', '-0.187'], 5, 7, 'nearest'], {'codes': [-42, 49, -6], 'saturated': 0, 'max_error': '7/800'}], ['control: random quantize 23', [['-0.61', '1.01'], 6, 8, 'nearest'], {'codes': [-39, 65], 'saturated': 0, 'max_error': '9/1600'}], ['control: random quantize 25', [['0.186', '0.87', '-2.12', '1.06'], 6, 9, 'nearest'], {'codes': [12, 56, -136, 68], 'saturated': 0, 'max_error': '1/200'}], ['control: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Negative coefficients in truncate mode quantize one LSB too high.

Input [coeffs, f, B, mode]: quantize each decimal coefficient to a B-bit two's-complement code with f fractional bits; "nearest" rounds half away from zero, "truncate" floors. Codes saturate to [-2^(B-1), 2^(B-1)-1]. Return {"codes", "saturated": count of clipped codes, "max_error": max |code/2^f - c| after saturation as a fraction string}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1), -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = math.floor(v)
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['regression: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['repair check: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: random quantize 29', [['2.017', '-0.693', '1.94'], 2, 4, 'nearest'], {'codes': [7, -3, 7], 'saturated': 2, 'max_error': '267/1000'}], ['regression: random quantize 38', [['2.146', '-1.527', '-1.91'], 5, 6, 'nearest'], {'codes': [31, -32, -32], 'saturated': 3, 'max_error': '4709/4000'}], ['repair check: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}]], [['regression: random quantize 48', [['0.88', '-2.15', '-1.86', '2.016'], 3, 5, 'nearest'], {'codes': [7, -16, -15, 15], 'saturated': 2, 'max_error': '3/20'}], ['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['repair check: random quantize 10', [['-1.232', '-0.29', '-0.82', '0.594'], 4, 5, 'truncate'], {'codes': [-16, -5, -14, 9], 'saturated': 1, 'max_error': '29/125'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}]], [['regression: random quantize 21', [['1.81'], 6, 7, 'truncate'], {'codes': [63], 'saturated': 1, 'max_error': '1321/1600'}], ['regression: random quantize 29', [['2.017', '-0.693', '1.94'], 2, 4, 'nearest'], {'codes': [7, -3, 7], 'saturated': 2, 'max_error': '267/1000'}], ['repair check: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}], ['control: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}]], [['regression: random quantize 39', [['1.85', '-1.902'], 3, 4, 'truncate'], {'codes': [7, -8], 'saturated': 2, 'max_error': '39/40'}], ['regression: random quantize 48', [['0.88', '-2.15', '-1.86', '2.016'], 3, 5, 'nearest'], {'codes': [7, -16, -15, 15], 'saturated': 2, 'max_error': '3/20'}], ['repair check: random quantize 36', [['-1.29', '-1.846'], 3, 4, 'truncate'], {'codes': [-8, -8], 'saturated': 2, 'max_error': '423/500'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['control: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}], ['control: random quantize 19', [['-0.718', '0.68', '0.97'], 2, 5, 'truncate'], {'codes': [-3, 2, 3], 'saturated': 0, 'max_error': '11/50'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -(2 ** (B - 1) - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = math.floor(v)
        if q > hi or q < lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['regression: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['repair check: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: random quantize 29', [['2.017', '-0.693', '1.94'], 2, 4, 'nearest'], {'codes': [7, -3, 7], 'saturated': 2, 'max_error': '267/1000'}], ['regression: random quantize 38', [['2.146', '-1.527', '-1.91'], 5, 6, 'nearest'], {'codes': [31, -32, -32], 'saturated': 3, 'max_error': '4709/4000'}], ['repair check: random quantize 4', [['-1.49', '-1.91'], 2, 3, 'nearest'], {'codes': [-4, -4], 'saturated': 2, 'max_error': '91/100'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}]], [['regression: random quantize 48', [['0.88', '-2.15', '-1.86', '2.016'], 3, 5, 'nearest'], {'codes': [7, -16, -15, 15], 'saturated': 2, 'max_error': '3/20'}], ['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['repair check: random quantize 10', [['-1.232', '-0.29', '-0.82', '0.594'], 4, 5, 'truncate'], {'codes': [-16, -5, -14, 9], 'saturated': 1, 'max_error': '29/125'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}]], [['regression: random quantize 21', [['1.81'], 6, 7, 'truncate'], {'codes': [63], 'saturated': 1, 'max_error': '1321/1600'}], ['regression: random quantize 29', [['2.017', '-0.693', '1.94'], 2, 4, 'nearest'], {'codes': [7, -3, 7], 'saturated': 2, 'max_error': '267/1000'}], ['repair check: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}], ['control: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}]], [['regression: random quantize 39', [['1.85', '-1.902'], 3, 4, 'truncate'], {'codes': [7, -8], 'saturated': 2, 'max_error': '39/40'}], ['regression: random quantize 48', [['0.88', '-2.15', '-1.86', '2.016'], 3, 5, 'nearest'], {'codes': [7, -16, -15, 15], 'saturated': 2, 'max_error': '3/20'}], ['repair check: random quantize 36', [['-1.29', '-1.846'], 3, 4, 'truncate'], {'codes': [-8, -8], 'saturated': 2, 'max_error': '423/500'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['control: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}], ['control: random quantize 19', [['-0.718', '0.68', '0.97'], 2, 5, 'truncate'], {'codes': [-3, 2, 3], 'saturated': 0, 'max_error': '11/50'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A coefficient of 1.0 in Q7 yields code 128, which does not fit in eight bits.

Input [coeffs, f, B, mode]: quantize each decimal coefficient to a B-bit two's-complement code with f fractional bits; "nearest" rounds half away from zero, "truncate" floors. Codes saturate to [-2^(B-1), 2^(B-1)-1]. Return {"codes", "saturated": count of clipped codes, "max_error": max |code/2^f - c| after saturation as a fraction string}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = math.floor(v)
        if q >= hi or q <= lo:
            sat += 1
            q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['regression: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['repair check: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['regression: random quantize 44', [['0.844', '-1.78', '0.304', '0.84'], 2, 3, 'nearest'], {'codes': [3, -4, 1, 3], 'saturated': 1, 'max_error': '39/50'}], ['repair check: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}]], [['regression: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['regression: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['repair check: random quantize 15', [['-1.005', '-0.11', '-0.221', '-0.679'], 6, 7, 'truncate'], {'codes': [-64, -8, -15, -44], 'saturated': 1, 'max_error': '3/200'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}]], [['regression: random quantize 44', [['0.844', '-1.78', '0.304', '0.84'], 2, 3, 'nearest'], {'codes': [3, -4, 1, 3], 'saturated': 1, 'max_error': '39/50'}], ['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['repair check: random quantize 32', [['-1.79', '-1.633', '-0.38'], 3, 4, 'nearest'], {'codes': [-8, -8, -3], 'saturated': 2, 'max_error': '79/100'}], ['control: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}]], [['regression: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['repair check: random quantize 37', [['-1.95', '-1.54', '0.854'], 6, 7, 'truncate'], {'codes': [-64, -64, 54], 'saturated': 2, 'max_error': '19/20'}], ['control: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['control: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    coeffs, f, B, mode = x
    scale = 2 ** f
    hi, lo = 2 ** (B - 1) - 1, -2 ** (B - 1)
    codes = []
    sat = 0
    err = Fraction(0)
    for s in coeffs:
        c = Fraction(s)
        v = c * scale
        if mode == 'nearest':
            q = math.floor(abs(v) + Fraction(1, 2)) * (1 if v >= 0 else -1)
        else:
            q = math.floor(v)
        if q > hi:
            sat += 1
        q = max(lo, min(hi, q))
        codes.append(q)
        err = max(err, abs(Fraction(q, scale) - c))
    return {'codes': codes, 'saturated': sat, 'max_error': str(err)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['regression: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['repair check: negative overflow', [['-1.5'], 7, 8, 'truncate'], {'codes': [-128], 'saturated': 1, 'max_error': '1/2'}], ['control: tie cases nearest', [['0.125', '0.375', '-0.125'], 2, 8, 'nearest'], {'codes': [1, 2, -1], 'saturated': 0, 'max_error': '1/8'}], ['control: negative truncate', [['-0.3', '0.3'], 3, 8, 'truncate'], {'codes': [-3, 2], 'saturated': 0, 'max_error': '3/40'}], ['control: exact grid', [['0.5', '-0.25'], 2, 4, 'nearest'], {'codes': [2, -1], 'saturated': 0, 'max_error': '0'}], ['control: exact grid truncate', [['0.5', '-0.25', '0.75'], 2, 4, 'truncate'], {'codes': [2, -1, 3], 'saturated': 0, 'max_error': '0'}]], [['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['regression: random quantize 44', [['0.844', '-1.78', '0.304', '0.84'], 2, 3, 'nearest'], {'codes': [3, -4, 1, 3], 'saturated': 1, 'max_error': '39/50'}], ['repair check: random quantize 8', [['0.08', '-2.194', '-0.29'], 4, 6, 'nearest'], {'codes': [1, -32, -5], 'saturated': 1, 'max_error': '97/500'}], ['control: integer multiple truncate', [['-0.5', '1.25'], 3, 6, 'truncate'], {'codes': [-4, 10], 'saturated': 0, 'max_error': '0'}], ['control: random quantize 0', [['-1.79', '1.59'], 3, 5, 'truncate'], {'codes': [-15, 12], 'saturated': 0, 'max_error': '9/100'}], ['control: random quantize 1', [['-1.919', '-2.19', '0.764', '-2.046'], 4, 7, 'truncate'], {'codes': [-31, -36, 12, -33], 'saturated': 0, 'max_error': '3/50'}], ['control: random quantize 2', [['-0.09', '-0.09', '0.397'], 4, 7, 'nearest'], {'codes': [-1, -1, 6], 'saturated': 0, 'max_error': '11/400'}]], [['regression: negative full scale', [['-1'], 7, 8, 'nearest'], {'codes': [-128], 'saturated': 0, 'max_error': '0'}], ['regression: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['repair check: random quantize 15', [['-1.005', '-0.11', '-0.221', '-0.679'], 6, 7, 'truncate'], {'codes': [-64, -8, -15, -44], 'saturated': 1, 'max_error': '3/200'}], ['control: random quantize 3', [['-1.8'], 3, 5, 'nearest'], {'codes': [-14], 'saturated': 0, 'max_error': '1/20'}], ['control: random quantize 5', [['1.92'], 4, 6, 'truncate'], {'codes': [30], 'saturated': 0, 'max_error': '9/200'}], ['control: random quantize 6', [['2.04', '0.49'], 3, 6, 'nearest'], {'codes': [16, 4], 'saturated': 0, 'max_error': '1/25'}], ['control: random quantize 7', [['-2.111', '2.022', '-1.492'], 3, 6, 'truncate'], {'codes': [-17, 16, -12], 'saturated': 0, 'max_error': '11/500'}]], [['regression: random quantize 44', [['0.844', '-1.78', '0.304', '0.84'], 2, 3, 'nearest'], {'codes': [3, -4, 1, 3], 'saturated': 1, 'max_error': '39/50'}], ['regression: positive overflow', [['1', '0.99'], 7, 8, 'nearest'], {'codes': [127, 127], 'saturated': 1, 'max_error': '1/128'}], ['repair check: random quantize 32', [['-1.79', '-1.633', '-0.38'], 3, 4, 'nearest'], {'codes': [-8, -8, -3], 'saturated': 2, 'max_error': '79/100'}], ['control: random quantize 9', [['-0.92', '1.79'], 4, 5, 'truncate'], {'codes': [-15, 15], 'saturated': 1, 'max_error': '341/400'}], ['control: random quantize 11', [['-0.158', '1.311', '1.13', '-0.89'], 6, 8, 'truncate'], {'codes': [-11, 83, 72, -57], 'saturated': 0, 'max_error': '113/8000'}], ['control: random quantize 12', [['-0.52', '-2.037', '1.37'], 3, 6, 'nearest'], {'codes': [-4, -16, 11], 'saturated': 0, 'max_error': '37/1000'}], ['control: random quantize 13', [['0.615'], 3, 4, 'nearest'], {'codes': [5], 'saturated': 0, 'max_error': '1/100'}]], [['regression: random quantize 26', [['-1.848', '0.82', '-1.59'], 2, 3, 'truncate'], {'codes': [-4, 3, -4], 'saturated': 2, 'max_error': '106/125'}], ['regression: random quantize 28', [['1.91', '-1.259'], 2, 4, 'truncate'], {'codes': [7, -6], 'saturated': 0, 'max_error': '241/1000'}], ['repair check: random quantize 37', [['-1.95', '-1.54', '0.854'], 6, 7, 'truncate'], {'codes': [-64, -64, 54], 'saturated': 2, 'max_error': '19/20'}], ['control: random quantize 14', [['-1.84', '1.735', '0.56', '1.37'], 3, 6, 'truncate'], {'codes': [-15, 13, 4, 10], 'saturated': 0, 'max_error': '3/25'}], ['control: random quantize 16', [['0.334'], 6, 7, 'truncate'], {'codes': [21], 'saturated': 0, 'max_error': '47/8000'}], ['control: random quantize 17', [['-0.86'], 2, 5, 'truncate'], {'codes': [-4], 'saturated': 0, 'max_error': '7/50'}], ['control: random quantize 18', [['0.742', '0.423', '0.231', '-0.3'], 4, 6, 'truncate'], {'codes': [11, 6, 3, -5], 'saturated': 0, 'max_error': '109/2000'}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A coefficient that quantizes exactly to the most negative code is reported as saturated.

Input [coeffs, f, B, mode]: quantize each decimal coefficient to a B-bit two's-complement code with f fractional bits; "nearest" rounds half away from zero, "truncate" floors. Codes saturate to [-2^(B-1), 2^(B-1)-1]. Return {"codes", "saturated": count of clipped codes, "max_error": max |code/2^f - c| after saturation as a fraction string}.

split: open-access