ASSIGNMENT 1 IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - FALL 2014 1. Problem set 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]
dot product
u.v = 0.5*3 + 0.5*-4
u.v
## [1] -0.5
len_u = sqrt(u^2)
len_u = sqrt(0.5*0.5+0.5*0.5)
len_u
## [1] 0.7071068
len_v = sqrt(v^2)
len_v = sqrt(3*3+(-4*-4))
len_v
## [1] 5
\[3(0.5;0.5) - 2(3;-4) = (1.5;1.5) - (6;-8) = (-4.5;9.5)\]
u <- c(0.5,0.5)
v <- c(3, -4)
x = 3*u - 2*v
x
## [1] -4.5 9.5
\[u.v = |u| * |v| * cos(θ)\] \[cos(θ) = u.v / (|u| * |v|)\] \[θ = acos (u.v / (|u| * |v|))\]
*** Solution 1 ***
library(matlib)
## Warning: package 'matlib' was built under R version 3.6.2
u = matrix(c(0.5,0.5), nrow = 1)
v = matrix(c(3, -4), nrow = 1)
theta = acos(sum(u*v) / (sqrt(sum(u * u)) * sqrt(sum(v*v))))
theta
## [1] 1.712693
*** Solution 2 ***
angle <- function(u,v){
dot.prod <- u%*%v
norm.u <- norm(u,type="2")
norm.v <- norm(v,type="2")
theta <- acos(dot.prod / (norm.u * norm.v))
as.numeric(theta)
}
u <- as.matrix(c(0.5,0.5))
v <- as.matrix(c(3, -4))
angle(t(u),v)
## [1] 1.712693
You can use R-markdown to submit your responses to this problem set. If you decide to do it in paper, then please either scan it or take a picture using a smartphone and attach that picture. Please make sure that the picture is legible before submitting.
A = matrix(c(1,1,3,
2,-1,5,
-1,-2,4), 3, 3, byrow = TRUE)
b = c(1, 2, 6)
Gauss = gaussianElimination(A, b, verbose = FALSE)
Gauss
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 -1.5454546
## [2,] 0 1 0 -0.3181818
## [3,] 0 0 1 0.9545455
Note: The algorithm below is from this site. https://martin-thoma.com/solving-linear-equations-with-gaussian-elimination/
solve_matrix <- function(matrix){
n = nrow(matrix)
for (i in (1:n)){
max <- abs(matrix[i,i])
max.row <- i
if (i+1 <= n) {
for (k in ((i+1): n)){
if (abs(matrix[k,i]) > max){
max = abs(matrix[k,i])
max.row <- k
}
}
}
# Swap maximum row with current row (column by column)
for (k in (i: (n+1))){
tmp = matrix[max.row,k]
matrix[max.row,k] = matrix[i,k]
matrix[i,k] = tmp
}
# Make all rows below this one 0 in current column
if (i+1 <= n) {
for (k in ((i+1): n)){
c = -matrix[k,i]/matrix[i,i]
for (j in (i: (n+1))){
if (i == j){
matrix[k,j] = 0
}
else{
matrix[k,j] = matrix[k,j] + (c * matrix[i,j] )
}
}
}
}
}
# Solve equation Ax=b for an upper triangular matrix A
x = c(1:n)*0
for (i in (n:1)){
x[i] = matrix[i,(n+1)]/matrix[i,i]
for (k in ((i-1): 1)){
matrix[k,(n+1)] = matrix[k,(n+1)] - matrix[k,i] * x[i]
}
}
return(x)
}
solution <- solve_matrix(cbind(A,b))
print(solution)
## [1] -1.5454545 -0.3181818 0.9545455
Please send your code (as an R-markdown file, named using your first initial, last name, assignment, problem set. For instance, if I submit the code for this assignment, it will be called GIyengar Assign1.Rmd