7.4 Conversion
Convert a flat list to an
m×
n matrix and back;
both
m and
n must be positive, and
(* m n) = (length xs).
The entries in
xs are in row-major order.
Examples: |
> (list->matrix 2 3 '(1 2 3 4 5 6)) | - : (Array Positive-Byte) | (array #[#[1 2 3] #[4 5 6]]) | > (matrix->list (matrix [[1 2] [3 4] [5 6]])) | - : (Listof Positive-Byte) | '(1 2 3 4 5 6) |
|
Examples: |
> (vector->matrix 2 3 #(1 2 3 4 5 6)) | - : (Mutable-Array Integer) | (mutable-array #[#[1 2 3] #[4 5 6]]) | > (matrix->vector (matrix [[1 2] [3 4] [5 6]])) | - : (Vectorof Integer) | '#(1 2 3 4 5 6) |
|
Convert a list, vector, or array into a row or column matrix.
If xs is an array, it must be nonempty and not have more than one axis with length
greater than 1.
Examples: |
> (->row-matrix '(1 2 3)) | - : (Array Positive-Byte) | (array #[#[1 2 3]]) | > (->row-matrix #(1 2 3)) | - : (Array Integer) | (array #[#[1 2 3]]) | > (->row-matrix (col-matrix [1 2 3])) | - : (Array Positive-Byte) | (array #[#[1 2 3]]) | > (->col-matrix (array #[#[#[1]] #[#[2]] #[#[3]]])) | - : (Array Positive-Byte) | (array #[#[1] #[2] #[3]]) | > (->col-matrix (matrix [[1 0] [0 1]])) | - : (Array (U Zero One)) | array->col-matrix: contract violation | expected: nonempty Array with exactly one axis of length | >= 1 | given: (array #[#[1 0] #[0 1]]) |
|
Convert a list of lists of entries into a matrix and back.
Examples: |
> (list*->matrix '((1 2 3) (4 5 6))) | - : (Array Positive-Byte) | (array #[#[1 2 3] #[4 5 6]]) | > (matrix->list* (matrix [[1 2 3] [4 5 6]])) | - : (Listof (Listof Positive-Byte)) | '((1 2 3) (4 5 6)) |
|
These functions are like
list*->array and
array->list*, but use a fixed-depth
(i.e. non-recursive) list type, and do not require a predicate to distinguish entries from rows.
As in the first example, Typed Racket often needs help inferring the type A.