mathhook_core/core/polynomial/
educational.rs

1//! Polynomial Educational Module
2//!
3//! Provides step-by-step explanations for polynomial operations.
4//! Integrates with the main educational framework to provide
5//! detailed, pedagogically sound explanations for:
6//!
7//! - Polynomial division (long division algorithm)
8//! - GCD computation (Euclidean algorithm)
9//! - Factorization steps
10//! - Resultant and discriminant computation
11//!
12//! # Architecture
13//!
14//! This module provides the `PolynomialEducational` trait which can be
15//! implemented by polynomial expressions to generate educational explanations.
16//! The explanations follow the existing `Step` and `StepByStepExplanation`
17//! structures from the main educational module.
18
19use crate::core::{Expression, Symbol};
20use crate::educational::step_by_step::{Step, StepByStepExplanation};
21
22pub mod division;
23pub mod factorization;
24pub mod gcd;
25
26mod impl_trait;
27
28#[cfg(test)]
29mod tests;
30
31/// Trait for generating educational explanations for polynomial operations
32///
33/// This trait provides methods to generate step-by-step explanations
34/// for various polynomial algorithms, suitable for educational purposes.
35pub trait PolynomialEducational {
36    /// Generate step-by-step explanation for polynomial division
37    ///
38    /// Explains the polynomial long division algorithm step by step,
39    /// showing how the quotient and remainder are computed.
40    ///
41    /// # Arguments
42    ///
43    /// * `divisor` - The polynomial to divide by
44    /// * `var` - The variable to treat as the polynomial indeterminate
45    ///
46    /// # Returns
47    ///
48    /// A `StepByStepExplanation` detailing each step of the division
49    fn explain_poly_division(&self, divisor: &Expression, var: &Symbol) -> StepByStepExplanation;
50
51    /// Generate step-by-step explanation for GCD computation
52    ///
53    /// Explains the Euclidean algorithm for polynomial GCD,
54    /// showing each division and remainder step.
55    ///
56    /// # Arguments
57    ///
58    /// * `other` - The other polynomial
59    ///
60    /// # Returns
61    ///
62    /// A `StepByStepExplanation` detailing each step of GCD computation
63    fn explain_poly_gcd(&self, other: &Expression) -> StepByStepExplanation;
64
65    /// Generate step-by-step explanation for factorization
66    ///
67    /// Explains the factorization process, including content extraction
68    /// and common factor identification.
69    ///
70    /// # Arguments
71    ///
72    /// * `var` - The variable to factor with respect to
73    ///
74    /// # Returns
75    ///
76    /// A `StepByStepExplanation` detailing the factorization steps
77    fn explain_poly_factorization(&self, var: &Symbol) -> StepByStepExplanation;
78}
79
80/// Helper function to create a StepByStepExplanation from components
81pub(crate) fn create_explanation(
82    initial: Expression,
83    final_expr: Expression,
84    steps: Vec<Step>,
85) -> StepByStepExplanation {
86    let total_steps = steps.len().saturating_sub(2);
87    let rules_used: Vec<String> = steps.iter().map(|s| s.rule_applied.clone()).collect();
88
89    StepByStepExplanation {
90        initial_expression: initial,
91        final_expression: final_expr,
92        steps,
93        total_steps,
94        rules_used,
95    }
96}