use crate::IntegrateFloat;
use scirs2_core::ndarray::{Array1, ArrayView1};
#[allow(dead_code)]
pub fn stability_metrics<F: IntegrateFloat>(t: &[F], y: &[Array1<F>]) -> F {
if t.len() < 2 || y.len() < 2 {
return F::infinity(); }
let mut max_growth = F::zero();
for i in 1..t.len() {
let dt = t[i] - t[i - 1];
if dt <= F::zero() {
continue;
}
for j in 0..y[i].len() {
if j >= y[i - 1].len() {
break;
}
let dy = (y[i][j] - y[i - 1][j]).abs();
let growth = dy / dt;
if growth > max_growth {
max_growth = growth;
}
}
}
max_growth
}
#[allow(dead_code)]
pub fn stiffness_detector<F, Func>(t: &[F], y: &[Array1<F>], f: &Func) -> F
where
F: IntegrateFloat,
Func: Fn(F, ArrayView1<F>) -> Array1<F>,
{
if t.len() < 3 || y.len() < 3 {
return F::zero(); }
let mut stiffness = F::zero();
for i in 2..t.len() {
let h1 = t[i - 1] - t[i - 2];
let h2 = t[i] - t[i - 1];
if h1 <= F::zero() || h2 <= F::zero() {
continue;
}
if h2 < h1 {
let ratio = h2 / h1;
stiffness += F::one() - ratio;
}
}
stiffness
}