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]

u <- c(.5,.5)
v <- c(3,-4)
dot_uv <- u%*%v
dot_uv
##      [,1]
## [1,] -0.5


(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 definition.

#u
u_norm <- norm(u, type ="2")
u_norm
## [1] 0.7071068
#testing
sqrt(u[1]^2 + u[2]^2)
## [1] 0.7071068
#v
v_norm <- norm(v, type = "2")
v_norm
## [1] 5
#testing
sqrt(v[1]^2 + v[2]^2)
## [1] 5


(3) What is the linear combination: 3u - 2v?

3*u - 2*v
## [1] -4.5  9.5



(4) What is the angle between u and v? 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.

#cos theta = (u * v)/(mag u * mag v)
#then convert from radians to degrees
acos(dot_uv/(v_norm * u_norm)) * (180/pi)
##         [,1]
## [1,] 98.1301




2. Problem set 2

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.

Please test it with the system below and it should produce a solution x = [-1.55, -0.32, 0.95]
\[ \left(\begin{array}{cc} 1 & 1 & 3\\ 2 & -1 & 5\\ -1 & -2 & 4 \end{array}\right) \left(\begin{array}{cc} x_1\\ x_2\\ x_3 \end{array}\right) = \left(\begin{array}{cc} 1\\ 2\\ 6 \end{array}\right) \]

We’re going to go the Upper Triangular Matrix route. this means all entries below the main diagnoal are zero. We’re building this specifically for a 3 x 3, which means we need 0s at [2,1], [3,1], and [3,2].

this will be our M.O.:
(1) Start with row 1 of the co-efficient matrix
(2) Pivot: The first non-zero element in the row being evaluated
(3) Multiplier: The element being eliminated divided by the Pivot
(4) Subtract Multiplier times row n from row n+1
(5) Advance to the next row and repeat

Create the function.

utm_solve <- function(A,b){

  #create the augmented matrix  
  a_m <- cbind(A,b)
  
  pivot_1 <- a_m[1,1]
  
  multiplier_1 <- a_m[2,1]/pivot_1
  
  a_m[2,] <- a_m[2,] - (multiplier_1 * a_m[1,])
  
  multiplier_2 <- a_m[3,1]/pivot_1
  a_m[3,] <- a_m[3,] - (multiplier_2 * a_m[1,])
  
  pivot_2 <- a_m[2,2]
  
  multiplier_3 <- a_m[3,2]/pivot_2
  a_m[3,] <- a_m[3,] - (multiplier_3 * a_m[2,])

  #return(a_m)
  
  x3 <- a_m[3,4]/a_m[3,3]
  x2 <- (a_m[2,4] - (a_m[2,3] * x3))/a_m[2,2]
  x1 <- (a_m[1,4] - (a_m[1,3] * x3) - (a_m[1,2] * x2))/a_m[1,1]
  
  ans_vec <- matrix(c(x1,x2,x3), nrow =3, ncol = 1)
  return(ans_vec)
}
#Verifying
A <- matrix(c(1,1,3,2,-1,5,-1,-2,4), nrow = 3, ncol = 3, byrow = TRUE)
b <- matrix(c(1,2,6), nrow = 3, ncol = 1)

#Test our function
utm_solve(A,b)
##            [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455
#Verify with R solve function
solve (A,b)
##            [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455