hexga_math 0.0.11-beta.53

Math related crate that define number and casting, and support array programming...
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use super::*;

// Most function are suffixed with `_elementwise` like min, max, clamp, because there are already defiend in [std::cmp::Ord] and I don't want to fully qualify them...

/// Max function in a component way
pub trait Max
{
    /// Max function in a component way
    ///
    /// Can cause a panic for grid if the size mismatch
    fn max_elementwise(self, other: Self) -> Self;
}
/// Max function in a component way
///
/// Can cause a panic for grid if the size mismatch
pub fn max<S>(a: S, b: S) -> S
where
    S: Max,
{
    a.max_elementwise(b)
}

map_on_integer!(
    ($type_name: tt) =>
    {
        impl Max for $type_name
        {
            fn max_elementwise(self, other: Self) -> Self { Ord::max(self, other) }
        }
    }
);
map_on_float!(
    ($type_name: tt) =>
    {
        impl Max for $type_name
        {
            fn max_elementwise(self, other: Self) -> Self { Self::max(self, other) }
        }
    }
);
impl Max for bool
{
    fn max_elementwise(self, other: Self) -> Self { self | other }
}

impl<S> Max for S
where
    S: MapInternWith,
    S::Item: Max,
{
    fn max_elementwise(self, other: Self) -> Self
    {
        self.map_with_intern(other, Max::max_elementwise)
    }
}

/// Min function in a component way
pub trait Min
{
    /// Min function in a component way
    ///
    /// Can cause a panic for grid if the size mismatch
    fn min_elementwise(self, other: Self) -> Self;
}
/// Min function in a component way
///
/// Can cause a panic for grid if the size mismatch
pub fn min<S>(a: S, b: S) -> S
where
    S: Min,
{
    a.min_elementwise(b)
}

map_on_integer!(
    ($type_name: tt) =>
    {
        impl Min for $type_name
        {
            fn min_elementwise(self, other: Self) -> Self { Ord::min(self, other) }
        }
    }
);
map_on_float!(
    ($type_name: tt) =>
    {
        impl Min for $type_name
        {
            fn min_elementwise(self, other: Self) -> Self { Self::min(self, other) }
        }
    }
);
impl Min for bool
{
    fn min_elementwise(self, other: Self) -> Self { self & other }
}

impl<S> Min for S
where
    S: MapInternWith,
    S::Item: Min,
{
    fn min_elementwise(self, other: Self) -> Self
    {
        self.map_with_intern(other, Min::min_elementwise)
    }
}

/// Clamp function in a component way
pub trait Clamp
{
    /// Clamp function in a component way
    ///
    /// Clamps self between min_val and max_val (inclusive)
    ///
    /// Can cause a panic for grid if the size mismatch
    fn clamp_elementwise(self, min_val: Self, max_val: Self) -> Self;
}
/// Clamp function in a component way
///
/// Clamps self between min_val and max_val (inclusive)
///
/// Can cause a panic for grid if the size mismatch
pub fn clamp<S>(value: S, min_val: S, max_val: S) -> S
where
    S: Clamp,
{
    value.clamp_elementwise(min_val, max_val)
}

impl<T> Clamp for T
where
    T: Min + Max,
{
    fn clamp_elementwise(self, min_val: Self, max_val: Self) -> Self
    {
        self.max_elementwise(min_val).min_elementwise(max_val)
    }
}

/// Mix function in a component way
pub trait Mix<C = float>: Sized
where
    C: Float,
{
    /// Mix function in a component way
    ///
    /// The coef is clamped between `0.0..1.`
    ///
    /// 0. => self,
    /// 1. => dest.
    ///
    /// Can cause a panic for grid if the size mismatch
    fn mix(self, dest: Self, coef: C) -> Self
    {
        self.mix_unchecked(dest, coef.clamp_partial(zero(), one()))
    }

    /// Mix function in a component way
    ///
    /// The coef is **not** clamped between `0.0..1.`
    ///
    /// 0. => self,
    /// 1. => dest.
    ///
    /// Can cause a panic for grid if the size mismatch
    fn mix_unchecked(self, dest: Self, coef: C) -> Self;
}
/// Mix function in a component way
///
/// The coef is clamped between `0.0..1.`
///
/// 0. => self,
/// 1. => dest.
///
/// Can cause a panic for grid if the size mismatch
pub fn mix<S, F>(src: S, dest: S, coef: F) -> S
where
    S: Mix<F>,
    F: Float,
{
    src.mix(dest, coef)
}

