"""Independent finite checks of the new radial formulas, not analytic proofs.""" from collections import Counter import itertools import math import unittest import sympy as sp class RadialRefinements(unittest.TestCase): def test_first_correction_from_exact_gamma_ratios(self): # Substitute m=1/u into the exact coefficient after radial integration. # Differentiate its finite numerator/denominator at u=0, rather than # using the logarithmic expansion used in the manuscript's proof. u = sp.Symbol("u") for n, degree in itertools.product((1, 2, 5), (2, 3, 4)): for ks in itertools.product(range(5), repeat=degree): count = sum(ks) if count > 4: continue weight = sum((j + 1) * k for j, k in enumerate(ks)) excess = weight - count numerator = sp.prod(1 - i * u for i in range(count)) denominator = sp.prod(degree + (n - r) * u for r in range(1, weight + 1)) ratio = sp.sympify(numerator) / denominator constant = ratio.subs(u, 0) if excess == 0 else 0 linear = (sp.diff(ratio, u).subs(u, 0) if excess == 0 else ratio.subs(u, 0) if excess == 1 else 0) expected_constant = sp.Rational(1, degree ** count) if excess == 0 else 0 expected_linear = 0 if excess == 0: expected_linear = -sp.Rational( (degree - 1) * count * (count - 1) + 2 * (n - 1) * count, 2 * degree ** (count + 1)) elif excess == 1: expected_linear = sp.Rational(1, degree ** weight) with self.subTest(n=n, degree=degree, multi_index=ks): self.assertEqual(constant, expected_constant) self.assertEqual(linear, expected_linear) def test_zero_leading_period_example(self): t = sp.Symbol("t", real=True) phase = sp.exp(2 * sp.pi * sp.I * t) self.assertEqual(sp.integrate(phase, (t, 0, 1)), 0) correction = sp.integrate( phase * (sp.pi ** 2 * t ** 2 - sp.pi * sp.I * t), (t, 0, 1)) self.assertEqual(sp.simplify(correction), -sp.pi * sp.I / 2) def test_coupled_monomial_with_repeated_powers(self): # The first block carries the extra radial degree. Its scale cancels # when the degree-next-to-top monomial is evaluated on the simplex. powers = (1, 2, 2) degrees = (2, 2, 2) angles = sp.symbols("u:3") scale = sp.Symbol("z") normalized = (degrees[0] * scale * sp.prod((d * u) ** a for a, d, u in zip(powers, degrees, angles)) / sp.prod(d ** d for d in degrees)) phase = angles[0] * angles[1] ** 2 * angles[2] ** 2 self.assertEqual(sp.expand(normalized - scale * phase), 0) for k in range(6): coefficient = sp.integrate(phase ** k, *((u, 0, 1) for u in angles)) # Includes the reducible b=1 term and both orders at b=1/2. expected = (sp.Rational(1, k + 1) - sp.Rational(2, 2 * k + 1) + sp.Rational(2, (2 * k + 1) ** 2)) self.assertEqual(coefficient, expected) def test_simplex_hypergeometric_parameters(self): # Compare the reduced hypergeometric series with raw Dirichlet moments. cases = [ ((1, 2), (1, 1)), # Unequal powers, nonresonant. ((1, 1, 2), (1, 1, 1)), # Exact cancellation leaves resonance. ((2, 1), (sp.Rational(1, 2), 2)), ] cases.extend(((q,) * d, (1,) * d) for d in range(2, 6) for q in range(1, 5)) for powers, shapes in cases: total = sum(powers) mass = sum(shapes) upper = Counter(sp.Rational(b + h, a) for a, b in zip(powers, shapes) for h in range(a)) lower = Counter([sp.Integer(1)]) lower.update(sp.Rational(mass + h, total) for h in range(total)) reduced_upper, reduced_lower = upper - lower, lower - upper scale = sp.Rational(math.prod(a ** a for a in powers), total ** total) with self.subTest(powers=powers, shapes=shapes): resonance = any((v - u).is_integer for u in reduced_upper for v in reduced_lower) if len(set(powers)) == 1 and all(b == 1 for b in shapes): self.assertEqual(reduced_lower.total(), powers[0] * (len(powers) - 1)) self.assertFalse(resonance) if powers == (1, 1, 2): self.assertTrue(resonance) if powers == (1, 2): self.assertFalse(resonance) for k in range(7): coefficient = (scale ** k * sp.prod(sp.rf(u, k) for u in reduced_upper.elements()) / sp.prod(sp.rf(v, k) for v in reduced_lower.elements())) moment = (sp.prod(sp.rf(b, a * k) for a, b in zip(powers, shapes)) / sp.rf(mass, total * k)) self.assertEqual(coefficient * math.factorial(k), moment) def test_cube_relations_and_shift_reduction(self): # Compare the coefficient identities before evaluating any E-function. k = sp.Symbol("k") w1, w2 = 1 / (1 + k), 1 / (1 + 2 * k) self.assertEqual(sp.cancel(w1 * w2 - 2 * w2 + w1), 0) for b in (sp.Rational(1, 3), 1, sp.Rational(3, 2)): for j in (1, 2, 3): # w H_(b+1,j) has zero constant coefficient. self.assertEqual(sp.cancel((k + b) ** (1 - j) - b / (k + b) ** j - k / (k + b) ** j), 0) v = sp.Symbol("v") self.assertEqual(2 * sp.integrate(v * sp.exp(v), (v, 0, 1)), 2) def test_scale_polynomials_in_both_factorial_families(self): x, y, scale = sp.symbols("x y scale") for top, coupling, first_root in [((x + y) ** 2, 2 * x, -3), ((x + y) ** 3, 3 * x * y, -8)]: for m in (1, 3, 5): terms = sp.Poly((top + scale * coupling) ** m, x, y).terms() moment = sp.Poly(sum(c * math.factorial(i) * math.factorial(j) for (i, j), c in terms), scale) self.assertEqual(moment.degree(), m) self.assertGreater(moment.LC(), 0) self.assertGreater(moment.eval(0), 0) if m == 1: self.assertEqual(moment.eval(first_root), 0) def test_integer_gamma_replication(self): for alpha in range(1, 5): xs = sp.symbols("x:" + str(alpha)) for exponent in range(7): polynomial = sp.Poly(sum(xs) ** exponent, *xs) replicated = sum(coefficient * math.prod(math.factorial(e) for e in powers) for powers, coefficient in polynomial.terms()) self.assertEqual(replicated, sp.rf(alpha, exponent)) if __name__ == "__main__": unittest.main()