FloatIterExt

Trait FloatIterExt 

Source
pub trait FloatIterExt<T: Float + FromPrimitive>: Iterator<Item = T> {
    // Provided methods
    fn nanmin(self, ignore_nans: bool) -> T
       where Self: Sized { ... }
    fn nanmax(self, ignore_nans: bool) -> T
       where Self: Sized { ... }
    fn nanminmax(self, ignore_nans: bool) -> NanMinMaxResult<T>
       where Self: Sized { ... }
    fn nanmean(self, ignore_nans: bool) -> T
       where Self: Sized { ... }
}
Expand description

Helper trait for calculating summary statistics on floating point iterators with alternative NaN handling.

This is intended to be similar to numpy’s nanmean, nanmin, nanmax etc.

Provided Methods§

Source

fn nanmin(self, ignore_nans: bool) -> T
where Self: Sized,

Returns the minimum of all elements in the iterator, handling NaN values.

If ignore_nans is true, NaN values will be ignored and not included in the minimum. Otherwise, the minimum will be NaN if any element is NaN.

§Examples
§Simple usage
use augurs_core::FloatIterExt;

let x = [1.0, 2.0, 3.0, f64::NAN, 5.0];
assert_eq!(x.iter().copied().nanmin(true), 1.0);
assert!(x.iter().copied().nanmin(false).is_nan());
§Empty iterator
use augurs_core::FloatIterExt;

let x: [f64; 0] = [];
assert!(x.iter().copied().nanmin(true).is_nan());
assert!(x.iter().copied().nanmin(false).is_nan());
§Only NaN values
use augurs_core::FloatIterExt;

let x = [f64::NAN, f64::NAN];
assert!(x.iter().copied().nanmin(true).is_nan());
assert!(x.iter().copied().nanmin(false).is_nan());
Source

fn nanmax(self, ignore_nans: bool) -> T
where Self: Sized,

Returns the maximum of all elements in the iterator, handling NaN values.

If ignore_nans is true, NaN values will be ignored and not included in the maximum. Otherwise, the maximum will be NaN if any element is NaN.

§Examples
§Simple usage
use augurs_core::FloatIterExt;

let x = [1.0, 2.0, 3.0, f64::NAN, 5.0];
assert_eq!(x.iter().copied().nanmax(true), 5.0);
assert!(x.iter().copied().nanmax(false).is_nan());
§Empty iterator
use augurs_core::FloatIterExt;

let x: [f64; 0] = [];
assert!(x.iter().copied().nanmax(true).is_nan());
assert!(x.iter().copied().nanmax(false).is_nan());
§Only NaN values
use augurs_core::FloatIterExt;

let x = [f64::NAN, f64::NAN];
assert!(x.iter().copied().nanmax(true).is_nan());
assert!(x.iter().copied().nanmax(false).is_nan());
Source

fn nanminmax(self, ignore_nans: bool) -> NanMinMaxResult<T>
where Self: Sized,

Returns the minimum and maximum of all elements in the iterator, handling NaN values.

If ignore_nans is true, NaN values will be ignored and not included in the minimum or maximum. Otherwise, the minimum and maximum will be NaN if any element is NaN.

The return value is a NanMinMaxResult, which is similar to itertools::MinMaxResult and provides more granular information on the result.

§Examples
§Simple usage, ignoring NaNs
use augurs_core::{FloatIterExt, NanMinMaxResult};

let x = [1.0, 2.0, 3.0, f64::NAN, 5.0];
let min_max = x.iter().copied().nanminmax(true);
assert_eq!(min_max, NanMinMaxResult::MinMax(1.0, 5.0));
§Simple usage, including NaNs
use augurs_core::{FloatIterExt, NanMinMaxResult};

let x = [1.0, 2.0, 3.0, f64::NAN, 5.0];
let min_max = x.iter().copied().nanminmax(false);
assert_eq!(min_max, NanMinMaxResult::NaN);
§Only NaNs
use augurs_core::{FloatIterExt, NanMinMaxResult};

let x = [f64::NAN, f64::NAN, f64::NAN];
let min_max = x.iter().copied().nanminmax(true);
assert_eq!(min_max, NanMinMaxResult::NoElements);

let min_max = x.iter().copied().nanminmax(false);
assert_eq!(min_max, NanMinMaxResult::NaN);
§Empty iterator
use augurs_core::{FloatIterExt, NanMinMaxResult};

let x: [f64; 0] = [];
let min_max = x.iter().copied().nanminmax(true);
assert_eq!(min_max, NanMinMaxResult::NoElements);

let min_max = x.iter().copied().nanminmax(false);
assert_eq!(min_max, NanMinMaxResult::NoElements);
§Only one distinct element
use augurs_core::{FloatIterExt, NanMinMaxResult};

let x = [1.0, f64::NAN, 1.0];
let min_max = x.iter().copied().nanminmax(true);
assert_eq!(min_max, NanMinMaxResult::OneElement(1.0));

let min_max = x.iter().copied().nanminmax(false);
assert_eq!(min_max, NanMinMaxResult::NaN);
Source

fn nanmean(self, ignore_nans: bool) -> T
where Self: Sized,

Returns the mean of all elements in the iterator, handling NaN values.

If ignore_nans is true, NaN values will be ignored and not included in the mean. Otherwise, the mean will be NaN if any element is NaN.

§Examples
§Simple usage
use augurs_core::FloatIterExt;

let x = [1.0, 2.0, 3.0, f64::NAN, 4.0];
assert_eq!(x.iter().copied().nanmean(true), 2.5);
assert!(x.iter().copied().nanmean(false).is_nan());
§Empty iterator
use augurs_core::FloatIterExt;

let x: [f64; 0] = [];
assert!(x.iter().copied().nanmean(true).is_nan());
assert!(x.iter().copied().nanmean(false).is_nan());
§Only NaN values
use augurs_core::FloatIterExt;

let x = [f64::NAN, f64::NAN];
assert!(x.iter().copied().nanmean(true).is_nan());
assert!(x.iter().copied().nanmean(false).is_nan());

Implementors§

Source§

impl<T: Float + FromPrimitive, I: Iterator<Item = T>> FloatIterExt<T> for I