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



Noncommutative Algebra

Topic: advanced.noncommutative_algebra

Support for noncommutative algebra in MathHook with four symbol types: Scalar (commutative), Matrix, Operator, and Quaternion (all noncommutative). Essential for quantum mechanics, linear algebra, and 3D rotations.

Mathematical Definition

Noncommutative multiplication:

Symbol types:

  • Scalar: (commutative)
  • Matrix: (noncommutative)
  • Operator:
  • Quaternion: , but

Examples

Matrix Symbols

Create noncommutative matrix symbols

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

// Order matters!
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  # Noncommutative
BA = B * A  # Different!

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

const AB = A.mul(B);  // Noncommutative
const BA = B.mul(A);  // Different!

Quantum Operators

Position and momentum operators (canonical commutation relation)

Rust
#![allow(unused)]
fn main() {
let x = symbol!(x; operator);
let p = symbol!(p; operator);

// Commutator: [x, p] = xp - px
let commutator = expr!((x * p) - (p * x));
// Physical result: [x, p] = iℏ

}
Python
x = Operator('x')
p = Operator('p')

# Commutator
commutator = Commutator(x, p)
# Result: I*hbar

JavaScript
const x = symbol('x', {type: 'operator'});
const p = symbol('p', {type: 'operator'});

// Commutator
const comm = x.mul(p).sub(p.mul(x));

Quaternions

3D rotation with quaternion multiplication

Rust
#![allow(unused)]
fn main() {
let i = symbol!(i; quaternion);
let j = symbol!(j; quaternion);

let ij = expr!(i * j);  // i*j = k
let ji = expr!(j * i);  // j*i = -k (different!)

}
Python
from sympy.algebras.quaternion import Quaternion

i = Quaternion(0, 1, 0, 0)
j = Quaternion(0, 0, 1, 0)

ij = i * j  # k
ji = j * i  # -k

JavaScript
const i = symbol('i', {type: 'quaternion'});
const j = symbol('j', {type: 'quaternion'});

const ij = i.mul(j);  // k
const ji = j.mul(i);  // -k

LaTeX Type Inference

Parser infers types from LaTeX notation

Rust
#![allow(unused)]
fn main() {
let parser = Parser::new(ParserConfig::default());

// Bold notation → Matrix type
let eq1 = parser.parse(r"\mathbf{A}\mathbf{X} = \mathbf{B}").unwrap();

// Hat notation → Operator type
let eq2 = parser.parse(r"\hat{H}\hat{\psi} = E\hat{\psi}").unwrap();

}
Python
from sympy.parsing.latex import parse_latex

# Bold → Matrix
eq1 = parse_latex(r'\mathbf{A}\mathbf{X} = \mathbf{B}')

# Hat → Operator
eq2 = parse_latex(r'\hat{H}\hat{\psi} = E\hat{\psi}')

JavaScript
const parser = new Parser();

// Bold → Matrix
const eq1 = parser.parse('\\mathbf{A}\\mathbf{X} = \\mathbf{B}');

// Hat → Operator
const eq2 = parser.parse('\\hat{H}\\hat{\\psi} = E\\hat{\\psi}');

API Reference

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

See Also