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-map-rows
matrix-map-cols

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) (::))

- : (Array (U Zero One))

(array #[#[0 1 0 0 0] #[0 0 0 1 0]])

> (submatrix (identity-matrix 5) '() '(1 2 4))

- : (Array (U Zero One))

(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

- : (Array Positive-Fixnum)

(array

 #[#[1 2 3 4 5 6 7]

   #[1 2 3 4 5 6 7]

   #[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-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 (Array Real) 'FAILURE)

'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.