use num_complex::Complex64;
use statrs::function::gamma::gamma;
use std::f64::consts::PI;

fn iv(v: f64, x: Complex64, terms: usize) -> Complex64 {
    let mut result = Complex64::new(0.0, 0.0);
    let half_x = x / 2.0;
    for k in 0..terms {
        let numerator = half_x.powf(2.0 * k as f64 + v);
        let denom = (gamma(k as f64 + 1.0) * gamma(k as f64 + v + 1.0)) as f64;
        result += numerator / denom;
    }
    result
}
fn kv(v: f64, x: Complex64, terms: usize) -> Complex64 {
    let sin_pi_v = (PI * v).sin();
    if sin_pi_v.abs() < 1e-10 {
        panic!("整数阶 v 尚未支持");
    }
    let iv_pos = iv(v, x, terms);
    let iv_neg = iv(-v, x, terms);
    let numerator = iv_neg - iv_pos;
    let denominator = sin_pi_v;
    (PI / 2.0) * (numerator / denominator)
}
pub fn kve(v: f64, x: Complex64, terms: usize) -> Complex64 {
    x.exp() * kv(v, x, terms)
}
