Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt

Use this file to discover all available pages before exploring further.

MathCore provides two complementary integration modes: symbolic integration, which returns an exact antiderivative as an Expr; and numerical integration, which evaluates a definite integral over a closed interval using Simpson’s rule. Together they cover the most common integration needs — from finding antiderivatives of polynomials and trigonometric functions to approximating areas under arbitrary curves.

MathCore::integrate

Parses a string expression and returns its symbolic indefinite integral.
pub fn integrate(expression: &str, var: &str) -> Result<Expr, MathError>

Parameters

ParameterTypeDescription
expression&strA string representation of the expression to integrate.
var&strThe integration variable.

Return value

Returns Ok(Expr) containing the symbolic antiderivative (without a constant of integration), or Err(MathError) if the expression cannot be parsed.
MathCore does not append a constant of integration (+ C) to symbolic results. If you need it, add Expr::Symbol("C".to_string()) to the result manually.

Symbolic integration examples

use mathcore::MathCore;

// ∫ x dx = x^2 / 2
let result = MathCore::integrate("x", "x").unwrap();
println!("{}", result); // x^2 / 2
use mathcore::MathCore;

// ∫ sin(x) dx = -cos(x)
let result = MathCore::integrate("sin(x)", "x").unwrap();
println!("{}", result); // -cos(x)

Integration rules supported

MathCore applies these antiderivative rules by structural pattern-matching on the Expr tree:
PatternResultNotes
Constant nn * var∫ 5 dx = 5x
var (identity)var^2 / 2∫ x dx = x²/2
Non-integration symbolsymbol * varTreated as a constant factor
f + g / f - g∫f ± ∫gLinearity of integration
c * f (c constant)c * ∫fConstant-factor rule
var^n (n ≠ −1)var^(n+1) / (n+1)Power rule
var^(-1)ln(var)Special case of power rule
sin(var)-cos(var)Direct trig antiderivative
cos(var)sin(var)Direct trig antiderivative
exp(var)exp(var)Self-antiderivative

Using Calculus::integrate directly

If you already hold an Expr, skip the parsing step and call the lower-level function directly:
use mathcore::MathCore;
use mathcore::calculus::Calculus;

let expr = MathCore::parse("x^3 + 2*x + 1").unwrap();
let integral = Calculus::integrate(&expr, "x").unwrap();
println!("∫(x^3 + 2*x + 1) dx = {}", integral); // x^4/4 + x^2 + x
pub fn integrate(expr: &Expr, var: &str) -> Result<Expr, MathError>

MathCore::numerical_integrate — definite integration

For expressions that have no closed-form antiderivative, or whenever you need a numeric result over a specific interval, use the definite integration function. Internally it calls Calculus::numerical_integrate with 1 000 steps.
pub fn numerical_integrate(
    expression: &str,
    var: &str,
    lower: f64,
    upper: f64,
) -> Result<f64, MathError>

Parameters

ParameterTypeDescription
expression&strThe integrand as a string.
var&strThe integration variable.
lowerf64Lower bound of integration.
upperf64Upper bound of integration.

Return value

Returns Ok(f64) with the numeric approximation, or Err(MathError) on parse or evaluation failure.

Examples

use mathcore::MathCore;

// Exact value = 1/3 ≈ 0.3333...
let area = MathCore::numerical_integrate("x^2", "x", 0.0, 1.0).unwrap();
println!("∫₀¹ x² dx ≈ {:.6}", area); // 0.333333

Calculus::numerical_integrate — configurable steps

The lower-level function exposes the steps parameter so you can trade off speed against accuracy:
pub fn numerical_integrate(
    expr: &Expr,
    var: &str,
    lower: f64,
    upper: f64,
    steps: usize,
) -> Result<f64, MathError>

Parameters

ParameterTypeDescription
expr&ExprA parsed expression.
var&strThe integration variable.
lowerf64Lower bound.
upperf64Upper bound.
stepsusizeNumber of sub-intervals. More steps → greater accuracy, slower.
The integrator uses composite Simpson’s 1/3 rule (evaluating the function at the left endpoint, midpoint, and right endpoint of each sub-interval, weighted 1 : 4 : 1) for O(h⁴) accuracy per step.
use mathcore::MathCore;
use mathcore::calculus::Calculus;

let expr = MathCore::parse("exp(-x^2)").unwrap(); // Gaussian — no closed form

// High accuracy with 10 000 steps
let result = Calculus::numerical_integrate(&expr, "x", 0.0, 1.0, 10_000).unwrap();
println!("∫₀¹ exp(-x²) dx ≈ {:.8}", result);
The default of 1 000 steps (used by MathCore::numerical_integrate) gives excellent accuracy for smooth functions. Increase steps when integrating highly oscillatory integrands.

Unsupported symbolic expressions

When MathCore cannot find a closed-form antiderivative — for example, a product of two functions that both depend on the integration variable — it returns a symbolic Integral node rather than an error:
use mathcore::MathCore;

// MathCore cannot integrate x * sin(x) symbolically (no IBP support yet)
let result = MathCore::integrate("x * sin(x)", "x").unwrap();
println!("{}", result); // ∫(x * sin(x)) dx  — unevaluated Integral node
An Integral node in the result means the integral is unevaluated. You can still display it as a symbolic placeholder, but you cannot numerically evaluate the Integral node itself — use MathCore::numerical_integrate instead to obtain a numeric result for such expressions.

Build docs developers (and LLMs) love