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
use crate::{
    Elementary::{self, *},
    Error,
};

#[derive(Debug, PartialEq)]
pub enum Category {
    Exponential,
    Polynomial,
    Trigonometric,
    Constant,
    ClusterFuck,
}

impl Elementary {
    pub fn classify(&self) -> Result<Category, Error> {
        if self.is_constant() {
            Ok(Category::Constant)
        } else if self.is_exponential()? {
            Ok(Category::Exponential)
        } else if self.is_polynomial()? {
            Ok(Category::Polynomial)
        } else if self.is_trig() {
            Ok(Category::Trigonometric)
        } else {
            Ok(Category::ClusterFuck)
        }
    }

    // checks if the provided function is constant, that is, it contains no independent variable
    // of the type f(x) = C
    pub fn is_constant(&self) -> bool {
        match self {
            Sin(func) => func.is_constant(),
            Cos(func) => func.is_constant(),
            Tan(func) => func.is_constant(),

            Sec(func) => func.is_constant(),
            Csc(func) => func.is_constant(),
            Cot(func) => func.is_constant(),

            Asin(func) => func.is_constant(),
            Acos(func) => func.is_constant(),
            Atan(func) => func.is_constant(),

            Sinh(func) => func.is_constant(),
            Cosh(func) => func.is_constant(),
            Tanh(func) => func.is_constant(),

            // for the operations, both of their functions must be constant for the whole function
            // to be considered constant
            Add(func1, func2) => func1.is_constant() && func2.is_constant(),
            Sub(func1, func2) => func1.is_constant() && func2.is_constant(),
            Mul(func1, func2) => func1.is_constant() && func2.is_constant(),
            Div(func1, func2) => func1.is_constant() && func2.is_constant(),
            Pow(func1, func2) => func1.is_constant() && func2.is_constant(),
            Log(func1, func2) => func1.is_constant() && func2.is_constant(),

            Factorial(func) => func.is_constant(),

            Gamma(func) => func.is_constant(),
            Polygamma(func, _) => func.is_constant(),

            Abs(func) => func.is_constant(),

            Con(_) => true,
            X => false,
        }
    }

    // returns true if the function is of type f(x) = Cx
    fn is_linear(&self) -> bool {
        if let Mul(func1, func2) = self {
            if (func1.is_constant() && (func2.is_linear() || func2.clone() == X.into()))
                || (func2.is_constant() && (func1.is_linear() || func1.clone() == X.into()))
            {
                return true;
            }
        }
        false
    }

    // returns true if the function is a constant digit f(x) = C, C ∈ ℤ
    pub fn is_digit(&self) -> Result<bool, Error> {
        if let Con(numb) = self {
            if numb.fract() == 0.0 {
                return Ok(true);
            }
        }
        Ok(false)
    }

    // returns true if the function is of type f(x) = a^(cx)
    pub fn is_exponential(&self) -> Result<bool, Error> {
        if let Pow(base, exp) = self {
            if base.is_constant() && exp.is_linear() {
                return Ok(true);
            }
        }
        if let Mul(func1, func2) = self {
            if (func1.is_exponential()? && func2.is_constant())
                || (func1.is_constant() && func2.is_exponential()?)
            {
                return Ok(true);
            }
        }

        Ok(false)
    }

    // returns true if the function is of type f(x) = ax^n + bx^(n-1) + cx^(n-2) + ...
    // (a, b, c, ... ∈ ℝ)
    fn is_polynomial(&self) -> Result<bool, Error> {
        if let Pow(base, exp) = self {
            if base.clone().is_polynomial()? && exp.is_digit()? {
                return Ok(true);
            }
        } else if self.clone() == X.into() {
            return Ok(true);
        } else if let Con(_) = self {
            return Ok(true);
        } else if let Mul(func1, func2) = self {
            if func1.is_polynomial()? && func2.is_polynomial()? {
                return Ok(true);
            }
        } else if let Add(func1, func2) = self {
            if func1.is_polynomial()? && func2.is_polynomial()? {
                return Ok(true);
            }
        } else if let Sub(func1, func2) = self {
            if func1.is_polynomial()? && func2.is_polynomial()? {
                return Ok(true);
            }
        } else if let Div(func1, func2) = self {
            if func1.is_polynomial()? && func2.is_polynomial()? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    // returns true if the function is of type trig * trig, constant * trig, trig^constant, or
    // log constant (trig)
    fn is_trig(&self) -> bool {
        match self {
            Sin(_) => true,
            Cos(_) => true,
            Tan(_) => true,

            Sec(_) => true,
            Csc(_) => true,
            Cot(_) => true,

            Asin(_) => true,
            Acos(_) => true,
            Atan(_) => true,

            Sinh(_) => true,
            Cosh(_) => true,
            Tanh(_) => true,

            Add(func1, func2) => func1.is_trig() && func2.is_trig(),
            Sub(func1, func2) => func1.is_trig() && func2.is_trig(),
            Mul(func1, func2) => {
                (func1.is_trig() && func2.is_trig()) // trig * trig
                    || (func1.is_trig() && func2.is_constant()) // trig * constant
                    || (func2.is_trig() && func1.is_constant()) // constant * trig
            }
            Div(func1, func2) => {
                (func1.is_trig() && func2.is_trig())
                    || (func1.is_trig() && func2.is_constant())
                    || (func2.is_trig() && func1.is_constant())
            }
            Pow(func1, func2) => func1.is_trig() && func2.is_constant(), // trig^constant
            Log(func1, func2) => func1.is_constant() && func2.is_trig(), // log constant (trig)

            Factorial(_) => false,

            Gamma(_) => false,
            Polygamma(_, _) => false,

            Abs(_) => false,
            Con(_) => false,
            X => false,
        }
    }
}