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
//! Missing operations & comprehensive number structures
//!
//! ## `Numeric` trait
//!
//! * `Numeric` requires `PowOps, TrigOps, ExpLogOps` & `std::Ops<Self>` & `std::Ops<f64>`

use std::ops::{Add, Div, Mul, Neg, Sub};

pub trait PowOps: Sized {
    type Float;
    fn powi(&self, n: i32) -> Self;
    fn powf(&self, f: Self::Float) -> Self;
    fn pow(&self, f: Self) -> Self;
    fn sqrt(&self) -> Self;
}

pub trait TrigOps: Sized + Div<Output = Self> {
    fn sin_cos(&self) -> (Self, Self);
    fn sin(&self) -> Self {
        let (s, _) = self.sin_cos();
        s
    }
    fn cos(&self) -> Self {
        let (_, c) = self.sin_cos();
        c
    }
    fn tan(&self) -> Self {
        let (s, c) = self.sin_cos();
        s / c
    }
    fn sinh(&self) -> Self;
    fn cosh(&self) -> Self;
    fn tanh(&self) -> Self;
    fn asin(&self) -> Self;
    fn acos(&self) -> Self;
    fn atan(&self) -> Self;
    fn asin_acos(&self) -> (Self, Self) {
        (self.asin(), self.acos())
    }
    fn asinh(&self) -> Self;
    fn acosh(&self) -> Self;
    fn atanh(&self) -> Self;
    fn asinh_acosh(&self) -> (Self, Self) {
        (self.asinh(), self.acosh())
    }
}

pub trait ExpLogOps: Sized {
    type Float;
    fn exp(&self) -> Self;
    fn ln(&self) -> Self;
    fn log(&self, base: Self::Float) -> Self;
    fn log2(&self) -> Self;
    fn log10(&self) -> Self;
}

pub trait Float:
    PowOps<Float = Self>
    + TrigOps
    + ExpLogOps<Float = Self>
    + Neg<Output = Self>
    + Add<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Sub<Output = Self>
    + PartialOrd
    + Copy
    + Clone
{
}

pub trait Numeric<T: Float>:
    PowOps<Float = T>
    + TrigOps
    + ExpLogOps<Float = T>
    + Neg<Output = Self>
    + Add<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Sub<Output = Self>
    + Add<T, Output = Self>
    + Mul<T, Output = Self>
    + Div<T, Output = Self>
    + Sub<T, Output = Self>
    + Clone
{
}

// =============================================================================
// Numeric Traits for f32, f64
// =============================================================================

macro_rules! impl_float {
    ($($t:ty),*) => {
        $(
            impl PowOps for $t {
                type Float = $t;
                fn powi(&self, n: i32) -> Self {
                    (*self).powi(n)
                }

                fn powf(&self, f: Self::Float) -> Self {
                    (*self).powf(f)
                }

                fn pow(&self, f: Self) -> Self {
                    (*self).powf(f)
                }

                fn sqrt(&self) -> Self {
                    (*self).sqrt()
                }
            }

            impl TrigOps for $t {
                fn sin_cos(&self) -> (Self, Self) {
                    (*self).sin_cos()
                }

                fn sin(&self) -> Self {
                    (*self).sin()
                }

                fn cos(&self) -> Self {
                    (*self).cos()
                }

                fn tan(&self) -> Self {
                    (*self).tan()
                }

                fn sinh(&self) -> Self {
                    (*self).sinh()
                }

                fn cosh(&self) -> Self {
                    (*self).cosh()
                }

                fn tanh(&self) -> Self {
                    (*self).tanh()
                }

                fn asin(&self) -> Self {
                    (*self).asin()
                }

                fn acos(&self) -> Self {
                    (*self).acos()
                }

                fn atan(&self) -> Self {
                    (*self).atan()
                }

                fn asinh(&self) -> Self {
                    (*self).asinh()
                }

                fn acosh(&self) -> Self {
                    (*self).acosh()
                }

                fn atanh(&self) -> Self {
                    (*self).atanh()
                }
            }

            impl ExpLogOps for $t {
                type Float = $t;
                fn exp(&self) -> Self {
                    (*self).exp()
                }
                fn ln(&self) -> Self {
                    (*self).ln()
                }
                fn log(&self, base: Self::Float) -> Self {
                    (*self).log(base)
                }
                fn log2(&self) -> Self {
                    (*self).log2()
                }
                fn log10(&self) -> Self {
                    (*self).log10()
                }
            }

            impl Float for $t {}
        )*
    };
}

impl_float!(f32, f64);

impl Numeric<f32> for f32 {}
impl Numeric<f64> for f64 {}

// ┌──────────────────────────────────────────────────────────┐
//  Fundamental Traits
// └──────────────────────────────────────────────────────────┘
pub trait Group: Sized + Add<Self, Output = Self> {
    fn zero() -> Self;
}

pub trait Ring: Group + Mul<Self, Output = Self> {
    fn one() -> Self;
}

impl Group for f32 {
    fn zero() -> Self {
        0.0
    }
}

impl Ring for f32 {
    fn one() -> Self {
        1.0
    }
}

impl Group for f64 {
    fn zero() -> Self {
        0.0
    }
}

impl Ring for f64 {
    fn one() -> Self {
        1.0
    }
}