Skip to main content

ndarray_glm/
num.rs

1//! numerical trait constraints
2use std::cmp;
3
4use ndarray::ScalarOperand;
5use ndarray_linalg::Lapack;
6
7pub trait Float: Sized + num_traits::Float + Lapack + ScalarOperand {
8    // Return 1/2 = 0.5
9    fn half() -> Self;
10
11    // Return 2.0
12    fn two() -> Self;
13
14    /// A more conventional sign function, because the built-in signum treats signed zeros as
15    /// positive and negative: https://github.com/rust-lang/rust/issues/57543
16    fn sign(self) -> Self {
17        if self == Self::zero() {
18            Self::zero()
19        } else {
20            self.signum()
21        }
22    }
23
24    /// total_cmp is not implemented in num_traits, so implement it here.
25    fn total_cmp(&self, other: &Self) -> cmp::Ordering;
26}
27
28impl Float for f32 {
29    fn half() -> Self {
30        0.5
31    }
32
33    fn two() -> Self {
34        2.0
35    }
36
37    fn total_cmp(&self, other: &Self) -> cmp::Ordering {
38        self.total_cmp(other)
39    }
40}
41impl Float for f64 {
42    fn half() -> Self {
43        0.5
44    }
45
46    fn two() -> Self {
47        2.0
48    }
49
50    fn total_cmp(&self, other: &Self) -> cmp::Ordering {
51        self.total_cmp(other)
52    }
53}