def print_results(self, results: List[Match], latex=False, convergence_rate=True): """ pretty print the the results. :param convergence_rate: if True calculate convergence rate and print it as well. :param results: list of final results as received from refine_results. :param latex: if True print in latex form, otherwise pretty print in unicode. """ formatted_results = self.__get_formatted_results(results) for r, raw_r in zip(formatted_results, results): result = sympy.Eq(r.LHS, r.RHS) if latex: print(f'$$ {sympy.latex(result)} $$') print( f'$$ {sympy.latex(self.__get_formatted_polynomials(raw_r))} $$\n' ) else: sympy.pprint(result) sympy.pprint(self.__get_formatted_polynomials(raw_r)) print('') if convergence_rate: with mpmath.workdps(self.verify_dps): rate = calculate_convergence( r.GCF, lambdify((), r.LHS, 'mpmath')()) print("Converged with a rate of {} digits per term".format( mpmath.nstr(rate, 5)))
def print_results(self, results, latex=True): """ Print results in either unicode or LaTex. :param results: verified results. :param latex: LaTex printing flag. """ for res_num, res in enumerate(results): var_sym = res[0] lfsr = res[3] cycle = res[1] initials = res[2] var_gen = lambdify((), var_sym, modules="mpmath") a_ = create_series_from_shift_reg(lfsr, initials, self.depth) b_ = (cycle * (self.depth // len(cycle)))[:self.depth] gcf = GeneralizedContinuedFraction(a_, b_) rate = calculate_convergence(gcf, lambdify((), var_sym, 'mpmath')()) if not latex: print(str(res_num)) print('lhs: ') sympy.pprint(var_sym) print('rhs :') gcf.print(8) print('lhs value: ' + mpmath.nstr(var_gen(), 50)) print('rhs value: ' + mpmath.nstr(gcf.evaluate(), 50)) print('a_n LFSR: {},\n With initialization: {}'.format(lfsr, initials)) print('b_n period: ' + str(cycle)) print("Converged with a rate of {} digits per term".format(mpmath.nstr(rate, 5))) else: equation = sympy.Eq(var_sym, gcf.sym_expression(5)) print('\n\n' + str(res_num + 1) + '. $$ ' + sympy.latex(equation) + ' $$\n') print('$\\{a_n\\}$ LFSR: \\quad \\quad \\quad \\quad \\;' + str(lfsr)) print('\n$\\{a_n\\}$ initialization: \\quad \\; ' + str(initials)) print('\n$\\{b_n\\}$ Sequence period: \\! ' + str(cycle)) print('\nConvergence rate: ${}$ digits per term\n\n'.format(mpmath.nstr(rate, 5)))
def test_known_catalan(self): """ test new findings of zeta values in data.py """ with mpmath.workdps(2000): for t in data.data.catalan: with self.subTest(test_constant=t): d = data.data.catalan[t] rhs_an = [d.rhs_an(i) for i in range(400)] rhs_bn = [d.rhs_bn(i) for i in range(400)] rhs = GeneralizedContinuedFraction(rhs_an, rhs_bn) self.compare(d.lhs, rhs, 100) rate = calculate_convergence( rhs, lambdify((), d.lhs, 'mpmath')()) print("Converged with a rate of {} digits per term".format( mpmath.nstr(rate, 5)))
def test_known_constants(self): """ unittests for our Continued Fraction class. all examples were taken from the Ramanujan Machine paper. when I didn't know how to describe some series's rules, i used the Massey algorithm to generate it's shift register. very useful! """ TestConstant = namedtuple('TestConstant', 'name lhs rhs_an rhs_bn') test_cases = [ TestConstant(name='e1', lhs=e / (e - 2), rhs_an=create_series_from_polynomial([4, 1], self.depth), rhs_bn=create_series_from_polynomial([-1, -1], self.depth)), TestConstant(name='e2', lhs=1 / (e - 2) + 1, rhs_an=create_series_from_polynomial([1], self.depth), rhs_bn=create_series_from_shift_reg([1, 0, -2, 0, 1], [1, -1, 2, -1], self.depth)), TestConstant( name='pi1', lhs=4 / (3 * pi - 8), rhs_an=create_series_from_polynomial([3, 3], self.depth), rhs_bn=create_series_from_shift_reg([1, -3, 3, -1], [-1 * 1, -2 * 3, -3 * 5], self.depth)), TestConstant(name='phi1', lhs=phi, rhs_an=create_series_from_polynomial([1], self.depth), rhs_bn=create_series_from_polynomial([1], self.depth)) ] with mpmath.workdps(self.precision): for t in test_cases: with self.subTest(test_constant=t.name): rhs = GeneralizedContinuedFraction(t.rhs_an, t.rhs_bn) self.compare(t.lhs, rhs, self.precision) rate = calculate_convergence( rhs, lambdify((), t.lhs, 'mpmath')()) print("Converged with a rate of {} digits per term".format( mpmath.nstr(rate, 5)))
def known_data_test(self, cf_data, print_convergence=False): """ test all "known data" that is in data.py :param print_convergence: :param cf_data: a database defined in data.py :type cf_data: CFData """ with mpmath.workdps(2000): for t in cf_data: with self.subTest(test_constant=t): d = cf_data[t] rhs_an = create_series_from_shift_reg( d.rhs_an.shift_reg, d.rhs_an.initials, 400) rhs_bn = create_series_from_shift_reg( d.rhs_bn.shift_reg, d.rhs_bn.initials, 400) rhs = GeneralizedContinuedFraction(rhs_an, rhs_bn) self.compare(d.lhs, rhs, 100) if print_convergence: rate = calculate_convergence( rhs, lambdify((), d.lhs, 'mpmath')(), False, t) print("Converged with a rate of {} digits per term". format(mpmath.nstr(rate, 5)))