File size: 1,751 Bytes
8bb503f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
"""Reconstruct an existing row using exact rational arithmetic. Does not factor or write."""
import argparse
from fractions import Fraction
import json
from pathlib import Path

def multiply(left, right):
    product = [Fraction(0)] * (len(left) + len(right) - 1)
    for i, a in enumerate(left):
        for j, b in enumerate(right):
            product[i + j] += a * b
    return product

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--id', default='pos-000')
    args = parser.parse_args()
    path = Path(__file__).resolve().parents[1]/'data/polynomials.jsonl'
    rows = [json.loads(line) for line in path.read_text().splitlines()]
    matches = [row for row in rows if row['id'] == args.id]
    if len(matches) != 1:
        parser.error('No unique row with this ID')
    row = matches[0]
    product = [Fraction(row['rational_unit'])]
    for factor in row['monic_factors_ascending']:
        product = multiply(product, [Fraction(c) for c in factor])
    expected = [Fraction(c) for c in row['coefficients_ascending']]
    if product != expected:
        raise ValueError('Exact reconstruction differs from the stored polynomial')
    print(json.dumps({'id':row['id'], 'status':'exact_reconstruction_passed',
                      'coefficients_ascending':row['coefficients_ascending'],
                      'factor_degrees_with_multiplicity':row['factor_degrees_with_multiplicity'],
                      'construction_label':row['construction_label'],
                      'constructed_factor_degree':row['constructed_factor_degree'],
                      'exact_min_factor_degree':row['exact_min_factor_degree']}, indent=2))

if __name__ == '__main__':
    main()