def __get_formatted_results(self,
                             results: List[Match]) -> List[FormattedResult]:
     ret = []
     for r in results:
         an = self.create_an_series(r.rhs_an_poly, 250)
         bn = self.create_bn_series(r.rhs_bn_poly, 250)
         print_length = max(
             max(get_size_of_nested_list(r.rhs_an_poly),
                 get_size_of_nested_list(r.rhs_bn_poly)), 5)
         gcf = GeneralizedContinuedFraction(an, bn)
         sym_lhs = self.hash_table.evaluate_sym(r.lhs_key, self.const_sym)
         ret.append(
             FormattedResult(sym_lhs, gcf.sym_expression(print_length),
                             gcf))
     return ret
Пример #2
0
 def test_enumeration_over_gcf(self):
     enumerator = EnumerateOverGCF([sympy.pi], 4)
     results = enumerator.find_hits([[0, 1, 2]] * 2, [[0, 1, 2]] * 3,
                                    print_results=False)
     self.assertEqual(len(results), 1)
     r = results[0]
     an = create_series_from_compact_poly(r.rhs_an_poly, 1000)
     bn = create_series_from_compact_poly(r.rhs_bn_poly, 1000)
     gcf = GeneralizedContinuedFraction(an, bn)
     with mpmath.workdps(100):
         lhs_val = mpmath.nstr(gcf.evaluate(), 50)
         rhs_val = enumerator.hash_table.evaluate(r.lhs_key, [mpmath.pi])
         rhs_val = mpmath.nstr(rhs_val, 50)
         rhs_sym = enumerator.hash_table.evaluate_sym(r.lhs_key, [sympy.pi])
         self.assertEqual(lhs_val, rhs_val)
         self.assertTrue(rhs_sym == 4 / pi)
 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)))
Пример #4
0
 def test_alternating_sign_simple_cf(self):
     f_sym = e / (e - 1)
     shift_reg_cmp = [1, 0, -2, 0, 1]
     f_const = lambdify((), f_sym, modules="mpmath")
     with mpmath.workdps(self.precision):
         lhs = f_sym
         rhs = GeneralizedContinuedFraction.from_irrational_constant(
             f_const, [1, -1] * (self.precision // 10))
         self.compare(lhs, rhs, self.precision // 20)
         shift_reg = massey.slow_massey(rhs.a_, 199)
         self.assertEqual(len(shift_reg), len(shift_reg_cmp))
         for i in range(len(shift_reg)):
             self.assertEqual(shift_reg[i], shift_reg_cmp[i])
Пример #5
0
 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)))
Пример #6
0
 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)))
Пример #7
0
 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)))
 def find_signed_rcf_conj(self):
     """
     Builds the final domain.
     Iterates throgh the domain:
     extraction->massey->check->save.
     Additional checks are performed to exclude degenerated cases.
     If a generic enumeration is given will use it instead of enumerating.
     """
     inter_results = []
     redundant_cycles = set()
     # Enumerate:
     if self.custom_enum is None:
         lhs = self.create_rational_variations_enum()
     else:
         if self.do_print:
             print("Substituting " + str(self.const_sym) + ' into generic LHS:')
         strt = time()
         lhs = [var.subs({sympy.symbols('x'): self.const_sym}) for var in self.custom_enum]
         if self.do_print:
             print("Took {} sec".format(time() - strt))
     sign_seqs = []
     for cyc_len in range(self.min_cycle_len, self.max_cycle_len + 1):
         sign_seqs = sign_seqs + list(itertools.product([-1, 1], repeat=cyc_len))
     domain_size = len(lhs) * len(sign_seqs)
     if self.do_print:
         print("De-Facto Domain Size is: {}\n Starting preliminary search...".format(domain_size))
     checkpoint = max(domain_size // 20, 5)
     count = 0
     start = time()
     # Iterate
     bad_variation = []
     for instance in itertools.product(lhs, sign_seqs):
         count += 1
         var, sign_period = instance[0], list(instance[1])
         if var == bad_variation:
             continue
         bad_variation = []
         if ''.join([str(c) for c in sign_period]) in redundant_cycles:
             continue
         # if this cycle was not redundant it renders some future cycles redundant:
         for i in range(2, (self.max_cycle_len // len(sign_period)) + 1):
             redun = sign_period * i
             redundant_cycles.add(''.join([str(c) for c in redun]))
         var_gen = lambdify((), var, modules="mpmath")
         seq_len = len(sign_period)
         if (count % checkpoint == 0) and (self.do_print):
             print("\n{}% of domain searched.".format(round(100 * count / domain_size, 2)))
             print("{} possible results found".format(len(inter_results)))
             print("{} minutes passed.\n".format(round((time() - start) / 60, 2)))
         b_ = (sign_period * ((self.depth // seq_len) + 1))  # Concatenate periods to form sequence.
         b_ = b_[:self.depth]  # Cut to proper size.
         with mpmath.workdps(self.enum_dps):
             try:
                 signed_rcf = GeneralizedContinuedFraction.from_irrational_constant(const_gen=var_gen, b_=b_)
             except ZeroDivisionError:
                 if self.do_print:
                     print('lhs:')
                 sympy.pprint(var)
                 bad_variation = var
                 continue
         a_ = signed_rcf.a_
         if 0 in a_:
             continue
         if len(a_) < self.depth:
             continue
         a_lfsr = list(slow_massey(a_, self.prime))
         clear_end_zeros(a_lfsr)
         if len(a_lfsr) < self.beauty_standard:
             inter_results.append([var, sign_period, a_[:(len(a_lfsr)-1)], a_lfsr])
     return inter_results