Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help



Matrix Operations

Topic: advanced.matrices

Work with matrices symbolically and numerically in MathHook, with full support for noncommutative algebra where order matters. Create matrices, perform operations, and solve matrix equations.

Mathematical Definition

Matrix multiplication: For and :

Matrix inverse:

Determinant (2×2):

Examples

Creating Matrices

Create matrix symbols and numeric matrices

Rust
#![allow(unused)]
fn main() {
// Matrix symbols (noncommutative)
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);

// Numeric 2×2 matrix
let M = Expression::matrix(vec![
    vec![expr!(1), expr!(2)],
    vec![expr!(3), expr!(4)],
]);

}
Python
# Matrix symbols
A = MatrixSymbol('A', n, m)
B = MatrixSymbol('B', m, p)

# Numeric matrix
M = Matrix([[1, 2], [3, 4]])

JavaScript
// Matrix symbols
const A = symbol('A', {type: 'matrix'});
const B = symbol('B', {type: 'matrix'});

// Numeric matrix
const M = matrix([[1, 2], [3, 4]]);

Matrix Multiplication (Noncommutative)

AB ≠ BA in general

Rust
#![allow(unused)]
fn main() {
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);

let AB = expr!(A * B);  // A*B
let BA = expr!(B * A);  // B*A ≠ A*B

}
Python
A = MatrixSymbol('A', n, n)
B = MatrixSymbol('B', n, n)

AB = A * B  # Matrix product
BA = B * A  # Different result!

JavaScript
const A = symbol('A', {type: 'matrix'});
const B = symbol('B', {type: 'matrix'});

const AB = A.mul(B);  // A*B
const BA = B.mul(A);  // B*A ≠ A*B

Solving Linear System Ax=b

Solve matrix equation using inverse

Rust
#![allow(unused)]
fn main() {
let A = Expression::matrix(vec![
    vec![expr!(2), expr!(1)],
    vec![expr!(1), expr!(-1)],
]);
let b = Expression::matrix(vec![
    vec![expr!(5)],
    vec![expr!(1)],
]);

// Solution: x = A^(-1)*b
let x = expr!(A^(-1) * b);
// Result: [[2], [1]]

}
Python
A = Matrix([[2, 1], [1, -1]])
b = Matrix([[5], [1]])

# Solution
x = A.inv() * b
# Result: Matrix([[2], [1]])

JavaScript
const A = matrix([[2, 1], [1, -1]]);
const b = matrix([[5], [1]]);

// Solution
const x = A.inv().mul(b);
// Result: [[2], [1]]

Matrix Equation A*X=B (Left Division)

Solve for matrix unknown X

Rust
#![allow(unused)]
fn main() {
let solver = MatrixEquationSolver::new();
let A = symbol!(A; matrix);
let X = symbol!(X; matrix);
let B = symbol!(B; matrix);

let equation = expr!((A * X) - B);
let solution = solver.solve(&equation, &X);
// Returns: X = A^(-1)*B (left division)

}
Python
A, X, B = symbols('A X B', matrix=True)
equation = Eq(A*X, B)
solution = solve(equation, X)
# Returns: X = A^(-1)*B

JavaScript
const A = symbol('A', {type: 'matrix'});
const X = symbol('X', {type: 'matrix'});
const B = symbol('B', {type: 'matrix'});

const equation = A.mul(X).sub(B);
const solution = solve(equation, X);
// Returns: X = A.inv().mul(B)

Performance

Time Complexity: O(n^3) for n×n matrix operations

API Reference

  • Rust: mathhook_core::matrix
  • Python: mathhook.matrix
  • JavaScript: mathhook.matrix

See Also