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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::cmp::PartialOrd;
use std::ops::{Add, AddAssign, DivAssign, MulAssign, Sub, SubAssign};

use num_traits::{MulAdd, Num};

use super::cast::Cast;
use super::isnone::IsNone;

/// Kahan summation, see https://en.wikipedia.org/wiki/Kahan_summation_algorithm
#[inline]
fn kh_sum<T>(sum: T, v: T, c: &mut T) -> T
where
    T: Add<Output = T> + Sub<Output = T> + Copy,
{
    let y = v - *c;
    let t = sum + y;
    *c = (t - sum) - y;
    t
}
/// A trait representing numeric types with various operations and conversions.
///
/// This trait combines several other traits and provides additional functionality
/// for numeric types. It includes operations for arithmetic, comparison, conversion,
/// and special numeric functions.
///
/// # Type Constraints
///
/// The type implementing this trait must satisfy the following constraints:
/// - `Copy`: The type can be copied bit-for-bit.
/// - `Send`: The type can be safely transferred across thread boundaries.
/// - `Sync`: The type can be safely shared between threads.
/// - `IsNone`: The type has a concept of a "none" value.
/// - `Sized`: The type has a known size at compile-time.
/// - `Default`: The type has a default value.
/// - `Num`: The type supports basic numeric operations.
/// - `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`: The type supports compound assignment operations.
/// - `PartialOrd`: The type can be partially ordered.
/// - `MulAdd`: The type supports fused multiply-add operations.
/// - `Cast<f64>`, `Cast<f32>`, `Cast<usize>`, `Cast<i32>`, `Cast<i64>`: The type can be cast to these numeric types.
/// - `'static`: The type has a static lifetime.
pub trait Number:
    Copy
    // + Clone
    + Send
    + Sync
    + IsNone
    + Sized
    + Default
    + Num
    + AddAssign
    + SubAssign
    + MulAssign
    + DivAssign
    + PartialOrd
    + MulAdd
    + Cast<f64>
    + Cast<f32>
    + Cast<usize>
    + Cast<i32>
    + Cast<i64>
    + 'static
{
    // type Dtype;
    /// Returns the minimum value of the data type.
    fn min_() -> Self;

    /// Returns the maximum value of the data type.
    fn max_() -> Self;

    /// Computes the absolute value of the number.
    fn abs(self) -> Self;

    /// Computes the ceiling of the number.
    ///
    /// For integer types, this is typically the identity function.
    #[inline(always)]
    fn ceil(self) -> Self {
        self
    }

    /// Computes the floor of the number.
    ///
    /// For integer types, this is typically the identity function.
    #[inline(always)]
    fn floor(self) -> Self {
        self
    }

    /// Returns the minimum of self and other.
    #[inline]
    fn min_with(self, other: Self) -> Self {
        if other < self {
            other
        } else {
            self
        }
    }

    /// Returns the maximum of self and other.
    #[inline]
    fn max_with(self, other: Self) -> Self {
        if other > self {
            other
        } else {
            self
        }
    }

    /// Casts the number to f32.
    #[inline(always)]
    fn f32(self) -> f32 {
        Cast::<f32>::cast(self)
    }

    /// Casts the number to f64.
    #[inline(always)]
    fn f64(self) -> f64 {
        Cast::<f64>::cast(self)
    }

    /// Casts the number to i32.
    #[inline(always)]
    fn i32(self) -> i32 {
        Cast::<i32>::cast(self)
    }

    /// Casts the number to i64.
    #[inline(always)]
    fn i64(self) -> i64 {
        Cast::<i64>::cast(self)
    }

    /// Casts the number to usize.
    #[inline(always)]
    fn usize(self) -> usize {
        Cast::<usize>::cast(self)
    }

    /// Creates a value of type Self using a value of type U using `Cast`.
    #[inline(always)]
    fn fromas<U>(v: U) -> Self
    where
        U: Number + Cast<Self>,
        Self: 'static,
    {
        v.to::<Self>()
    }

    /// Casts self to another type T using `Cast`.
    #[inline(always)]
    fn to<T: Number>(self) -> T
    where
        Self: Cast<T>,
    {
        Cast::<T>::cast(self)
    }

    /// Performs Kahan summation.
    ///
    /// This method implements the Kahan summation algorithm, which helps reduce
    /// numerical error in the sum of a sequence of floating point numbers.
    #[inline(always)]
    #[must_use]
    fn kh_sum(self, v: Self, c: &mut Self) -> Self {
        kh_sum(self, v, c)
    }

    /// Conditionally adds `other` to `self` and increments `n`.
    ///
    /// If `other` is not none, it adds `other` to `self` and increments `n`.
    /// Otherwise, it returns `self` unchanged.
    #[inline]
    fn n_add(self, other: Self, n: &mut usize) -> Self {
        // note: only check if other is NaN
        // assume that self is not NaN
        if other.not_none() {
            *n += 1;
            self + other
        } else {
            self
        }
    }

    /// Conditionally multiplies `self` by `other` and increments `n`.
    ///
    /// If `other` is not none, it multiplies `self` by `other` and increments `n`.
    /// Otherwise, it returns `self` unchanged.
    #[inline]
    fn n_prod(self, other: Self, n: &mut usize) -> Self {
        // note: only check if other is NaN
        // assume that self is not NaN
        if other.not_none() {
            *n += 1;
            self * other
        } else {
            self
        }
    }
}

macro_rules! impl_number {
    (@ base_impl $dtype:ty, $datatype:ident) => {

        #[inline(always)]
        fn min_() -> $dtype {
            <$dtype>::MIN
        }

        #[inline(always)]
        fn max_() -> $dtype {
            <$dtype>::MAX
        }

    };
    // special impl for float
    (float $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);

            #[inline]
            fn ceil(self) -> Self {
                self.ceil()
            }

            #[inline]
            fn floor(self) -> Self {
                self.floor()
            }

            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }

        })*
    };
    // special impl for other type
    (signed $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);
            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }
        })*
    };
    // special impl for other type
    (unsigned $($dtype:ty, $datatype:ident); *) => {
        $(impl Number for $dtype {
            impl_number!(@ base_impl $dtype, $datatype);
            #[inline]
            fn abs(self) -> Self {
                self
            }
        })*
    };
}

impl_number!(
    float
    f32, F32;
    f64, F64
);

impl_number!(
    signed
    i32, I32;
    i64, I64
);

impl_number!(
    unsigned
    u64, U64;
    usize, Usize
);

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_ceil() {
        fn _ceil<T: Number>(v: T) -> T {
            v.ceil()
        }
        assert_eq!(_ceil(1.23_f64), 2.);
        assert_eq!(_ceil(-1.23_f32), -1.);
        assert_eq!(_ceil(0_usize), 0);
        assert_eq!(_ceil(-3i32), -3);
    }

    #[test]
    fn test_floor() {
        fn _floor<T: Number>(v: T) -> T {
            v.floor()
        }
        assert_eq!(_floor(1.23_f64), 1.);
        assert_eq!(_floor(-1.23_f32), -2.);
        assert_eq!(_floor(0_usize), 0);
        assert_eq!(_floor(-3i32), -3);
    }
}