Created
November 16, 2018 14:15
-
-
Save korkridake/0b1793845337ce3ac8cbec13968bef87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# https://stackoverflow.com/questions/53337864/solve-a-linear-equation-system-b-0-rstudio | |
# Would like to solve a linear equation system like this: | |
# x_1*3+x_2*4+x_3*5+x_4*6+x_5,1*2=0 | |
# x_1*21+x_2*23+x_3*45+x_4*37*+x_5,1*0=0 | |
# x_1*340+x_2*24+x_3*25+x_4*31+x_5,1*0=0 | |
# x_1*32+x_2*45+x_3*5+x_4*6+x_5,2*2=0 | |
# x_1*9+x_2*11+x_3*13+x_4*49+x_5,2*0=0 | |
# x_1*5+x_2*88+x_3*100+x_4*102+X_5,3*2=0 | |
# [x_1] [x_2] [x_3] [x_4] [,5] | |
# [1,] 3 4 5 6 2 | |
# [2,] 21 23 45 37 0 | |
# [3,] 340 24 25 31 0 | |
# [4,] 32 45 5 6 2 | |
# [5,] 9 11 13 49 0 | |
# [6,] 5 88 100 102 2 | |
# Use the Singular Value Decomposition (SVD), if all elements of the diagonal part of the SVD are non zero | |
# only the trivial solutions exists | |
solution_space <- function(A){ | |
my_svd <- svd(A) | |
if(all(my_svd$d != 0)){ | |
return(rep(0, ncol(A))) | |
} else { | |
return(my_svd$u[,my_svd$d == 0, drop=F]) | |
} | |
} | |
A %*% solution_space(A) | |
# You can try the code with these matrices: | |
A <- matrix(c(1,1,0,1,1,0,0,0,1), 3, 3) | |
A <- matrix(c(1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1), 4, 4) | |
A <- matrix(c(1,1,0,1,1,0,0,0,0), 3, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment