Solving Equations
Find solutions to equations symbolically and numerically.
📐
Mathematical Definition
Linear Equation:
Quadratic Formula:
Discriminant ():
- : Two distinct real roots
- : One repeated real root
- : Two complex conjugate roots
Matrix Equations (Noncommutative): - Left division: - Right division: - Note: 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!