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
302
303
304
305
306
307
308
309
310
311
312
313
314
/// A value which has some sensible notion of arithmetic,
/// a [`Debug`][1] implementation, and is [`Copy`]. This
/// trait is sealed, and can not be implemented outside of
/// `const_linear`.
///
/// # Examples
///
/// ## One and Zero values
/// ```
/// use const_linear::traits::Scalar;
///
/// let x = i32::ZERO;
/// let y = i32::ONE;
///
/// assert_eq!(x - y, -1);
/// ```
///
/// ## Converting to floating point
///
/// ```
/// use const_linear::traits::Scalar;
///
/// assert_eq!(1.to_f64(), 1 as f64);
/// assert_eq!(1.to_f32(), 1 as f32);
/// ```
///
/// [1]: std::fmt::Debug
pub trait Scalar: sealed::Ops<Self> {
    /// The "zero" value for this Scalar type. Acts as the
    /// additive identity.
    const ZERO: Self;

    /// The "one" value for this Scalar type. Acts as the
    /// multiplicative identity.
    const ONE: Self;

    /// Converts `T` to an [`f64`]. Equivalent to `T as f64`.
    fn to_f64(self) -> f64;

    /// Converts `T` to an [`f32`]. Equivalent to `T as f32`.
    fn to_f32(self) -> f32;
}

/// A value which is a scalar value and is also
/// capable of representing real numbers. This trait
/// is sealed, and can not be implemented outside of
/// `const_linear`.
pub trait Real: Scalar + sealed::Ops<Self> {
    /// Euler's number, [`e`](https://en.wikipedia.org/wiki/E_(mathematical_constant))
    fn e() -> Self;

    /// The square root of `2`.
    fn sqrt_2() -> Self;

    /// Archimedes' constant, [`π`](https://en.wikipedia.org/wiki/Pi)
    fn pi() -> Self;

    /// `1 / π`
    fn one_over_pi() -> Self;

    /// `2 / π`
    fn two_over_pi() -> Self;

    /// `2 / √π`
    fn two_over_sqrt_pi() -> Self;

    /// `1 / √2`
    fn one_over_sqrt_two() -> Self;

    /// `π / 2`
    fn pi_over_2() -> Self;

    /// `π / 3`
    fn pi_over_3() -> Self;

    /// `π / 4`
    fn pi_over_4() -> Self;

    /// `π / 6`
    fn pi_over_6() -> Self;

    /// `π / 8`
    fn pi_over_8() -> Self;

    /// `ln(2)`
    fn ln_2() -> Self;

    /// `ln(10)`
    fn ln_10() -> Self;

    /// `log2(e)`
    fn log2_e() -> Self;

    /// `log(e)`
    fn log10_e() -> Self;

    /// Tests for approximate equality between two real numbers. This is a
    /// workaround for the limited precision of floating point numbers.
    ///
    /// # Examples
    ///
    /// ```
    /// use const_linear::traits::Real;
    ///
    /// // 0.1 + 0.2 != 0.3 (In floating point arithmetic).
    /// assert_ne!(0.1 + 0.2, 0.3);
    ///
    /// // ... But it is *approximately* equal.
    /// assert!((0.1 + 0.2).approx_eq(0.3, std::f64::EPSILON, 1.0E-15))
    /// ```
    fn approx_eq(self, other: Self, epsilon: Self, max_relative: Self) -> bool;
}

macro_rules! scalar_impls {
    ($($t:ty, $zero:literal, $one:literal),+) => {
        $(
            impl $crate::traits::Scalar for $t {
                const ZERO: Self = $zero;
                const ONE: Self = $one;

                fn to_f64(self) -> f64 {
                    self as f64
                }

                fn to_f32(self) -> f32 {
                    self as f32
                }
            }

            // Unfortunately, it's not possible to do a blanket impl, so this is a workaround
            // for all types which impl Scalar. Inverse of impl Mul<T> for Matrix<T>
            impl<const M: usize, const N: usize> ::std::ops::Mul<$crate::Matrix<$t, { M }, { N }>> for $t {
                type Output = $crate::Matrix<$t, { M }, { N }>;

                fn mul(self, mut rhs: Self::Output) -> Self::Output {
                    for elem in rhs.column_iter_mut() {
                        *elem *= self;
                    }

                    rhs
                }
            }
        )*
    }
}

