7.4 Conversion

► |  | 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:

list->matrix
matrix->list
vector->matrix
matrix->vector
->row-matrix
->col-matrix
list*->matrix
matrix->list*
vector*->matrix
matrix->vector*

top contents ← prev up next →

#### 7.4`` Conversion🔗ℹ

> procedure (list->matrix m n xs) → (Matrix A)
> m : Integer
> n : Integer
> xs : (Listof A)
> procedure (matrix->list M) → (Listof A)
> M : (Matrix A)

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)

> procedure (vector->matrix m n xs) → (Matrix A)
> m : Integer
> n : Integer
> xs : (Vectorof A)
> procedure (matrix->vector M) → (Vectorof A)
> M : (Matrix A)

Like list->matrix and matrix->list, but for vectors.

Examples:

> > (vector->matrix 2 3 #(1 2 3 4 5 6))
> - : #(struct:Mutable-Array
> (Indexes
> Index
> (Boxof Boolean)
> (-> Void)
> (-> Indexes Integer)
> (-> Indexes Integer Void)
> (Vectorof Integer))
> #<syntax:.../array/typed-mutable-array.rkt:14:13 prop:custom-write>)
> (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)

> procedure (->row-matrix xs) → (Matrix A)
> xs : (U (Listof A) (Vectorof A) (Array A))
> procedure (->col-matrix xs) → (Matrix A)
> xs : (U (Listof A) (Vectorof A) (Array A))

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 Positive-Byte)
> (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->col-matrix: contract violation
> expected: nonempty Array with exactly one axis of length
> >= 1
> given: (array #[#[1 0] #[0 1]])

> procedure (list*->matrix xss) → (Matrix A)
> xss : (Listof (Listof A))
> procedure (matrix->list* M) → (Listof (Listof A))
> M : (Matrix A)

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.

> procedure (vector*->matrix xss) → (Matrix A)
> xss : (Vectorof (Vectorof A))
> procedure (matrix->vector* M) → (Vectorof (Vectorof A))
> M : (Matrix A)

Like list*->matrix and matrix*->list, but for vectors.

Examples:

> > ((inst vector*->matrix Integer) #(#(1 2 3) #(4 5 6)))
> - : #(struct:Mutable-Array
> (Indexes
> Index
> (Boxof Boolean)
> (-> Void)
> (-> Indexes Integer)
> (-> Indexes Integer Void)
> (Vectorof Integer))
> #<syntax:.../array/typed-mutable-array.rkt:14:13 prop:custom-write>)
> (mutable-array #[#[1 2 3] #[4 5 6]])
> > (matrix->vector* (matrix [[1 2 3] [4 5 6]]))
> - : (Vectorof (Vectorof Integer))
> '#(#(1 2 3) #(4 5 6))

As in the first example, Typed Racket often needs help inferring the type A.
top contents ← prev up next →