► |  | 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+
matrix-
matrix*
matrix-expt
matrix-scale
matrix-kronecker
matrix-map
matrix-sum
matrix=

top ← prev up next →

#### 7.5`` Entrywise Operations and Arithmetic

> > procedure

> > (matrix+ M N ...) → (Matrix Number)

> M : (Matrix Number)
> N : (Matrix Number)

> > procedure

> > (matrix- M N ...) → (Matrix Number)

> M : (Matrix Number)
> N : (Matrix Number)

> > procedure

> > (matrix* M N ...) → (Matrix Number)

> M : (Matrix Number)
> N : (Matrix Number)

Matrix addition, subtraction and products respectively.

For matrix addition and subtraction all matrices must have the same shape.

For matrix product the number of columns of one matrix must equal the number of rows in the following matrix.

Examples:

> > (define A (matrix ([1 2]
> [3 4])))

> > (define B (matrix ([5 6]
> [7 8])))

> > (define C (matrix ([ 9 10 11]
> [12 13 14])))

> > (matrix+ A B)
> (array #[#[6 8] #[10 12]])
> > (matrix- A B)
> (array #[#[-4 -4] #[-4 -4]])
> > (matrix* A C)
> (array #[#[33 36 39] #[75 82 89]])

> > procedure

> > (matrix-expt M n) → (Matrix Number)

> M : (Matrix Number)
> n : Integer

Computes (matrix* M ...) with n arguments, but more efficiently. M must be a square-matrix? and n must be nonnegative.

Examples:

> > (matrix* (matrix-expt (matrix [[1 1] [1 0]]) 100)
> (col-matrix [0 1]))

> (array #[#[354224848179261915075] #[218922995834555169026]])
> > (->col-matrix (list (fibonacci 100) (fibonacci 99)))
> (array #[#[354224848179261915075] #[218922995834555169026]])

> > procedure

> > (matrix-scale M z) → (Matrix Number)

> M : (Matrix Number)
> z : Number

Computes the matrix zM, a matrix of the same shape as M where each entry in M is multiplied with z.

Example:

> > (matrix-scale (matrix [[1 2] [3 4]]) 2)
> (array #[#[2 4] #[6 8]])

> > procedure

> > (matrix-kronecker M N ...) → (Matrix Number)

> M : (Matrix Number)
> N : (Matrix Number)

Wikipedia: Kronecker product Computes the Kronecker product.

Example:

> > (matrix-kronecker (matrix [[1 2] [3 4] [5 6]])
> (matrix [[7 8] [9 10]]))

> (mutable-array
> #[#[7 8 14 16]
> #[9 10 18 20]
> #[21 24 28 32]
> #[27 30 36 40]
> #[35 40 42 48]
> #[45 50 54 60]])

Added in version 1.2 of package math-lib.

> > procedure

> > (matrix-map f M) → (Matrix R)

> f : (A -> R)
> M : (Matrix A)
> (matrix-map f M0 M1 N ...) → (Matrix R)
> f : (A B Ts ... -> R)
> M0 : (Matrix A)
> M1 : (Matrix B)
> N : (Matrix Ts)

Like array-map, but requires at least one matrix argument and never broadcasts.

Examples:

> > (matrix-map sqr (matrix [[1 2] [3 4]]))
> (array #[#[1 4] #[9 16]])

> > (matrix-map + (matrix [[1 2] [3 4]])
> (matrix [[5 6] [7 8]]))

> (array #[#[6 8] #[10 12]])

> > procedure

> > (matrix-sum Ms) → (Matrix Number)

> Ms : (Listof (Matrix Number))

Like (apply matrix+ Ms), but raises a runtime error when Ms is empty.

> > procedure

> > (matrix= M0 M1 N ...) → Boolean

> M0 : (Matrix Number)
> M1 : (Matrix Number)
> N : (Matrix Number)

Returns #t when its arguments are the same size and are equal entrywise.

See matrix-relative-error and matrix-absolute-error for equality testing that is tolerant to floating-point error.

top ← prev up next →