use num_complex::Complex;
use num_traits::Float;
use crate::Vector;
pub trait VAbs: Vector
{
type Output;
fn abs_sqr(&self) -> Self::Output;
fn abs(&self) -> Self::Output;
}
impl<const N: usize> VAbs for [f32; N]
{
type Output = f32;
fn abs_sqr(&self) -> Self::Output
{
self.iter().map(|x| x.abs().powi(2)).reduce(|a, b| a + b).unwrap_or(0.0)
}
fn abs(&self) -> Self::Output
{
self.abs_sqr().sqrt()
}
}
impl<const N: usize> VAbs for [f64; N]
{
type Output = f64;
fn abs_sqr(&self) -> Self::Output
{
self.iter().map(|x| x.abs().powi(2)).reduce(|a, b| a + b).unwrap_or(0.0)
}
fn abs(&self) -> Self::Output
{
self.abs_sqr().sqrt()
}
}
impl<F: Float, const N: usize> VAbs for [Complex<F>; N]
{
type Output = F;
fn abs_sqr(&self) -> Self::Output
{
self.iter().map(|x| x.norm_sqr()).reduce(|a, b| a + b).unwrap_or(F::zero())
}
fn abs(&self) -> Self::Output
{
self.abs_sqr().sqrt()
}
}