Carries out simple Gram-Schmidt orthogonalization of a matrix.
Treating the columns of the matrix X
in the given order,
each successive column after the first is made orthogonal to all
previous columns by subtracting their projections on the current
column.
Usage
GramSchmidt(
X,
normalize = TRUE,
verbose = FALSE,
tol = sqrt(.Machine$double.eps),
omit_zero_columns = TRUE
)
Arguments
- X
a matrix
- normalize
logical; should the resulting columns be normalized to unit length? The default is
TRUE
- verbose
logical; if
TRUE
, print intermediate steps. The default isFALSE
- tol
the tolerance for detecting linear dependencies in the columns of a. The default is
sqrt(.Machine$double.eps)
- omit_zero_columns
if
TRUE
(the default), remove linearly dependent columns from the result
Value
A matrix of the same size as X
, with orthogonal columns (but with 0 columns removed by default)
Examples
(xx <- matrix(c( 1:3, 3:1, 1, 0, -2), 3, 3))
#> [,1] [,2] [,3]
#> [1,] 1 3 1
#> [2,] 2 2 0
#> [3,] 3 1 -2
crossprod(xx)
#> [,1] [,2] [,3]
#> [1,] 14 10 -5
#> [2,] 10 14 1
#> [3,] -5 1 5
(zz <- GramSchmidt(xx, normalize=FALSE))
#> Error in GramSchmidt(xx, normalize = FALSE): unused argument (normalize = FALSE)
zapsmall(crossprod(zz))
#> Error: object 'zz' not found
# normalized
(zz <- GramSchmidt(xx))
#> Error in GramSchmidt(xx): argument "v2" is missing, with no default
zapsmall(crossprod(zz))
#> Error: object 'zz' not found
# print steps
GramSchmidt(xx, verbose=TRUE)
#> Error in GramSchmidt(xx, verbose = TRUE): unused argument (verbose = TRUE)
# A non-invertible matrix; hence, it is of deficient rank
(xx <- matrix(c( 1:3, 3:1, 1, 0, -1), 3, 3))
#> [,1] [,2] [,3]
#> [1,] 1 3 1
#> [2,] 2 2 0
#> [3,] 3 1 -1
R(xx)
#> [1] 2
crossprod(xx)
#> [,1] [,2] [,3]
#> [1,] 14 10 -2
#> [2,] 10 14 2
#> [3,] -2 2 2
# GramSchmidt finds an orthonormal basis
(zz <- GramSchmidt(xx))
#> Error in GramSchmidt(xx): argument "v2" is missing, with no default
zapsmall(crossprod(zz))
#> Error: object 'zz' not found