def get_pt(pt_str): xjs = [] for xj in pt_str.strip('()').split(','): if '/' in xj: n, d = xj.split('/') xjf = FastFraction(int(n), int(d)) else: xjf = FastFraction(int(xj), 1) xjs.append(xjf) return tuple(xjs)
def test_vertex_moments(self): known_moments = [ { 'curve': get_haverkort_curve_A26(), 'moments': [FastFraction(k, 28) for k in [0, 5, 9, 12, 16, 19, 23, 28]], }, { 'curve': get_haverkort_curve_2(), 'moments': [FastFraction(k, 28) for k in [1, 6, 8, 13, 15, 20, 22, 27]], }, ] for d in known_moments: moments = d['curve'].get_vertex_moments().values() self.assertSetEqual(set(d['moments']), set(moments))
def test_55_ratio(self): good_proto = Proto(dim=2, div=5, cubes=[ (0, 0), (0, 1), (1, 1), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2), (0, 3), (0, 4), (1, 4), (1, 3), (2, 3), (2, 4), (3, 4), (4, 4), (4, 3), (3, 3), (3, 2), (4, 2), (4, 1), (3, 1), (3, 0), (4, 0), ]) # in new version we have (0,0)->(0,1) gate good_proto = BaseMap.from_basis('ji') * good_proto paths_gen = PathsGenerator(dim=2, div=5, hdist=1, max_cdist=1, verbose=1) for paths in paths_gen.generate_paths(): if paths[0].proto == good_proto: path0 = paths[0] break pcurve = PathFuzzyCurve.init_from_paths([path0]) estimator = Estimator(utils.ratio_l2_squared) curve = estimator.estimate_ratio(pcurve, rel_tol_inv=10000, verbose=False)['curve'] ratio = estimator.estimate_ratio(curve, rel_tol_inv=10000, use_vertex_brkline=True, verbose=False) assert ratio['lo'] == (FastFraction(408, 73)**2)
def test_mul(self): assert FastFraction(2, 3) * FastFraction(-1, 2) == FastFraction(-1, 3) assert FastFraction(1, 5) * FastFraction(5, -1) == FastFraction(-1, 1)
def test_neg(self): assert -FastFraction(3, 5) == FastFraction(-3, 5) assert -FastFraction(0, 1) == FastFraction(0, -1)
def test_sub(self): assert FastFraction(1, 2) - FastFraction(1, 6) == FastFraction(1, 3)
def test_add(self): assert FastFraction(1, 3) + FastFraction(1, 6) == FastFraction(1, 2) assert FastFraction(1, 5) + FastFraction(1, -5) == FastFraction(0, 1)
kwargs['ratio_func'] = funcs.get(kwargs.pop('metric')) verbosity = kwargs.pop('verbose') if verbosity == 1: logging.basicConfig(level=logging.INFO) elif verbosity == 2: logging.basicConfig(level=logging.DEBUG) if (not args.output_stats) and (not args.output_gates) and (args.metric is None): raise ValueError("Define metric to estimate ratio!") gates_file = kwargs.pop('gates_file') if gates_file is not None: with open(gates_file) as fh: for line in fh: gates = [ Gate.parse(token) for token in line.strip().split('|') ] gate_list.append(gates) kwargs['gate_list'] = gate_list gates_str = kwargs.pop('gates') if gates_str is not None: gates = [Gate.parse(token) for token in gates_str.strip().split('|')] kwargs['gate_list'] = [gates] if args.upper_bound is not None: kwargs['upper_bound'] = FastFraction.parse(args.upper_bound) run_estimator(**kwargs)
def cnt_bnd_coords(pt): return len( list(xj for xj in pt if xj == FastFraction(0, 1) or xj == FastFraction(1, 1)))
def is_vertex(pt): if any(FastFraction(0, 1) < xj < FastFraction(1, 1) for xj in pt): return False return True
for idx, gates in enumerate(gates_list): pts = [] for g in gates: pts += [g.entrance, g.exit] vertex_count = len(list(pt for pt in pts if is_vertex(pt))) if vertex_count != 0: continue total_bnd_coords = sum( cnt_bnd_coords(gate.entrance) + cnt_bnd_coords(gate.exit) for gate in gates) add_bnd_coords = total_bnd_coords - 2 * pattern_count if add_bnd_coords != 0: continue conf = { 'dim': 2, 'div': 2, 'ratio_func_name': 'linf', #'ratio_func_name': 'l2', 'gates': gates, 'rel_tol_inv': 10000, 'upper_bound': FastFraction(5, 1), #'upper_bound': FastFraction(51, 10), } print('GATE:', idx, [str(g) for g in gates]) perebor(conf) print('===') print('')