Download examples/reconstruct.py from duj626/kea-polynomial-factorization-certificates: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/datasets/duj626/kea-polynomial-factorization-certificates/resolve/main/examples/reconstruct.py
- Command line
-
hf download hf://datasets/duj626/kea-polynomial-factorization-certificates/examples/reconstruct.py
-
curl -L -o reconstruct.py https://huggingface.co/datasets/duj626/kea-polynomial-factorization-certificates/resolve/main/examples/reconstruct.py
1.75 kB
| #!/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() | |