On this page:
Matrix
matrix?
row-matrix?
col-matrix?
square-matrix?
matrix-shape
matrix-num-rows
matrix-num-cols
square-matrix-size

7.2 Types, Predicates and Accessors

syntax

(Matrix A)

Equivalent to (Array A), but used for values M for which (matrix? M) is #t.

procedure

(matrix? arr)  Boolean

  arr : (Array A)
Returns #t when arr is a matrix: a nonempty array with exactly two axes.

Examples:

> (matrix? (array 10))

- : Boolean

#f

> (matrix? (array #[1 2 3]))

- : Boolean

#f

> (matrix? (make-array #(5 0) 0))

- : Boolean

#f

> (matrix? (array #[#[1 0] #[0 1]]))

- : Boolean

#t

procedure

(row-matrix? arr)  Boolean

  arr : (Array A)
Returns #t when arr is a row matrix: a matrix with exactly one row.

procedure

(col-matrix? arr)  Boolean

  arr : (Array A)
Returns #t when arr is a column matrix: a matrix with exactly one column.

procedure

(square-matrix? arr)  Boolean

  arr : (Array A)
Returns #t when arr is a matrix with the same number of rows and columns.

procedure

(matrix-shape M)  (Values Index Index)

  M : (Matrix A)
Returns M’s row and column count, respectively. Raises an error if (matrix? M) is #f.

Examples:

> (matrix-shape (row-matrix [1 2 3]))

- : (values Integer Integer) [more precisely: (Values Index Index)]

1

3

> (matrix-shape (col-matrix [1 2 3]))

- : (values Integer Integer) [more precisely: (Values Index Index)]

3

1

> (matrix-shape (identity-matrix 3))

- : (values Integer Integer) [more precisely: (Values Index Index)]

3

3

procedure

(matrix-num-rows M)  Index

  M : (Matrix A)
Returns the number of rows in M, or the first value of (matrix-shape M).

procedure

(matrix-num-cols M)  Index

  M : (Matrix A)
Returns the number of columns in M, or the second value of (matrix-shape M).

procedure

(square-matrix-size M)  Index

  M : (Matrix A)
Returns the number of rows/columns in M. Raises an error if (square-matrix? M) is #f.

Examples:

> (square-matrix-size (identity-matrix 3))

- : Integer [more precisely: Index]

3

> (square-matrix-size (row-matrix [1 2 3]))

- : Integer [more precisely: Index]

square-matrix-size: contract violation

  expected: square-matrix?

  given: (array #[#[1 2 3]])