Module trigonometric

Module trigonometric 

Source
Expand description

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^mcos^n, tan^msec^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)

Enums§

TrigPattern
Trigonometric pattern types

Functions§

detect_trig_pattern
Detect trigonometric patterns in expression
extract_trig_function_with_coeff
Extract trig function name and coefficient from expressions like sin(mx) or cos(nx)
integrate_trig_product
Integrate products like sin(mx)*cos(nx), sin(mx)*sin(nx), cos(mx)*cos(nx)
try_trigonometric_integration
Try to integrate trigonometric expressions