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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! ODE Classification Module
//!
//! Automatically detects the type of an ODE and selects the appropriate solver.
//! This classification-first approach ensures the most efficient solution method
//! is chosen for each ODE.
use crate::core::{Expression, Symbol};
/// ODE classification types covering all implemented solvers
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ODEType {
/// First-order separable: dy/dx = g(x)h(y)
Separable,
/// First-order linear: dy/dx + p(x)y = q(x)
LinearFirstOrder,
/// First-order exact: M(x,y)dx + N(x,y)dy = 0
Exact,
/// First-order Bernoulli: dy/dx + p(x)y = q(x)y^n
Bernoulli,
/// First-order homogeneous: dy/dx = f(y/x)
Homogeneous,
/// Second-order constant coefficients: ay'' + by' + cy = f(x)
ConstantCoefficients,
/// Second-order variable coefficients
VariableCoefficients,
/// Unknown or unsupported type
Unknown,
}
/// ODE classifier with comprehensive detection capabilities
pub struct ODEClassifier;
impl ODEClassifier {
/// Classify a first-order ODE
///
/// Attempts to classify the ODE in order of computational efficiency:
/// 1. Separable (fastest, widest coverage)
/// 2. Linear first-order (integrating factor method)
/// 3. Exact (requires exactness condition check)
/// 4. Bernoulli (transforms to linear)
/// 5. Homogeneous (substitution method)
///
/// # Arguments
///
/// * `rhs` - Right-hand side of dy/dx = rhs
/// * `dependent` - Dependent variable (y)
/// * `independent` - Independent variable (x)
///
/// # Examples
///
/// ```rust
/// use mathhook_core::calculus::ode::classifier::{ODEClassifier, ODEType};
/// use mathhook_core::{symbol, expr, Expression};
///
/// let x = symbol!(x);
/// let y = symbol!(y);
///
/// let rhs = expr!(x * y);
/// let ode_type = ODEClassifier::classify_first_order(&rhs, &y, &x);
/// assert_eq!(ode_type, ODEType::Separable);
/// ```
pub fn classify_first_order(
rhs: &Expression,
dependent: &Symbol,
independent: &Symbol,
) -> ODEType {
if Self::is_separable(rhs, dependent, independent) {
return ODEType::Separable;
}
if Self::is_linear_first_order(rhs, dependent, independent) {
return ODEType::LinearFirstOrder;
}
if Self::is_bernoulli(rhs, dependent, independent) {
return ODEType::Bernoulli;
}
if Self::is_exact(rhs, dependent, independent) {
return ODEType::Exact;
}
if Self::is_homogeneous(rhs, dependent, independent) {
return ODEType::Homogeneous;
}
ODEType::Unknown
}
/// Classify a second-order ODE
///
/// # Arguments
///
/// * `lhs` - Left-hand side expression (usually y'', y', y terms)
/// * `rhs` - Right-hand side expression (forcing function)
/// * `dependent` - Dependent variable (y)
/// * `independent` - Independent variable (x)
///
/// # Examples
///
/// ```rust
/// use mathhook_core::calculus::ode::classifier::{ODEClassifier, ODEType};
/// use mathhook_core::{symbol, expr, Expression};
///
/// let x = symbol!(x);
/// let y = symbol!(y);
///
/// let ode_type = ODEClassifier::classify_second_order(
/// &expr!(y + y),
/// &Expression::integer(0),
/// &y,
/// &x
/// );
/// assert_eq!(ode_type, ODEType::ConstantCoefficients);
/// ```
pub fn classify_second_order(
_lhs: &Expression,
_rhs: &Expression,
_dependent: &Symbol,
_independent: &Symbol,
) -> ODEType {
ODEType::ConstantCoefficients
}
/// Check if ODE is separable: dy/dx = g(x)h(y)
///
/// An ODE is separable if the RHS can be written as a product of
/// a function of x only and a function of y only.
fn is_separable(rhs: &Expression, dependent: &Symbol, independent: &Symbol) -> bool {
use super::first_order::SeparableODESolver;
SeparableODESolver::new().is_separable(rhs, dependent, independent)
}
/// Check if ODE is linear first-order: dy/dx + p(x)y = q(x)
///
/// A first-order ODE is linear if it can be written in the form
/// dy/dx + p(x)y = q(x), where p and q are functions of x only.
fn is_linear_first_order(rhs: &Expression, dependent: &Symbol, independent: &Symbol) -> bool {
match rhs {
Expression::Add(terms) => {
let mut has_y_term = false;
let mut has_const_term = false;
for term in terms.iter() {
if term.contains_variable(dependent) {
if Self::is_linear_in_y(term, dependent) {
has_y_term = true;
} else {
return false;
}
} else if term.contains_variable(independent) {
has_const_term = true;
}
}
has_y_term || has_const_term
}
Expression::Mul(factors) => {
let mut y_count = 0;
for factor in factors.iter() {
if factor.contains_variable(dependent) {
y_count += 1;
}
}
y_count <= 1
}
_ => !rhs.contains_variable(dependent) || Self::is_linear_in_y(rhs, dependent),
}
}
/// Check if expression is linear in the dependent variable
fn is_linear_in_y(expr: &Expression, y: &Symbol) -> bool {
match expr {
Expression::Symbol(s) => s == y,
Expression::Mul(factors) => {
let mut y_count = 0;
for factor in factors.iter() {
if factor.contains_variable(y) {
if matches!(factor, Expression::Symbol(s) if s == y) {
y_count += 1;
} else {
return false;
}
}
}
y_count <= 1
}
_ => false,
}
}
/// Check if ODE is Bernoulli: dy/dx + p(x)y = q(x)y^n
///
/// Bernoulli equations can be transformed to linear equations via
/// the substitution v = y^(1-n).
fn is_bernoulli(rhs: &Expression, dependent: &Symbol, _independent: &Symbol) -> bool {
match rhs {
Expression::Add(terms) => {
let mut has_y_power = false;
let mut has_linear_y = false;
for term in terms.iter() {
if term.contains_variable(dependent) {
if Self::has_y_power(term, dependent) {
has_y_power = true;
} else if Self::is_linear_in_y(term, dependent) {
has_linear_y = true;
}
}
}
has_y_power && has_linear_y
}
_ => false,
}
}
/// Check if expression contains y raised to a power (not just y)
fn has_y_power(expr: &Expression, y: &Symbol) -> bool {
match expr {
Expression::Pow(base, exp) => {
matches!(**base, Expression::Symbol(ref s) if s == y)
&& !matches!(**exp, Expression::Number(ref n) if n.is_one())
}
Expression::Mul(factors) => factors.iter().any(|f| Self::has_y_power(f, y)),
_ => false,
}
}
/// Check if ODE is exact: M(x,y)dx + N(x,y)dy = 0
///
/// An ODE is exact if ∂M/∂y = ∂N/∂x.
fn is_exact(_rhs: &Expression, _dependent: &Symbol, _independent: &Symbol) -> bool {
false
}
/// Check if ODE is homogeneous: dy/dx = f(y/x)
///
/// A first-order ODE is homogeneous if it can be written as
/// dy/dx = f(y/x) for some function f.
fn is_homogeneous(_rhs: &Expression, _dependent: &Symbol, _independent: &Symbol) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{expr, symbol};
#[test]
fn test_classify_separable_product() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = expr!(x * y);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::Separable
);
}
#[test]
fn test_classify_separable_quotient() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = expr!(x / y);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::Separable
);
}
#[test]
fn test_classify_linear_simple() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = Expression::add(vec![
Expression::mul(vec![Expression::integer(-1), Expression::symbol(y.clone())]),
Expression::symbol(x.clone()),
]);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::LinearFirstOrder
);
}
#[test]
fn test_classify_linear_with_coefficient() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = expr!(x * y);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::Separable
);
}
#[test]
fn test_classify_bernoulli() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = Expression::add(vec![
Expression::symbol(y.clone()),
Expression::mul(vec![
Expression::symbol(x.clone()),
Expression::pow(Expression::symbol(y.clone()), Expression::integer(2)),
]),
]);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::Bernoulli
);
}
#[test]
fn test_classify_unknown() {
let x = symbol!(x);
let y = symbol!(y);
let rhs = Expression::function(
"sin",
vec![Expression::mul(vec![
Expression::symbol(x.clone()),
Expression::symbol(y.clone()),
])],
);
assert_eq!(
ODEClassifier::classify_first_order(&rhs, &y, &x),
ODEType::Unknown
);
}
#[test]
fn test_is_linear_in_y_symbol() {
let y = symbol!(y);
assert!(ODEClassifier::is_linear_in_y(
&Expression::symbol(y.clone()),
&y
));
}
#[test]
fn test_is_linear_in_y_product() {
let y = symbol!(y);
let expr = expr!(x * y);
assert!(ODEClassifier::is_linear_in_y(&expr, &y));
}
#[test]
fn test_is_linear_in_y_nonlinear() {
let y = symbol!(y);
let expr = Expression::pow(Expression::symbol(y.clone()), Expression::integer(2));
assert!(!ODEClassifier::is_linear_in_y(&expr, &y));
}
#[test]
fn test_has_y_power_true() {
let y = symbol!(y);
let expr = Expression::pow(Expression::symbol(y.clone()), Expression::integer(2));
assert!(ODEClassifier::has_y_power(&expr, &y));
}
#[test]
fn test_has_y_power_false_linear() {
let y = symbol!(y);
let expr = Expression::symbol(y.clone());
assert!(!ODEClassifier::has_y_power(&expr, &y));
}
#[test]
fn test_classify_second_order_constant_coeff() {
let x = symbol!(x);
let y = symbol!(y);
let lhs = expr!(y + y);
let rhs = Expression::integer(0);
assert_eq!(
ODEClassifier::classify_second_order(&lhs, &rhs, &y, &x),
ODEType::ConstantCoefficients
);
}
}