number_general/
instance.rs

1use std::cmp::Ordering;
2use std::fmt;
3use std::hash::{Hash, Hasher};
4use std::iter::{Product, Sum};
5use std::ops::*;
6use std::str::FromStr;
7
8use collate::Collate;
9use get_size::GetSize;
10use get_size_derive::*;
11use num::traits::Pow;
12use safecast::*;
13
14use super::class::*;
15use super::{Error, Number, _Complex};
16
17const ERR_COMPLEX_POWER: &str = "complex exponent is not yet supported";
18
19macro_rules! fmt_debug {
20    ($t:ty) => {
21        impl fmt::Debug for $t {
22            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23                write!(f, "{} ({})", self, self.class())
24            }
25        }
26    };
27}
28
29/// A boolean value.
30#[derive(Clone, Copy, Hash, Ord, PartialEq, PartialOrd)]
31pub struct Boolean(bool);
32
33impl GetSize for Boolean {
34    fn get_size(&self) -> usize {
35        1
36    }
37}
38
39impl Boolean {
40    fn as_radians(self) -> f32 {
41        if self.0 {
42            2. * std::f32::consts::PI
43        } else {
44            0.
45        }
46    }
47}
48
49impl NumberInstance for Boolean {
50    type Abs = Self;
51    type Exp = Float;
52    type Log = Float;
53    type Round = Self;
54    type Class = BooleanType;
55
56    fn class(&self) -> BooleanType {
57        BooleanType
58    }
59
60    fn into_type(self, _dtype: BooleanType) -> Boolean {
61        self
62    }
63
64    fn abs(self) -> Self {
65        self
66    }
67
68    fn exp(self) -> Self::Exp {
69        Float::cast_from(self).exp()
70    }
71
72    fn ln(self) -> Self::Log {
73        Float::cast_from(self).ln()
74    }
75
76    fn log<N: NumberInstance>(self, base: N) -> Self::Log
77    where
78        Float: From<N>,
79    {
80        Float::cast_from(self).log(base)
81    }
82
83    fn pow(self, exp: Number) -> Self {
84        if exp.cast_into() {
85            self
86        } else {
87            self.class().one()
88        }
89    }
90
91    fn and(self, other: Self) -> Self {
92        Self(self.0 && other.0)
93    }
94
95    fn not(self) -> Self {
96        Self(!self.0)
97    }
98
99    fn or(self, other: Self) -> Self {
100        Self(self.0 || other.0)
101    }
102
103    fn round(self) -> Self::Round {
104        self
105    }
106
107    fn xor(self, other: Self) -> Self {
108        Self(self.0 ^ other.0)
109    }
110}
111
112impl RealInstance for Boolean {
113    const ONE: Self = Boolean(true);
114    const ZERO: Self = Boolean(false);
115}
116
117impl Default for Boolean {
118    fn default() -> Boolean {
119        Self(false)
120    }
121}
122
123impl From<bool> for Boolean {
124    fn from(b: bool) -> Boolean {
125        Self(b)
126    }
127}
128
129impl FromStr for Boolean {
130    type Err = Error;
131
132    fn from_str(s: &str) -> Result<Self, Self::Err> {
133        bool::from_str(s).map(Self::from).map_err(Error::new)
134    }
135}
136
137impl From<Boolean> for bool {
138    fn from(b: Boolean) -> bool {
139        b.0
140    }
141}
142
143impl From<&Boolean> for bool {
144    fn from(b: &Boolean) -> bool {
145        b.0
146    }
147}
148
149impl Eq for Boolean {}
150
151impl Add for Boolean {
152    type Output = Self;
153
154    fn add(self, other: Self) -> Self::Output {
155        self.or(other)
156    }
157}
158
159impl AddAssign for Boolean {
160    fn add_assign(&mut self, other: Self) {
161        self.0 |= other.0
162    }
163}
164
165impl Rem for Boolean {
166    type Output = Self;
167
168    fn rem(self, other: Self) -> Self::Output {
169        self - other
170    }
171}
172
173impl RemAssign for Boolean {
174    fn rem_assign(&mut self, other: Self) {
175        *self -= other
176    }
177}
178
179impl Sub for Boolean {
180    type Output = Self;
181
182    fn sub(self, other: Self) -> Self::Output {
183        self.xor(other)
184    }
185}
186
187impl SubAssign for Boolean {
188    fn sub_assign(&mut self, other: Self) {
189        self.0 ^= other.0
190    }
191}
192
193impl Sum for Boolean {
194    fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
195        Self(iter.any(|b| b.0))
196    }
197}
198
199impl Mul for Boolean {
200    type Output = Self;
201
202    fn mul(self, other: Self) -> Self {
203        self.and(other)
204    }
205}
206
207impl MulAssign for Boolean {
208    fn mul_assign(&mut self, other: Self) {
209        self.0 &= other.0
210    }
211}
212
213impl Div for Boolean {
214    type Output = Self;
215
216    fn div(self, other: Self) -> Self::Output {
217        if let Self(false) = other {
218            panic!("divide by zero!")
219        } else {
220            self
221        }
222    }
223}
224
225impl DivAssign for Boolean {
226    fn div_assign(&mut self, other: Self) {
227        let div = *self / other;
228        *self = div;
229    }
230}
231
232impl Product for Boolean {
233    fn product<I: Iterator<Item = Self>>(mut iter: I) -> Self {
234        Self(iter.all(|b| b.0))
235    }
236}
237
238macro_rules! trig_bool {
239    ($fun:ident) => {
240        fn $fun(self) -> Self::Out {
241            self.as_radians().$fun().into()
242        }
243    };
244}
245
246impl Trigonometry for Boolean {
247    type Out = Float;
248
249    trig_bool! {asin}
250    trig_bool! {sin}
251    trig_bool! {sinh}
252    trig_bool! {asinh}
253
254    trig_bool! {acos}
255    trig_bool! {cos}
256    trig_bool! {cosh}
257    trig_bool! {acosh}
258
259    trig_bool! {atan}
260    trig_bool! {tan}
261    trig_bool! {tanh}
262    trig_bool! {atanh}
263}
264
265impl CastFrom<Boolean> for u64 {
266    fn cast_from(b: Boolean) -> u64 {
267        UInt::from(b).into()
268    }
269}
270
271fmt_debug!(Boolean);
272
273impl fmt::Display for Boolean {
274    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
275        fmt::Display::fmt(&self.0, f)
276    }
277}
278
279/// A complex number.
280#[derive(Clone, Copy)]
281pub enum Complex {
282    C32(_Complex<f32>),
283    C64(_Complex<f64>),
284}
285
286impl GetSize for Complex {
287    fn get_size(&self) -> usize {
288        match self {
289            Self::C32(c) => c.re.get_size() + c.im.get_size(),
290            Self::C64(c) => c.re.get_size() + c.im.get_size(),
291        }
292    }
293}
294
295impl Complex {
296    /// Borrow the imaginary part of this [`Complex`] number
297    pub fn im(&self) -> Float {
298        match self {
299            Self::C32(c) => Float::F32(c.im),
300            Self::C64(c) => Float::F64(c.im),
301        }
302    }
303
304    /// Borrow the real part of this [`Complex`] number
305    pub fn re(&self) -> Float {
306        match self {
307            Self::C32(c) => Float::F32(c.re),
308            Self::C64(c) => Float::F64(c.re),
309        }
310    }
311}
312
313impl NumberInstance for Complex {
314    type Abs = Float;
315    type Exp = Self;
316    type Log = Self;
317    type Round = Self;
318    type Class = ComplexType;
319
320    fn class(&self) -> ComplexType {
321        match self {
322            Self::C32(_) => ComplexType::C32,
323            Self::C64(_) => ComplexType::C64,
324        }
325    }
326
327    fn into_type(self, dtype: ComplexType) -> Complex {
328        use ComplexType::*;
329        match dtype {
330            C32 => match self {
331                Self::C64(c) => Self::C32(_Complex::new(c.re as f32, c.im as f32)),
332                this => this,
333            },
334            C64 => match self {
335                Self::C32(c) => Self::C64(_Complex::new(c.re as f64, c.im as f64)),
336                this => this,
337            },
338            Complex => self,
339        }
340    }
341
342    fn abs(self) -> Float {
343        match self {
344            Self::C32(c) => Float::F32(c.norm_sqr().sqrt()),
345            Self::C64(c) => Float::F64(c.norm_sqr().sqrt()),
346        }
347    }
348
349    fn exp(self) -> Self::Exp {
350        match self {
351            Self::C32(c) => Self::C32(c.exp()),
352            Self::C64(c) => Self::C64(c.exp()),
353        }
354    }
355
356    fn ln(self) -> Self::Log {
357        match self {
358            Self::C32(c) => c.ln().into(),
359            Self::C64(c) => c.ln().into(),
360        }
361    }
362
363    fn log<N: NumberInstance>(self, base: N) -> Self::Log
364    where
365        Float: From<N>,
366    {
367        match self {
368            Self::C32(c) => c.log(Float::from(base).cast_into()).into(),
369            Self::C64(c) => c.log(Float::from(base).cast_into()).into(),
370        }
371    }
372
373    fn pow(self, exp: Number) -> Self {
374        match self {
375            Self::C64(this) => match exp {
376                Number::Complex(exp) => unimplemented!("{}: {}", ERR_COMPLEX_POWER, exp),
377                Number::Float(Float::F64(exp)) => Self::C64(this.pow(exp)),
378                Number::Float(Float::F32(exp)) => Self::C64(this.pow(exp)),
379                exp => Self::C64(this.pow(f64::cast_from(exp))),
380            },
381            Self::C32(this) => match exp {
382                Number::Complex(exp) => unimplemented!("{}: {}", ERR_COMPLEX_POWER, exp),
383                Number::Float(Float::F64(exp)) => {
384                    let this = _Complex::<f64>::new(this.re.into(), this.im.into());
385                    Self::C64(this.pow(exp))
386                }
387                Number::Float(Float::F32(exp)) => Self::C32(this.pow(exp)),
388                exp => Self::C32(this.pow(f32::cast_from(exp))),
389            },
390        }
391    }
392
393    fn round(self) -> Self::Round {
394        match self {
395            Self::C32(c) => Self::C32(_Complex::new(c.re.round(), c.im.round())),
396            Self::C64(c) => Self::C64(_Complex::new(c.re.round(), c.im.round())),
397        }
398    }
399}
400
401impl FloatInstance for Complex {
402    fn is_infinite(&self) -> bool {
403        match self {
404            Self::C32(c) => c.im.is_infinite() || c.re.is_infinite(),
405            Self::C64(c) => c.im.is_infinite() || c.re.is_infinite(),
406        }
407    }
408
409    fn is_nan(&self) -> bool {
410        match self {
411            Self::C32(c) => c.im.is_nan() || c.re.is_nan(),
412            Self::C64(c) => c.im.is_nan() || c.re.is_nan(),
413        }
414    }
415}
416
417impl From<[f32; 2]> for Complex {
418    fn from(arr: [f32; 2]) -> Self {
419        Self::C32(num::Complex::new(arr[0], arr[1]))
420    }
421}
422
423impl From<[f64; 2]> for Complex {
424    fn from(arr: [f64; 2]) -> Self {
425        Self::C64(num::Complex::new(arr[0], arr[1]))
426    }
427}
428
429impl<R, I> From<(R, I)> for Complex
430where
431    R: Into<f64>,
432    I: Into<f64>,
433{
434    fn from(value: (R, I)) -> Self {
435        let re = value.0.into();
436        let im = value.1.into();
437        Self::C64(num::Complex::new(re, im))
438    }
439}
440
441impl CastFrom<Number> for Complex {
442    fn cast_from(number: Number) -> Complex {
443        use Number::*;
444        match number {
445            Number::Bool(b) => Self::from(b),
446            Complex(c) => c,
447            Float(f) => Self::from(f),
448            Int(i) => Self::from(i),
449            UInt(u) => Self::from(u),
450        }
451    }
452}
453
454impl CastFrom<Complex> for Boolean {
455    fn cast_from(c: Complex) -> Self {
456        match c {
457            Complex::C32(c) if c.norm_sqr() == 0f32 => Self(false),
458            Complex::C64(c) if c.norm_sqr() == 0f64 => Self(false),
459            _ => Self(true),
460        }
461    }
462}
463
464impl CastFrom<Complex> for _Complex<f32> {
465    fn cast_from(c: Complex) -> Self {
466        match c {
467            Complex::C32(c) => c,
468            Complex::C64(_Complex { re, im }) => Self::new(re as f32, im as f32),
469        }
470    }
471}
472
473impl Add for Complex {
474    type Output = Self;
475
476    fn add(self, other: Complex) -> Self {
477        match (self, other) {
478            (Self::C32(l), Self::C32(r)) => Self::C32(l + r),
479            (Self::C64(l), Self::C64(r)) => Self::C64(l + r),
480            (Self::C64(l), r) => {
481                let r: _Complex<f64> = r.into();
482                Self::C64(l + r)
483            }
484            (l, r) => r + l,
485        }
486    }
487}
488
489impl AddAssign for Complex {
490    fn add_assign(&mut self, other: Self) {
491        let sum = *self + other;
492        *self = sum;
493    }
494}
495
496impl Rem for Complex {
497    type Output = Self;
498
499    fn rem(self, other: Self) -> Self::Output {
500        match (self, other) {
501            (Self::C32(l), Self::C32(r)) => Self::C32(l - r),
502            (l, r) => {
503                let l: _Complex<f64> = l.into();
504                let r: _Complex<f64> = r.into();
505                Self::C64(l % r)
506            }
507        }
508    }
509}
510
511impl Sub for Complex {
512    type Output = Self;
513
514    fn sub(self, other: Complex) -> Self {
515        match (self, other) {
516            (Self::C32(l), Self::C32(r)) => Self::C32(l - r),
517            (l, r) => {
518                let l: _Complex<f64> = l.into();
519                let r: _Complex<f64> = r.into();
520                Self::C64(l - r)
521            }
522        }
523    }
524}
525
526impl SubAssign for Complex {
527    fn sub_assign(&mut self, other: Self) {
528        let diff = *self - other;
529        *self = diff;
530    }
531}
532
533impl Sum for Complex {
534    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
535        let mut sum = ComplexType::Complex.zero();
536        for i in iter {
537            sum = sum + i;
538        }
539        sum
540    }
541}
542
543impl Mul for Complex {
544    type Output = Self;
545
546    fn mul(self, other: Complex) -> Self {
547        match (self, other) {
548            (Self::C32(l), Self::C32(r)) => Self::C32(l * r),
549            (Self::C64(l), Self::C64(r)) => Self::C64(l * r),
550            (Self::C64(l), r) => {
551                let r: _Complex<f64> = r.into();
552                Self::C64(l * r)
553            }
554            (l, r) => r * l,
555        }
556    }
557}
558
559impl MulAssign for Complex {
560    fn mul_assign(&mut self, other: Self) {
561        let product = *self * other;
562        *self = product;
563    }
564}
565
566impl Div for Complex {
567    type Output = Self;
568
569    fn div(self, other: Complex) -> Self {
570        match (self, other) {
571            (Self::C32(l), Self::C32(r)) => Self::C32(l / r),
572            (Self::C64(l), Self::C64(r)) => Self::C64(l / r),
573            (Self::C64(l), r) => {
574                let r: _Complex<f64> = r.into();
575                Self::C64(l / r)
576            }
577            (Self::C32(l), Self::C64(r)) => {
578                let l = _Complex::<f64>::new(l.re as f64, l.im as f64);
579                Self::C64(l / r)
580            }
581        }
582    }
583}
584
585impl DivAssign for Complex {
586    fn div_assign(&mut self, other: Self) {
587        let div = *self / other;
588        *self = div;
589    }
590}
591
592impl Product for Complex {
593    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
594        let zero = ComplexType::Complex.zero();
595        let mut product = ComplexType::Complex.one();
596
597        for i in iter {
598            if i == zero {
599                return zero;
600            }
601
602            product = product * i;
603        }
604        product
605    }
606}
607
608macro_rules! trig_complex {
609    ($fun:ident) => {
610        fn $fun(self) -> Self {
611            match self {
612                Complex::C32(c) => c.$fun().into(),
613                Complex::C64(c) => c.$fun().into(),
614            }
615        }
616    };
617}
618
619impl Trigonometry for Complex {
620    type Out = Self;
621
622    trig_complex! {asin}
623    trig_complex! {sin}
624    trig_complex! {sinh}
625    trig_complex! {asinh}
626
627    trig_complex! {acos}
628    trig_complex! {cos}
629    trig_complex! {cosh}
630    trig_complex! {acosh}
631
632    trig_complex! {atan}
633    trig_complex! {tan}
634    trig_complex! {tanh}
635    trig_complex! {atanh}
636}
637
638impl PartialEq for Complex {
639    fn eq(&self, other: &Self) -> bool {
640        match (self, other) {
641            (Self::C32(l), Self::C32(r)) => l.eq(r),
642            (Self::C64(l), Self::C64(r)) => l.eq(r),
643            (Self::C64(l), Self::C32(r)) => l.re as f32 == r.re && l.im as f32 == r.im,
644            (l, r) => r.eq(l),
645        }
646    }
647}
648
649impl Eq for Complex {}
650
651impl Hash for Complex {
652    fn hash<H: Hasher>(&self, state: &mut H) {
653        match self {
654            Self::C32(c) => {
655                Float::F32(c.re).hash(state);
656                Float::F32(c.im).hash(state);
657            }
658            Self::C64(c) => {
659                Float::F64(c.re).hash(state);
660                Float::F64(c.im).hash(state);
661            }
662        }
663    }
664}
665
666impl Default for Complex {
667    fn default() -> Complex {
668        Complex::C32(_Complex::<f32>::default())
669    }
670}
671
672impl From<Complex> for _Complex<f64> {
673    fn from(c: Complex) -> Self {
674        match c {
675            Complex::C32(c) => Self::new(c.re as f64, c.im as f64),
676            Complex::C64(c64) => c64,
677        }
678    }
679}
680
681impl From<Float> for Complex {
682    fn from(f: Float) -> Self {
683        match f {
684            Float::F64(f) => Self::C64(_Complex::new(f, 0.0f64)),
685            Float::F32(f) => Self::C32(_Complex::new(f, 0.0f32)),
686        }
687    }
688}
689
690impl From<Int> for Complex {
691    fn from(i: Int) -> Self {
692        match i {
693            Int::I8(i) => Self::C32(_Complex::new(i as f32, 0.0f32)),
694            Int::I64(i) => Self::C64(_Complex::new(i as f64, 0.0f64)),
695            Int::I32(i) => Self::C32(_Complex::new(i as f32, 0.0f32)),
696            Int::I16(i) => Self::C32(_Complex::new(i as f32, 0.0f32)),
697        }
698    }
699}
700
701impl From<UInt> for Complex {
702    fn from(u: UInt) -> Self {
703        match u {
704            UInt::U64(u) => Self::C64(_Complex::new(u as f64, 0.0f64)),
705            UInt::U32(u) => Self::C32(_Complex::new(u as f32, 0.0f32)),
706            UInt::U16(u) => Self::C32(_Complex::new(u as f32, 0.0f32)),
707            UInt::U8(u) => Self::C32(_Complex::new(u as f32, 0.0f32)),
708        }
709    }
710}
711
712impl From<Boolean> for Complex {
713    fn from(b: Boolean) -> Self {
714        match b {
715            Boolean(true) => Self::C32(_Complex::new(1.0f32, 0.0f32)),
716            Boolean(false) => Self::C32(_Complex::new(1.0f32, 0.0f32)),
717        }
718    }
719}
720
721impl From<_Complex<f32>> for Complex {
722    fn from(c: _Complex<f32>) -> Self {
723        Self::C32(c)
724    }
725}
726
727impl From<_Complex<f64>> for Complex {
728    fn from(c: _Complex<f64>) -> Self {
729        Self::C64(c)
730    }
731}
732
733impl FromStr for Complex {
734    type Err = Error;
735
736    fn from_str(s: &str) -> Result<Self, Self::Err> {
737        num::Complex::<f64>::from_str(s)
738            .map(Self::from)
739            .map_err(Error::new)
740    }
741}
742
743impl From<Complex> for (Float, Float) {
744    fn from(n: Complex) -> Self {
745        match n {
746            Complex::C32(n) => (Float::F32(n.re), Float::F32(n.im)),
747            Complex::C64(n) => (Float::F64(n.re), Float::F64(n.im)),
748        }
749    }
750}
751
752impl From<Complex> for [Float; 2] {
753    fn from(n: Complex) -> Self {
754        match n {
755            Complex::C32(n) => [Float::F32(n.re), Float::F32(n.im)],
756            Complex::C64(n) => [Float::F64(n.re), Float::F64(n.im)],
757        }
758    }
759}
760
761fmt_debug!(Complex);
762
763impl fmt::Display for Complex {
764    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
765        match self {
766            Complex::C32(c) => fmt::Display::fmt(c, f),
767            Complex::C64(c) => fmt::Display::fmt(c, f),
768        }
769    }
770}
771
772/// Defines a collation order for [`Complex`].
773#[derive(Copy, Clone, Default, Eq, PartialEq)]
774pub struct ComplexCollator {
775    float: FloatCollator,
776}
777
778impl Collate for ComplexCollator {
779    type Value = Complex;
780
781    fn cmp(&self, left: &Self::Value, right: &Self::Value) -> Ordering {
782        self.float.cmp(&left.abs(), &right.abs())
783    }
784}
785
786/// A floating-point number.
787#[derive(Clone, Copy, GetSize)]
788pub enum Float {
789    F32(f32),
790    F64(f64),
791}
792
793impl NumberInstance for Float {
794    type Abs = Float;
795    type Exp = Self;
796    type Log = Self;
797    type Round = Int;
798    type Class = FloatType;
799
800    fn class(&self) -> FloatType {
801        match self {
802            Self::F32(_) => FloatType::F32,
803            Self::F64(_) => FloatType::F64,
804        }
805    }
806
807    fn into_type(self, dtype: FloatType) -> Float {
808        use FloatType::*;
809        match dtype {
810            F32 => match self {
811                Self::F64(f) => Self::F32(f as f32),
812                this => this,
813            },
814            F64 => match self {
815                Self::F32(f) => Self::F64(f as f64),
816                this => this,
817            },
818            Float => self,
819        }
820    }
821
822    fn abs(self) -> Float {
823        match self {
824            Self::F32(f) => Self::F32(f.abs()),
825            Self::F64(f) => Self::F64(f.abs()),
826        }
827    }
828
829    fn exp(self) -> Self::Exp {
830        match self {
831            Self::F32(f) => Self::F32(f.exp()),
832            Self::F64(f) => Self::F64(f.exp()),
833        }
834    }
835
836    fn ln(self) -> Self::Log {
837        match self {
838            Self::F32(f) => f.ln().into(),
839            Self::F64(f) => f.ln().into(),
840        }
841    }
842
843    fn log<N: NumberInstance>(self, base: N) -> Self::Log
844    where
845        Float: From<N>,
846    {
847        match self {
848            Self::F32(f) => f.log(Float::from(base).cast_into()).into(),
849            Self::F64(f) => f.log(Float::from(base).cast_into()).into(),
850        }
851    }
852
853    fn pow(self, exp: Number) -> Self {
854        match self {
855            Self::F64(this) => match exp {
856                Number::Complex(exp) => unimplemented!("{}: {}", ERR_COMPLEX_POWER, exp),
857                exp => this.pow(f64::cast_from(exp)).into(),
858            },
859            Self::F32(this) => match exp {
860                Number::Complex(exp) => unimplemented!("{}: {}", ERR_COMPLEX_POWER, exp),
861                Number::Float(Float::F64(exp)) => Self::F64(f64::from(self).pow(exp)),
862                exp => this.pow(f32::cast_from(exp)).into(),
863            },
864        }
865    }
866
867    fn round(self) -> Self::Round {
868        match self {
869            Self::F32(f) => (f.round() as i32).into(),
870            Self::F64(f) => (f.round() as i64).into(),
871        }
872    }
873}
874
875impl RealInstance for Float {
876    const ONE: Self = Float::F32(1.);
877    const ZERO: Self = Float::F32(0.);
878}
879
880impl FloatInstance for Float {
881    fn is_infinite(&self) -> bool {
882        match self {
883            Self::F32(f) => f.is_infinite(),
884            Self::F64(f) => f.is_infinite(),
885        }
886    }
887
888    fn is_nan(&self) -> bool {
889        match self {
890            Self::F32(f) => f.is_nan(),
891            Self::F64(f) => f.is_nan(),
892        }
893    }
894}
895
896impl Eq for Float {}
897
898impl Add for Float {
899    type Output = Self;
900
901    fn add(self, other: Float) -> Self {
902        match (self, other) {
903            (Self::F32(l), Self::F32(r)) => Self::F32(l + r),
904            (Self::F64(l), Self::F64(r)) => Self::F64(l + r),
905            (Self::F64(l), Self::F32(r)) => Self::F64(l + r as f64),
906            (l, r) => r + l,
907        }
908    }
909}
910
911impl AddAssign for Float {
912    fn add_assign(&mut self, other: Self) {
913        let sum = *self + other;
914        *self = sum;
915    }
916}
917
918impl Sub for Float {
919    type Output = Self;
920
921    fn sub(self, other: Float) -> Self {
922        match (self, other) {
923            (Self::F32(l), Self::F32(r)) => Self::F32(l - r),
924            (l, r) => {
925                let l: f64 = l.into();
926                let r: f64 = r.into();
927                Self::F64(l - r)
928            }
929        }
930    }
931}
932
933impl SubAssign for Float {
934    fn sub_assign(&mut self, other: Self) {
935        let diff = *self - other;
936        *self = diff;
937    }
938}
939
940impl Sum for Float {
941    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
942        let mut sum = FloatType::Float.zero();
943        for i in iter {
944            sum = sum + i;
945        }
946        sum
947    }
948}
949
950impl Rem for Float {
951    type Output = Self;
952
953    fn rem(self, other: Self) -> Self::Output {
954        match (self, other) {
955            (Self::F64(l), Self::F64(r)) => Self::F64(l % r),
956            (Self::F32(l), Self::F32(r)) => Self::F32(l % r),
957            (Self::F64(l), Self::F32(r)) => Self::F64(l % r as f64),
958            (Self::F32(l), Self::F64(r)) => Self::F64(l as f64 % r),
959        }
960    }
961}
962
963impl RemAssign for Float {
964    fn rem_assign(&mut self, other: Self) {
965        let rem = *self % other;
966        *self = rem;
967    }
968}
969
970impl Mul for Float {
971    type Output = Self;
972
973    fn mul(self, other: Float) -> Self {
974        match (self, other) {
975            (Self::F32(l), Self::F32(r)) => Self::F32(l * r),
976            (Self::F64(l), Self::F64(r)) => Self::F64(l * r),
977            (Self::F64(l), Self::F32(r)) => Self::F64(l * r as f64),
978            (l, r) => r * l,
979        }
980    }
981}
982
983impl MulAssign for Float {
984    fn mul_assign(&mut self, other: Self) {
985        let product = *self * other;
986        *self = product;
987    }
988}
989
990impl Div for Float {
991    type Output = Self;
992
993    fn div(self, other: Self) -> Self::Output {
994        match (self, other) {
995            (Self::F32(l), Self::F32(r)) => Self::F32(l / r),
996            (Self::F64(l), Self::F64(r)) => Self::F64(l / r),
997            (Self::F32(l), Self::F64(r)) => Self::F64((l as f64) / r),
998            (Self::F64(l), Self::F32(r)) => Self::F64(l / (r as f64)),
999        }
1000    }
1001}
1002
1003impl DivAssign for Float {
1004    fn div_assign(&mut self, other: Self) {
1005        let div = *self / other;
1006        *self = div;
1007    }
1008}
1009
1010impl Product for Float {
1011    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
1012        let zero = FloatType::Float.zero();
1013        let mut product = FloatType::Float.one();
1014
1015        for i in iter {
1016            if i == zero {
1017                return zero;
1018            }
1019
1020            product = product * i;
1021        }
1022        product
1023    }
1024}
1025
1026macro_rules! trig_float {
1027    ($fun:ident) => {
1028        fn $fun(self) -> Self {
1029            match self {
1030                Float::F32(f) => f.$fun().into(),
1031                Float::F64(f) => f.$fun().into(),
1032            }
1033        }
1034    };
1035}
1036
1037impl Trigonometry for Float {
1038    type Out = Self;
1039
1040    trig_float! {asin}
1041    trig_float! {sin}
1042    trig_float! {sinh}
1043    trig_float! {asinh}
1044
1045    trig_float! {acos}
1046    trig_float! {cos}
1047    trig_float! {cosh}
1048    trig_float! {acosh}
1049
1050    trig_float! {atan}
1051    trig_float! {tan}
1052    trig_float! {tanh}
1053    trig_float! {atanh}
1054}
1055
1056impl Hash for Float {
1057    fn hash<H: Hasher>(&self, state: &mut H) {
1058        match self {
1059            Self::F32(f) => f.to_be_bytes().hash(state),
1060            Self::F64(f) => f.to_be_bytes().hash(state),
1061        }
1062    }
1063}
1064
1065impl PartialEq for Float {
1066    fn eq(&self, other: &Self) -> bool {
1067        match (self, other) {
1068            (Self::F32(l), Self::F32(r)) => l.eq(r),
1069            (Self::F64(l), Self::F64(r)) => l.eq(r),
1070            (Self::F64(l), Self::F32(r)) => (*l as f32).eq(r),
1071            (l, r) => r.eq(l),
1072        }
1073    }
1074}
1075
1076impl PartialOrd for Float {
1077    fn partial_cmp(&self, other: &Float) -> Option<Ordering> {
1078        match (self, other) {
1079            (Float::F32(l), Float::F32(r)) => l.partial_cmp(r),
1080            (Float::F64(l), Float::F64(r)) => l.partial_cmp(r),
1081            (l, r) => f64::from(*l).partial_cmp(&f64::from(*r)),
1082        }
1083    }
1084}
1085
1086impl Default for Float {
1087    fn default() -> Float {
1088        Float::F32(f32::default())
1089    }
1090}
1091
1092impl From<Boolean> for Float {
1093    fn from(b: Boolean) -> Self {
1094        match b {
1095            Boolean(true) => Self::F32(1.0f32),
1096            Boolean(false) => Self::F32(0.0f32),
1097        }
1098    }
1099}
1100
1101impl From<f32> for Float {
1102    fn from(f: f32) -> Self {
1103        Self::F32(f)
1104    }
1105}
1106
1107impl From<f64> for Float {
1108    fn from(f: f64) -> Self {
1109        Self::F64(f)
1110    }
1111}
1112
1113impl From<Int> for Float {
1114    fn from(i: Int) -> Self {
1115        match i {
1116            Int::I8(i) => Self::F32(i as f32),
1117            Int::I64(i) => Self::F64(i as f64),
1118            Int::I32(i) => Self::F32(i as f32),
1119            Int::I16(i) => Self::F32(i as f32),
1120        }
1121    }
1122}
1123
1124impl From<UInt> for Float {
1125    fn from(u: UInt) -> Self {
1126        match u {
1127            UInt::U64(u) => Self::F64(u as f64),
1128            UInt::U32(u) => Self::F32(u as f32),
1129            UInt::U16(u) => Self::F32(u as f32),
1130            UInt::U8(u) => Self::F32(u as f32),
1131        }
1132    }
1133}
1134
1135impl FromStr for Float {
1136    type Err = Error;
1137
1138    fn from_str(s: &str) -> Result<Self, Self::Err> {
1139        f64::from_str(s).map(Self::from).map_err(Error::new)
1140    }
1141}
1142
1143impl CastFrom<Complex> for Float {
1144    fn cast_from(c: Complex) -> Float {
1145        use Complex::*;
1146        match c {
1147            C32(c) => Self::F32(c.re),
1148            C64(c) => Self::F64(c.re),
1149        }
1150    }
1151}
1152
1153impl CastFrom<Float> for Boolean {
1154    fn cast_from(f: Float) -> Boolean {
1155        use Float::*;
1156        let b = match f {
1157            F32(f) if f == 0f32 => false,
1158            F64(f) if f == 0f64 => false,
1159            _ => true,
1160        };
1161
1162        Boolean(b)
1163    }
1164}
1165
1166impl CastFrom<Float> for f32 {
1167    fn cast_from(f: Float) -> f32 {
1168        match f {
1169            Float::F32(f) => f,
1170            Float::F64(f) => f as f32,
1171        }
1172    }
1173}
1174
1175impl From<Float> for f64 {
1176    fn from(f: Float) -> f64 {
1177        match f {
1178            Float::F32(f) => f as f64,
1179            Float::F64(f) => f,
1180        }
1181    }
1182}
1183
1184fmt_debug!(Float);
1185
1186impl fmt::Display for Float {
1187    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1188        match self {
1189            Float::F32(n) => fmt::Display::fmt(n, f),
1190            Float::F64(n) => fmt::Display::fmt(n, f),
1191        }
1192    }
1193}
1194
1195#[derive(Copy, Clone, Default, Eq, PartialEq)]
1196struct F32Collator;
1197
1198impl Collate for F32Collator {
1199    type Value = f32;
1200
1201    fn cmp(&self, left: &f32, right: &f32) -> Ordering {
1202        if left == right {
1203            Ordering::Equal
1204        } else {
1205            left.total_cmp(right)
1206        }
1207    }
1208}
1209
1210#[derive(Copy, Clone, Default, Eq, PartialEq)]
1211struct F64Collator;
1212
1213impl Collate for F64Collator {
1214    type Value = f64;
1215
1216    fn cmp(&self, left: &f64, right: &f64) -> Ordering {
1217        if left == right {
1218            Ordering::Equal
1219        } else {
1220            left.total_cmp(right)
1221        }
1222    }
1223}
1224
1225/// Defines a collation order for [`Float`].
1226#[derive(Copy, Clone, Default, Eq, PartialEq)]
1227pub struct FloatCollator {
1228    f32: F32Collator,
1229    f64: F64Collator,
1230}
1231
1232impl Collate for FloatCollator {
1233    type Value = Float;
1234
1235    fn cmp(&self, left: &Self::Value, right: &Self::Value) -> Ordering {
1236        if let Some(order) = left.partial_cmp(right) {
1237            order
1238        } else {
1239            match (left, right) {
1240                (Float::F32(l), Float::F32(r)) => self.f32.cmp(l.into(), r.into()),
1241                (Float::F64(l), Float::F64(r)) => self.f64.cmp(l.into(), r.into()),
1242                (l, r) => self.f64.cmp(&(*l).cast_into(), &(*r).cast_into()),
1243            }
1244        }
1245    }
1246}
1247
1248/// A signed integer.
1249#[derive(Clone, Copy, Hash, GetSize)]
1250pub enum Int {
1251    I8(i8),
1252    I16(i16),
1253    I32(i32),
1254    I64(i64),
1255}
1256
1257impl NumberInstance for Int {
1258    type Abs = Self;
1259    type Exp = Float;
1260    type Log = Float;
1261    type Round = Self;
1262    type Class = IntType;
1263
1264    fn class(&self) -> IntType {
1265        match self {
1266            Self::I8(_) => IntType::I8,
1267            Self::I16(_) => IntType::I16,
1268            Self::I32(_) => IntType::I32,
1269            Self::I64(_) => IntType::I64,
1270        }
1271    }
1272
1273    fn into_type(self, dtype: IntType) -> Int {
1274        use IntType::*;
1275        match dtype {
1276            I8 => match self {
1277                Self::I16(i) => Self::I8(i as i8),
1278                Self::I32(i) => Self::I8(i as i8),
1279                Self::I64(i) => Self::I8(i as i8),
1280                this => this,
1281            },
1282            I16 => match self {
1283                Self::I8(i) => Self::I16(i as i16),
1284                Self::I32(i) => Self::I16(i as i16),
1285                Self::I64(i) => Self::I16(i as i16),
1286                this => this,
1287            },
1288            I32 => match self {
1289                Self::I8(i) => Self::I32(i as i32),
1290                Self::I16(i) => Self::I32(i as i32),
1291                Self::I64(i) => Self::I32(i as i32),
1292                this => this,
1293            },
1294            I64 => match self {
1295                Self::I8(i) => Self::I64(i as i64),
1296                Self::I16(i) => Self::I64(i as i64),
1297                Self::I32(i) => Self::I64(i as i64),
1298                this => this,
1299            },
1300            Int => self,
1301        }
1302    }
1303
1304    fn abs(self) -> Self {
1305        match self {
1306            Self::I8(i) => Int::I8(i.abs()),
1307            Self::I16(i) => Int::I16(i.abs()),
1308            Self::I32(i) => Int::I32(i.abs()),
1309            Self::I64(i) => Int::I64(i.abs()),
1310        }
1311    }
1312
1313    fn exp(self) -> Self::Exp {
1314        Float::from(self).exp()
1315    }
1316
1317    fn ln(self) -> Self::Log {
1318        Float::from(self).ln()
1319    }
1320
1321    fn log<N: NumberInstance>(self, base: N) -> Self::Log
1322    where
1323        Float: From<N>,
1324    {
1325        let this: Float = self.into();
1326        this.log(base)
1327    }
1328
1329    fn pow(self, exp: Number) -> Self {
1330        if exp < exp.class().zero() {
1331            return self.class().zero();
1332        }
1333
1334        match self {
1335            Self::I8(this) => Self::I8(this.pow(exp.cast_into())),
1336            Self::I16(this) => Self::I16(this.pow(exp.cast_into())),
1337            Self::I32(this) => Self::I32(this.pow(exp.cast_into())),
1338            Self::I64(this) => Self::I64(this.pow(exp.cast_into())),
1339        }
1340    }
1341
1342    fn round(self) -> Self::Round {
1343        self
1344    }
1345}
1346
1347impl RealInstance for Int {
1348    const ONE: Self = Int::I8(1);
1349    const ZERO: Self = Int::I8(0);
1350}
1351
1352impl CastFrom<Complex> for Int {
1353    fn cast_from(c: Complex) -> Int {
1354        use Complex::*;
1355        match c {
1356            C32(c) => Self::I32(c.re as i32),
1357            C64(c) => Self::I64(c.re as i64),
1358        }
1359    }
1360}
1361
1362impl CastFrom<Float> for Int {
1363    fn cast_from(f: Float) -> Int {
1364        use Float::*;
1365        match f {
1366            F32(f) => Self::I32(f as i32),
1367            F64(f) => Self::I64(f as i64),
1368        }
1369    }
1370}
1371
1372impl CastFrom<Int> for Boolean {
1373    fn cast_from(i: Int) -> Boolean {
1374        use Int::*;
1375        let b = match i {
1376            I16(i) if i == 0i16 => false,
1377            I32(i) if i == 0i32 => false,
1378            I64(i) if i == 0i64 => false,
1379            _ => true,
1380        };
1381
1382        Boolean(b)
1383    }
1384}
1385
1386impl CastFrom<Int> for i8 {
1387    fn cast_from(i: Int) -> i8 {
1388        match i {
1389            Int::I8(i) => i,
1390            Int::I16(i) => i as i8,
1391            Int::I32(i) => i as i8,
1392            Int::I64(i) => i as i8,
1393        }
1394    }
1395}
1396
1397impl CastFrom<Int> for i16 {
1398    fn cast_from(i: Int) -> i16 {
1399        match i {
1400            Int::I8(i) => i as i16,
1401            Int::I16(i) => i,
1402            Int::I32(i) => i as i16,
1403            Int::I64(i) => i as i16,
1404        }
1405    }
1406}
1407
1408impl CastFrom<Int> for i32 {
1409    fn cast_from(i: Int) -> i32 {
1410        match i {
1411            Int::I8(i) => i as i32,
1412            Int::I16(i) => i as i32,
1413            Int::I32(i) => i,
1414            Int::I64(i) => i as i32,
1415        }
1416    }
1417}
1418
1419impl Eq for Int {}
1420
1421impl Add for Int {
1422    type Output = Self;
1423
1424    fn add(self, other: Int) -> Self {
1425        match (self, other) {
1426            (Self::I64(l), Self::I64(r)) => Self::I64(l + r),
1427            (Self::I64(l), Self::I32(r)) => Self::I64(l + r as i64),
1428            (Self::I64(l), Self::I16(r)) => Self::I64(l + r as i64),
1429            (Self::I64(l), Self::I8(r)) => Self::I64(l + r as i64),
1430            (Self::I32(l), Self::I32(r)) => Self::I32(l + r),
1431            (Self::I32(l), Self::I16(r)) => Self::I32(l + r as i32),
1432            (Self::I32(l), Self::I8(r)) => Self::I32(l + r as i32),
1433            (Self::I16(l), Self::I16(r)) => Self::I16(l + r),
1434            (Self::I16(l), Self::I8(r)) => Self::I16(l + r as i16),
1435            (Self::I8(l), Self::I8(r)) => Self::I8(l + r),
1436            (l, r) => r + l,
1437        }
1438    }
1439}
1440
1441impl AddAssign for Int {
1442    fn add_assign(&mut self, other: Self) {
1443        let sum = *self + other;
1444        *self = sum;
1445    }
1446}
1447
1448impl Rem for Int {
1449    type Output = Self;
1450
1451    fn rem(self, other: Self) -> Self::Output {
1452        match (self, other) {
1453            (Self::I64(l), Self::I64(r)) => Self::I64(l % r),
1454            (Self::I64(l), Self::I32(r)) => Self::I64(l % r as i64),
1455            (Self::I64(l), Self::I16(r)) => Self::I64(l % r as i64),
1456            (Self::I64(l), Self::I8(r)) => Self::I64(l % r as i64),
1457            (Self::I32(l), Self::I64(r)) => Self::I64(l as i64 % r),
1458            (Self::I32(l), Self::I32(r)) => Self::I32(l % r),
1459            (Self::I32(l), Self::I16(r)) => Self::I32(l % r as i32),
1460            (Self::I32(l), Self::I8(r)) => Self::I32(l % r as i32),
1461            (Self::I16(l), Self::I64(r)) => Self::I64(l as i64 % r),
1462            (Self::I16(l), Self::I32(r)) => Self::I32(l as i32 % r),
1463            (Self::I16(l), Self::I16(r)) => Self::I16(l % r),
1464            (Self::I16(l), Self::I8(r)) => Self::I16(l % r as i16),
1465            (Self::I8(l), Self::I64(r)) => Self::I64(l as i64 % r),
1466            (Self::I8(l), Self::I32(r)) => Self::I32(l as i32 % r),
1467            (Self::I8(l), Self::I16(r)) => Self::I16(l as i16 % r),
1468            (Self::I8(l), Self::I8(r)) => Self::I8(l % r),
1469        }
1470    }
1471}
1472
1473impl RemAssign for Int {
1474    fn rem_assign(&mut self, other: Self) {
1475        let rem = *self % other;
1476        *self = rem;
1477    }
1478}
1479
1480impl Sub for Int {
1481    type Output = Self;
1482
1483    fn sub(self, other: Int) -> Self {
1484        match (self, other) {
1485            (Self::I64(l), Self::I64(r)) => Self::I64(l - r),
1486            (Self::I64(l), Self::I32(r)) => Self::I64(l - r as i64),
1487            (Self::I64(l), Self::I16(r)) => Self::I64(l - r as i64),
1488            (Self::I64(l), Self::I8(r)) => Self::I64(l - r as i64),
1489            (Self::I32(l), Self::I64(r)) => Self::I64(l as i64 - r),
1490            (Self::I32(l), Self::I32(r)) => Self::I32(l - r),
1491            (Self::I32(l), Self::I16(r)) => Self::I32(l - r as i32),
1492            (Self::I32(l), Self::I8(r)) => Self::I32(l - r as i32),
1493            (Self::I16(l), Self::I64(r)) => Self::I64(l as i64 - r),
1494            (Self::I16(l), Self::I32(r)) => Self::I32(l as i32 - r),
1495            (Self::I16(l), Self::I16(r)) => Self::I16(l - r),
1496            (Self::I16(l), Self::I8(r)) => Self::I16(l - r as i16),
1497            (Self::I8(l), Self::I64(r)) => Self::I64(l as i64 - r),
1498            (Self::I8(l), Self::I32(r)) => Self::I32(l as i32 - r),
1499            (Self::I8(l), Self::I16(r)) => Self::I16(l as i16 - r),
1500            (Self::I8(l), Self::I8(r)) => Self::I8(l - r),
1501        }
1502    }
1503}
1504
1505impl SubAssign for Int {
1506    fn sub_assign(&mut self, other: Self) {
1507        let diff = *self - other;
1508        *self = diff;
1509    }
1510}
1511
1512impl Sum for Int {
1513    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
1514        let mut sum = IntType::Int.zero();
1515        for i in iter {
1516            sum = sum + i;
1517        }
1518        sum
1519    }
1520}
1521
1522impl Mul for Int {
1523    type Output = Self;
1524
1525    fn mul(self, other: Int) -> Self {
1526        match (self, other) {
1527            (Self::I64(l), Self::I64(r)) => Self::I64(l * r),
1528            (Self::I64(l), Self::I32(r)) => Self::I64(l * r as i64),
1529            (Self::I64(l), Self::I16(r)) => Self::I64(l * r as i64),
1530            (Self::I32(l), Self::I32(r)) => Self::I32(l * r),
1531            (Self::I32(l), Self::I16(r)) => Self::I32(l * r as i32),
1532            (Self::I16(l), Self::I16(r)) => Self::I16(l * r),
1533            (l, r) => r * l,
1534        }
1535    }
1536}
1537
1538impl MulAssign for Int {
1539    fn mul_assign(&mut self, other: Self) {
1540        let product = *self * other;
1541        *self = product;
1542    }
1543}
1544
1545impl Div for Int {
1546    type Output = Self;
1547
1548    fn div(self, other: Int) -> Self {
1549        match (self, other) {
1550            (Self::I64(l), Self::I64(r)) => Self::I64(l / r),
1551            (Self::I64(l), Self::I32(r)) => Self::I64(l / r as i64),
1552            (Self::I64(l), Self::I16(r)) => Self::I64(l / r as i64),
1553            (Self::I64(l), Self::I8(r)) => Self::I64(l / r as i64),
1554
1555            (Self::I32(l), Self::I64(r)) => Self::I64(l as i64 / r),
1556            (Self::I32(l), Self::I32(r)) => Self::I32(l / r),
1557            (Self::I32(l), Self::I16(r)) => Self::I32(l / r as i32),
1558            (Self::I32(l), Self::I8(r)) => Self::I32(l / r as i32),
1559
1560            (Self::I16(l), Self::I64(r)) => Self::I64(l as i64 / r),
1561            (Self::I16(l), Self::I32(r)) => Self::I32(l as i32 / r),
1562            (Self::I16(l), Self::I16(r)) => Self::I16(l / r),
1563            (Self::I16(l), Self::I8(r)) => Self::I16(l / r as i16),
1564
1565            (Self::I8(l), Self::I64(r)) => Self::I64(l as i64 / r),
1566            (Self::I8(l), Self::I32(r)) => Self::I32(l as i32 / r),
1567            (Self::I8(l), Self::I16(r)) => Self::I16(l as i16 / r),
1568            (Self::I8(l), Self::I8(r)) => Self::I8(l / r),
1569        }
1570    }
1571}
1572
1573impl DivAssign for Int {
1574    fn div_assign(&mut self, other: Self) {
1575        let div = *self / other;
1576        *self = div;
1577    }
1578}
1579
1580impl Product for Int {
1581    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
1582        let zero = IntType::Int.zero();
1583        let mut product = IntType::Int.one();
1584
1585        for i in iter {
1586            if i == zero {
1587                return zero;
1588            }
1589
1590            product = product * i;
1591        }
1592        product
1593    }
1594}
1595
1596macro_rules! trig_int {
1597    ($fun:ident) => {
1598        fn $fun(self) -> Self::Out {
1599            match self {
1600                Self::I8(i) => (i as f32).$fun().into(),
1601                Self::I16(i) => (i as f32).$fun().into(),
1602                Self::I32(i) => (i as f32).$fun().into(),
1603                Self::I64(i) => (i as f64).$fun().into(),
1604            }
1605        }
1606    };
1607}
1608
1609impl Trigonometry for Int {
1610    type Out = Float;
1611
1612    trig_int! {asin}
1613    trig_int! {sin}
1614    trig_int! {sinh}
1615    trig_int! {asinh}
1616
1617    trig_int! {acos}
1618    trig_int! {cos}
1619    trig_int! {cosh}
1620    trig_int! {acosh}
1621
1622    trig_int! {atan}
1623    trig_int! {tan}
1624    trig_int! {tanh}
1625    trig_int! {atanh}
1626}
1627
1628impl PartialEq for Int {
1629    fn eq(&self, other: &Self) -> bool {
1630        match (self, other) {
1631            (Self::I16(l), Self::I16(r)) => l.eq(r),
1632            (Self::I32(l), Self::I32(r)) => l.eq(r),
1633            (Self::I64(l), Self::I64(r)) => l.eq(r),
1634            (Self::I64(l), r) => l.eq(&i64::from(*r)),
1635            (l, r) => i64::from(*l).eq(&i64::from(*r)),
1636        }
1637    }
1638}
1639
1640impl PartialOrd for Int {
1641    fn partial_cmp(&self, other: &Int) -> Option<Ordering> {
1642        Some(self.cmp(other))
1643    }
1644}
1645
1646impl Ord for Int {
1647    fn cmp(&self, other: &Self) -> Ordering {
1648        match (self, other) {
1649            (Int::I8(l), Int::I8(r)) => l.cmp(r),
1650            (Int::I16(l), Int::I16(r)) => l.cmp(r),
1651            (Int::I32(l), Int::I32(r)) => l.cmp(r),
1652            (Int::I64(l), Int::I64(r)) => l.cmp(r),
1653            (l, r) => i64::from(*l).cmp(&i64::from(*r)),
1654        }
1655    }
1656}
1657
1658impl Default for Int {
1659    fn default() -> Int {
1660        Int::I16(i16::default())
1661    }
1662}
1663
1664impl From<i8> for Int {
1665    fn from(i: i8) -> Int {
1666        Int::I8(i)
1667    }
1668}
1669
1670impl From<i16> for Int {
1671    fn from(i: i16) -> Int {
1672        Int::I16(i)
1673    }
1674}
1675
1676impl From<i32> for Int {
1677    fn from(i: i32) -> Int {
1678        Int::I32(i)
1679    }
1680}
1681
1682impl From<i64> for Int {
1683    fn from(i: i64) -> Int {
1684        Int::I64(i)
1685    }
1686}
1687
1688impl From<UInt> for Int {
1689    fn from(u: UInt) -> Int {
1690        match u {
1691            UInt::U64(u) => Int::I64(u as i64),
1692            UInt::U32(u) => Int::I32(u as i32),
1693            UInt::U16(u) => Int::I16(u as i16),
1694            UInt::U8(u) => Int::I16(u as i16),
1695        }
1696    }
1697}
1698
1699impl From<Boolean> for Int {
1700    fn from(b: Boolean) -> Int {
1701        match b {
1702            Boolean(true) => Int::I16(1),
1703            Boolean(false) => Int::I16(0),
1704        }
1705    }
1706}
1707
1708impl From<Int> for i64 {
1709    fn from(i: Int) -> i64 {
1710        match i {
1711            Int::I8(i) => i as i64,
1712            Int::I16(i) => i as i64,
1713            Int::I32(i) => i as i64,
1714            Int::I64(i) => i,
1715        }
1716    }
1717}
1718
1719impl FromStr for Int {
1720    type Err = Error;
1721
1722    fn from_str(s: &str) -> Result<Self, Self::Err> {
1723        i64::from_str(s).map(Self::from).map_err(Error::new)
1724    }
1725}
1726
1727fmt_debug!(Int);
1728
1729impl fmt::Display for Int {
1730    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1731        match self {
1732            Int::I8(i) => fmt::Display::fmt(i, f),
1733            Int::I16(i) => fmt::Display::fmt(i, f),
1734            Int::I32(i) => fmt::Display::fmt(i, f),
1735            Int::I64(i) => fmt::Display::fmt(i, f),
1736        }
1737    }
1738}
1739
1740/// An unsigned integer.
1741#[derive(Clone, Copy, Hash, GetSize)]
1742pub enum UInt {
1743    U8(u8),
1744    U16(u16),
1745    U32(u32),
1746    U64(u64),
1747}
1748
1749impl NumberInstance for UInt {
1750    type Abs = Self;
1751    type Exp = Float;
1752    type Log = Float;
1753    type Round = Self;
1754    type Class = UIntType;
1755
1756    fn class(&self) -> UIntType {
1757        match self {
1758            Self::U8(_) => UIntType::U8,
1759            Self::U16(_) => UIntType::U16,
1760            Self::U32(_) => UIntType::U32,
1761            Self::U64(_) => UIntType::U64,
1762        }
1763    }
1764
1765    fn into_type(self, dtype: UIntType) -> UInt {
1766        use UIntType::*;
1767        match dtype {
1768            U8 => match self {
1769                Self::U16(u) => Self::U8(u as u8),
1770                Self::U32(u) => Self::U8(u as u8),
1771                Self::U64(u) => Self::U8(u as u8),
1772                this => this,
1773            },
1774            U16 => match self {
1775                Self::U8(u) => Self::U16(u as u16),
1776                Self::U32(u) => Self::U16(u as u16),
1777                Self::U64(u) => Self::U16(u as u16),
1778                this => this,
1779            },
1780            U32 => match self {
1781                Self::U8(u) => Self::U32(u as u32),
1782                Self::U16(u) => Self::U32(u as u32),
1783                Self::U64(u) => Self::U32(u as u32),
1784                this => this,
1785            },
1786            U64 => match self {
1787                Self::U8(u) => Self::U64(u as u64),
1788                Self::U16(u) => Self::U64(u as u64),
1789                Self::U32(u) => Self::U64(u as u64),
1790                this => this,
1791            },
1792            UInt => self,
1793        }
1794    }
1795
1796    fn abs(self) -> UInt {
1797        self
1798    }
1799
1800    fn exp(self) -> Self::Exp {
1801        Float::from(self).exp()
1802    }
1803
1804    fn ln(self) -> Self::Log {
1805        Float::from(self).ln()
1806    }
1807
1808    fn log<N: NumberInstance>(self, base: N) -> Self::Log
1809    where
1810        Float: From<N>,
1811    {
1812        let this: Float = self.into();
1813        this.log(base)
1814    }
1815
1816    fn pow(self, exp: Number) -> Self {
1817        if exp < Number::from(0) {
1818            return self.class().zero();
1819        }
1820
1821        match self {
1822            Self::U8(this) => Self::U8(this.pow(exp.cast_into())),
1823            Self::U16(this) => Self::U16(this.pow(exp.cast_into())),
1824            Self::U32(this) => Self::U32(this.pow(exp.cast_into())),
1825            Self::U64(this) => Self::U64(this.pow(exp.cast_into())),
1826        }
1827    }
1828
1829    fn round(self) -> Self::Round {
1830        self
1831    }
1832}
1833
1834impl RealInstance for UInt {
1835    const ONE: Self = UInt::U8(1);
1836    const ZERO: Self = UInt::U8(0);
1837}
1838
1839impl CastFrom<Complex> for UInt {
1840    fn cast_from(c: Complex) -> UInt {
1841        use Complex::*;
1842        match c {
1843            C32(c) => Self::U32(c.re as u32),
1844            C64(c) => Self::U64(c.re as u64),
1845        }
1846    }
1847}
1848
1849impl CastFrom<Float> for UInt {
1850    fn cast_from(f: Float) -> UInt {
1851        use Float::*;
1852        match f {
1853            F32(f) => Self::U32(f as u32),
1854            F64(f) => Self::U64(f as u64),
1855        }
1856    }
1857}
1858
1859impl CastFrom<Int> for UInt {
1860    fn cast_from(i: Int) -> UInt {
1861        use Int::*;
1862        match i {
1863            I8(i) => Self::U8(i as u8),
1864            I16(i) => Self::U16(i as u16),
1865            I32(i) => Self::U32(i as u32),
1866            I64(i) => Self::U64(i as u64),
1867        }
1868    }
1869}
1870
1871impl CastFrom<UInt> for bool {
1872    fn cast_from(u: UInt) -> bool {
1873        use UInt::*;
1874        match u {
1875            U8(u) if u == 0u8 => false,
1876            U16(u) if u == 0u16 => false,
1877            U32(u) if u == 0u32 => false,
1878            U64(u) if u == 0u64 => false,
1879            _ => true,
1880        }
1881    }
1882}
1883
1884impl CastFrom<UInt> for u8 {
1885    fn cast_from(u: UInt) -> u8 {
1886        use UInt::*;
1887        match u {
1888            U8(u) => u,
1889            U16(u) => u as u8,
1890            U32(u) => u as u8,
1891            U64(u) => u as u8,
1892        }
1893    }
1894}
1895
1896impl CastFrom<UInt> for u16 {
1897    fn cast_from(u: UInt) -> u16 {
1898        use UInt::*;
1899        match u {
1900            U8(u) => u as u16,
1901            U16(u) => u,
1902            U32(u) => u as u16,
1903            U64(u) => u as u16,
1904        }
1905    }
1906}
1907
1908impl CastFrom<UInt> for u32 {
1909    fn cast_from(u: UInt) -> u32 {
1910        use UInt::*;
1911        match u {
1912            U8(u) => u as u32,
1913            U16(u) => u as u32,
1914            U32(u) => u,
1915            U64(u) => u as u32,
1916        }
1917    }
1918}
1919
1920impl Add for UInt {
1921    type Output = Self;
1922
1923    fn add(self, other: UInt) -> Self {
1924        match (self, other) {
1925            (UInt::U64(l), UInt::U64(r)) => UInt::U64(l + r),
1926            (UInt::U64(l), UInt::U32(r)) => UInt::U64(l + r as u64),
1927            (UInt::U64(l), UInt::U16(r)) => UInt::U64(l + r as u64),
1928            (UInt::U64(l), UInt::U8(r)) => UInt::U64(l + r as u64),
1929            (UInt::U32(l), UInt::U32(r)) => UInt::U32(l + r),
1930            (UInt::U32(l), UInt::U16(r)) => UInt::U32(l + r as u32),
1931            (UInt::U32(l), UInt::U8(r)) => UInt::U32(l + r as u32),
1932            (UInt::U16(l), UInt::U16(r)) => UInt::U16(l + r),
1933            (UInt::U16(l), UInt::U8(r)) => UInt::U16(l + r as u16),
1934            (UInt::U8(l), UInt::U8(r)) => UInt::U8(l + r),
1935            (l, r) => r + l,
1936        }
1937    }
1938}
1939
1940impl AddAssign for UInt {
1941    fn add_assign(&mut self, other: Self) {
1942        let sum = *self + other;
1943        *self = sum;
1944    }
1945}
1946
1947impl Rem for UInt {
1948    type Output = Self;
1949
1950    fn rem(self, other: Self) -> Self::Output {
1951        match (self, other) {
1952            (UInt::U64(l), UInt::U64(r)) => UInt::U64(l % r),
1953            (UInt::U64(l), UInt::U32(r)) => UInt::U64(l % r as u64),
1954            (UInt::U64(l), UInt::U16(r)) => UInt::U64(l % r as u64),
1955            (UInt::U64(l), UInt::U8(r)) => UInt::U64(l % r as u64),
1956            (UInt::U32(l), UInt::U32(r)) => UInt::U32(l % r),
1957            (UInt::U32(l), UInt::U16(r)) => UInt::U32(l % r as u32),
1958            (UInt::U32(l), UInt::U8(r)) => UInt::U32(l % r as u32),
1959            (UInt::U16(l), UInt::U16(r)) => UInt::U16(l % r),
1960            (UInt::U16(l), UInt::U8(r)) => UInt::U16(l % r as u16),
1961            (UInt::U8(l), UInt::U8(r)) => UInt::U8(l % r),
1962            (UInt::U8(l), UInt::U16(r)) => UInt::U16(l as u16 % r),
1963            (UInt::U8(l), UInt::U32(r)) => UInt::U32(l as u32 % r),
1964            (UInt::U8(l), UInt::U64(r)) => UInt::U64(l as u64 % r),
1965            (UInt::U16(l), r) => {
1966                let r: u64 = r.into();
1967                UInt::U16(l % r as u16)
1968            }
1969            (UInt::U32(l), r) => {
1970                let r: u64 = r.into();
1971                UInt::U32(l % r as u32)
1972            }
1973        }
1974    }
1975}
1976
1977impl Sub for UInt {
1978    type Output = Self;
1979
1980    fn sub(self, other: UInt) -> Self {
1981        match (self, other) {
1982            (UInt::U64(l), UInt::U64(r)) => UInt::U64(l - r),
1983            (UInt::U64(l), UInt::U32(r)) => UInt::U64(l - r as u64),
1984            (UInt::U64(l), UInt::U16(r)) => UInt::U64(l - r as u64),
1985            (UInt::U64(l), UInt::U8(r)) => UInt::U64(l - r as u64),
1986            (UInt::U32(l), UInt::U32(r)) => UInt::U32(l - r),
1987            (UInt::U32(l), UInt::U16(r)) => UInt::U32(l - r as u32),
1988            (UInt::U32(l), UInt::U8(r)) => UInt::U32(l - r as u32),
1989            (UInt::U16(l), UInt::U16(r)) => UInt::U16(l - r),
1990            (UInt::U16(l), UInt::U8(r)) => UInt::U16(l - r as u16),
1991            (UInt::U8(l), UInt::U8(r)) => UInt::U8(l - r),
1992            (UInt::U8(l), UInt::U16(r)) => UInt::U16(l as u16 - r),
1993            (UInt::U8(l), UInt::U32(r)) => UInt::U32(l as u32 - r),
1994            (UInt::U8(l), UInt::U64(r)) => UInt::U64(l as u64 - r),
1995            (UInt::U16(l), r) => {
1996                let r: u64 = r.into();
1997                UInt::U16(l - r as u16)
1998            }
1999            (UInt::U32(l), r) => {
2000                let r: u64 = r.into();
2001                UInt::U32(l - r as u32)
2002            }
2003        }
2004    }
2005}
2006
2007impl SubAssign for UInt {
2008    fn sub_assign(&mut self, other: Self) {
2009        let diff = *self - other;
2010        *self = diff;
2011    }
2012}
2013
2014impl Sum for UInt {
2015    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
2016        let mut sum = UIntType::UInt.zero();
2017        for i in iter {
2018            sum = sum + i;
2019        }
2020        sum
2021    }
2022}
2023
2024impl Mul for UInt {
2025    type Output = Self;
2026
2027    fn mul(self, other: UInt) -> Self {
2028        match (self, other) {
2029            (UInt::U64(l), UInt::U64(r)) => UInt::U64(l * r),
2030            (UInt::U64(l), UInt::U32(r)) => UInt::U64(l * r as u64),
2031            (UInt::U64(l), UInt::U16(r)) => UInt::U64(l * r as u64),
2032            (UInt::U64(l), UInt::U8(r)) => UInt::U64(l * r as u64),
2033            (UInt::U32(l), UInt::U32(r)) => UInt::U32(l * r),
2034            (UInt::U32(l), UInt::U16(r)) => UInt::U32(l * r as u32),
2035            (UInt::U32(l), UInt::U8(r)) => UInt::U32(l * r as u32),
2036            (UInt::U16(l), UInt::U16(r)) => UInt::U16(l * r),
2037            (UInt::U16(l), UInt::U8(r)) => UInt::U16(l * r as u16),
2038            (UInt::U8(l), UInt::U8(r)) => UInt::U8(l * r),
2039            (l, r) => r * l,
2040        }
2041    }
2042}
2043
2044impl MulAssign for UInt {
2045    fn mul_assign(&mut self, other: Self) {
2046        let product = *self * other;
2047        *self = product;
2048    }
2049}
2050
2051impl Div for UInt {
2052    type Output = Self;
2053
2054    fn div(self, other: UInt) -> Self {
2055        match (self, other) {
2056            (UInt::U64(l), UInt::U64(r)) => UInt::U64(l / r),
2057            (UInt::U64(l), UInt::U32(r)) => UInt::U64(l / r as u64),
2058            (UInt::U64(l), UInt::U16(r)) => UInt::U64(l / r as u64),
2059            (UInt::U64(l), UInt::U8(r)) => UInt::U64(l / r as u64),
2060
2061            (UInt::U32(l), UInt::U64(r)) => UInt::U64(l as u64 / r),
2062            (UInt::U32(l), UInt::U32(r)) => UInt::U32(l / r),
2063            (UInt::U32(l), UInt::U16(r)) => UInt::U32(l / r as u32),
2064            (UInt::U32(l), UInt::U8(r)) => UInt::U32(l / r as u32),
2065
2066            (UInt::U16(l), UInt::U64(r)) => UInt::U64(l as u64 / r),
2067            (UInt::U16(l), UInt::U32(r)) => UInt::U32(l as u32 / r),
2068            (UInt::U16(l), UInt::U16(r)) => UInt::U16(l / r),
2069            (UInt::U16(l), UInt::U8(r)) => UInt::U16(l / r as u16),
2070
2071            (UInt::U8(l), UInt::U64(r)) => UInt::U64(l as u64 / r),
2072            (UInt::U8(l), UInt::U32(r)) => UInt::U32(l as u32 / r),
2073            (UInt::U8(l), UInt::U16(r)) => UInt::U16(l as u16 / r),
2074            (UInt::U8(l), UInt::U8(r)) => UInt::U8(l / r),
2075        }
2076    }
2077}
2078
2079impl DivAssign for UInt {
2080    fn div_assign(&mut self, other: Self) {
2081        let div = *self / other;
2082        *self = div;
2083    }
2084}
2085
2086impl Product for UInt {
2087    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
2088        let zero = UIntType::UInt.zero();
2089        let mut product = UIntType::UInt.one();
2090
2091        for i in iter {
2092            if i == zero {
2093                return zero;
2094            }
2095
2096            product = product * i;
2097        }
2098        product
2099    }
2100}
2101
2102macro_rules! trig_uint {
2103    ($fun:ident) => {
2104        fn $fun(self) -> Self::Out {
2105            match self {
2106                Self::U8(u) => (u as f32).$fun().into(),
2107                Self::U16(u) => (u as f32).$fun().into(),
2108                Self::U32(u) => (u as f32).$fun().into(),
2109                Self::U64(u) => (u as f64).$fun().into(),
2110            }
2111        }
2112    };
2113}
2114
2115impl Trigonometry for UInt {
2116    type Out = Float;
2117
2118    trig_uint! {asin}
2119    trig_uint! {sin}
2120    trig_uint! {sinh}
2121    trig_uint! {asinh}
2122
2123    trig_uint! {acos}
2124    trig_uint! {cos}
2125    trig_uint! {cosh}
2126    trig_uint! {acosh}
2127
2128    trig_uint! {atan}
2129    trig_uint! {tan}
2130    trig_uint! {tanh}
2131    trig_uint! {atanh}
2132}
2133
2134impl Eq for UInt {}
2135
2136impl PartialEq for UInt {
2137    fn eq(&self, other: &UInt) -> bool {
2138        match (self, other) {
2139            (Self::U8(l), Self::U8(r)) => l.eq(r),
2140            (Self::U16(l), Self::U16(r)) => l.eq(r),
2141            (Self::U32(l), Self::U32(r)) => l.eq(r),
2142            (Self::U64(l), Self::U64(r)) => l.eq(r),
2143            (l, r) => u64::from(*l).eq(&u64::from(*r)),
2144        }
2145    }
2146}
2147
2148impl PartialOrd for UInt {
2149    fn partial_cmp(&self, other: &UInt) -> Option<Ordering> {
2150        Some(self.cmp(other))
2151    }
2152}
2153
2154impl Ord for UInt {
2155    fn cmp(&self, other: &UInt) -> Ordering {
2156        match (self, other) {
2157            (Self::U8(l), Self::U8(r)) => l.cmp(r),
2158            (Self::U16(l), Self::U16(r)) => l.cmp(r),
2159            (Self::U32(l), Self::U32(r)) => l.cmp(r),
2160            (Self::U64(l), Self::U64(r)) => l.cmp(r),
2161            (l, r) => u64::from(*l).cmp(&u64::from(*r)),
2162        }
2163    }
2164}
2165
2166impl Default for UInt {
2167    fn default() -> UInt {
2168        UInt::U8(u8::default())
2169    }
2170}
2171
2172impl From<Boolean> for UInt {
2173    fn from(b: Boolean) -> UInt {
2174        match b {
2175            Boolean(true) => UInt::U8(1),
2176            Boolean(false) => UInt::U8(0),
2177        }
2178    }
2179}
2180
2181impl From<u8> for UInt {
2182    fn from(u: u8) -> UInt {
2183        UInt::U8(u)
2184    }
2185}
2186
2187impl From<u16> for UInt {
2188    fn from(u: u16) -> UInt {
2189        UInt::U16(u)
2190    }
2191}
2192
2193impl From<u32> for UInt {
2194    fn from(u: u32) -> UInt {
2195        UInt::U32(u)
2196    }
2197}
2198
2199impl From<u64> for UInt {
2200    fn from(u: u64) -> UInt {
2201        UInt::U64(u)
2202    }
2203}
2204
2205impl From<UInt> for u64 {
2206    fn from(u: UInt) -> u64 {
2207        match u {
2208            UInt::U64(u) => u,
2209            UInt::U32(u) => u as u64,
2210            UInt::U16(u) => u as u64,
2211            UInt::U8(u) => u as u64,
2212        }
2213    }
2214}
2215
2216impl From<UInt> for usize {
2217    fn from(u: UInt) -> usize {
2218        match u {
2219            UInt::U64(u) => u as usize,
2220            UInt::U32(u) => u as usize,
2221            UInt::U16(u) => u as usize,
2222            UInt::U8(u) => u as usize,
2223        }
2224    }
2225}
2226
2227impl FromStr for UInt {
2228    type Err = Error;
2229
2230    fn from_str(s: &str) -> Result<Self, Self::Err> {
2231        u64::from_str(s).map(Self::from).map_err(Error::new)
2232    }
2233}
2234
2235fmt_debug!(UInt);
2236
2237impl fmt::Display for UInt {
2238    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2239        match self {
2240            UInt::U8(u) => fmt::Display::fmt(u, f),
2241            UInt::U16(u) => fmt::Display::fmt(u, f),
2242            UInt::U32(u) => fmt::Display::fmt(u, f),
2243            UInt::U64(u) => fmt::Display::fmt(u, f),
2244        }
2245    }
2246}