Nhairun-Exact-Complexity / code /quickstart.py
PureOne's picture
Release Nhairun Delta research dataset, manuscript revision 3
1263695 verified
Raw History Blame Contribute Delete
1.18 kB
#!/usr/bin/env python3
"""Two small exact examples. Does not write to the repository."""
import sys
sys.dont_write_bytecode=True
from fractions import Fraction as F
from preemptive_ordering import optimal_one_split_order
from scalar_ordering import build_scalar_reduction,endpoint
from sentinel_ordering import balance_scalar_centers
result=optimal_one_split_order([-1,1],[F(1,2)]*2,target=0)
assert result['endpoint']==0 and result['objective']==0
assert len(result['fragments'])<=3
print('One interruption:')
print(' centers:',[-1,1])
print(' fragments (zero-based label, retention):',[(i,str(a)) for i,a in result['fragments']])
print(' endpoint:',result['endpoint'],'squared error:',result['objective'])
base=build_scalar_reduction([1,1])
balanced=balance_scalar_centers(base['centers'],base['rho'])
order=[0,1,3,2]+list(balanced['sentinel_suffix'])
assert sum(balanced['centers'])==0
assert endpoint(balanced['centers'],order,balanced['rho'])==0
print('Balanced scalar construction:')
print(' requests:',len(balanced['centers']),'mean:',sum(balanced['centers'])/len(balanced['centers']))
print(' zero-endpoint witness:',order)
print('Both exact examples passed.')