Solving Equations

Find solutions to equations symbolically and numerically.

📐

Mathematical Definition

Linear Equation:

ax+b=0    x=baax + b = 0 \implies x = -\frac{b}{a}

Quadratic Formula:

ax2+bx+c=0    x=b±b24ac2aax^2 + bx + c = 0 \implies x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

Discriminant (Δ\Delta):

Δ=b24ac\Delta = b^2 - 4ac
- Δ>0\Delta > 0: Two distinct real roots - Δ=0\Delta = 0: One repeated real root - Δ<0\Delta < 0: Two complex conjugate roots

Matrix Equations (Noncommutative): - Left division: AX=B    X=A1BAX = B \implies X = A^{-1}B - Right division: XA=B    X=BA1XA = B \implies X = BA^{-1} - Note: A1BBA1A^{-1}B \neq BA^{-1} for matrices!

Code Examples

Linear Equations

Solve ax + b = 0

use mathhook::prelude::*;

let x = symbol!(x);

// Solve: 2x + 3 = 0
let eq1 = expr!(2 * x + 3);
let mut solver = MathSolver::new();
let sol1 = solver.solve(&eq1, &x);
// Result: x = -3/2

// Solve: 5x - 10 = 0
let eq2 = expr!(5 * x - 10);
let sol2 = solver.solve(&eq2, &x);
// Result: x = 2

Quadratic Equations

Solve ax² + bx + c = 0

use mathhook::prelude::*;

let x = symbol!(x);

// Solve: x² - 5x + 6 = 0
let eq1 = expr!(x ^ 2 - 5 * x + 6);
let mut solver = MathSolver::new();
let solutions = solver.solve(&eq1, &x);
// Result: [x = 2, x = 3]

// Solve: x² - 4 = 0 (difference of squares)
let eq2 = expr!(x ^ 2 - 4);
let sol2 = solver.solve(&eq2, &x);
// Result: [x = -2, x = 2]

Complex Roots

When discriminant is negative

use mathhook::prelude::*;

let x = symbol!(x);

// Solve: x² + 1 = 0 (complex roots)
let equation = expr!(x ^ 2 + 1);
let mut solver = MathSolver::new();
let solutions = solver.solve(&equation, &x);
// Result: [x = i, x = -i]

// Solve: x² - 2x + 5 = 0
// Discriminant: 4 - 20 = -16 < 0 (complex roots)
let eq2 = expr!(x ^ 2 - 2 * x + 5);
let sol2 = solver.solve(&eq2, &x);
// Result: [x = 1 + 2i, x = 1 - 2i]

Transcendental Equations

Trigonometric, exponential, logarithmic

use mathhook::prelude::*;

let x = symbol!(x);

// Solve: sin(x) = 0
let eq1 = expr!(sin(x));
let mut solver = MathSolver::new();
let solutions = solver.solve(&eq1, &x);
// Result: [x = 0, x = π, x = 2π, ...] (infinitely many)

// Solve: e^x = 5
let eq2 = expr!(exp(x) - 5);
let sol2 = solver.solve(&eq2, &x);
// Result: x = ln(5)

// Solve: log(x) = 2
let eq3 = expr!(log(x) - 2);
let sol3 = solver.solve(&eq3, &x);
// Result: x = e² (if natural log)

Matrix Equations (Noncommutative)

Left and right division for matrices

use mathhook::prelude::*;

// Matrix symbols
let A = symbol!(A; matrix);
let X = symbol!(X; matrix);
let B = symbol!(B; matrix);

// Left division: A*X = B → X = A⁻¹*B
let left_eq = expr!(A * X - B);
let mut solver = MathSolver::new();
let solution_left = solver.solve(&left_eq, &X);
// Result: X = A⁻¹*B

// Right division: X*A = B → X = B*A⁻¹
let right_eq = expr!(X * A - B);
let solution_right = solver.solve(&right_eq, &X);
// Result: X = B*A⁻¹

// Note: A⁻¹*B ≠ B*A⁻¹ for matrices!

🔗 Related Topics