7.6 Polymorphic Operations

► |  | Math Library

1 | Constants and Elementary Functions
2 | Flonums
3 | Special Functions
4 | Number Theory
5 | Arbitrary-Precision Floating-Point Numbers (Bigfloats)
6 | Arrays
7 | Matrices and Linear Algebra
8 | Statistics Functions
9 | Probability Distributions
10 | Stuff That Doesn’t Belong Anywhere Else

▼ | 7 | Matrices and Linear Algebra

7.1 | Introduction
7.2 | Types, Predicates and Accessors
7.3 | Construction
7.4 | Conversion
7.5 | Entrywise Operations and Arithmetic
7.6 | Polymorphic Operations
7.7 | Basic Operations
7.8 | Inner Product Space Operations
7.9 | Solving Systems of Equations
7.10 | Row-Based Algorithms
7.11 | Orthogonal Algorithms
7.12 | Operator Norms and Comparing Matrices

On this page:

matrix-ref
matrix-row
matrix-col
submatrix
matrix-diagonal
matrix-upper-triangle
matrix-lower-triangle
matrix-rows
matrix-cols
matrix-augment
matrix-stack
matrix-set-col
matrix-set-row
matrix-map-rows
matrix-map-cols

Racket
top contents ← prev up next →

### 7.6 Polymorphic Operations🔗ℹ

> procedure (matrix-ref M i j) → A
> M : (Matrix A)
> i : Integer
> j : Integer

Returns the entry on row i and column j.

Examples:

> > (define M (matrix ([1 2 3] [4 5 6])))
> > (matrix-ref M 0 2)
> 3
> > (matrix-ref M 1 2)
> 6

> procedure (matrix-row M i) → (Matrix A)
> M : (Matrix A)
> i : Integer
> procedure (matrix-col M j) → (Matrix A)
> M : (Matrix A)
> j : Integer

Returns the ith row or jth column as a matrix.

Examples:

