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.

Symbolic differentiation is one of MathCore’s core capabilities. Rather than approximating derivatives numerically, MathCore traverses the expression tree and applies analytic differentiation rules — producing an exact symbolic result you can inspect, simplify, or evaluate further. Whether you’re computing the slope of a polynomial, applying the chain rule to a composed trigonometric function, or differentiating a multivariate expression with respect to one of its variables, the same unified API handles it all.

MathCore::differentiate

The top-level entry point parses a string expression and returns the symbolic derivative as an Expr.
pub fn differentiate(expression: &str, var: &str) -> Result<Expr, MathError>

Parameters

ParameterTypeDescription
expression&strA string representation of the expression to differentiate.
var&strThe variable with respect to which the derivative is taken.

Return value

Returns Ok(Expr) containing the simplified symbolic derivative, or Err(MathError) if the expression cannot be parsed or differentiated.
MathCore::differentiate is a static (associated) function — you do not need to construct a MathCore instance to call it.

Basic examples

use mathcore::MathCore;

// d/dx (x^2 + 2*x + 1) = 2*x + 2
let deriv = MathCore::differentiate("x^2 + 2*x + 1", "x").unwrap();
println!("{}", deriv); // 2*x + 2

Differentiation rules supported

MathCore applies standard calculus rules by structural pattern-matching on the Expr tree:
RuleTriggerResult
ConstantAny Number or non-matching Symbol0
IdentitySymbol matching var1
Sum / Differencef ± gf' ± g'
Product rulef * gf' * g + f * g'
Quotient rulef / g(f' * g - f * g') / g^2
Power rulef^n (constant exponent)n * f^(n-1) * f'
Chain rule — sinsin(u)cos(u) * u'
Chain rule — coscos(u)-sin(u) * u'
Chain rule — tantan(u)sec(u)^2 * u'
Chain rule — secsec(u)sec(u) * tan(u) * u'
Chain rule — lnln(u)(1/u) * u'
Chain rule — expexp(u)exp(u) * u'
Chain rule — sqrtsqrt(u)(1 / (2 * sqrt(u))) * u'
Negation-f-f'
Abs|f|(f / |f|) * f'
Chain rule application is automatic — every function derivative is multiplied by the derivative of its inner argument.

Product and chain rule examples

use mathcore::MathCore;

// d/dx (x * sin(x)) = sin(x) + x * cos(x)
let deriv = MathCore::differentiate("x * sin(x)", "x").unwrap();
println!("{}", deriv);

Using Calculus::differentiate directly

If you have already parsed or constructed an Expr value, you can call the lower-level Calculus::differentiate directly and skip the parsing step:
use mathcore::MathCore;
use mathcore::calculus::Calculus;

// Parse once, differentiate multiple times or with different variables
let expr = MathCore::parse("x^2 + y^2").unwrap();

let d_dx = Calculus::differentiate(&expr, "x").unwrap();
let d_dy = Calculus::differentiate(&expr, "y").unwrap();

println!("∂/∂x = {}", d_dx); // 2*x
println!("∂/∂y = {}", d_dy); // 2*y
pub fn differentiate(expr: &Expr, var: &str) -> Result<Expr, MathError>
The result is automatically simplified with simplify_basic before being returned — e.g. 1 * x becomes x, 0 + f becomes f.

Partial derivatives (multivariate expressions)

For expressions in more than one variable, differentiate with respect to each variable in turn using the same API:
use mathcore::MathCore;
use mathcore::calculus::Calculus;

let expr = MathCore::parse("x^3 + 3*x*y^2 + y^3").unwrap();

let d_dx = Calculus::differentiate(&expr, "x").unwrap();
let d_dy = Calculus::differentiate(&expr, "y").unwrap();

println!("∂f/∂x = {}", d_dx); // 3*x^2 + 3*y^2
println!("∂f/∂y = {}", d_dy); // 6*x*y + 3*y^2
Chain partial derivatives to compute second-order mixed partials. Because Calculus::differentiate takes any Expr, you can pass the result of a first differentiation straight back in:
let d2_dxdy = Calculus::differentiate(&d_dx, "y").unwrap();
println!("∂²f/∂x∂y = {}", d2_dxdy);

Higher-order derivatives

Passing the result of one call back into differentiate gives higher-order derivatives:
use mathcore::MathCore;
use mathcore::calculus::Calculus;

let expr = MathCore::parse("x^4").unwrap();

let d1 = Calculus::differentiate(&expr, "x").unwrap();  // 4*x^3
let d2 = Calculus::differentiate(&d1, "x").unwrap();    // 12*x^2
let d3 = Calculus::differentiate(&d2, "x").unwrap();    // 24*x
let d4 = Calculus::differentiate(&d3, "x").unwrap();    // 24

println!("f''''(x) = {}", d4);

Unsupported operations

When MathCore encounters a function it does not have a built-in derivative rule for (anything outside the table above), it returns a symbolic Derivative node rather than erroring out:
use mathcore::MathCore;

// "arctan" has no built-in rule — result is a Derivative node
let deriv = MathCore::differentiate("arctan(x)", "x").unwrap();
println!("{}", deriv); // d/dx[arctan(x)]
A Derivative node in the result means the derivative is unevaluated — MathCore could not reduce it further. You can still display or manipulate the expression, but numeric evaluation of such a node will return an error unless you evaluate it with a custom engine extension.

Build docs developers (and LLMs) love