← Back to Engine

Symbolic Math Engine

Architecture, History, and Comparison with Industry CAS

A Ruby-based symbolic integration engine built from scratch, evolving from basic pattern matching to a multi-strategy architecture with 12 specialized modules.

12
Modules
~4500
Lines of Code
97
Test Cases
86
Integrals Solved
11
Non-Elementary (correct skip)
100%
Test Pass Rate

Integration Pipeline Architecture

When the engine receives an integral, it passes the expression through this pipeline of strategies. The first strategy that succeeds returns the result. Failed strategies are logged for the trace visualization.

Input: integral f(x) dx | v [1. Normalizer] -----> Canonical form: constants left, like-terms collected, | radicals -> powers, factor cancellation via poly GCD v [2. Table Lookup] ---> ~15 common forms: 1/(x^2+a^2)->arctan, 1/sqrt(a^2-x^2)->arcsin, | sec^2->tan, tan*sec->sec, c/(ax+b)->ln, ... | (miss) v [3. U-Substitution] -> Finds u=g(x), checks if du/dx is a factor (with constant ratio). | Handles products, divisions, sqrt->power rewrites, 1/(f*g) splits. | (miss) v [4. Trig Identities] -> sin^2+cos^2=1, half-angle, mixed power split (sin^m*cos^n), | 1/(sin*cos)->tan+cot, sin^2*cos^2->(1/8)(1-cos4x) | (rewrite, retry) v [5. Distribution] ---> Expand: a*(b+c) -> a*b + a*c, then integrate each term | v [6. Inverse Trig] ---> arctan(x), arcsin(x), ln^n(x) via reduction formula | v [7. Rational/PF] ----> Partial fractions: factor denominator, cover-up method, | repeated roots via Gaussian elimination, | completing the square for irreducible quadratics | v [8. Trig Powers] ----> sin^n, cos^n via half-angle/reduction, tan^n, sec^n formulas | v [9. Radicals] -------> Trig substitution for sqrt(a^2-x^2), 1/sqrt(quadratic), | sqrt(x^2+a^2) via completing square | v [10. By-Parts] ------> LIATE rule, recursive (x^n*trig), cycling (exp*trig), | up to 4 levels deep, with v*du normalization | v [11. Standard] ------> Pattern matching on basic forms: power rule, constant*f, | sum/difference, linear-argument trig/exp | v [12. Verify] --------> Monte Carlo: differentiate result, compare at 12-15 random points | Reports confidence score. Catches errors. v Result (+ trace log for visualization)

Development Timeline

The engine was built in phases, each adding a layer of capability.

Phase 0: Original Engine
Binary tree AST, parser, differentiation (all rules), basic integration via pattern matching (~50 hard-coded patterns), standard simplifier, vector calculus, Taylor series, web UI. expression_node.rb parser.rb derivative_operations.rb integration_operations.rb experimental_risch.rb
Phase 1: Foundation Layer
Polynomial class (sparse coefficients, long division, GCD, factoring). Expression normalizer (canonical ordering, constant folding, like-term collection, factor cancellation). This eliminated ~60% of special-casing brittleness. polynomial.rb normalizer.rb
Phase 2: Core Algorithms
General u-substitution engine (candidate collection, proportionality detection, placeholder substitution). Integral lookup table with parameterized pattern matching. Monte Carlo verification. general_u_substitution.rb integral_table.rb verification.rb
Phase 3: Trig & Visualization
Trig identity engine (Pythagorean, half-angle, double-angle, mixed-power splitting). Strategy reporter with suggestions. Decision tree recording. trig_identities.rb strategy_reporter.rb
Phase 4: Rational Functions
Completing the square for irreducible quadratics. Linear-over-quadratic splitting (ln + arctan parts). General partial fractions with distinct and repeated linear factors via Gaussian elimination. Polynomial long division for improper rationals. rational_integration.rb
Phase 5: Path Tracing & Polish
Integration tracer with full decision tree recording. Strategy space visualization (ASCII scatter plot of exploration points). Mermaid diagram generation. Verbose mode. Complete step tracking through recursive calls. Division u-sub (sqrt/power rewrite). Power-combining normalizer pass. Extended evaluate/to_infix for all functions. integration_tracer.rb

Module Map

