use mathhook_core::algebra::equation_analyzer::{
EquationAnalyzer, EquationType, SmartEquationSolver,
};
use mathhook_core::{symbol, Expression};
#[test]
fn test_numerical_detection_quintic() {
let x = symbol!(x);
let quintic = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(5)),
Expression::mul(vec![Expression::integer(-1), Expression::symbol(x.clone())]),
Expression::integer(-1),
]);
assert_eq!(
EquationAnalyzer::analyze(&quintic, &x),
EquationType::Numerical,
"Quintic polynomial should be detected as numerical equation"
);
}
#[test]
fn test_numerical_detection_transcendental_mixed() {
let x = symbol!(x);
let equation = Expression::add(vec![
Expression::function("cos", vec![Expression::symbol(x.clone())]),
Expression::mul(vec![Expression::integer(-1), Expression::symbol(x.clone())]),
]);
assert_eq!(
EquationAnalyzer::analyze(&equation, &x),
EquationType::Numerical,
"Mixed transcendental-polynomial should be detected as numerical"
);
}
#[test]
fn test_numerical_detection_high_degree() {
let x = symbol!(x);
let high_degree = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(6)),
Expression::symbol(x.clone()),
Expression::integer(-10),
]);
assert_eq!(
EquationAnalyzer::analyze(&high_degree, &x),
EquationType::Numerical,
"Polynomial degree > 4 should be detected as numerical"
);
}
#[test]
fn test_numerical_solver_routing() {
let x = symbol!(x);
let quintic = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(5)),
Expression::mul(vec![Expression::integer(-1), Expression::symbol(x.clone())]),
Expression::integer(-1),
]);
let solver = SmartEquationSolver::new();
let (_result, explanation) = solver.solve_with_equation(&quintic, &x);
assert!(
explanation
.steps
.iter()
.any(|s| s.description.contains("numerical methods")
|| s.description.contains("Newton-Raphson")),
"Should explain numerical method requirement"
);
assert!(
explanation
.steps
.iter()
.any(|s| s.title == "Equation Analysis"),
"Should include equation analysis step"
);
assert!(
explanation
.steps
.iter()
.any(|s| s.title == "Solver Selection"),
"Should include solver selection step"
);
}
#[test]
fn test_numerical_vs_symbolic_boundary() {
let x = symbol!(x);
let quartic = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(4)),
Expression::mul(vec![Expression::integer(-1), Expression::symbol(x.clone())]),
Expression::integer(-1),
]);
assert_eq!(
EquationAnalyzer::analyze(&quartic, &x),
EquationType::Quartic,
"Quartic should use symbolic solver"
);
let quintic = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(5)),
Expression::mul(vec![Expression::integer(-1), Expression::symbol(x.clone())]),
Expression::integer(-1),
]);
assert_eq!(
EquationAnalyzer::analyze(&quintic, &x),
EquationType::Numerical,
"Quintic should use numerical solver"
);
}
#[test]
fn test_transcendental_equation_detection() {
let x = symbol!(x);
let pure_trig = Expression::function("sin", vec![Expression::symbol(x.clone())]);
assert_eq!(
EquationAnalyzer::analyze(&pure_trig, &x),
EquationType::Transcendental,
"Pure sin(x) should be transcendental"
);
let mixed = Expression::add(vec![
Expression::function("sin", vec![Expression::symbol(x.clone())]),
Expression::symbol(x.clone()),
]);
assert_eq!(
EquationAnalyzer::analyze(&mixed, &x),
EquationType::Numerical,
"sin(x) + x should be numerical"
);
}
#[test]
fn test_numerical_solver_educational_output() {
let x = symbol!(x);
let equation = Expression::add(vec![
Expression::pow(Expression::symbol(x.clone()), Expression::integer(5)),
Expression::integer(-32),
]);
let solver = SmartEquationSolver::new();
let (_result, explanation) = solver.solve_with_equation(&equation, &x);
let step_titles: Vec<&str> = explanation.steps.iter().map(|s| s.title.as_str()).collect();
assert!(
step_titles.contains(&"Equation Analysis"),
"Should include equation analysis"
);
assert!(
step_titles.contains(&"Solver Selection"),
"Should include solver selection"
);
assert!(
explanation.steps.iter().any(
|s| s.description.contains("numerical") || s.description.contains("Newton-Raphson")
),
"Should explain numerical method approach"
);
}