GramSchmidt: The Gram-Schmidt algorithm

Description Usage Arguments Details Value Author(s) Examples

View source: R/cylinder3d.R

Description

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

Usage

1
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(s)

Duncan Murdoch

Examples

1
2
3
4
5
6
7
# Proceed through the rows in order
print(A <- matrix(rnorm(9), 3, 3))
GramSchmidt(A[1, ], A[2, ], A[3, ])

# Keep the middle row unchanged
print(A <- matrix(c(rnorm(2), 0, 1, 0, 0, rnorm(3)), 3, 3, byrow = TRUE))
GramSchmidt(A[1, ], A[2, ], A[3, ], order = c(2, 1, 3))

Example output

Warning messages:
1: In rgl.init(initValue, onlyNULL) : RGL: unable to open X11 display
2: 'rgl_init' failed, running with rgl.useNULL = TRUE 
3: .onUnload failed in unloadNamespace() for 'rgl', details:
  call: fun(...)
  error: object 'rgl_quit' not found 
           [,1]       [,2]      [,3]
[1,] -0.3722391 -0.2810744 0.6572835
[2,]  0.7737364  2.1018446 1.4526077
[3,]  2.8455723  1.1114047 0.8868566
         [,1]       [,2]      [,3]
v1 -0.4618527 -0.3487408 0.8155194
v2  0.3063399  0.8001563 0.5156605
v3  0.8323748 -0.4879854 0.2627211
           [,1]       [,2]       [,3]
[1,] -0.7480716  1.3547163 0.00000000
[2,]  1.0000000  0.0000000 0.00000000
[3,]  0.1812679 -0.6844128 0.02757329
   [,1] [,2] [,3]
v2    0    1    0
v1    1    0    0
v3    0    0    1

rgl documentation built on Feb. 1, 2021, 3:01 a.m.