ruut_functions/
traits.rs

1use crate::{FType, Func, F1D, F2D, F3D};
2
3impl PartialEq<i32> for Func {
4    fn eq(&self, other: &i32) -> bool {
5        if let Func::Num(val) = self {
6            return val == other;
7        }
8
9        false
10    }
11}
12impl PartialOrd<i32> for Func {
13    fn partial_cmp(&self, other: &i32) -> Option<std::cmp::Ordering> {
14        if let Func::Num(val) = self {
15            Some(val.cmp(other))
16        } else {
17            None
18        }
19    }
20}
21
22impl Eq for Func {}
23impl PartialOrd for Func {
24    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
25        Some(self.cmp(other))
26    }
27}
28impl Ord for Func {
29    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
30        if let (Func::Pow(base1, exp1), Func::Pow(base2, exp2)) = (self, other) {
31            if let Func::Num(e1) = **exp1 {
32                if let Func::Num(e2) = **exp2 {
33                    if e1 != e2 {
34                        return e2.cmp(&e1);
35                    } else {
36                        return base1.cmp(base2);
37                    }
38                }
39            }
40        }
41
42        func_order(self).cmp(&func_order(other))
43    }
44}
45
46fn func_order(func: &Func) -> u32 {
47    match func {
48        Func::Num(_) => 0,
49        Func::PI => 1,
50        Func::E => 2,
51        Func::Param(..) => 3,
52        Func::Var(char) => char.to_ascii_lowercase() as u32,
53        Func::Mul(_) => 123,
54        Func::Add(_) => 124,
55        Func::S(kind, _) => match kind {
56            FType::Abs => 126,
57            FType::Ln => 127,
58            FType::Sin => 128,
59            FType::Cos => 129,
60            FType::Tan => 130,
61            FType::Cot => 131,
62            FType::Sec => 132,
63            FType::Csc => 133,
64            FType::ASin => 134,
65            FType::ACos => 135,
66            FType::ATan => 136,
67            FType::Sinh => 137,
68            FType::Cosh => 138,
69            FType::Tanh => 139,
70            FType::Coth => 140,
71            FType::Sech => 141,
72            FType::Csch => 142,
73            FType::ASinh => 143,
74            FType::ACosh => 144,
75            FType::ATanh => 145,
76        },
77        Func::Pow(_, exp) => {
78            if let Func::Num(val) = **exp {
79                if val < 0 {
80                    147
81                } else {
82                    146
83                }
84            } else {
85                146
86            }
87        }
88    }
89}
90
91impl std::iter::Sum for Func {
92    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
93        iter.fold(Func::Num(0), |acc, func| acc + func)
94    }
95}
96
97impl std::iter::Product for Func {
98    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
99        iter.fold(Func::Num(1), |acc, func| acc * func)
100    }
101}
102
103impl From<F1D> for F3D {
104    fn from(value: F1D) -> Self {
105        F3D(value.0)
106    }
107}
108impl From<F2D> for F3D {
109    fn from(value: F2D) -> Self {
110        F3D(value.0)
111    }
112}
113impl From<F1D> for F2D {
114    fn from(value: F1D) -> Self {
115        F2D(value.0)
116    }
117}