ndhistogram/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/// Creates a [Histogram](crate::Histogram).
///
/// This macro is the recommended method for constructing new Histograms.
/// The arguments are a command separated list of [Axis](crate::axis::Axis).
/// Optionally, the type of the [Histogram](crate::Histogram) bin values may specified after a semi-colon after
/// the list of [Axis](crate::axis::Axis). If the bin value type is not specified, the default is f64.
///
/// # Example
///
/// ## Creating a 1D Histogram
/// ```rust
/// use ndhistogram::axis::Uniform;
/// use std::fmt::Display;
/// use ndhistogram::{ndhistogram, Histogram};
///
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let hist1D = ndhistogram!(Uniform::new(10, -5.0, 5.0)?);
/// println!("{}", hist1D);
/// # Ok(()) }
/// ```
///
/// ## Creating a 2D Histogram
/// ```rust
/// use ndhistogram::axis::{Uniform, Axis};
/// use ndhistogram::{ndhistogram, Histogram};
///
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let hist2D = ndhistogram!(Uniform::new(10, -5.0, 5.0)?, Uniform::new(10, -5.0, 5.0)?);
/// // each axis has 10+2 bins due to under/overflow
/// assert_eq!(hist2D.axes().num_bins(), 12*12);
/// # Ok(()) }
/// ```
///
/// ## Creating a Histogram with a specific bin value type
///
/// ```rust
/// use ndhistogram::axis::Uniform;
/// use ndhistogram::{ndhistogram, Histogram};
/// use ndhistogram::value::Mean;
///
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let mut hist = ndhistogram!(Uniform::new(10, -5.0, 5.0)?; Mean);
/// hist.fill_with(&0.0, 1.0);
/// hist.fill_with(&0.0, 3.0);
/// assert_eq!(hist.value(&0.0).unwrap().get(), 2.0); // mean of [1.0, 3.0] is 2.0
/// # Ok(()) }
/// ```
#[macro_export]
macro_rules! ndhistogram {
($( $x:expr ),+ $(,)*; $type:ty $(;)*) => {
{
let axes = (
$(
$x,
)*
);
let axes: $crate::AxesTuple<_> = axes.into();
$crate::VecHistogram::<_, $type>::new(axes)
}
};
($( $x:expr ),+ $(,)*) => {
{
let axes = (
$(
$x,
)*
);
let axes: $crate::AxesTuple<_> = axes.into();
$crate::VecHistogram::<_, f64>::new(axes)
}
};
}
/// Creates a sparse [Histogram](crate::Histogram).
///
/// As [ndhistogram], but creates a sparse histogram of type [HashHistogram](crate::HashHistogram).
/// The arguments are a command separated list of [Axis](crate::axis::Axis).
/// Optionally, the type of the [Histogram](crate::Histogram) bin values may specified after a semi-colon after
/// the list of [Axis](crate::axis::Axis). If the bin value type is not specified, the default is f64.
///
/// # Example
///
/// ## Creating a sparse 1D Histogram
/// ```rust
/// use ndhistogram::axis::Uniform;
/// use std::fmt::Display;
/// use ndhistogram::{sparsehistogram, Histogram};
///
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let hist1D = sparsehistogram!(Uniform::new(10, -5.0, 5.0)?);
/// println!("{}", hist1D);
/// # Ok(()) }
/// ```
///
/// ## Creating a sparse 2D Histogram
/// ```rust
/// use ndhistogram::axis::{Uniform, Axis};
/// use ndhistogram::{sparsehistogram, Histogram};
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let hist2D = sparsehistogram!(Uniform::new(10, -5.0, 5.0)?, Uniform::new(10, -5.0, 5.0)?);
/// // each axis has 10+2 bins due to under/overflow
/// assert_eq!(hist2D.axes().num_bins(), 12*12);
/// # Ok(()) }
/// ```
///
/// ## Creating a Histogram with a specific bin value type
///
/// ```rust
/// use ndhistogram::axis::Uniform;
/// use ndhistogram::{sparsehistogram, Histogram};
/// use ndhistogram::value::Mean;
///
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let mut hist = sparsehistogram!(Uniform::new(10, -5.0, 5.0)?; Mean);
/// hist.fill_with(&0.0, 1.0);
/// hist.fill_with(&0.0, 3.0);
/// assert_eq!(hist.value(&0.0).unwrap().get(), 2.0); // mean of [1.0, 3.0] is 2.0
/// # Ok(()) }
/// ```
#[macro_export]
macro_rules! sparsehistogram {
($( $x:expr ),+ $(,)*; $type:ty $(;)*) => {
{
let axes = (
$(
$x,
)*
);
let axes: $crate::AxesTuple<_> = axes.into();
$crate::HashHistogram::<_, $type>::new(axes)
}
};
($( $x:expr ),+ $(,)*) => {
{
let axes = (
$(
$x,
)*
);
let axes: $crate::AxesTuple<_> = axes.into();
$crate::HashHistogram::<_, f64>::new(axes)
}
};
}