"""Direct q_ctl-to-uniform-q_in profile objective and steady diagnostics."""
from __future__ import annotations
import numpy as np


def profile_errors(ux: np.ndarray, uy: np.ndarray, *, u_inf: float) -> dict[str, float]:
    ux=np.asarray(ux,dtype=float); uy=np.asarray(uy,dtype=float)
    if ux.shape != uy.shape or ux.ndim != 1: raise ValueError("profiles must be aligned 1-D arrays")
    if not np.isfinite(ux).all() or not np.isfinite(uy).all() or u_inf <= 0: raise ValueError("invalid profile")
    du=ux/u_inf-1.0; dv=uy/u_inf
    vec=np.hypot(du,dv)
    return {"E_inf_x":float(np.max(np.abs(du))), "E_inf_y":float(np.max(np.abs(dv))),
            "E_inf_vector":float(np.max(vec)), "E_L2_vector":float(np.sqrt(np.mean(vec*vec)))}


def two_window_change(samples: np.ndarray) -> float:
    a=np.asarray(samples,dtype=float)
    if a.ndim < 1 or a.shape[0] < 4: return float("inf")
    n=a.shape[0]//2
    x=np.mean(a[:n],axis=0); y=np.mean(a[-n:],axis=0)
    return float(np.sqrt(np.mean((y-x)**2)))
