Symbolic Calculus: Differentiation and Integration

Symbolic differentiation and integration using automatic differentiation rules, integration strategies, and the Risch algorithm. Supports chain rule, product rule, quotient rule, and comprehensive integration techniques from table lookup to complete Risch algorithm for elementary functions.

Code Examples

Basic Differentiation

Compute derivatives using power rule and chain rule

use mathhook::prelude::*;

let x = symbol!(x);
let expr = expr!(x ^ 3);

// First derivative: 3x^2
let derivative = expr.derivative(&x, 1);

// Second derivative: 6x
let second_derivative = expr.derivative(&x, 2);

// Complex function with chain rule
let expr = expr!(sin(x ^ 2));
let deriv = expr.derivative(&x, 1);
// Result: cos(x^2) * 2x

Product and Quotient Rules

Differentiate products and quotients

use mathhook::prelude::*;

let x = symbol!(x);

// Product rule: d/dx(x^2 * x^3) = 2x * x^3 + x^2 * 3x^2 = 5x^4
let f = expr!(x ^ 2);
let g = expr!(x ^ 3);
let product = expr!(mul: f, g);
let deriv = product.derivative(&x, 1);
// Result: 5*x^4

// Quotient rule: d/dx(x^2 / (x+1))
let numerator = expr!(x ^ 2);
let denominator = expr!(x + 1);
let quotient = expr!(div: numerator, denominator);
let deriv = quotient.derivative(&x, 1);
// Result: (2*x*(x+1) - x^2*1) / (x+1)^2

Partial Derivatives (Multivariable)

Compute partial derivatives with respect to each variable

use mathhook::prelude::*;

let x = symbol!(x);
let y = symbol!(y);
let expr = expr!((x ^ 2) * y);

// Partial derivative with respect to x
let df_dx = expr.derivative(&x, 1);
// Result: 2*x*y

// Partial derivative with respect to y
let df_dy = expr.derivative(&y, 1);
// Result: x^2

Basic Integration

Symbolic integration using layered strategy

use mathhook::prelude::*;
use mathhook::calculus::integrals::Integration;

let x = symbol!(x);

// Simple polynomial (Layer 1: Table Lookup)
let expr = expr!(x ^ 2);
let result = expr.integrate(x.clone());
// Result: x^3/3 + C

// Rational function (Layer 2: Partial fractions)
let expr = expr!(1 / (x + 1));
let result = expr.integrate(x.clone());
// Result: ln|x+1| + C

// Trigonometric (Layer 3: Function registry)
let expr = expr!(sin(x));
let result = expr.integrate(x.clone());
// Result: -cos(x) + C

Integration by Parts and Substitution

Advanced integration techniques

use mathhook::prelude::*;
use mathhook::calculus::integrals::Integration;

let x = symbol!(x);

// Integration by parts: ∫x*e^x dx
let expr = expr!(x * exp(x));
let result = expr.integrate(x.clone());
// Result: e^x(x-1) + C

// U-substitution: ∫2x*sin(x^2) dx
let expr = expr!(2 * x * sin(x ^ 2));
let result = expr.integrate(x.clone());
// Result: -cos(x^2) + C

Real-World Application: Velocity and Acceleration

Physics application of derivatives

use mathhook::prelude::*;

let t = symbol!(t);
let position = expr!((t ^ 3) - (6 * (t ^ 2)) + (9 * t));

let velocity = position.derivative(&t, 1);
// v(t) = 3t^2 - 12t + 9

let acceleration = position.derivative(&t, 2);
// a(t) = 6t - 12

// Find when velocity is zero (critical points)
// Solve: 3t^2 - 12t + 9 = 0

🔗 Related Topics