#!/usr/bin/env python3
"""
Final closed-form equation exactly as written in the frozen manuscript.
Ec = 9.9 * fc^0.31 * (1 + 0.0055 * Vf^0.78) * exp(-0.0002 * fc) + 0.011 * (L/D)^0.85
"""
from __future__ import annotations
import numpy as np
import pandas as pd

DEFAULT_PARAMS = {
    "A": 9.9,
    "alpha": 0.31,
    "beta": 0.0055,
    "gamma": 0.78,
    "kappa": 0.0002,
    "delta": 0.011,
    "epsilon": 0.85,
}

def ec_model(fc_mpa, vf_percent, aspect_ratio_ld, params=None):
    p = DEFAULT_PARAMS if params is None else params
    fc = np.asarray(fc_mpa, dtype=float)
    vf = np.asarray(vf_percent, dtype=float)
    ld = np.asarray(aspect_ratio_ld, dtype=float)
    return (
        p["A"] * np.power(fc, p["alpha"]) * (1.0 + p["beta"] * np.power(vf, p["gamma"])) * np.exp(-p["kappa"] * fc)
        + p["delta"] * np.power(ld, p["epsilon"])
    )

def add_prediction_columns(df: pd.DataFrame, params=None) -> pd.DataFrame:
    out = df.copy()
    out["ec_pred_from_final_formula_gpa"] = ec_model(out["fc_mpa"], out["vf_percent"], out["aspect_ratio_ld"], params=params)
    out["residual_from_final_formula_gpa"] = out["ec_exp_gpa"] - out["ec_pred_from_final_formula_gpa"]
    out["residual_from_final_formula_sq_gpa2"] = out["residual_from_final_formula_gpa"] ** 2
    return out
