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
//! Variable, function, and constant collection — `impl Expression` methods.
use crate::ast::{Expression, MathConstant};
use std::collections::HashSet;
use super::collect_consts::cc_core;
use super::collect_fns::cf_core;
use super::collect_vars::{cv_contains, cv_core};
impl Expression {
/// Finds all unique variable names in the expression.
///
/// Recursively traverses the AST and collects all `Variable` nodes,
/// returning their names as a set. Index variables from summations
/// and products are also included.
///
/// # Examples
///
/// ```
/// use mathlex::ast::{ExprKind, Expression, BinaryOp};
///
/// // x + y
/// let expr: Expression = ExprKind::Binary {
/// op: BinaryOp::Add,
/// left: Box::new(Expression::variable("x".to_string())),
/// right: Box::new(Expression::variable("y".to_string())),
/// }.into();
///
/// let vars = expr.find_variables();
/// assert_eq!(vars.len(), 2);
/// assert!(vars.contains("x"));
/// assert!(vars.contains("y"));
/// ```
pub fn find_variables(&self) -> HashSet<String> {
let mut variables = HashSet::new();
self.collect_variables(&mut variables);
variables
}
pub(crate) fn collect_variables(&self, variables: &mut HashSet<String>) {
cv_core(self, variables);
}
/// Finds all unique function names in the expression.
///
/// Recursively traverses the AST and collects all `Function` node names,
/// returning them as a set.
///
/// # Examples
///
/// ```
/// use mathlex::ast::{ExprKind, Expression, BinaryOp};
///
/// // sin(x) + cos(y)
/// let expr: Expression = ExprKind::Binary {
/// op: BinaryOp::Add,
/// left: Box::new(ExprKind::Function {
/// name: "sin".to_string(),
/// args: vec![Expression::variable("x".to_string())],
/// }.into()),
/// right: Box::new(ExprKind::Function {
/// name: "cos".to_string(),
/// args: vec![Expression::variable("y".to_string())],
/// }.into()),
/// }.into();
///
/// let funcs = expr.find_functions();
/// assert_eq!(funcs.len(), 2);
/// assert!(funcs.contains("sin"));
/// assert!(funcs.contains("cos"));
/// ```
pub fn find_functions(&self) -> HashSet<String> {
let mut functions = HashSet::new();
self.collect_functions(&mut functions);
functions
}
pub(crate) fn collect_functions(&self, functions: &mut HashSet<String>) {
cf_core(self, functions);
}
/// Finds all unique mathematical constants in the expression.
///
/// Recursively traverses the AST and collects all `Constant` nodes,
/// returning them as a set.
///
/// # Examples
///
/// ```
/// use mathlex::ast::{ExprKind, Expression, MathConstant, BinaryOp};
///
/// // 2 * π + e
/// let expr: Expression = ExprKind::Binary {
/// op: BinaryOp::Add,
/// left: Box::new(ExprKind::Binary {
/// op: BinaryOp::Mul,
/// left: Box::new(Expression::integer(2)),
/// right: Box::new(Expression::constant(MathConstant::Pi)),
/// }.into()),
/// right: Box::new(Expression::constant(MathConstant::E)),
/// }.into();
///
/// let consts = expr.find_constants();
/// assert_eq!(consts.len(), 2);
/// assert!(consts.contains(&MathConstant::Pi));
/// assert!(consts.contains(&MathConstant::E));
/// ```
pub fn find_constants(&self) -> HashSet<MathConstant> {
let mut constants = HashSet::new();
self.collect_constants(&mut constants);
constants
}
pub(crate) fn collect_constants(&self, constants: &mut HashSet<MathConstant>) {
cc_core(self, constants);
}
/// Returns `true` if this expression contains a variable with the given name.
///
/// Uses short-circuit evaluation: returns as soon as the variable is found,
/// making it more efficient than `find_variables().contains(name)` for
/// membership queries.
///
/// # Examples
///
/// ```
/// use mathlex::ast::{ExprKind, Expression, BinaryOp};
///
/// // x + y
/// let expr: Expression = ExprKind::Binary {
/// op: BinaryOp::Add,
/// left: Box::new(Expression::variable("x".to_string())),
/// right: Box::new(Expression::variable("y".to_string())),
/// }.into();
///
/// assert!(expr.contains_variable("x"));
/// assert!(expr.contains_variable("y"));
/// assert!(!expr.contains_variable("z"));
/// ```
pub fn contains_variable(&self, name: &str) -> bool {
cv_contains(self, name)
}
}