macro_rules! real_impls {
    ($($t:ident),+) => {
        $(
            impl Real for $t {
                fn e() -> Self {
                    ::std::$t::consts::E
                }

                fn sqrt_2() -> Self {
                    ::std::$t::consts::SQRT_2
                }

                fn pi() -> Self {
                    ::std::$t::consts::PI
                }

                fn one_over_pi() -> Self {
                    ::std::$t::consts::FRAC_1_PI
                }

                fn two_over_pi() -> Self {
                    ::std::$t::consts::FRAC_2_PI
                }

                fn two_over_sqrt_pi() -> Self {
                    ::std::$t::consts::FRAC_2_SQRT_PI
                }

                fn one_over_sqrt_two() -> Self {
                    ::std::$t::consts::FRAC_1_SQRT_2
                }

                fn pi_over_2() -> Self {
                    ::std::$t::consts::FRAC_PI_2
                }

                fn pi_over_3() -> Self {
                    ::std::$t::consts::FRAC_PI_3
                }

                fn pi_over_4() -> Self {
                    ::std::$t::consts::FRAC_PI_4
                }

                fn pi_over_6() -> Self {
                    ::std::$t::consts::FRAC_PI_6
                }

                fn pi_over_8() -> Self {
                    ::std::$t::consts::FRAC_PI_8
                }

                fn ln_2() -> Self {
                    ::std::$t::consts::LN_2
                }

                fn ln_10() -> Self {
                    ::std::$t::consts::LN_10
                }

                fn log2_e() -> Self {
                    ::std::$t::consts::LOG2_E
                }

                fn log10_e() -> Self {
                    ::std::$t::consts::LOG10_E
                }

                // Adapted from the relative_eq method in the approx crate.
                // https://docs.rs/approx/0.3.2/src/approx/relative_eq.rs.html#54-83
                fn approx_eq(self, other: Self, epsilon: Self, max_relative: Self) -> bool {
                    // The special case in which both are *exactly* equal.
                    if self == other {
                        return true;
                    } else if self.is_infinite() || other.is_infinite()
                        || self.is_nan() || other.is_nan()
                    {
                        return false;
                    }

                    let diff = (self - other).abs();

                    if diff <= epsilon {
                        true
                    } else {
                        let abs_self = self.abs();
                        let abs_other = other.abs();

                        let largest = Self::max(abs_self, abs_other);

                        diff < largest * max_relative
                    }
                }
            }
        )*
    }
}

scalar_impls! {
    i8, 0, 1,
    u8, 0, 1,
    i16, 0, 1,
    u16, 0, 1,
    i32, 0, 1,
    u32, 0, 1,
    i64, 0, 1,
    u64, 0, 1,
    i128, 0, 1,
    u128, 0, 1,
    isize, 0, 1,
    usize, 0, 1,
    f32, 0.0, 1.0,
    f64, 0.0, 1.0
}

real_impls! {
    f32,
    f64
}

mod sealed {
    use std::cmp::PartialEq;
    use std::fmt::Debug;
    use std::ops::*;

    pub trait Ops<T>:
        Copy
        + Clone
        + Debug
        + Add<Output = T>
        + AddAssign<T>
        + Div<Output = T>
        + DivAssign<T>
        + Mul<Output = T>
        + MulAssign<T>
        + Rem<Output = T>
        + RemAssign<T>
        + Sub<Output = T>
        + SubAssign<T>
        + PartialEq<T>
    {
    }

    macro_rules! ops_impls {
        ($($t:ty),+) => {
            $(
                impl self::Ops<$t> for $t {}
            )*
        }
    }

    ops_impls! {
        i8,
        u8,
        i16,
        u16,
        i32,
        u32,
        i64,
        u64,
        i128,
        u128,
        isize,
        usize,
        f32,
        f64
    }
}