map_on_number_and_bool!(
    ($type_name: tt) =>
    {
        impl<F> Mix<F> for $type_name where F: Float
        {
            fn mix_unchecked(self, dest: Self, coef: F) -> Self
            {
                let src  = F::cast_from(self);
                let dest = F::cast_from(dest);
                let r = src * (F::ONE - coef) + dest * coef;
                r.cast_into()
            }
        }
    }
);
impl<S, F> Mix<F> for S
where
    S: MapInternWith,
    S::Item: Mix<F>,
    F: Float,
{
    fn mix_unchecked(self, dest: Self, coef: F) -> Self
    {
        self.map_with_intern(dest, |a, b| a.mix_unchecked(b, coef))
    }
}

/// Abs function in a component way
pub trait Abs
{
    type Output;
    /// Absolute value in a component way.
    ///
    /// ## Overflow behavior
    /// The absolute value of `iX::MIN` cannot be represented as an `iX`, and attempting to calculate it will cause an overflow.
    /// This means that code in debug mode will trigger a panic on this case and optimized code will return `iX::MIN` without a panic.
    ///
    /// Can cause a panic for grid if the size mismatch
    fn abs(self) -> Self::Output;
}
map_on_integer_unsigned!(
    ($primitive_name: ty) =>
    {
        impl Abs for $primitive_name
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { self }
        }
        impl Abs for Saturating<$primitive_name>
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { self }
        }
        impl Abs for Wrapping<$primitive_name>
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { self }
        }
    }
);
map_on_integer_signed!(
    ($primitive_name: ty) =>
    {
        impl Abs for $primitive_name
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { self.abs() }
        }
        impl Abs for Saturating<$primitive_name>
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { Self::abs(self) }
        }
        impl Abs for Wrapping<$primitive_name>
        {
            type Output = Self;
            // TODO: replace it by Wrapping<$primitive_name>::abs once stabilized
            #[inline(always)]
            fn abs(self) -> Self { if self.is_max() { Self(<$primitive_name>::MIN) } else { Self(<$primitive_name>::abs(self.0)) } }
        }
    }
);
map_on_float!(
    ($primitive_name: ty) =>
    {
        impl Abs for $primitive_name
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { self.abs() }
        }
        impl Abs for Saturating<$primitive_name>
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { Self::abs(self) }
        }
        impl Abs for Wrapping<$primitive_name>
        {
            type Output = Self;
            #[inline(always)]
            fn abs(self) -> Self { Self::abs(self) }
        }
    }
);
impl Abs for bool
{
    type Output = Self;
    #[inline(always)]
    fn abs(self) -> Self { self }
}
impl Abs for Saturating<bool>
{
    type Output = Self;
    #[inline(always)]
    fn abs(self) -> Self { self }
}
impl Abs for Wrapping<bool>
{
    type Output = Self;
    #[inline(always)]
    fn abs(self) -> Self { self }
}
impl<S> Abs for S
where
    S: Map,
    S::Item: Abs,
{
    type Output = S::WithType<<S::Item as Abs>::Output>;
    fn abs(self) -> Self::Output { self.map(Abs::abs) }
}
/// Absolute value in a component way.
///
/// ## Overflow behavior
/// The absolute value of `iX::MIN` cannot be represented as an `iX`, and attempting to calculate it will cause an overflow.
/// This means that code in debug mode will trigger a panic on this case and optimized code will return `iX::MIN` without a panic.
///
/// Can cause a panic for grid if the size mismatch
pub fn abs<S>(value: S) -> S::Output
where
    S: Abs,
{
    value.abs()
}

