Substitution
Replace variables with values or expressions to evaluate, simplify, or transform expressions.
📐
Mathematical Definition
Function Evaluation:
Substitute into function .
Composition:
Substitute into function .
U-Substitution (Integration):
where and .
Change of Variables (Multivariable):
Code Examples
Single Variable Substitution
Replace variable with number
use mathhook::prelude::*;
let x = symbol!(x);
// Substitute x = 2 into x² + 3x
let expr1 = expr!(x ^ 2 + 3 * x);
let result1 = expr1.substitute(&x, &expr!(2));
// Result: 4 + 6 = 10
// Substitute x = -1 into x³ - 2x + 1
let expr2 = expr!(x ^ 3 - 2 * x + 1);
let result2 = expr2.substitute(&x, &expr!(-1));
// Result: -1 + 2 + 1 = 2
Expression Substitution
Replace with another expression
use mathhook::prelude::*;
let x = symbol!(x);
let y = symbol!(y);
// Substitute x = y + 1 into x² + 3x
let expression = expr!(x ^ 2 + 3 * x);
let substituted = expression.substitute(&x, &expr!(y + 1));
// Result: (y+1)² + 3(y+1)
// Expand for cleaner form
let expanded = substituted.expand();
// Result: y² + 2y + 1 + 3y + 3 = y² + 5y + 4
U-Substitution for Integration
Transform difficult integrals
use mathhook::prelude::*;
use integrals::Integration;
let x = symbol!(x);
let u = symbol!(u);
// Integrate: ∫ 2x·e^(x²) dx
// Let u = x², then du = 2x dx
let integrand = expr!(2 * x * exp(x ^ 2));
// Manual substitution
let u_expr = expr!(x ^ 2); // u = x²
let integrand_u = integrand.substitute(&expr!(x ^ 2), &u);
// Result: ∫ e^u du = e^u + C
// Back-substitute: e^(x²) + C
let result = expr!(exp(u)).substitute(&u, &u_expr);
// Result: e^(x²) + C