mathhook-core 0.2.0

Core mathematical engine for MathHook - expressions, algebra, and solving
Documentation
//! Chebyshev Polynomial Intelligence
//!
//! Mathematically accurate implementation of Chebyshev polynomials T_n(x), U_n(x)
//! for approximation theory with verified properties.

use crate::core::{Expression, Symbol};
use crate::functions::properties::*;
use std::collections::HashMap;
use std::sync::Arc;

/// Chebyshev Polynomial Intelligence
///
/// Complete mathematical intelligence for Chebyshev polynomials
pub struct ChebyshevIntelligence {
    /// Function properties for Chebyshev polynomials
    properties: HashMap<String, FunctionProperties>,
}

impl Default for ChebyshevIntelligence {
    fn default() -> Self {
        Self::new()
    }
}

impl ChebyshevIntelligence {
    /// Create new Chebyshev polynomial intelligence system
    pub fn new() -> Self {
        let mut intelligence = Self {
            properties: HashMap::with_capacity(8),
        };

        intelligence.initialize_chebyshev_polynomials();

        intelligence
    }

    /// Get all Chebyshev polynomial properties
    pub fn get_properties(&self) -> HashMap<String, FunctionProperties> {
        self.properties.clone()
    }

    /// Check if function is a Chebyshev polynomial
    pub fn has_function(&self, name: &str) -> bool {
        self.properties.contains_key(name)
    }

