Refactor and document operators and periods in sage module
This commit is contained in:
@@ -5,93 +5,6 @@ from sage.all import * # noqa: F401
|
||||
load(os.path.join(os.path.dirname(__file__), "..", "sage", "period.sage"))
|
||||
|
||||
|
||||
def test_apply_operator_univariate():
|
||||
# theta^2 - z applied to log(z) should give -z*log(z), since theta(log z) = 1
|
||||
# and theta^2(log z) = theta(1) = 0.
|
||||
period = Period(no_variables=1, coefficients={(1,): {(0,): 1}}, order=5)
|
||||
op = PFOperator("theta0^2 - z0", no_variables=1)
|
||||
|
||||
result = period.apply_operator(op)
|
||||
|
||||
assert result.coefficients == {(1,): {(1,): -1}}
|
||||
|
||||
|
||||
def test_apply_operator_mixes_variables():
|
||||
# z0*theta1 applied to z0*log(z1): theta1 strips log(z1) down to a bare 1
|
||||
# (leaving z0 untouched), then multiplying by z0 gives z0^2.
|
||||
period = Period(no_variables=2, coefficients={(0, 1): {(1, 0): 1}}, order=5)
|
||||
op = PFOperator("z0*theta1", no_variables=2)
|
||||
|
||||
result = period.apply_operator(op)
|
||||
|
||||
assert result.coefficients == {(0, 0): {(2, 0): 1}}
|
||||
|
||||
|
||||
def test_apply_operator_truncates_to_order():
|
||||
# Multiplying by z0^2 pushes some terms above the period's order, so they
|
||||
# should be dropped rather than kept with a nonzero coefficient.
|
||||
period = Period(no_variables=1, coefficients={(0,): {(0,): 1, (1,): 1, (2,): 1}}, order=2)
|
||||
op = PFOperator("z0^2", no_variables=1)
|
||||
|
||||
result = period.apply_operator(op)
|
||||
|
||||
assert result.coefficients == {(0,): {(2,): 1}}
|
||||
assert result.order == 2
|
||||
|
||||
|
||||
def test_find_annihilating_operators_recovers_theta_squared():
|
||||
# theta0^2 annihilates log(z0): theta0(log z0) = 1, theta0^2(log z0) = theta0(1) = 0.
|
||||
# Among degree-(0, 2) Ansatze this should be the only solution, up to scaling.
|
||||
period = Period(no_variables=1, coefficients={(1,): {(0,): 1}}, order=5)
|
||||
|
||||
ops = period.find_annihilating_operators(z_degree=0, theta_degree=2)
|
||||
|
||||
# A one-dimensional solution space means exactly one basis operator.
|
||||
assert len(ops) == 1
|
||||
assert period.apply_operator(ops[0]).coefficients == {}
|
||||
assert str(ops[0].operator) == "theta0^2"
|
||||
|
||||
|
||||
def test_find_annihilating_operators_recovers_geometric_series_operator():
|
||||
# sum_{k=0}^{4} z0^k is annihilated (up to truncation order) by
|
||||
# (1 - z0)*theta0 - z0, i.e. theta0 - z0*theta0 - z0.
|
||||
period = Period(
|
||||
no_variables=1,
|
||||
coefficients={(0,): {(0,): 1, (1,): 1, (2,): 1, (3,): 1, (4,): 1}},
|
||||
order=4,
|
||||
)
|
||||
|
||||
ops = period.find_annihilating_operators(z_degree=1, theta_degree=1)
|
||||
|
||||
assert len(ops) == 1
|
||||
assert period.apply_operator(ops[0]).coefficients == {}
|
||||
|
||||
|
||||
def test_find_annihilating_operators_returns_empty_list_when_no_solution_exists():
|
||||
# No degree-0 (constant) operator other than the zero operator can annihilate a
|
||||
# nonzero constant period, so the linear system's only solution is trivial.
|
||||
period = Period(no_variables=1, coefficients={(0,): {(0,): 1}}, order=0)
|
||||
|
||||
ops = period.find_annihilating_operators(z_degree=0, theta_degree=0)
|
||||
|
||||
assert ops == []
|
||||
|
||||
|
||||
def test_simplify_factorises_theta_polynomial_per_z_monomial():
|
||||
# theta0^4 - 5*z0*(5*theta0+1)*(5*theta0+2)*(5*theta0+3)*(5*theta0+4), expanded, is the
|
||||
# quintic's Picard-Fuchs operator. simplify() should recover the factorised form: the
|
||||
# z0^0 part (theta0^4) has no theta-factor to pull out, while the z0^1 part factorises
|
||||
# into the four linear pieces.
|
||||
expanded = "theta0^4 - 3125*z0*theta0^4 - 6250*z0*theta0^3 - 4375*z0*theta0^2 - 1250*z0*theta0 - 120*z0"
|
||||
op = PFOperator(expanded, no_variables=1)
|
||||
|
||||
simplified = op.simplify()
|
||||
|
||||
# The underlying (expanded) operator is unchanged - only the display string differs.
|
||||
assert simplified.operator == op.operator
|
||||
assert simplified.operator_string == "-5*(5*theta0 + 4)*(5*theta0 + 3)*(5*theta0 + 2)*(5*theta0 + 1)*z0 + theta0^4"
|
||||
|
||||
|
||||
def test_find_power_series_solution_recovers_quintic_period():
|
||||
# The quintic's Picard-Fuchs operator has indicial equation theta0^4 = 0 at z0 = 0
|
||||
# (a quadruple root at 0), so its holomorphic (non-logarithmic) power series solution
|
||||
@@ -190,14 +103,3 @@ def test_single_operator_recovers_itself_from_its_holomorphic_period():
|
||||
assert ratio != 0
|
||||
assert recovered[0].operator == ratio * L.operator
|
||||
|
||||
|
||||
def test_change_coordinates_handles_non_monomial_denominator():
|
||||
op = PFOperator("theta0^2 - z0", no_variables=1)
|
||||
|
||||
result = op.change_coordinates("z0/(1-z0)")
|
||||
|
||||
expected = PFOperator(
|
||||
"(1+z0)^3*theta0^2 + z0*(1+z0)^2*theta0 - z0",
|
||||
no_variables=1,
|
||||
)
|
||||
assert result.operator == expected.operator
|
||||
|
||||
Reference in New Issue
Block a user