Matrix Operations and Noncommutative Algebra
Symbolic and numeric matrix operations with full support for noncommutative algebra. Matrix symbols preserve multiplication order (AB ≠ BA), enabling correct handling of linear algebra, quantum mechanics operators, and transformation matrices.
Code Examples
Creating Matrix Symbols
Matrix symbols are noncommutative - order matters
use mathhook::prelude::*;
// Matrix symbols (noncommutative)
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);
let X = symbol!(X; matrix);
// Matrix multiplication - ORDER MATTERS!
let product_ab = expr!(A * B); // A*B
let product_ba = expr!(B * A); // B*A ≠ A*B in general!
// Matrix equation: A*X = B
let equation = expr!(A * X);
Creating Numeric Matrices
Build matrices from expression arrays
use mathhook::prelude::*;
use mathhook::Expression;
let x = symbol!(x);
// 2×2 matrix with symbolic entries
let matrix = Expression::matrix(vec![
vec![expr!(x), expr!(1)],
vec![expr!(0), expr!(x)],
]);
// 3×3 identity matrix
let identity = Expression::identity_matrix(3);
// Zero matrix (2 rows, 3 columns)
let zero = Expression::zero_matrix(2, 3);
Matrix Multiplication (Critical!)
Order matters - demonstrate noncommutativity
use mathhook::prelude::*;
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);
let C = symbol!(C; matrix);
// Order matters!
let ab = expr!(A * B); // AB
let ba = expr!(B * A); // BA ≠ AB in general
// Associative (but not commutative)
let abc_left = expr!((A * B) * C); // (AB)C
let abc_right = expr!(A * (B * C)); // A(BC)
// (AB)C = A(BC) always
// Mixed scalar-matrix
let x = symbol!(x);
let xAB = expr!(x * A * B); // x(AB) = (xA)B = A(xB)
Left Division vs Right Division
Solving matrix equations correctly
use mathhook::prelude::*;
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 = MatrixEquationSolver::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!
Matrix Operations: Inverse and Determinant
Compute matrix properties
use mathhook::prelude::*;
use mathhook::Expression;
let A = symbol!(A; matrix);
// Symbolic inverse
let A_inv = expr!(A ^ (-1)); // A^(-1)
// Symbolic determinant
let det_A = expr!(det(A));
// Numeric determinant
let matrix = Expression::matrix(vec![
vec![expr!(1), expr!(2)],
vec![expr!(3), expr!(4)],
]);
let det = expr!(det(matrix));
// Evaluates to: 1*4 - 2*3 = -2
Real-World Application: Quantum Mechanics
Pauli matrices and commutation relations
use mathhook::prelude::*;
use mathhook::Expression;
// Pauli matrices
let sigma_x = Expression::matrix(vec![
vec![expr!(0), expr!(1)],
vec![expr!(1), expr!(0)],
]);
let i = Expression::i(); // Imaginary unit
let sigma_y = Expression::matrix(vec![
vec![expr!(0), expr!(-i)],
vec![i, expr!(0)],
]);
let sigma_z = Expression::matrix(vec![
vec![expr!(1), expr!(0)],
vec![expr!(0), expr!(-1)],
]);
// Commutation relations: [σ_x, σ_y] = 2iσ_z
let comm_xy = expr!((sigma_x * sigma_y) - (sigma_y * sigma_x));
let expected = expr!(2 * i * sigma_z);
// Verify
let difference = expr!(comm_xy - expected);
let simplified = difference.simplify();
// Should equal zero matrix