    /// Initialize Chebyshev polynomials with ABSOLUTE MATHEMATICAL ACCURACY
    fn initialize_chebyshev_polynomials(&mut self) {
        // Chebyshev Polynomials T_n(x) - MATHEMATICALLY VERIFIED
        // Reference: Approximation Theory, Numerical Analysis textbooks
        // Used for polynomial approximation and numerical integration
        self.properties.insert(
            "chebyshev_first".to_owned(),
            FunctionProperties::Polynomial(Box::new(PolynomialProperties {
                family: PolynomialFamily::Chebyshev,
                // THREE-TERM RECURRENCE RELATION (MATHEMATICALLY VERIFIED)
                // T_{n+1}(x) = 2x T_n(x) - T_{n-1}(x)
                recurrence: ThreeTermRecurrence {
                    // Coefficient of x*T_n(x): 2x
                    alpha_coeff: Expression::mul(vec![
                        Expression::integer(2),
                        Expression::symbol("x"),
                    ]),

                    // No constant term
                    beta_coeff: Expression::integer(0),

                    // Coefficient of T_{n-1}(x): -1
                    gamma_coeff: Expression::integer(-1),

                    // Initial conditions (mathematically verified)
                    // T_0(x) = 1, T_1(x) = x
                    initial_conditions: (Expression::integer(1), Expression::symbol("x")),
                },

                // Orthogonality properties (mathematically verified)
                // ∫_{-1}^{1} T_m(x) T_n(x) / √(1-x²) dx = π/2 δ_{mn} (n > 0), π δ_{0n} (n = 0)
                orthogonality: Some(OrthogonalityData {
                    // Weight function: w(x) = 1/√(1-x²)
                    weight_function: Expression::function(
                        "chebyshev_weight",
                        vec![Expression::symbol("x")],
                    ),

                    // Orthogonality interval: [-1, 1]
                    interval: (Expression::integer(-1), Expression::integer(1)),

                    // Normalization depends on n
                    norm_squared: Expression::function(
                        "chebyshev_norm_squared",
                        vec![Expression::symbol("n")],
                    ),
                }),

                // Rodrigues' formula (alternative representation)
                rodrigues_formula: Some(RodriguesFormula {
                    formula: "T_n(x) = cos(n arccos(x))".to_owned(),
                    normalization: Expression::integer(1),
                    weight_function: Expression::function(
                        "chebyshev_weight",
                        vec![Expression::symbol("x")],
                    ),
                }),

                // Generating function (mathematically verified)
                // (1-tx)/(1-2tx+t²) = Σ_{n=0}^∞ T_n(x) t^n
                generating_function: Some(GeneratingFunction {
                    function: Expression::function(
                        "chebyshev_generating",
                        vec![Expression::symbol("x"), Expression::symbol("t")],
                    ),
                    gf_type: GeneratingFunctionType::Ordinary,
                }),

                // Special values (mathematically verified)
                special_values: vec![
                    // T_n(1) = 1 for all n ≥ 0
                    SpecialValue {
                        input: "1".to_owned(),
                        output: Expression::integer(1),
                        latex_explanation: "T_n(1) = 1 \\text{ for all } n \\geq 0".to_owned(),
                    },
                    // T_n(-1) = (-1)^n for all n ≥ 0
                    SpecialValue {
                        input: "-1".to_owned(),
                        output: Expression::pow(Expression::integer(-1), Expression::symbol("n")),
                        latex_explanation: "T_n(-1) = (-1)^n \\text{ for all } n \\geq 0".to_owned(),
                    },
                ],

                // Evaluation method: Recurrence is most stable
                evaluation_method: EvaluationMethod::Recurrence,

                // Numerical evaluator using recurrence relation),

                // Symbolic expansion method for intelligence-driven computation
                symbolic_expander: Some(super::super::properties::special::SymbolicExpander::Custom(
                    super::symbolic::expand_chebyshev_first_symbolic
                )),

                antiderivative_rule: AntiderivativeRule {
                    rule_type: AntiderivativeRuleType::Custom {
                        builder: Arc::new(|var: Symbol| {
                            Expression::integral(
                                Expression::function("chebyshev_first", vec![Expression::symbol(var.clone())]),
                                var
                            )
                        }),
                    },
                    result_template: "∫T_n(x) dx (symbolic - orthogonal polynomial integration requires specialized techniques)".to_owned(),
                    constant_handling: ConstantOfIntegration::AddConstant,
                },
                wolfram_name: None,
            })),
        );

        // Chebyshev Polynomials of Second Kind U_n(x)
        self.properties.insert(
            "chebyshev_second".to_owned(),
            FunctionProperties::Polynomial(Box::new(PolynomialProperties {
                family: PolynomialFamily::Chebyshev,
                // THREE-TERM RECURRENCE RELATION (MATHEMATICALLY VERIFIED)
                // U_{n+1}(x) = 2x U_n(x) - U_{n-1}(x)
                recurrence: ThreeTermRecurrence {
                    // Same recurrence as T_n but different initial conditions
                    alpha_coeff: Expression::mul(vec![
                        Expression::integer(2),
                        Expression::symbol("x"),
                    ]),
                    beta_coeff: Expression::integer(0),
                    gamma_coeff: Expression::integer(-1),

                    // Initial conditions (mathematically verified)
                    // U_0(x) = 1, U_1(x) = 2x
                    initial_conditions: (
                        Expression::integer(1),
                        Expression::mul(vec![Expression::integer(2), Expression::symbol("x")]),
                    ),
                },

                // Orthogonality properties (mathematically verified)
                // ∫_{-1}^{1} U_m(x) U_n(x) √(1-x²) dx = π/2 δ_{mn}
                orthogonality: Some(OrthogonalityData {
                    // Weight function: w(x) = √(1-x²)
                    weight_function: Expression::function(
                        "chebyshev_u_weight",
                        vec![Expression::symbol("x")],
                    ),

                    // Orthogonality interval: [-1, 1]
                    interval: (Expression::integer(-1), Expression::integer(1)),

                    // Normalization: π/2
                    norm_squared: Expression::function("chebyshev_u_norm", vec![]),
                }),

                rodrigues_formula: None, // Different representation for U_n
                generating_function: Some(GeneratingFunction {
                    function: Expression::function(
                        "chebyshev_u_generating",
                        vec![Expression::symbol("x"), Expression::symbol("t")],
                    ),
                    gf_type: GeneratingFunctionType::Ordinary,
                }),

                special_values: vec![
                    // U_n(1) = n + 1
                    SpecialValue {
                        input: "1".to_owned(),
                        output: Expression::add(vec![
                            Expression::symbol("n"),
                            Expression::integer(1),
                        ]),
                        latex_explanation: "U_n(1) = n + 1".to_owned(),
                    },
                ],

                evaluation_method: EvaluationMethod::Recurrence,

                // Numerical evaluator using recurrence relation),

                // Symbolic expansion method for intelligence-driven computation
                symbolic_expander: Some(super::super::properties::special::SymbolicExpander::Custom(
                    super::symbolic::expand_chebyshev_second_symbolic
                )),

                antiderivative_rule: AntiderivativeRule {
                    rule_type: AntiderivativeRuleType::Custom {
                        builder: Arc::new(|var: Symbol| {
                            Expression::integral(
                                Expression::function("chebyshev_second", vec![Expression::symbol(var.clone())]),
                                var
                            )
                        }),
                    },
                    result_template: "∫U_n(x) dx (symbolic - orthogonal polynomial integration requires specialized techniques)".to_owned(),
                    constant_handling: ConstantOfIntegration::AddConstant,
                },
                wolfram_name: None,
            })),
        );
    }
}