use ndarray::Array1;
#[must_use]
pub fn entropy(px: &Array1<f64>) -> f64 {
(0..px.len()).fold(0.0, |acc, idx| {
if px[idx] == 0.0 {
acc
} else {
acc - (px[idx] * px[idx].ln())
}
})
}
#[cfg(test)]
mod testing {
use super::entropy;
use ndarray::array;
#[test]
fn test_entropy() {
assert_eq!(entropy(&array![0.5, 0.5]), 0.6931471805599453);
assert_eq!(
entropy(&array![1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0]),
1.0986122886681096
);
assert_eq!(
entropy(&array![0.25, 0.25, 0.25, 0.25]),
1.38629436111989061
);
}
}