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:
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.
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))
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)
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
> B
> (matrix-add A B)
> (matrix-sub A B)
> (matrix-mul A B)
Scale a matrix by a factor 2.
> (matrix-scale 2 (matrix 2 2 1 2 3 4))
Multiply a matrix on a column-vector.
> (matrix-mul (matrix 2 2 1 2 3 4) (column-vector 1 0))
2 Constructors
> (matrix 2 2 1 2 3 4)
(row-vector x ...) → matrix x : number?
> (row-vector 1 2 3)
(column-vector x ...) → matrix x : number?
> (column-vector 1 2 3)
(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)
(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 +)
Construct a mxn-matrix with elements from row i to i+m and from column j to j+m.
(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))
(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)))
(matrix-copy M) → matrix M : matrix?
Copy the matrix M.
> (matrix-copy (matrix 1 2 3 4))
(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
> (matrix-row M 1)
(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
> (matrix-column M 2)
(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-identity 3)
> (matrix-identity 3 2)
(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))
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))
(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)) '(
![]()
![]()
)
(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.
(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)) '(
![]()
![]()
)