Last active
April 15, 2018 10:29
-
-
Save pgilad/68d5d9821696255c8a815a8558148876 to your computer and use it in GitHub Desktop.
Gram-Schmidt on a subspace of vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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