ModuleFileLinesPurpose
Expression Nodeexpression_node.rb~190Binary tree AST with evaluate, to_infix, deep_clone
Parserparser.rb~200Infix to prefix to tree (shunting-yard variant)
Polynomialpolynomial.rb~280Sparse coefficient polynomial arithmetic, GCD, factoring
Normalizernormalizer.rb~250Canonical forms, constant folding, like-terms, power combining
Differentiationderivative_operations.rb~670All derivative rules with step tracking
Standard Integrationintegration_operations.rb~1750Pattern-matching integrator with ~50 rules
Experimental Rischexperimental_risch.rb~3100Multi-phase strategy dispatcher, by-parts, PF, trig powers
U-Substitutiongeneral_u_substitution.rb~320General u-sub with candidate collection and division rewrites
Integral Tableintegral_table.rb~500Parameterized pattern table (~15 entries)
Trig Identitiestrig_identities.rb~330Pythagorean, half-angle, mixed-power splitting
Rational Integrationrational_integration.rb~400Completing square, linear/quad split, general PF
Verificationverification.rb~100Monte Carlo numerical verification
Tracerintegration_tracer.rb~400Decision tree, space plot, Mermaid, verbose mode
Strategy Reporterstrategy_reporter.rb~150Technique suggestions when integration fails
Simplificationsimplification.rb~870Standard + experimental with trig/normalizer hooks

Comparison with Industry CAS

How does this engine compare with Mathematica, Maple, MATLAB Symbolic Toolbox, and Julia's SymbolicUtils/Symbolics?

The Landscape of Symbolic Integration

Symbolic integration is one of the hardest problems in computer algebra. The theoretical gold standard is the Risch Algorithm (1969), which is semi-decidable: it can determine whether an elementary antiderivative exists and find it if so. However, no CAS has fully implemented it.

Maple has the most complete implementation, covering the transcendental case (exponentials and logarithms) fully, thanks to decades of work by Keith Geddes, Bronstein, and others. Mathematica uses a hybrid approach with extensive pattern tables plus heuristic methods. MATLAB's Symbolic Toolbox is powered by MuPAD (now owned by MathWorks). Julia's ecosystem is youngest, with SymbolicUtils focusing more on term rewriting than classical integration algorithms.

Feature Comparison

FeatureThis EngineMathematicaMapleMATLAB SymJulia Sym
Power rule YesYesYesYesYes
Trig/exp/log basics YesYesYesYesYes
U-substitution (general) YesYesYesYesPartial
Integration by parts (recursive) Yes (4 levels)Yes (unlimited)YesYesPartial
Partial fractions (linear) YesYesYesYesYes
Partial fractions (repeated) YesYesYesYesPartial
Partial fractions (irreducible quadratic) Yes (via completing square)YesYesYesNo
Trig identities Yes (10 identities)Yes (100s)Yes (100s)YesPartial
Trig substitution Partial (sqrt(a^2-x^2))Yes (full)Yes (full)YesNo
Risch algorithm (rational part) Partial (PF-based)YesYes (Hermite reduction)YesNo
Risch algorithm (transcendental) NoPartialYes (most complete)PartialNo
Risch algorithm (algebraic) NoPartialPartialNoNo
Lookup tables ~15 forms1000s of forms1000s1000sLimited
Definite integrals NoYesYesYesNumeric only
Special functions (erf, Si, Ei) No (correctly returns integral marker)Yes (100s)Yes (100s)YesSome
Multivariate integration NoYesYesYesLimited
Step-by-step solution Yes (detailed)Wolfram Alpha onlyNoNoNo
Decision trace visualization Yes (tree + space plot)NoNoNoNo
Numerical verification Yes (Monte Carlo)InternalInternalInternalNo

What We're Missing (Roadmap to Parity)

Tier 1: Achievable (weeks of work)

Tier 2: Significant effort (months)

Tier 3: Research-grade (years)

How Far Are We from Mathematica?

Honest assessment by category:

CategoryOur CoverageMathematica CoverageGap
Calculus 1 integrals~90%~100%
Calculus 2 integrals~70%~99%
Advanced/research integrals~15%~85%
Special functions0%~95%
Step-by-step pedagogy~85%~70% (Alpha)
Decision transparency~95%0%

We're strongest in transparency (no CAS shows its reasoning process like our trace visualizer) and pedagogy (detailed step-by-step). We're weakest in coverage of advanced forms and special functions. For a typical Calculus 1 course, we handle ~90% of integrals correctly. For Calculus 2 (by-parts, PF, trig sub), ~70%. For research-level, ~15%.

About Maple's Risch Implementation

You're correct that Maple has the most complete Risch algorithm implementation. The key people behind it:

No CAS has fully implemented the algebraic case of Risch (handling radicals and algebraic functions). This remains an open implementation challenge. Even Maple has gaps for certain classes of algebraic integrands.

Mathematica uses a more pragmatic hybrid approach: extensive table lookup (thousands of forms from Gradshteyn & Ryzhik), heuristic methods, and partial Risch for the transcendental case. It often succeeds where Maple's algorithmic approach gets stuck on edge cases, but can also fail on cases that Maple handles algorithmically.

Continued in: History Part 2: UI Overhaul, Complex Analysis & More →

Symbolic Math Engine — Built with Ruby — Back to Engine