You can think of vectors representing many dimensions of related information. For instance, Netix 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 nd 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] (2) What are the lengths of u and v? Please note that the mathematical notion of the length of a vector is not the same as a computer science deonition. (3) What is the linear combination: 3u - 2v? (4) What is the angle between u and v You can use R-markdown to submit your responses to this problem set.
u <- matrix(c(0.5,0.5),nrow=1 , ncol=2)
v <- matrix(c(3,4) , nrow=1, ncol=2)
transpose_v <- t(v)
uv_dot <- u %*% transpose_v
uv_dot
## [,1]
## [1,] 3.5
# Second method
u <- c(0.5 , 0.5)
v <- c(3,4)
u.v <- dot(u,v)
u.v
## [1] 3.5
Llength of the vector x = (x1, x2, …, xn) is captured by the formula :square root of the sum of squares.
u <- c(0.5 , 0.5)
v <- c(3,4)
vec_Len_u <- sqrt(dot(u,u))
vec_Len_v <- sqrt(dot(v,v))
print(paste("The length of vector u is " , round(vec_Len_u,2)))
## [1] "The length of vector u is 0.71"
print(paste("The length of vector v is " , round(vec_Len_v,2)))
## [1] "The length of vector v is 5"
lc <- 3*u - 2*v
lc <- matrix(lc)
print(paste("linear combination: 3u - 2v -> " )) ; print(lc)
## [1] "linear combination: 3u - 2v -> "
## [,1]
## [1,] -4.5
## [2,] -6.5
The cosine of the angle between two vectors is equal to dot product of two vectors divided by the product of vector magnitude.
uv_dot <- dot(u,v)
angle_vector <- acos(uv_dot/(vec_Len_u*vec_Len_v))
print(paste("angle between u and v " , angle_vector ))
## [1] "angle between u and v 0.141897054604164"
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.
### matrix row-echelon form to show a linear system
row_echelon_form <- function(X, W){
sol_matrix <- cbind(X,W)
val <- (X[,1]/(-1*X[1,1]))
multi <- matrix(c(1, val[2], val[3], 0, 1, 0, 0, 0, 1), nrow=3, ncol=3)
transform_1 <- multi %*% sol_matrix
val_2 <- -1*(transform_1[3,2]/transform_1[2,2])
multi_2 <- matrix(c(1, 0, 0, 0, 1, val_2, 0, 0, 1), nrow=3, ncol=3)
upper_tri_m <- multi_2 %*% transform_1
z = upper_tri_m[3,4]/upper_tri_m[3,3]
y = (upper_tri_m[2,4]-(upper_tri_m[2,3]*z))/upper_tri_m[2,2]
x = (upper_tri_m[1,4]-(upper_tri_m[1,3]*z)-(upper_tri_m[1,2]*y))/upper_tri_m[1,1]
ans <- rbind(x, y, z)
return (ans)
}
mat_3 <- matrix(
c(1, 2, -1, 1, -1, -2, 4, 5, 4),
nrow=3,
ncol=3)
const_3 <- matrix(
c(1, 2, 6),
nrow=3,
ncol=1)
row_echelon_form(mat_3, const_3)
## [,1]
## x -1.3333333
## y -0.7777778
## z 0.7777778