mathhook_core/core/expression/classification.rs
1//! Expression Classification
2//!
3//! Classification of expression types for intelligent algorithm routing.
4//! This provides a foundation for smart dispatch in polynomial operations,
5//! simplification strategies, and solver selection.
6
7use crate::core::Symbol;
8
9/// Classification of expression type for algorithm routing
10///
11/// Used to select optimal algorithms based on expression structure.
12/// The classification determines which specialized algorithms to use
13/// for operations like GCD, factorization, and simplification.
14///
15/// # Examples
16///
17/// ```rust
18/// use mathhook_core::core::expression::classification::ExpressionClass;
19/// use mathhook_core::core::polynomial::PolynomialClassification;
20/// use mathhook_core::{expr, symbol};
21///
22/// let x = symbol!(x);
23/// let poly = expr!(x ^ 2);
24///
25/// match poly.classify() {
26/// ExpressionClass::UnivariatePolynomial { var, degree } => {
27/// assert_eq!(degree, 2);
28/// }
29/// _ => panic!("Expected univariate polynomial"),
30/// }
31/// ```
32#[derive(Debug, Clone, PartialEq)]
33pub enum ExpressionClass {
34 /// Integer constant
35 ///
36 /// Pure integer values like 0, 1, -5, 42.
37 /// Fast-path for integer arithmetic.
38 Integer,
39
40 /// Rational number
41 ///
42 /// Fractions like 1/2, 3/4, -7/11.
43 /// Uses rational arithmetic algorithms.
44 Rational,
45
46 /// Univariate polynomial in one variable
47 ///
48 /// Polynomials like x^2 + 2x + 1, 3y^5 - y.
49 /// Enables specialized univariate algorithms (Euclidean GCD, etc.).
50 UnivariatePolynomial {
51 /// The variable of the polynomial
52 var: Symbol,
53 /// The degree of the polynomial
54 degree: i64,
55 },
56
57 /// Multivariate polynomial in multiple variables
58 ///
59 /// Polynomials like x^2 + xy + y^2, x*y*z + 1.
60 /// Uses multivariate algorithms (Buchberger, etc.).
61 MultivariatePolynomial {
62 /// All variables in the polynomial
63 vars: Vec<Symbol>,
64 /// Total degree (sum of degrees in all variables)
65 total_degree: i64,
66 },
67
68 /// Rational function (ratio of polynomials)
69 ///
70 /// Expressions like (x+1)/(x-1), 1/(x^2+1).
71 /// Requires special handling for simplification.
72 RationalFunction,
73
74 /// Contains transcendental functions (sin, cos, exp, log)
75 ///
76 /// Expressions involving sin, cos, tan, exp, log, etc.
77 /// Limited algebraic simplification possible.
78 Transcendental,
79
80 /// Symbolic expression that doesn't fit other categories
81 ///
82 /// General symbolic expressions that don't match
83 /// any of the more specific classifications.
84 Symbolic,
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn test_expression_class_eq() {
93 assert_eq!(ExpressionClass::Integer, ExpressionClass::Integer);
94 assert_eq!(ExpressionClass::Rational, ExpressionClass::Rational);
95 assert_ne!(ExpressionClass::Integer, ExpressionClass::Rational);
96 }
97
98 #[test]
99 fn test_expression_class_clone() {
100 let class = ExpressionClass::UnivariatePolynomial {
101 var: crate::symbol!(x),
102 degree: 3,
103 };
104 let cloned = class.clone();
105 assert_eq!(class, cloned);
106 }
107
108 #[test]
109 fn test_expression_class_debug() {
110 let class = ExpressionClass::Integer;
111 let debug_str = format!("{:?}", class);
112 assert!(debug_str.contains("Integer"));
113 }
114}