Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active April 15, 2018 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/68d5d9821696255c8a815a8558148876 to your computer and use it in GitHub Desktop.
Save pgilad/68d5d9821696255c8a815a8558148876 to your computer and use it in GitHub Desktop.
Gram-Schmidt on a subspace of vectors
cols <- c(
c(3, 9, 2, 3, 2),
c(8, 8, 1, 9, 0),
c(1, 9, 5, 9, 8),
c(8, 10, 7, 5, 3),
c(5, 8, 8, 0, 8),
c(5, 10, 4, 7, 2))
# prepare column vectors matrix
mat <- matrix(cols, byrow = FALSE, nrow = 5)
# prepare orthogonal complement matrix
complement_matrix <- matrix(mat[, 1], ncol = 1)
# define projection function
project <- function(from, onto) {
nom <- t(from) %*% onto
denom <- t(onto) %*% onto
return ((nom / denom) %*% onto)
}
# loop through each column starting from 2
for (i in 2:ncol(mat)) {
v <- mat[, i]
u <- v
for (j in 1:(i-1)) {
u <- u - project(v, complement_matrix[, j])
}
# append new column to our matrix
complement_matrix <- cbind(complement_matrix, t(u))
}
# show our complement matrix
print(complement_matrix)
# we now have the orthogonal complement matrix, lets make it orthonormal
normalize_vector <- function(vector) {
return (vector / sqrt(vector %*% vector))
}
orthonormal_matrix <- apply(complement_matrix, 2, normalize_vector)
# show our orthonormal_matrix
print(orthonormal_matrix)
# prove that 2 vectors are orthogonal:
print(orthonormal_matrix[, 2] %*% orthonormal_matrix[, 4])
# prove that any vector is unit length
print(sqrt(orthonormal_matrix[, 2] %*% orthonormal_matrix[, 2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment