You can think of vectors representing many dimensions of related information. For instance, Netflix might store all the ratings a user gives to movies in a vector. This is clearly a vector of very large dimensions (in the millions) and very sparse as the user might have rated only a few movies. Similarly, Amazon might store the items purchased by a user in a vector, with each slot or dimension representing a unique product and the value of the slot, the number of such items the user bought. One task that is frequently done in these settings is to find similarities between users. And, we can use dot-product between vectors to do just that. As you know, the dot-product is proportional to the length of two vectors and to the angle between them. In fact, the dot-product between two vectors, normalized by their lengths is called as the cosine distance and is frequently used in recommendation engines.
Set up a system of equations with 3 variables and 3 constraints and solve for x. Please write a function in R that will take two variables (matrix A & constraint vector b) and solve using elimination. Your function should produce the right answer for the system of equations for any 3-variable, 3-equation system. You don’t have to worry about degenerate cases and can safely assume that the function will only be tested with a system of equations that has a solution. Please note that you do have to worry about zero pivots, though. Please note that you should not use the built-in function solve to solve this system or use matrix inverses. The approach that you should employ is to construct an Upper Triangular Matrix and then back-substitute to get the solution. Alternatively, you can augment the matrix A with vector b and jointly apply the Gauss Jordan elimination procedure.
library(matlib)
# The below function takes in a matrix (i.e. matrix(c(1,2,-1,1,-1,-2,3,5,4), 3,3)), and a constraint vector (i.e. c(1,2,6))
# and solves the system of equations by constructing an upper triangle matrix and then using back-subsitution to get the solutions.
# The function also identifies "zero pivots"
# This function solves a system of equations without using "solve" or the reduced row echelon form functions.
three_by_three_solver <- function(mat, vect) {
no_constraint_matrix <- mat
#Creating full matrix out of matrix and constraint vector
full_matrix <- cbind(no_constraint_matrix, vect)
print("The matrix is: ")
print(full_matrix)
print("---------------------------")
#reducing matrix to echelon form which is an upper triangle matrix (NOT REDUCED ROW ECHELON FORM!)
upper_triangle <- echelon(full_matrix, reduced = FALSE)
print("The upper triangle matrix is: ")
print(upper_triangle)
print("---------------------------")
#checks to see if there are zero pivots, if so, it will print "infinite solutions"
zero_pivots <- upper_triangle[3,1] + upper_triangle[3,2] + upper_triangle[3,3]
if(zero_pivots == 0) {
print("There are infinitely many solutions to the system of equations")
} else {
#solving for x, y, and z with back substitution
z <- upper_triangle[3,4]
y <- upper_triangle[2,4] - upper_triangle[2,3] * z
x <- upper_triangle[1,4] - (upper_triangle[1,2] * y) - (upper_triangle[1,3] * z)
print(paste0("The rounded solutions to the system of equations are: ", round(x,2),", ",round(y,2),", ", round(z,2)))
}
}
Now let’s test our function with the test system provided:
x <- matrix(c(1,2,-1,1,-1,-2,3,5,4), 3,3)
y <- c(1,2,6)
three_by_three_solver(x, y)
## [1] "The matrix is: "
## vect
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
## [1] "---------------------------"
## [1] "The upper triangle matrix is: "
## vect
## [1,] 1 -0.5 2.5 1.0000000
## [2,] 0 1.0 -2.6 -2.8000000
## [3,] 0 0.0 1.0 0.9545455
## [1] "---------------------------"
## [1] "The rounded solutions to the system of equations are: -1.55, -0.32, 0.95"
The function has correctly calculated the rounded answers. Now let’s test the function on a system that will have a zero pivot column (infinitely many solutions):
x <- matrix(c(3,1,4,-3,2,-1,4,-3,1), 3,3)
y <- c(-23,25,25)
three_by_three_solver(x, y)
## [1] "The matrix is: "
## vect
## [1,] 3 -3 4 -23
## [2,] 1 2 -3 25
## [3,] 4 -1 1 25
## [1] "---------------------------"
## [1] "The upper triangle matrix is: "
## vect
## [1,] 1 -0.25 0.250000 6.250000
## [2,] 0 1.00 -1.444444 8.333333
## [3,] 0 0.00 0.000000 1.000000
## [1] "---------------------------"
## [1] "There are infinitely many solutions to the system of equations"
Again the function has correctly identified that the above system of equations will have infinitely many solutions.