imageproc/
math.rs

1//! Assorted mathematical helper functions.
2
3use conv::ValueInto;
4
5/// L1 norm of a vector.
6pub fn l1_norm(xs: &[f32]) -> f32 {
7    xs.iter().fold(0f32, |acc, x| acc + x.abs())
8}
9
10/// L2 norm of a vector.
11pub fn l2_norm(xs: &[f32]) -> f32 {
12    xs.iter().fold(0f32, |acc, x| acc + x * x).sqrt()
13}
14
15/// Helper for a conversion that we know can't fail.
16pub fn cast<T, U>(x: T) -> U
17where
18    T: ValueInto<U>,
19{
20    match x.value_into() {
21        Ok(y) => y,
22        Err(_) => panic!("Failed to convert"),
23    }
24}