mathhook_core/functions/properties/
special.rs

1//! Special Function and Polynomial Properties
2//!
3//! Comprehensive mathematical properties for advanced special functions (gamma, bessel, zeta)
4//! and polynomial families (Legendre, Hermite, Laguerre, Chebyshev, etc.).
5//! Following SymPy's approach but optimized for performance.
6
7use super::rules::{
8    AntiderivativeRule, EvaluationMethod, RecurrenceRule, SpecialValue, ThreeTermRecurrence,
9};
10use crate::core::Expression;
11
12/// Symbolic polynomial expander for function intelligence
13///
14/// Provides symbolic expansion capability for orthogonal polynomials,
15/// enabling conversion from recurrence-based representation to explicit
16/// polynomial forms in the Expression system.
17#[derive(Debug, Clone, Copy)]
18pub enum SymbolicExpander {
19    /// Custom expansion function
20    ///
21    /// Takes polynomial degree n and returns explicit symbolic expression
22    Custom(fn(usize) -> Expression),
23}
24
25/// Special function properties (gamma, bessel, zeta, etc.)
26///
27/// Comprehensive mathematical properties for advanced special functions
28/// following SymPy's approach but optimized for performance.
29///
30/// **Note**: Evaluation is handled by direct dispatch through
31/// `Expression::evaluate_function_dispatch()` for performance.
32/// This struct stores only mathematical properties, not evaluation logic.
33#[derive(Clone)]
34pub struct SpecialProperties {
35    /// Quick derivative check
36    pub has_derivative: bool,
37
38    /// Quick antiderivative check
39    pub has_antiderivative: bool,
40
41    /// Antiderivative rule (if known)
42    pub antiderivative_rule: Option<AntiderivativeRule>,
43
44    /// Recurrence relations for symbolic computation
45    /// Examples: Γ(n+1) = n·Γ(n), J_{n+1} = (2n/x)J_n - J_{n-1}
46    pub recurrence_relations: Vec<RecurrenceRule>,
47
48    /// Differential equation the function satisfies
49    /// Examples: Bessel DE, hypergeometric DE
50    pub differential_equation: Option<DifferentialEquation>,
51
52    /// Special values for exact computation
53    pub special_values: Vec<SpecialValue>,
54
55    /// Asymptotic behavior for large arguments
56    pub asymptotic_behavior: Option<AsymptoticData>,
57
58    /// Wolfram Language function name
59    /// Used for Wolfram formatting without hardcoded matches
60    /// Example: "gamma" → "Gamma", "zeta" → "Zeta"
61    pub wolfram_name: Option<&'static str>,
62}
63
64impl std::fmt::Debug for SpecialProperties {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct("SpecialProperties")
67            .field("has_derivative", &self.has_derivative)
68            .field("has_antiderivative", &self.has_antiderivative)
69            .field("antiderivative_rule", &self.antiderivative_rule)
70            .field("recurrence_relations", &self.recurrence_relations)
71            .field("differential_equation", &self.differential_equation)
72            .field("special_values", &self.special_values)
73            .field("asymptotic_behavior", &self.asymptotic_behavior)
74            .field("wolfram_name", &self.wolfram_name)
75            .finish()
76    }
77}
78
79/// Polynomial function properties (legendre, hermite, laguerre, etc.)
80///
81/// Comprehensive properties for orthogonal polynomials and polynomial families
82/// with focus on computational efficiency and mathematical correctness.
83///
84/// **Note**: Evaluation is handled by direct dispatch through
85/// `Expression::evaluate_function_dispatch()` for performance.
86/// This struct stores only mathematical properties, not evaluation logic.
87#[derive(Clone)]
88pub struct PolynomialProperties {
89    /// Polynomial family classification
90    pub family: PolynomialFamily,
91
92    /// Three-term recurrence relation
93    /// Examples: P_{n+1} = ((2n+1)x P_n - n P_{n-1})/(n+1)
94    pub recurrence: ThreeTermRecurrence,
95
96    /// Orthogonality properties (if applicable)
97    pub orthogonality: Option<OrthogonalityData>,
98
99    /// Rodrigues' formula (if available)
100    /// Examples: P_n(x) = (1/2^n n!) d^n/dx^n (x²-1)^n
101    pub rodrigues_formula: Option<RodriguesFormula>,
102
103    /// Generating function
104    /// Examples: 1/√(1-2xt+t²) = Σ P_n(x) t^n
105    pub generating_function: Option<GeneratingFunction>,
106
107    /// Special values and boundary conditions
108    pub special_values: Vec<SpecialValue>,
109
110    /// Computational method for evaluation
111    pub evaluation_method: EvaluationMethod,
112
113    /// Symbolic expansion method for intelligence-driven computation
114    ///
115    /// Converts polynomial from recurrence-based representation to explicit
116    /// symbolic expression. This enables algebraic manipulation and simplification
117    /// of polynomial expressions in the Expression system.
118    pub symbolic_expander: Option<SymbolicExpander>,
119
120    /// Antiderivative rule (for polynomial integration)
121    /// All polynomials are integrable, so this is always Some(...)
122    pub antiderivative_rule: AntiderivativeRule,
123
124    /// Wolfram Language function name
125    /// Used for Wolfram formatting without hardcoded matches
126    /// Example: "legendre_p" → "LegendreP", "hermite_h" → "HermiteH"
127    pub wolfram_name: Option<&'static str>,
128}
129
130impl std::fmt::Debug for PolynomialProperties {
131    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132        f.debug_struct("PolynomialProperties")
133            .field("family", &self.family)
134            .field("recurrence", &self.recurrence)
135            .field("orthogonality", &self.orthogonality)
136            .field("rodrigues_formula", &self.rodrigues_formula)
137            .field("generating_function", &self.generating_function)
138            .field("special_values", &self.special_values)
139            .field("evaluation_method", &self.evaluation_method)
140            .field("symbolic_expander", &self.symbolic_expander)
141            .field("antiderivative_rule", &self.antiderivative_rule)
142            .field("wolfram_name", &self.wolfram_name)
143            .finish()
144    }
145}
146
147/// Polynomial family classification
148#[derive(Debug, Clone, Copy, PartialEq)]
149pub enum PolynomialFamily {
150    Legendre,
151    Hermite,
152    Laguerre,
153    Chebyshev,
154    Jacobi,
155    Gegenbauer,
156}
157
158/// Orthogonality properties for polynomial families
159#[derive(Debug, Clone)]
160pub struct OrthogonalityData {
161    /// Weight function w(x)
162    pub weight_function: Expression,
163
164    /// Orthogonality interval [a, b]
165    pub interval: (Expression, Expression),
166
167    /// Normalization constant
168    pub norm_squared: Expression,
169}
170
171/// Rodrigues' formula for polynomial construction
172#[derive(Debug, Clone)]
173pub struct RodriguesFormula {
174    /// Formula template
175    pub formula: String,
176
177    /// Normalization constant
178    pub normalization: Expression,
179
180    /// Weight function
181    pub weight_function: Expression,
182}
183
184/// Generating function for polynomial families
185#[derive(Debug, Clone)]
186pub struct GeneratingFunction {
187    /// Generating function expression
188    pub function: Expression,
189
190    /// Type: ordinary or exponential
191    pub gf_type: GeneratingFunctionType,
192}
193
194/// Types of generating functions
195#[derive(Debug, Clone, Copy)]
196pub enum GeneratingFunctionType {
197    Ordinary,
198    Exponential,
199}
200
201/// Differential equation representation
202#[derive(Debug, Clone)]
203pub struct DifferentialEquation {
204    /// Order of the differential equation
205    pub order: usize,
206
207    /// Equation in standard form
208    pub equation: String,
209
210    /// Coefficients (if polynomial)
211    pub coefficients: Vec<Expression>,
212}
213
214/// Asymptotic behavior data
215#[derive(Debug, Clone)]
216pub struct AsymptoticData {
217    /// Behavior as x → ∞
218    pub as_x_to_infinity: String,
219
220    /// Behavior as x → 0
221    pub as_x_to_zero: String,
222
223    /// Leading term coefficient
224    pub leading_coefficient: Expression,
225}