# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Regenerate the portable DynGMA cases on standard output."""

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reference"))
import models as reference


def build():
    affine = dict(
        start=[.42, -.37], lag=.63, substeps=2, matrix=[[-.6, .7], [-.25, -.8]],
        bias=[.2, -.15], noise_base=[[.55, 0.], [-.22, .38]],
        noise_slope=[[.09, .13], [.17, -.08]],
    )
    cases = [
        dict(name="midpoint and transported correlated covariance", operation="affine_mixture",
             inputs=dict(affine, steps=1), tol=1e-12),
        dict(name="two-dimensional cubature after the first covariance", operation="affine_mixture",
             inputs=dict(affine, steps=2), tol=1e-12),
        dict(name="two-dimensional mixture log density with correlated covariance", operation="affine_logpdf",
             inputs=dict(affine, steps=2, end=[.1, -.6]), tol=1e-12),
        dict(name="weighted nonlinear mixture after three stages", operation="benes_logpdf",
             inputs=dict(start=.51, end=-.42, lag=1.02, steps=3, substeps=2, weights=[.84,-.35]), tol=1e-12),
        dict(name="log density in an underflowing tail", operation="benes_logpdf",
             inputs=dict(start=.34, end=39., lag=.43, steps=2, substeps=2, weights=[.72,-.9]), tol=1e-9),
        dict(name="finite-lag training loss and both weight derivatives", operation="training_loss_gradient",
             inputs=dict(pairs=[[.52,.67,.39],[-.27,.09,.73]], steps=2,
                         substeps=2, weights=[.84,-.45]), tol=2e-8),
    ]
    for case in cases:
        case["expected"] = getattr(reference, case["operation"])(**case["inputs"])
    return {
        "format": 1, "method": "dyngma", "version": "0.1.1",
        "interface": (
            "Double precision, row-major matrices, state vectors treated as columns, absolute tolerances. "
            "Implement Algorithm 1 and Eq. (14) as given in SKILL.md, with exactly steps equal mixture stages "
            "and substeps equal inner iterations per stage, starting at a point. Advance the first stage once. "
            "For each later stage visit parents in order. Within each parent output the center, then the "
            "positive axes in increasing coordinate order, then the negative axes in increasing coordinate order. "
            "affine_mixture(start,lag,steps,substeps,matrix,bias,noise_base,noise_slope) returns a list of "
            "[weight,mean,covariance] triples. The drift is matrix*x+bias, its Jacobian is matrix, and "
            "sigma[i][j]=noise_base[i][j]+x[i]*noise_slope[i][j]. "
            "affine_logpdf(start,end,lag,steps,substeps,matrix,bias,noise_base,noise_slope) returns the normalized "
            "log density of the vector end under the same mixture, as in step 4 of the Algorithm. "
            "benes_logpdf(start,end,lag,steps,substeps,weights) takes scalar observations and returns the "
            "normalized log density under the scalar model f(x)=a*tanh(x), sigma=exp(b), for weights [a,b]. "
            "training_loss_gradient(pairs,weights,steps,substeps) returns [mean negative log density,"
            "[d_loss/d_a,d_loss/d_b]] for that model over [start,end,lag] triples. Differentiate every kernel "
            "and cubature center, with endpoints, steps and substeps fixed. Export all four functions from port_adapter.py."
        ),
        "cases": cases,
    }


if __name__ == "__main__":
    print(json.dumps(build(), indent=2, allow_nan=False))
