Simple Matrix Arithmetic
1 Introduction
1.1 Exact Numbers
matrix?
column
2 Arithmetic
matrix+
matrix*
matrix-expt
transpose
determinant
3 Utilities
adjugate
invert-matrix
9.3

Simple Matrix Arithmetic🔗ℹ

Deren Dohoda <deren.dohoda@gmail.com>

 (require simple-matrix) package: simple-matrix

1 Introduction🔗ℹ

This library is used to perform some simple matrix arithmetic like multiplication, transposition, addition, and inversion. It is truly "simple" and very naive in its implementation but it will do its job without much overhead from typed racket.

Matrices are represented as lists of rows, which are lists of numbers. So a 3x3 identity matrix would look like '((1 0 0) (0 1 0) (0 0 1)).

1.1 Exact Numbers🔗ℹ

The library is intended to be used with exact numbers: integers and rationals. determinant and invert-matrix decide whether a matrix is singular by testing for an exact zero, with no tolerance. With floating-point entries, rounding can make a singular matrix look invertible, so invert-matrix may return a matrix of huge entries instead of #f. Floating-point numbers still work for well-behaved matrices, but nothing checks the results.

procedure

(matrix? maybe-m) → boolean?

  maybe-m : any/c
Determines if the argument is a matrix in the sense this library understands.

procedure

(column vector) → matrix?

  vector : (listof number?)
A convenience for turning a simple list of numbers into a column vector.

Example:
> (column '(1 2 -2))

'((1) (2) (-2))

2 Arithmetic🔗ℹ

procedure

(matrix+ m ...+) → matrix?

  m : matrix?
Adds one or more matrices element-wise. All of them must have the same dimensions. With one argument, it is returned unchanged. It is an error to give no arguments.

Example:
> (matrix+ '((1 2) (3 4)) '((10 20) (30 40)) '((100 200) (300 400)))

'((111 222) (333 444))

procedure

(matrix* m ...+) → (or/c matrix? number?)

  m : (or/c matrix? number?)
Multiplies one or more arguments, left to right, using matrix multiplication. An argument may be a number, which scales the other factor. With one argument, it is returned unchanged. It is an error to give no arguments, or if the sizes of adjacent matrices do not correspond to proper matrix multiplication.

Examples:
> (matrix* 2 '((1 2) (3 4)) '((1 0) (0 1)))

'((2 4) (6 8))

> (matrix* '((1 2 3)) (column '(4 5 6)))

'((32))

procedure

(matrix-expt m e) → matrix?

  m : matrix?
  e : exact-nonnegative-integer?
Repeated multiplication of a square matrix. An exponent of 0 gives the identity matrix of the same size.

procedure

(transpose m) → matrix?

  m : matrix?
Exchange rows and columns of a matrix.

procedure

(determinant m) → number?

  m : matrix?
Calculate the determinant of a square matrix. Singular matrices are detected exactly; see Exact Numbers.

3 Utilities🔗ℹ

procedure

(adjugate m) → matrix?

  m : matrix?
The transpose of the cofactor matrix. Not efficiently calculated.

procedure

(invert-matrix m) → (or/c matrix? #f)

  m : matrix?
Attempts to invert a square matrix. If the matrix has no inverse, then this procedure evaluates to #f. Singular matrices are detected exactly; see Exact Numbers.

Examples:
> (invert-matrix '((2 1) (1 3)))

'((3/5 -1/5) (-1/5 2/5))

> (invert-matrix '((1 2) (2 4)))

#f