Coverage for src/hypra/hpl/arguments.py: 86.44%
39 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
1"""Explicit expression-level dispatch for supported HPL argument maps."""
3from typing import cast
5import sympy as s
7from .._typing import Boundary, Interval
8from ..errors import UnsupportedExpressionError
9from ..symbolic import HPL
10from .inversion import invert_hpl
11from .maps import fractional_hpl, square_hpl
12from .mobius import mobius_hpl
13from .transforms import negate_hpl, reflect_hpl
16def transform_hpl_arguments(
17 expression: s.Expr,
18 z: s.Symbol,
19 *,
20 domain: Interval,
21 boundary: Boundary | None = None,
22) -> s.Expr:
23 """Rewrite recognized HPL arguments in an expression to the common z.
25 Requires domain='0<z<1'. Supported arguments: z, -z, 1-z, z²,
26 (1-z)/(1+z), z/(z-1), 1/z. Cut-valued arguments require explicit
27 boundary='upper' or 'lower'. Constant-argument HPLs remain unchanged.
28 Plus/minus notation is first expanded into ordinary HPL letters.
29 Unknown variable-dependent arguments fail rather than pretending to unify.
31 Args:
32 expression: Exact expression containing HPL atoms.
33 z: Target argument shared by the transformed HPLs.
34 domain: Declared real interval, `"0<z<1"`.
35 boundary: Side of approach for arguments on a cut.
37 Raises:
38 UnsupportedExpressionError: A variable-dependent argument or its branch is unsupported.
39 """
40 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true
41 raise TypeError("Use an exact SymPy expression and symbolic target argument")
42 if domain != "0<z<1": 42 ↛ 43line 42 didn't jump to line 43 because the condition on line 42 was never true
43 raise UnsupportedExpressionError("Explicit domain='0<z<1' is required")
44 if boundary not in (None, "upper", "lower"): 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true
45 raise ValueError("boundary must be upper, lower, or omitted")
46 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 raise UnsupportedExpressionError("Argument transformation requires exact finite input")
48 expression = expression.rewrite(HPL)
49 changes = {}
50 for atom in expression.atoms(HPL):
51 arg = atom.argument
52 if arg == z or not arg.has(z):
53 continue
54 candidates = [
55 (-z, "negate"),
56 (1 - z, "reflect"),
57 (z**2, "square"),
58 ((1 - z) / (1 + z), "mobius"),
59 (z / (z - 1), "fractional"),
60 (1 / z, "inverse"),
61 ]
62 selected = next((name for target, name in candidates if s.cancel(arg - target) == 0), None)
63 if selected is None:
64 raise UnsupportedExpressionError(f"Unsupported HPL argument {arg}")
65 if selected in ("negate", "fractional", "inverse"):
66 if boundary is None:
67 raise UnsupportedExpressionError(
68 "Cut-valued arguments require an explicit boundary"
69 )
70 if selected == "negate":
71 value = negate_hpl(atom.word, z, domain=domain)
72 changes[atom] = value if boundary == "upper" else value.xreplace({s.I: -s.I})
73 else:
74 transform = fractional_hpl if selected == "fractional" else invert_hpl
75 changes[atom] = transform(atom.word, z, boundary=boundary, domain=domain)
76 else:
77 function = {"reflect": reflect_hpl, "square": square_hpl, "mobius": mobius_hpl}[
78 selected
79 ]
80 changes[atom] = function(atom.word, z, domain=domain)
81 return cast(s.Expr, expression.xreplace(changes))