1. Problem set 1

You can think of vectors representing many dimensions of related information. For nstance, 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]

  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 de nition.

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

  4. What is the angle between u and v ?

Answers:

  1. Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;4]
u <- matrix(c(0.5,0.5),nrow=1, ncol=2)
v <- matrix(c(3,4), nrow=1,ncol=2)
dot_uv <- u %*% t(v)
print(paste("The dot product of u.v is",dot_uv))
## [1] "The dot product of u.v is 3.5"
  1. 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 de nition.
length_u <- norm(u,"f")
length_v <- norm(v,"f")
print(paste("The length of vecotor u is",round(length_u,2)))
## [1] "The length of vecotor u is 0.71"
print(paste("The length of vecotor v is",round(length_v,2)))
## [1] "The length of vecotor v is 5"
  1. What is the linear combination: 3u-2v?
lc_3u2v <- (3)*u +(-2)*v
(lc_mx <- matrix(lc_3u2v))
##      [,1]
## [1,] -4.5
## [2,] -6.5
print("The linear combination of 3u-2v is:")
## [1] "The linear combination of 3u-2v is:"
\[\begin{equation*} \begin{bmatrix} -4.5\\ -6.5 \end{bmatrix} \end{equation*}\]
  1. What is the angle between u and v

Because the dot product of u and v is as following:

\[\begin{equation*} u.v = ||u||\times||v||\times\cos\theta\\ So\\ \cos\theta = \frac {u.v}{||u||\times||v||}\\ \theta = \cos^{-1}\left(\frac {u.v}{||u||\times||v||}\right) \end{equation*}\]
(theta <- acos(dot_uv/(length_u*length_v)))
##           [,1]
## [1,] 0.1418971
print(paste("The angle between u and v",round(theta,4)))
## [1] "The angle between u and v 0.1419"

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.

A Linear System:

\(-1x_2 +2x_3=1\)

\(2x_1+x_2+4x_3=8\)

\(x_1+2x_2+x_3=4\)

\[\begin{equation*} [A|b]= \begin{bmatrix} 0&-1&2&1\\ 2&1&4&8\\ 1&2&1&4 \end{bmatrix} \end{equation*}\]
# the matrix
arch_A <- matrix(c(0,-1,2,1,2,1,4,8,1,2,1,4), nrow=3, byrow = T)
# Reduced Row Echelone Form Function
ReducedRowEcheloneForm <- function(m){
  current_row <- 1
  current_col <- 1
  rw <- 1
  
  for(current_row in 1:nrow(m)){
    # check if the entry at current row and current column is 0
    rw <- current_row
    # find the first row that come after the current row and whose entry at current column is not 0
    while (rw < nrow(m) && m[rw,current_col] == 0){
      rw <- rw + 1
    }
    #swap the current row to the row whose entry at current column is not 0
    intmed <- m[current_row,]
    m[current_row,] <- m[rw,]
    m[rw,] <- intmed
    
    # apply row operation to convert the entries of current column except the one in the current row to 0
    for(i in 1:nrow(m)){
      if (i == current_row){
        m[i,] <- m[i,]}
      else {
        m[i,] <- m[i,]-m[current_row,]*(m[i,current_col]/m[current_row,current_col])
      }
      # convert the lead cofficient to 1
      if (m[current_row,current_col] != 0){
        m[current_row,] <- m[current_row,]/m[current_row,current_col]
      }
    }
    current_col <- current_row + 1 # move the index for current column to next column
    if (current_col == ncol(m)+1){
      return(m)
    }
  }
return(m)
}

(reduced <- ReducedRowEcheloneForm(arch_A))
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0 2.25
## [2,]    0    1    0 0.50
## [3,]    0    0    1 0.75

The LS has unique solution: \(x_1\)=2.25, \(x_2\)=0.5, \(x_3\)=.075

install.packages("pracma", repos="http://R-Forge.R-project.org")
install.packages("quadprog")
library(pracma)
rref(arch_A)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0 2.25
## [2,]    0    1    0 0.50
## [3,]    0    0    1 0.75
# get the same results when using R build-in rref function rref

reference: 1.https://rosettacode.org/wiki/Reduced_row_echelon_form