MathSteps: step-by-step calculus in Java
A symbolic maths engine written in pure Java (no libraries) that differentiates and integrates expressions and explains every step, like a teacher working on the board.
Try it: MathSteps web app. The same Java engine runs in your browser.
> java -jar math-steps.jar int "x e^x"
Step 1: Integration by parts: integral u dv = u v - integral v du (choose u by LIATE: logs, inverse trig, algebraic, trig, exponential)
u = x, dv = e^x dx, du = 1 dx, v = e^x
Step 2: Now integrate v du
integral e^x dx
- Standard integral: integral e^u du = e^u
Standard integral: integral e^u du = e^u => integral e^x dx = e^x
Step 3: So u v - integral v du
integral x*e^x dx = x*e^x - e^x
Step 4: Answer (add the constant of integration C)
integral x*e^x dx = x*e^x - e^x + C
Step 5: Check: differentiating the answer gives back the integrand
d/dx [x*e^x - e^x] = x*e^x (checked)
More worked examples are in examples.md.
What it can do
Derivatives:
- Rules: constant, power, sum, constant multiple, product, quotient and chain rules.
- Functions: exponentials (
e^u,a^u), logarithms, trig (sin cos tan sec csc cot) and inverse trig (arcsin arccos arctan). - Logarithmic differentiation for
f(x)^g(x), e.g.x^x. - Neat results: quotient-rule numerators are expanded and simplified.
Integrals:
| Technique | Example |
|---|---|
| Power, exponential, log and trig standard integrals | x^10, 2^x, 1/x, sec(x)tan(x) |
| Linear substitution | sin(3x), e^(5x), (3x+1)^4, 1/(2x+1) |
| u-substitution (automatic choice of u) | 2x cos(x^2), x e^(x^2), ln(x)/x, e^(sqrt(x))/sqrt(x) |
| Substitution inside roots | x sqrt(x+1), x/sqrt(x+1) |
| Integration by parts (LIATE, repeated) | x e^x, x^3 e^x, x^2 ln(x), ln(x), arctan(x) |
| Trig identities | sin^2(x), sin^3(x), sin^4(x), tan^2(x), sin(x)^3 cos(x)^2 |
| Partial fractions (with long division) | 1/(x^2-1), (x+3)/(x^2+3x+2), 1/(x(x+1)^2), (x^3+1)/(x-1) |
| Completing the square, giving ln + arctan | 1/(x^2+2x+5), (2x+1)/(x^2+x+1) |
| Inverse-trig forms | 1/sqrt(4-x^2), 1/sqrt(x^2+1) |
| e^x · sin / cos | e^x sin(x), e^(2x) cos(3x) |
| Definite integrals | sin(x) from 0 to π = 2 |
Exact arithmetic: fractions stay exact (x^3/3, not 0.333x^3), and special values are simplified
(sin(π/6) = 1/2, sqrt(8) = 2√2).
Honest: if an integral has no elementary answer (e^(x^2), sin(x)/x), it says so instead of guessing.
Every answer is checked
- Derivatives: compared with a numerical derivative at 10 points.
- Indefinite integrals: the answer is differentiated back and compared with the original function.
- Definite integrals: compared with Simpson's rule (4,000 intervals).
The test suite (test/…/MathStepsTest.java) runs 227 checks, and all pass:
- 62 derivatives,
- 90 integrals,
- 10 definite integrals,
- 3 integrals that have no elementary answer (correctly rejected),
- 62 "print, then read back" round trips.
Use it
Command line (Java 8 or newer):
java -jar math-steps.jar diff "x^2 sin(x)"
java -jar math-steps.jar int "1/(x^2+2x+5)"
java -jar math-steps.jar defint "x^2" 0 3
java -jar math-steps.jar int "t e^t" --var t
java -jar math-steps.jar int "x ln(x)" --json # machine-readable steps (with LaTeX)
java -jar math-steps.jar # interactive: d x^3 | i sin(x)^2 | i x^2 from 0 to 1
From Java:
import com.unnicr.mathsteps.*;
MathSteps.Result r = MathSteps.integral("x^2 ln(x)", "x");
System.out.println(r.answer); // x^3*ln(x)/3 - x^3/9
System.out.println(r.answer.toLatex()); // \frac{x^{3} \cdot \ln\left(x\right)}{3} - \frac{x^{3}}{9}
for (Steps.Step s : r.steps.all()) System.out.println(s.title + " :: " + s.text);
String json = MathSteps.toJson(r); // for web apps
Input syntax:
- Implicit multiplication:
2x,x sin(x),3(x+1). - Powers:
x^2orx**2. - Functions:
sqrt( ),e^( )orexp( ),ln( )orlog( ),sin^2(x),|x|,arctan( )oratan( ). - Constants:
piorπ. - Other letters are treated as constants:
a x^2 + b x + c.
How it works
Parser turns text into an expression tree, and Simplifier brings it into a canonical form. That means:
- exact
Rationalarithmetic, - like terms collected,
- powers combined (
x · x^2 = x^3), - special values applied.
Differentiator applies rules recursively and records each one.
Integrator tries these strategies in order, rolling back the recorded steps of any strategy that fails:
- Constant multiple and sum rule.
- Standard table, with linear substitution.
- Trig identities.
- Rational functions: exact polynomial algebra, rational-root factoring, Gaussian elimination for the partial-fraction coefficients.
- Expansion.
- u-substitution: tries inner functions as u and accepts u when
integrand / u'can be written in u alone. - Integration by parts.
Printer produces both re-parsable plain text and LaTeX.
This is a rule-based symbolic engine, not a neural network. It never "hallucinates" a step, and every answer is verified.
Build from source
build.bat (Windows) ./build.sh (Linux/macOS)
Both compile for Java 8, run the test suite and build math-steps.jar.
Limits
- Real-valued, single-variable calculus.
- Not yet covered: trig substitution beyond the arcsin/ln forms above, partial fractions with irrational
roots (e.g.
1/(x^2-2)), and repeated irreducible quadratic factors. - When a technique is missing, the solver says it couldn't find a method rather than giving a wrong answer.