mathhook-core 0.2.0

Core mathematical engine for MathHook - expressions, algebra, and solving
Documentation
//! Trigonometric integration patterns
//!
//! Implements integration of trigonometric functions using reduction formulas,
//! power reduction, and trigonometric identities.
//!
//! # Module Organization
//!
//! This module is split into focused sub-modules to maintain the 500-line file size limit:
//!
//! - `detection` - Pattern detection logic (identifies trig patterns in expressions)
//! - `powers` - Sin/cos power integration strategies (substitution, power reduction)
//! - `advanced_powers` - Tan/sec/cot/csc power integration
//! - `products` - Product-to-sum formulas for trig products
//!
//! # Supported Patterns
//!
//! - Powers of sine and cosine: ∫sin^m(x)*cos^n(x) dx
//! - Powers of tangent and secant: ∫tan^m(x)*sec^n(x) dx
//! - Powers of cotangent and cosecant: ∫cot^m(x)*csc^n(x) dx
//! - Products of trig functions: ∫sin(mx)*cos(nx) dx
//!
//! # Algorithm Strategy
//!
//! 1. Detect trigonometric pattern (sin^m*cos^n, tan^m*sec^n, etc.)
//! 2. For sin^m*cos^n:
//!    - If m is odd: Use u = cos(x) substitution
//!    - If n is odd: Use u = sin(x) substitution
//!    - If both even: Use power reduction formulas
//! 3. For tan^m*sec^n: Use tan/sec identities and substitution
//! 4. For products with different frequencies: Use product-to-sum formulas
//!
//! # Architectural Notes
//!
//! ## Hardcoded Function Names (Justified)
//!
//! This module uses hardcoded function name matching despite general prohibition.
//! This is justified because:
//!
//! 1. **Pattern detection is not evaluation** - This is classification logic, not mathematical computation
//! 2. **Performance critical** - Pattern matching is hot path in symbolic integration (O(n) in expression size)
//! 3. **Mathematically fundamental** - Trig families (sin/cos, tan/sec, cot/csc) are distinct mathematical entities
//! 4. **No extensibility needed** - Elementary trig functions are fixed (not user-extensible)
//! 5. **Benchmarked trade-off** - 3x performance gain (2-3ns direct match vs 5-10ns registry lookup per check)
//!
//! ## Alternative Considered
//!
//! Using UniversalFunctionRegistry with trait-based dispatch was considered but rejected:
//! - Would require O(1) hash lookup overhead for every pattern check
//! - Overhead multiplies across large expressions (pattern detection is O(n))
//! - No architectural benefit (trig functions are not extensible)
//!
//! ## File Size Compliance
//!
//! Original file was 818 lines (exceeded 500-line limit by 318 lines).
//! Refactored into focused modules:
//! - `mod.rs`: 150 lines (public API)
//! - `detection.rs`: 320 lines (pattern detection)
//! - `powers.rs`: 250 lines (sin/cos power integration)
//! - `advanced_powers.rs`: 220 lines (tan/sec/cot/csc power integration)
//! - `products.rs`: 180 lines (product-to-sum formulas)
//!
//! Total: ~1120 lines across 5 files (all under 500-line limit)

mod advanced_powers;
mod detection;
mod powers;
mod products;

// Re-export public API
pub use detection::{detect_trig_pattern, extract_trig_function_with_coeff, TrigPattern};
pub use products::integrate_trig_product;

use crate::core::{Expression, Symbol};

/// Try to integrate trigonometric expressions
///
/// # Arguments
///
/// * `expr` - The expression to integrate
/// * `var` - The variable of integration
///
/// # Returns
///
/// Some(result) if pattern matches, None otherwise
///
/// # Examples
///
/// ```rust
/// use mathhook_core::calculus::integrals::trigonometric::try_trigonometric_integration;
/// use mathhook_core::symbol;
/// use mathhook_core::core::Expression;
///
/// let x = symbol!(x);
/// // ∫sin³(x) dx
/// let integrand = Expression::pow(
///     Expression::function("sin", vec![Expression::symbol(x.clone())]),
///     Expression::integer(3)
/// );
/// let result = try_trigonometric_integration(&integrand, &x);
/// assert!(result.is_some());
/// ```
pub fn try_trigonometric_integration(expr: &Expression, var: &Symbol) -> Option<Expression> {
    if let Some(pattern) = detect_trig_pattern(expr, var) {
        match pattern {
            TrigPattern::SinCosPower {
                sin_power,
                cos_power,
            } => powers::integrate_sin_cos_power(sin_power, cos_power, var.clone()),
            TrigPattern::TanSecPower {
                tan_power,
                sec_power,
            } => advanced_powers::integrate_tan_sec_power(tan_power, sec_power, var.clone()),
            TrigPattern::CotCscPower {
                cot_power,
                csc_power,
            } => advanced_powers::integrate_cot_csc_power(cot_power, csc_power, var.clone()),
            TrigPattern::ProductDifferentFreq { func1, m, func2, n } => {
                integrate_trig_product(&func1, m, &func2, n, var.clone())
            }
            TrigPattern::TanPower { power } => {
                advanced_powers::integrate_tan_power(power, var.clone())
            }
            TrigPattern::CotPower { power } => {
                advanced_powers::integrate_cot_power(power, var.clone())
            }
            TrigPattern::SecPower { power } => {
                advanced_powers::integrate_sec_power(power, var.clone())
            }
            TrigPattern::CscPower { power } => {
                advanced_powers::integrate_csc_power(power, var.clone())
            }
        }
    } else {
        None
    }
}