Expansion and Factoring
Transform expressions between expanded and factored forms for easier manipulation and analysis.
📐
Mathematical Definition
Distributive Law:
Binomial Expansion:
For small powers: -
-
-
Special Products: - Difference of Squares: - Perfect Square Trinomial:
Noncommutative Expansion: For matrices (noncommutative):
Code Examples
Simple Products
Expand products of sums
use mathhook::prelude::*;
let x = symbol!(x);
// Expand 2(x + 3)
let expr1 = expr!(2 * (x + 3));
let expanded1 = expr1.expand();
// Result: 2x + 6
// Expand (x + 1)(x + 2)
let expr2 = expr!((x + 1) * (x + 2));
let expanded2 = expr2.expand();
// Result: x² + 3x + 2
// Expand (x + y)(x - y) - difference of squares
let y = symbol!(y);
let expr3 = expr!((x + y) * (x - y));
let expanded3 = expr3.expand();
// Result: x² - y²
Power Expansion
Expand expressions raised to integer powers
use mathhook::prelude::*;
let x = symbol!(x);
let y = symbol!(y);
// Expand (x + 1)^2
let expr1 = expr!((x + 1) ^ 2);
let expanded1 = expr1.expand();
// Result: x² + 2x + 1
// Expand (x + y)^3
let expr2 = expr!((x + y) ^ 3);
let expanded2 = expr2.expand();
// Result: x³ + 3x²y + 3xy² + y³
// Expand (x - 2)^4
let expr3 = expr!((x - 2) ^ 4);
let expanded3 = expr3.expand();
// Result: x⁴ - 8x³ + 24x² - 32x + 16
Noncommutative Matrix Expansion
For matrices, order matters - (A+B)² has 4 terms
use mathhook::prelude::*;
// Create matrix symbols
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);
let C = symbol!(C; matrix);
// Expand (A + B)^2 - preserves noncommutativity
let expr = expr!((A + B) ^ 2);
let expanded = expr.expand();
// Result: A² + AB + BA + B² (4 terms, NOT A² + 2AB + B²)
// Expand (A + B)(C)
let expr2 = expr!((A + B) * C);
let expanded2 = expr2.expand();
// Result: AC + BC (order preserved: A*C first, then B*C)