Generate a 3x3 orthogonal matrix using the Gram-Schmidt algorithm.

GramSchmidt(v1, v2, v3, order = 1:3)

Arguments

v1, v2, v3

Three length 3 vectors (taken as row vectors).

order

The precedence order for the vectors; see Details.

Details

This function orthogonalizes the matrix rbind(v1, v2, v3) using the Gram-Schmidt algorithm. It can handle rank 2 matrices (returning a rank 3 matrix). If the original is rank 1, it is likely to fail.

The order vector determines the precedence of the original vectors. For example, if it is c(i, j, k), then row i will be unchanged (other than normalization); row j will normally be transformed within the span of rows i and j. Row k will be transformed orthogonally to the span of the others.

Value

A 3x3 matrix whose rows are the orthogonalization of the original row vectors.

Author

Duncan Murdoch

Examples

# Proceed through the rows in order
print(A <- matrix(rnorm(9), 3, 3))
#>           [,1]       [,2]        [,3]
#> [1,] 0.5368287 -0.7590739 -0.23600612
#> [2,] 2.1138334 -0.4353578  0.24099255
#> [3,] 2.0409634 -0.2656385  0.07094729
GramSchmidt(A[1, ], A[2, ], A[3, ])
#>         [,1]       [,2]       [,3]
#> v1 0.5596592 -0.7913561 -0.2460431
#> v2 0.8075484  0.4540847  0.3763942
#> v3 0.1861374  0.4093442 -0.8931910

# Keep the middle row unchanged
print(A <- matrix(c(rnorm(2), 0, 1, 0, 0, rnorm(3)), 3, 3, byrow = TRUE))
#>            [,1]       [,2]        [,3]
#> [1,] -0.2014641 -1.1065067  0.00000000
#> [2,]  1.0000000  0.0000000  0.00000000
#> [3,] -0.6335407 -0.4830976 -0.03683726
GramSchmidt(A[1, ], A[2, ], A[3, ], order = c(2, 1, 3))
#>    [,1] [,2] [,3]
#> v2    0   -1    0
#> v1    1    0    0
#> v3    0    0   -1