1 A basic example
2 Constructors
matrix
row-vector
column-vector
make-matrix
build-matrix
submatrix
matrix-augment
matrix-augment*
matrix-copy
matrix-row
matrix-column
matrix-identity
matrix-diagonal
3 Operations
matrix-scale
matrix-add
matrix-mul
matrix-linear
4 Unary Operators
5 Comprehensions
6 Binary Operators
Version: 5.3.0.11

Matrix Library

> (matrix 2 3 1 2 3 4 5 6)
\(\left( \begin{array}{ccc}1&2&3\\4&5&6 \end{array} \right)\)

 (require (file "../matrix.rkt"))

A matrix is a rectangular array of numbers. A typical example:

image

The horizontal lines are called rows and the vertical lines are called columns. The matrix above has m=3 rows and n=2 columns and is an example of an 3x2-matrix. In general a matrix with m rows and n columns is called an mxn-matrix.

Matrices with one row (1xn) only are called row vectors and matrices with one column (mx1) only are called column vectors.

The entries or elements of a matrix is referred to by their row and column number. In this library the row and columns are counted from 0.

image

Note: In standard mathematical notation row and column numbers are counted from 1.

1 A basic example

First require the libary:

> (require "matrix.rkt")

Create a 2x3 matrix with entries 1, 2, 3, 4, 5, and, 6.
> (matrix 2 3  1 2 3 4 5 6)

'foo

(matrix 4 4 '#(1 -2 3/4 -1/2 0 0 1 -2 0 0 0 0 0 0 0 0))

(list (matrix 4 1 '#(1 0 0 0)) (matrix 4 1 '#(3/4 1 0 0)))

(list (matrix 4 1 '#(-2 0 0 0)) (matrix 4 1 '#(-1/2 -2 0 0)))

'(1 3)

(matrix 2 1 '#(7/2 7/5))

(matrix 2 3 '#(1 2 3 4 5 6))

Display the matrix as a picture:
> (matrix->pict
   (matrix 2 3  1 2 3 4 5 6))

image

Let’s change the print handler, so we don’t need to call "matrix->pict" ourselves.
> (current-print
   (let ([print (current-print)])
     (λ (v) (print (if (matrix? v) (matrix->pict v) v)))))
> (matrix 2 2  1 2 3 4)

image

The basic operations are addition, subtraction and multiplication.
> (define A (matrix 2 2  1 2 3 4))

'foo

(matrix 4 4 '#(1 -2 3/4 -1/2 0 0 1 -2 0 0 0 0 0 0 0 0))

(list (matrix 4 1 '#(1 0 0 0)) (matrix 4 1 '#(3/4 1 0 0)))

(list (matrix 4 1 '#(-2 0 0 0)) (matrix 4 1 '#(-1/2 -2 0 0)))

'(1 3)

(matrix 2 1 '#(7/2 7/5))

> (define B (matrix 2 2  1 -2 3 -4))
> A

image

> B

image

> (matrix-add A B)

image

> (matrix-sub A B)

image

> (matrix-mul A B)

image

Scale a matrix by a factor 2.
> (matrix-scale 2 (matrix 2 2  1 2 3 4))

image

Multiply a matrix on a column-vector.
> (matrix-mul (matrix 2 2  1 2 3 4)
              (column-vector 1 0))

image

2 Constructors

(matrix m n x ...)  matrix
  m : index/c
  n : index/c
  x : number?
Construct a mxn-matrix with elements x ....
> (matrix 2 2  1 2 3 4)

image

(row-vector x ...)  matrix
  x : number?
Construct a row vector (a 1xn-matrix) with elements x ....
> (row-vector 1 2 3)

image

(column-vector x ...)  matrix
  x : number?
Construct a column vector (a mx1-matrix) with elements x ....
> (column-vector 1 2 3)

image

(make-matrix m n x)  matrix
  m : size/c
  n : size/c
  x : number?
Construct a mxn-matrix where all entries are x.
> (make-matrix 2 3  4)

image

(build-matrix m n f)  matrix
  m : size/c
  n : size/c
  f : (index/c index/c -> number?)
Construct a mxn-matrix where element (i,j) is (f i j).
> (build-matrix 3 4 +)

image

(submatrix M i j m n)  matrix
  M : matrix?
  i : index/c
  j : index/c
  m : size/c
  n : size/c
Construct a mxn-matrix with elements from row i to i+m and from column j to j+m.
> (define A (build-matrix 5 5 (λ (i j) (+ (* i 5) j))))
> A

image

> (submatrix A 2 3 1 2)

image

(matrix-augment M N)  matrix
  M : matrix?
  N : matrix?
Augment the matrices M and N by "appending" their columns. The number of rows in M and N must be the same.
> (matrix-augment (matrix 2 2  1 2
                               3 4)
                  (matrix 2 3  5 6  7
                               8 9 10))

image

(matrix-augment* Ms)  matrix
  Ms : (listof matrix?)
Augment the matrices in the list Ms "appending" their columns. The number of rows in alle the matrices must be the same.
> (matrix-augment* (list (matrix 2 1  1 2)
                         (matrix 2 1  3 4)
                         (matrix 2 1  5 6)))

image

(matrix-copy M)  matrix
  M : matrix?
Copy the matrix M.
> (matrix-copy (matrix 1 2  3 4))

image

(matrix-row M i)  matrix
  M : matrix?
  i : index/c
Construct a row vector from the i’th column of M.
> (define M (matrix 2 4  1 2 3 4 5 6 7 8))
> M

image

> (matrix-row M 1)

image

(matrix-column M j)  matrix
  M : matrix?
  j : index/c
Construct a column vector from the j’th column of M.
> (define M (matrix 2 4  1 2 3 4 5 6 7 8))
> M

image

> (matrix-column M 2)

image

(matrix-identity m)  matrix
  m : size/c
(matrix-identity m n)  matrix
  m : size/c
  n : size/c
Return m x n matrix with ones on the diagonal and zeros elsewhere. If only one argument is given, a square matrix is produced.

(matrix-diagonal xs)  matrix
  xs : (listof number?)
Construct a square matrix with elements from xs on the diagonal and 0 elsewhere.
> (matrix-diagonal '(1 2 3))

image

3 Operations

(matrix-scale s M)  matrix
  s : number?
  M : matrix?
Multiply each element of M with s.
> (matrix-scale 3 (matrix 2 2  1 2 3 4))

image

(matrix-add M N)  matrix
  M : matrix?
  N : matrix?
Return the sum of M and N.
> (define M (matrix 2 2  1 2 3 4))
> (define N (matrix 2 2  1 -2 3 -4))
> (list M N (matrix-add M N))

'(image image image)

(matrix-mul M N)  matrix
  M : matrix?
  N : matrix?
Return the product of M and N. The number of columns in M must be equal to the number of rows in N.
> (define M (matrix 2 2  1 2 3 4))
> (define N (matrix 2 2  2 0 0 1))
> (list M N (matrix-mul M N))

'(image image image)

(matrix-linear a M b N)  matrix
  a : number?
  M : matrix?
  b : number?
  N : matrix?
Return the linear combination of M and N \(a*M + b*N\).
> (define M (matrix 2 2  1 1 1 0))
> (define N (matrix 2 2  0 0 1 1))
> (list M N (matrix-linear -1 M 2 N))

'(image image image)

4 Unary Operators

5 Comprehensions

6 Binary Operators