> > (define M (matrix ([1 2 3] [4 5 6])))
> > (matrix-row M 1)
> (array #[#[4 5 6]])
> > (matrix-col M 0)
> (array #[#[1] #[4]])

> procedure (submatrix M is js) → (Array A)
> M : (Matrix A)
> is : (U Slice (Sequenceof Integer))
> js : (U Slice (Sequenceof Integer))

Returns a submatrix or subarray of M, where is and js specify respectively the rows and columns to keep. Like array-slice-ref, but constrained so the result has exactly two axes.

Examples:

> > (submatrix (identity-matrix 5) (:: 1 #f 2) (::))
> - : #(struct:Array
> (Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))
> #<syntax:.../array/typed-array-struct.rkt:56:13 prop:equal+hash>
> #<syntax:.../array/typed-array-struct.rkt:55:13 prop:custom-write>
> #<syntax:.../array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
> (array #[#[0 1 0 0 0] #[0 0 0 1 0]])
> > (submatrix (identity-matrix 5) '() '(1 2 4))
> - : #(struct:Array
> (Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))
> #<syntax:.../array/typed-array-struct.rkt:56:13 prop:equal+hash>
> #<syntax:.../array/typed-array-struct.rkt:55:13 prop:custom-write>
> #<syntax:.../array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
> (array #[])

Note that submatrix may return an empty array, which is not a matrix.

> procedure (matrix-diagonal M) → (Array A)
> M : (Matrix A)

Returns array of the entries on the diagonal of M.

Example:

> > (matrix-diagonal
> (matrix ([1 2 3] [4 5 6] [7 8 9])))
> (array #[1 5 9])

> procedure (matrix-upper-triangle M [zero]) → (Matrix A)
> M : (Matrix A)
> zero : A = 0
> procedure (matrix-lower-triangle M [zero]) → (Matrix A)
> M : (Matrix A)
> zero : A = 0

The function matrix-upper-triangle returns an upper triangular matrix (entries below the diagonal have the value zero) with entries from the given matrix. Likewise the function matrix-lower-triangle returns a lower triangular matrix.

Examples:

> > (define M (array+ (array 1) (axis-index-array #(5 7) 1)))
> > M
> - : #(struct:Array
> (Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Fixnum))
> #<syntax:/build/racket/src/racket-9.1/share/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
> #<syntax:/build/racket/src/racket-9.1/share/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
> #<syntax:/build/racket/src/racket-9.1/share/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
> (array
> #[#[1 2 3 4 5 6 7]
> #[1 2 3 4 5 6 7]
> #[1 2 3 4 5 6 7]])
> > (matrix-upper-triangle M)
> - : (Array Nonnegative-Fixnum)
> (array
> #[#[1 2 3 4 5 6 7]
> #[0 2 3 4 5 6 7]
> #[0 0 3 4 5 6 7]
> #[0 0 0 4 5 6 7]
> #[0 0 0 0 5 6 7]])
> > (matrix-lower-triangle M)
> - : (Array Nonnegative-Fixnum)
> (array
> #[#[1 0 0 0 0 0 0]
> #[1 2 0 0 0 0 0]
> #[1 2 3 0 0 0 0]
> #[1 2 3 4 0 0 0]
> #[1 2 3 4 5 0 0]])
> > (matrix-lower-triangle (array->flarray M) 0.0)
> - : (Array Flonum)
> (array
> #[#[1.0 0.0 0.0 0.0 0.0 0.0 0.0]
> #[1.0 2.0 0.0 0.0 0.0 0.0 0.0]
> #[1.0 2.0 3.0 0.0 0.0 0.0 0.0]
> #[1.0 2.0 3.0 4.0 0.0 0.0 0.0]
> #[1.0 2.0 3.0 4.0 5.0 0.0 0.0]])

> procedure (matrix-rows M) → (Listof (Matrix A))
> M : (Matrix A)
> procedure (matrix-cols M) → (Listof (Matrix A))
> M : (Matrix A)

The functions respectively returns a list of the rows or columns of the matrix.

Examples:

> > (define M (matrix ([1 2 3] [4 5 6])))
> > (matrix-rows M)
> (list (array #[#[1 2 3]]) (array #[#[4 5 6]]))
> > (matrix-cols M)
> (list (array #[#[1] #[4]]) (array #[#[2] #[5]]) (array #[#[3] #[6]]))

> procedure (matrix-augment Ms) → (Matrix A)
> Ms : (Listof (Matrix A))
> procedure (matrix-stack Ms) → (Matrix A)
> Ms : (Listof (Matrix A))

The function matrix-augment returns a matrix whose columns are the columns of the matrices in Ms. The matrices in list must have the same number of rows.

The function matrix-stack returns a matrix whose rows are the rows of the matrices in Ms. The matrices in list must have the same number of columns.

Examples:

> > (define M0 (matrix ([1 1] [1 1])))
> > (define M1 (matrix ([2 2] [2 2])))
> > (define M2 (matrix ([3 3] [3 3])))
> > (matrix-augment (list M0 M1 M2))
> (array #[#[1 1 2 2 3 3] #[1 1 2 2 3 3]])
> > (matrix-stack (list M0 M1 M2))
> (array #[#[1 1] #[1 1] #[2 2] #[2 2] #[3 3] #[3 3]])

> procedure (matrix-set-col M idx new-col) → (Matrix A)
> M : (Matrix A)
> idx : Integer
> new-col : (Matrix A)
> procedure (matrix-set-row M idx new-row) → (Matrix A)
> M : (Matrix A)
> idx : Integer
> new-row : (Matrix A)

The function matrix-set-col returns a matrix whose idxth column is new-col. The function matrix-set-row returns a matrix whose idxth row is new-row.

Examples:

> > (define mat (matrix [[1 2 3]
> [4 5 6]
> [7 8 9]]))
> > (define new-col (col-matrix [-1 -2 -3]))
> > (define new-row (row-matrix [-1 -2 -3]))
> > (matrix-set-col mat 0 new-col)
> (array #[#[-1 2 3] #[-2 5 6] #[-3 8 9]])
> > (matrix-set-row mat 0 new-row)
> (array #[#[-1 -2 -3] #[4 5 6] #[7 8 9]])

> procedure (matrix-map-rows f M) → (Matrix B)
> f : ((Matrix A) -> (Matrix B))
> M : (Matrix A)
> (matrix-map-rows f M fail) → (U F (Matrix B))
> f : ((Matrix A) -> (U #f (Matrix B)))
> M : (Matrix A)
> fail : (-> F)

The two-argument case applies the function f to each row of M. If the rows are called r0, r1, ..., the result matrix has the rows (f r0), (f r1), ....

Examples:

> > (define M (matrix ([1 2 3] [4 5 6] [7 8 9] [10 11 12])))
> > (define (double-row r) (matrix-scale r 2))
> > (matrix-map-rows double-row M)
> (array #[#[2 4 6] #[8 10 12] #[14 16 18] #[20 22 24]])

In the three argument case, if f returns #f, the result of (fail) is returned:

> > (define Z (make-matrix 4 4 0))
> > Z
> - : (Array Zero)
> (array #[#[0 0 0 0] #[0 0 0 0] #[0 0 0 0] #[0 0 0 0]])
> > (matrix-map-rows (λ: ([r : (Matrix Real)])
> (matrix-normalize r 2 (λ () #f)))
> Z
> (λ () 'FAILURE))
> - : (U 'FAILURE (Array Real))
> 'FAILURE

> procedure (matrix-map-cols f M) → (Matrix B)
> f : ((Matrix A) -> (Matrix B))
> M : (Matrix A)
> (matrix-map-cols f M fail) → (U F (Matrix B))
> f : ((Matrix A) -> (U #f (Matrix B)))
> M : (Matrix A)
> fail : (-> F)

Like matrix-map-rows, but maps f over columns.
top contents ← prev up next →