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]\)

# Create vectors u and v
u <- c(0.5, 0.5)
v <- c(3, -4)
# Find dot product
dot_prod <- u %*% v

\(u = [0.5, 0.5]\)

\(v = [3, -4]\)

\(u \cdotp v = (0.5 \times 3) + (0.5 \times -4)\).

\(u \cdotp v =-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.

# find magnitude u
mag_u <- sqrt(u[1]**2 + u[2]**2)

# find magnitude of v
mag_v <- sqrt(v[1]**2 + v[2]**2) 

\(||u|| = \sqrt{0.5^2 + 0.5^2} = \sqrt{0.25 + 0.25} = \sqrt{0.5}\)

\(||v|| = \sqrt{3^2 + (-4)^2} = \sqrt{9 + 16} = \sqrt{25} = 5\)

Using R for the calculations we get the magnitude or length of \(u\) is 0.7071068 and the magnitude of \(v\) is 5.


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

lc_u_v <- 3 * u - 2 * v  

\(3u - 2v = (3) \begin{bmatrix} 0.5 \\ 0.5 \end{bmatrix} + (-2) \begin{bmatrix} 3 \\ -4 \end{bmatrix} = \begin{bmatrix} -4.5 \\ 9.5 \end{bmatrix}\)

The linear combination: \(3u - 2v\) = [-4.5, 9.5].


(4) What is the angle between \(u\) and \(v\)?

angle <- dot_prod/(mag_u * mag_v)

\(cos\theta = \frac{u \cdotp v}{||u|| \hspace{1mm} \cdotp \hspace{1mm} ||v||} = \frac{-0.5}{0.7071068 \hspace{1mm} \times \hspace{1mm} 5} = -0.1414214\)

The angle between \(u\) and \(v\) is \(-0.1414214^{\circ}\).


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 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]\)

\[ \begin{bmatrix} 1 & 1 & 3 \\ 2 & -1 & 5 \\ -1 & -2 & 4 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} 1 \\ 2 \\ 6 \end{bmatrix} \]

A <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4), nrow = 3, byrow = TRUE)
b <- c(1, 2, 6)

gauss_jordan_elim <- function(A, b) {
  # if the number of equations is less than the number of variables there will always be an infinite number of solutions
  if(nrow(A) < ncol(A)) {
    return(list(A, "This system of linear equations has an infinite number of solutions because there are more variables than equations."))
  }
  
  A <- cbind(A,b)
  r <- 0

  for(j in 1:(ncol(A)-1)) {
    i <- r+1
    while(i <= nrow(A) & A[i,j] == 0) {
      if(i == nrow(A) & j == ncol(A)-1 & A[i,j] == 0) {
        if(A[i,j+1] == 0){
          return(list(A, "This system of linear equations has an infinite number of solutions."))
          } else {
            return(list(A, "This system of linear equations has no solutions."))
          } 
        } else {
        i <- i+1
        }
    }
    if(i <= nrow(A)) {
      r <- r + 1
      A[c(i,r), ] <- A[c(r,i), ] # swap rows i and r (row op 1)
      A[r, ] <- A[r, ] * (1/A[r,j]) # scale pivot to equal 1 (row op 2)
      for(k in 1:nrow(A)) {
        if(k != r){
          # make other rows in column j zero by adding a multiple of row r 
          # (row op 3)
          A[k, ] <- A[k, ] + (A[r, ] * -(A[k,j]))
        }
      }
    }
  }
  return(A[ , ncol(A)])
}

# Test the given system of equations
gauss_jordan_elim(A, b)
## [1] -1.5454545 -0.3181818  0.9545455
# Test a system of equations with infinite solutions
B <- matrix(c(1, -1, 2, 2, 1, 1, 1, 1, 0), nrow = 3, byrow = TRUE)
b <- c(0, 0, 0)
gauss_jordan_elim(B, b)
## [[1]]
##             b
## [1,] 1 0  1 0
## [2,] 0 1 -1 0
## [3,] 0 0  0 0
## 
## [[2]]
## [1] "This system of linear equations has an infinite number of solutions."
# Test a system of equations with no solutions
C <- matrix(c(1, -1, 2, 2, 1, 1, 1, 1, 0), nrow = 3, byrow = TRUE)
b <- c(1, 2, 3)
gauss_jordan_elim(C, b)
## [[1]]
##             b
## [1,] 1 0  1 1
## [2,] 0 1 -1 0
## [3,] 0 0  0 2
## 
## [[2]]
## [1] "This system of linear equations has no solutions."
# Test a 3 x 4 matrix
D <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4, 5, 4, 2), nrow = 3, byrow = TRUE)
b <- c(1, 2, 6)
gauss_jordan_elim(D, b)
## [[1]]
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    3    2
## [2,]   -1    5   -1   -2
## [3,]    4    5    4    2
## 
## [[2]]
## [1] "This system of linear equations has an infinite number of solutions because there are more variables than equations."
# Test matrix with more equations than variables
# This doesn't work!!!
E <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4, 5, 4, 2), nrow = 4, byrow = TRUE)
b <- c(1, 2, 6, 7)
gauss_jordan_elim(E, b)
## [1] -1.5454545 -0.3181818  0.9545455 14.0909091