def one_centre_gram_energy(
composition: Sequence[int],
occurrence_channels: Sequence[int],
graph: DependenceGraph,
*,
centre_channel: int,
) -> Fraction:
"""Return the exact rational endpoint-coordinate Gram energy.
The active occurrence-channel graph must be a star with exactly one
occurrence of ``centre_channel``. Distinct leaf channels must commute.
The returned energy is zero exactly when the graph-block cell vanishes.
"""
occupancies, centre_block, leaf_degree = _prepare_star_cell(
composition,
occurrence_channels,
graph,
centre_channel,
)
channel_rows = tuple(
_leaf_rows(occupancy, centre_block, leaf_degree) for occupancy in occupancies
)
theta = _theta_table(leaf_degree)
energy = Fraction()
for left_power in range(leaf_degree + 1):
for right_power in range(leaf_degree + 1):
contracted: dict[_GramTotals, Fraction] = {(0, 0, 0, 0): Fraction(1)}
for rows in channel_rows:
local = _local_gram(rows[left_power], rows[right_power])
contracted = _contract(contracted, local)
if not contracted:
break
for (
left_degree,
left_centre_degree,
right_degree,
right_centre_degree,
), coefficient in contracted.items():
left_theta = theta[leaf_degree - left_degree][left_power]
right_theta = theta[leaf_degree - right_degree][right_power]
energy += (
coefficient
* left_theta
* right_theta
/ ((left_centre_degree + 1) * (right_centre_degree + 1))
)
return energy