ndhistogram/
macros.rs

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