kernel_density_estimation/float.rs
1//! Float constraints for generic math.
2
3use num_traits;
4
5// Custom Float implementation based on argmin crate:
6// https://docs.rs/argmin/latest/argmin/core/trait.ArgminFloat.html
7
8/// An alias for float types (f32, f64) which
9/// combines many commonly required traits.
10/// It is automatically implemented for all
11/// types which fulfill the trait bounds.
12pub trait KDEFloat:
13 'static
14 + num_traits::Float
15 + num_traits::FloatConst
16 + num_traits::FromPrimitive
17 + num_traits::ToPrimitive
18 + std::fmt::Debug
19 + std::fmt::Display
20 + std::iter::Sum
21 + std::ops::AddAssign
22{
23}
24
25/// `KDEFloat` is automatically implemented for all
26/// types which fulfill the trait bounds.
27impl<T> KDEFloat for T where
28 T: 'static
29 + num_traits::Float
30 + num_traits::FloatConst
31 + num_traits::FromPrimitive
32 + num_traits::ToPrimitive
33 + std::fmt::Debug
34 + std::fmt::Display
35 + std::iter::Sum
36 + std::ops::AddAssign
37{
38}
39
40// Macro to simplify primitive to Float conversion.
41// Note that the name of the generic parameter MUST be F.
42macro_rules! float {
43 ($x:expr) => {
44 F::from($x).unwrap()
45 };
46}
47pub(crate) use float;
48
49// Macro to simplify Float to f64 conversion.
50// Note that the argument MUST implement Float.
51macro_rules! primitive {
52 ($x:expr) => {
53 $x.to_f64().unwrap()
54 };
55}
56pub(crate) use primitive;