#[macro_export]
macro_rules! impl_number_basic_trait {
    () => {
        // NOTE: Most of these function clash with at least 2 traits (ex: max() is defined in the [`std::cmp::Ord`] (non component wise) and also in [`crate::number::Max`] (component wise)
        // Redefining them here and redirecting them to the correct trait avoid the need to fully qualify the method with the trait in order to call it

        /// Max function in a component way
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn max(self, other: Self) -> Self
        where
            Self: $crate::number::Max,
        {
            $crate::number::Max::max_elementwise(self, other)
        }

        /// Min function in a component way
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn min(self, other: Self) -> Self
        where
            Self: $crate::number::Min,
        {
            $crate::number::Min::min_elementwise(self, other)
        }

        /// Absolute value in a component way.
        ///
        /// ## Overflow behavior
        /// The absolute value of `iX::MIN` cannot be represented as an `iX`, and attempting to calculate it will cause an overflow.
        /// This means that code in debug mode will trigger a panic on this case and optimized code will return `iX::MIN` without a panic.
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn abs(self) -> <Self as $crate::number::Abs>::Output
        where
            Self: $crate::number::Abs,
        {
            $crate::number::Abs::abs(self)
        }

        /// Mix function in a component way
        ///
        /// The coef is clamped between `0.0..1.`
        ///
        /// 0. => self,
        /// 1. => dest.
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn mix<C>(self, other: Self, coef: C) -> Self
        where
            Self: $crate::number::Mix<C>,
            C: Float,
        {
            $crate::number::Mix::mix(self, other, coef)
        }

        /// Mix function in a component way
        ///
        /// The coef is **not** clamped between `0.0..1.`
        ///
        /// 0. => self,
        /// 1. => dest.
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn mix_unchecked<C>(self, other: Self, coef: C) -> Self
        where
            Self: $crate::number::Mix<C>,
            C: Float,
        {
            $crate::number::Mix::mix_unchecked(self, other, coef)
        }

        /// Clamp function in a component way
        ///
        /// Clamps self between min_val and max_val (inclusive)
        ///
        /// Can cause a panic for grid if the size mismatch
        pub fn clamp(self, min_val: Self, max_val: Self) -> Self
        where
            Self: $crate::number::Clamp,
        {
            $crate::number::Clamp::clamp_elementwise(self, min_val, max_val)
        }
    };
}

#[cfg(test)]
mod tests
{
    use super::*;

    #[test]
    fn max_test()
    {
        assert_eq!(max(1, 2), 2);
        assert_eq!(max(2, 1), 2);
        assert_eq!(max([1, 10], [4, 5]), [4, 10]);
        assert_eq!(max([10, 1], [5, 4]), [10, 4]);
    }

    #[test]
    fn min_test()
    {
        assert_eq!(min(1, 2), 1);
        assert_eq!(min(2, 1), 1);
        assert_eq!(min([1, 10], [4, 5]), [1, 5]);
        assert_eq!(min([10, 1], [5, 4]), [5, 1]);
    }

    #[test]
    fn mix_test()
    {
        assert_eq!(mix(0., 1., 0.), 0.);
        assert_eq!(mix(0., 1., 0.5), 0.5);
        assert_eq!(mix(0., 1., 1.), 1.);

        assert_eq!(mix(0., 100., 0.), 0.);
        assert_eq!(mix(0., 100., 1.), 100.);

        assert_eq!(mix(1., 0., 0.), 1.);
        assert_eq!(mix(1., 0., 0.5), 0.5);
        assert_eq!(mix(1., 0., 1.), 0.);

        assert_eq!(mix([1., 2.], [4., 8.], 0.5), [2.5, 5.]);
    }

    #[test]
    fn abs_test()
    {
        assert_eq!(abs(-2), 2);
        assert_eq!(abs(u8::MAX), u8::MAX);
        assert_eq!(abs(i8::MAX), i8::MAX);
        assert_eq!(abs(i8::MIN + 1), i8::MAX);
        assert_eq!(abs(i8::MIN + 1), i8::MAX);
    }

    #[test]
    fn clamp_test()
    {
        assert_eq!(clamp(5, 0, 10), 5);
        assert_eq!(clamp(-5, 0, 10), 0);
        assert_eq!(clamp(15, 0, 10), 10);
        assert_eq!(clamp(0, 0, 10), 0);
        assert_eq!(clamp(10, 0, 10), 10);

        assert_eq!(clamp(2.5, 1.0, 3.0), 2.5);
        assert_eq!(clamp(0.5, 1.0, 3.0), 1.0);
        assert_eq!(clamp(3.5, 1.0, 3.0), 3.0);

        assert_eq!(clamp([1, 5, 10], [2, 3, 8], [4, 7, 12]), [2, 5, 10]);
        assert_eq!(clamp([0, 2, 15], [1, 1, 10], [3, 5, 20]), [1, 2, 15]);

        assert_eq!(clamp(true, false, true), true);
        assert_eq!(clamp(true, false, false), false);
        assert_eq!(clamp(false, true, true), true);
        assert_eq!(clamp(false, false, false), false);
    }
}