"""Harmonic oscillator stepper.

The documented contract is a symplectic update: total energy stays conserved
up to a small bounded error, with no secular drift.
"""

from __future__ import annotations


def total_energy(state: tuple[float, float], k: float = 1.0, m: float = 1.0) -> float:
    """Total energy of a harmonic oscillator, ½mv² + ½kx²."""
    x, v = state
    return 0.5 * m * v * v + 0.5 * k * x * x


def step(state: tuple[float, float], dt: float, k: float = 1.0, m: float = 1.0) -> tuple[float, float]:
    """Advance the harmonic oscillator one step.

    Documented contract: symplectic update. Total energy is conserved
    up to a small bounded error, with no secular drift.
    """
    x, v = state
    a = -(k / m) * x
    x_new = x + dt * v
    v_new = v + dt * a
    return (x_new, v_new)
