#Problem 1
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.
1). Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;-4]
library(dplyr)
u<-c(0.5,0.5)
v<- c(3,-4)
(dotproduct<- sum(u*v))
## [1] -0.5
(u_length<-sqrt(sum(u^2)))
## [1] 0.7071068
(v_length<-sqrt(sum(v^2)))
## [1] 5
3*u-2*v
## [1] -4.5 9.5
acos(dotproduct/(u_length*v_length))/pi*180
## [1] 98.1301
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 equation system. You dont 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 not that you do not have to worry about zero pivots, though. Please not 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.
Matrix_Solver <- function(i, j) {
combination <- cbind(i,j)
p1 <- combination[2,1]/combination[1,1]
combination[2,] <- combination[2,] - (combination[1,]*p1)
p2 <- combination[3,1]/combination[1,1]
combination[3,] <- combination[3,] - (combination[1,]*p2)
p3 <- combination[3,2]/combination[2,2]
combination[3,] <- combination[3,] - (combination[2,]*p3)
x3 <- combination[3,4] / combination[3,3]
x2 <- (combination[2,4] - (combination[2,3]*x3)) / combination[2,2]
x1 <- (combination[1,4] - (combination[1,3]*x3) - (combination[1,2]*x2)) / combination[1,1]
final <- matrix(c(x1, x2, x3), nrow = 3)
return(round(final,2))
}
#test matrix
TestMatrix <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow=3, ncol=3)
TestVector<- c(1,2,6)
Matrix_Solver(TestMatrix,TestVector)
## [,1]
## [1,] -1.55
## [2,] -0.32
## [3,] 0.95