On this page:
matrix-1norm
matrix-2norm
matrix-inf-norm
matrix-norm
matrix-dot
matrix-cos-angle
matrix-angle
matrix-normalize
matrix-normalize-rows
matrix-normalize-cols
matrix-rows-orthogonal?
matrix-cols-orthogonal?

7.8 Inner Product Space Operations

The following functions treat matrices as vectors in an inner product space. It often makes most sense to use these vector-space functions only for row matrices and column matrices, which are essentially vectors as we normally think of them. There are exceptions, however, such as the fact that the Frobenius or Euclidean norm (implemented by matrix-2norm) can be used to measure error between matrices in a way that meets certain reasonable criteria (specifically, it is submultiplicative).

See Operator Norms and Comparing Matrices for similar functions (e.g. norms and angles) defined by considering matrices as operators between inner product spaces consisting of column matrices.

procedure

(matrix-1norm M)  Nonnegative-Real

  M : (Matrix Number)

procedure

(matrix-2norm M)  Nonnegative-Real

  M : (Matrix Number)

procedure

(matrix-inf-norm M)  Nonnegative-Real

  M : (Matrix Number)

procedure

(matrix-norm M [p])  Nonnegative-Real

  M : (Matrix Number)
  p : Real = 2
Wikipedia: Norm Respectively compute the L1 norm, L2 norm, L, and Lp norm.

The L1 norm is also known under the names Manhattan or taxicab norm. The L1 norm of a matrix is the sum of magnitudes of the entries in the matrix.

The L2 norm is also known under the names Euclidean or Frobenius norm. The L2 norm of a matrix is the square root of the sum of squares of magnitudes of the entries in the matrix.

The L norm is also known as the maximum or infinity norm. The L norm computes the maximum magnitude of the entries in the matrix.

For p >= 1, matrix-norm computes the Lp norm: the pth root of the sum of all entry magnitudes to the pth power.

Examples:
> (matrix-1norm    (col-matrix [1 2]))

3

> (matrix-2norm    (col-matrix [1 2]))

2.23606797749979

> (matrix-inf-norm (col-matrix [1 2]))

2

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

2.080083823051904

> (matrix-norm     (col-matrix [1 2]) +inf.0)

2

procedure

(matrix-dot M)  Nonnegative-Real

  M : (Matrix Number)
(matrix-dot M N)  Number
  M : (Matrix Number)
  N : (Matrix Number)
The call (matrix-dot M N) computes the Frobenius inner product of the two matrices with the same shape. In other words the sum of (* a (conjugate b)) is computed where a runs over the entries in M and b runs over the corresponding entries in N.

The call (matrix-dot M) computes (matrix-dot M M) efficiently.

Examples:
> (matrix-dot (col-matrix [1 2]) (col-matrix [3 4]))

11

> (+ (* 1 3) (* 2 4))

11

procedure

(matrix-cos-angle M N)  Number

  M : (Matrix Number)
  N : (Matrix Number)
Returns the cosine of the angle between two matrices w.r.t. the inner produce space induced by the Frobenius inner product. That is it returns

(/ (matrix-dot M N) (* (matrix-2norm M) (matrix-2norm N)))

Examples:
> (define M (col-matrix [1 0]))
> (define N (col-matrix [0 1]))
> (matrix-cos-angle M N)

0

> (matrix-cos-angle M (matrix+ M N))

0.7071067811865475

procedure

(matrix-angle M N)  Number

  M : (Matrix Number)
  N : (Matrix Number)
Equivalent to (acos (matrix-cos-angle M N)).

Examples:
> (require (only-in math/base radians->degrees))
> (define M (col-matrix [1 0]))
> (define N (col-matrix [0 1]))
> (radians->degrees (matrix-angle M N))

90.0

> (radians->degrees (matrix-angle M (matrix+ M N)))

45.00000000000001

procedure

(matrix-normalize M [p fail])  (U F (Matrix Number))

  M : (Matrix Number)
  p : Real = 2
  fail : (-> F) = (λ () (error ...))
Normalizes M with respect to the Lp norm.

Examples:
> (matrix-normalize (col-matrix [1 1]))

- : (Array Real)

(array #[#[0.7071067811865475] #[0.7071067811865475]])

> (matrix-normalize (col-matrix [1 1]) 1)

- : (Array Real)

(array #[#[1/2] #[1/2]])

> (matrix-normalize (col-matrix [1 1]) +inf.0)

- : (Array Real)

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

The result of applying the failure thunk fail is returned if M’s norm is zero.

procedure

(matrix-normalize-rows M [p fail])  (Matrix Number)

  M : (Matrix Number)
  p : Real = 2
  fail : (-> F) = (λ () (error ...))

procedure

(matrix-normalize-cols M [p fail])  (Matrix Number)

  M : (Matrix Number)
  p : Real = 2
  fail : (-> F) = (λ () (error ...))
As matrix-normalize but each row or column is normalized separately. The result is a matrix with unit vectors as rows or columns.

Examples:
> (matrix-normalize-rows (matrix [[1 2] [2 4]]))

- : (Array Real)

(array

 #[#[0.4472135954999579 0.8944271909999159]

   #[0.4472135954999579 0.8944271909999159]])

> (matrix-normalize-cols (matrix [[1 2] [2 4]]))

- : (Array Real)

(array

 #[#[0.4472135954999579 0.4472135954999579]

   #[0.8944271909999159 0.8944271909999159]])

The result of applying the failure thunk fail is returned if the norm of any row or column in M is zero.

procedure

(matrix-rows-orthogonal? M [eps])  Boolean

  M : (Matrix Number)
  eps : Real = (* 10 epsilon.0)

procedure

(matrix-cols-orthogonal? M [eps])  Boolean

  M : (Matrix Number)
  eps : Real = (* 10 epsilon.0)
Returns #t if the rows or columns of M are very close of being orthogonal (by default a few epsilons).

Examples:
> (matrix-rows-orthogonal? (matrix [[1 1] [-1 1]]))

#t

> (matrix-cols-orthogonal? (matrix [[1 1] [-1 1]]))

#t