Problem Set# 1

Given following vectors;
\(u\quad =\quad \left[ \begin{matrix} 0.5 \\ 0.5 \end{matrix} \right]\) and \(v\quad =\quad \left[ \begin{matrix} 3 \\ 4 \end{matrix} \right]\)

Perform the following:
1. Calculate the dot product \(u\cdot v\)
2. The lengths of u and v respectively
3. The linear Combination; \(3u-2v\)
4. Angle between u and v

Dot Product \(u\cdot v\)

Given \(u\quad =\quad \left[ \begin{matrix} { u }_{ 1 } \\ { u }_{ 2 } \end{matrix} \right]\) and \(v\quad =\quad \left[ \begin{matrix} { v }_{ 1 } \\ { v }_{ 2 } \end{matrix} \right]\)
The dot product \(u\cdot v\quad =\quad { u }_{ 1 }\cdot { v }_{ 1 }\quad +\quad { u }_{ 2 }\cdot { v }_{ 2 }\)
Hence, in this case; \(u\cdot v\quad =\quad { 0.5\times }3\quad +\quad 0.5\times \left( -4 \right) \quad =\quad -0.5\)

Lengths of u and v respectively

The legnths of a vector u in the mathematical sense is calculated by the following; \(\left\| u \right\| \quad =\quad \sqrt { u\cdot u }\)
In Computer Science, the length of a vector refers to the mathematical notion of dimension of vector.

\(\left\| u \right\| \quad =\quad \sqrt { u\cdot u } \quad =\quad \sqrt { { 0.5 }^{ 2 }\quad +\quad { 0.5 }^{ 2 } } \quad =\quad \sqrt { 0.5 } =\quad 0.71\)
\(\left\| v \right\| \quad =\quad \sqrt { v\cdot v } \quad =\quad \sqrt { { 3 }^{ 2 }\quad +\quad { \left( -4 \right) }^{ 2 } } \quad =\quad \sqrt { 9\quad +\quad 16 } =\quad \sqrt { 25 } \quad =\quad 5\)

Linear Combination; \(3u-2v\)

The linear combination \(3u-2v\) is a vector calculated as follows;

\(3u\quad -2v\quad =\quad 3\left[ \begin{matrix} 0.5 \\ 0.5 \end{matrix} \right] \quad -2\left[ \begin{matrix} 3 \\ -4 \end{matrix} \right] \quad =\quad \left[ \begin{matrix} 3\times 0.5\quad -2\times 3 \\ 3\times 0.5\quad -2\times \left( -4 \right) \end{matrix} \right] \quad =\quad \left[ \begin{matrix} 1.5\quad -\quad 6 \\ 1.5\quad +\quad 8 \end{matrix} \right] \quad =\quad \left[ \begin{matrix} -4.5 \\ 9.5 \end{matrix} \right]\)

Angle between u and v

If \(\theta\) is the angle between vector u and v, then if u and v are non-zero vectors which is the case, then
\(\cos { \theta } \quad =\quad \frac { u\cdot v }{ \left\| u \right\| \cdot \left\| v \right\| } \quad =\quad \frac { -0.5 }{ 0.71\times 5 } \quad 0.14\)

\(\theta \quad =\quad 1.43\quad radiant\)


Problem Set# 2

# Helper Function to swap rows of matrix

swap_rows <-function(A, m, n){
  
  temp  <- A[m,]
  A[m,] <- A[n,]
  A[n,] <- temp
  
  return(A)
}

# Helper Function to swap elements of vector

swap_elements <- function(v, m, n){
  
  temp_v <- v[m]
  v[m] <- v[n]
  v[n] <- temp_v
  
  return(v)
}



# Helper function to find pivot

find_pivot <- function (A, r, c){
# Initialize return variables and processing variables
  swap <- FALSE
  p <- 0
  swap_r <- 0
  
  loop <- TRUE
  i <- r
  #loop through each of matrix and find non-zero pivot point for column c
  while(loop){
    if(A[i,c] != 0){
      loop <- FALSE
      p<-A[i,c]
      if(i>r){
        swap_r <- i
        swap <- TRUE
      } # end of inner if statement
    } # end of outer if statement
    
    i <- i + 1     #increment row
    
    if (i > nrow(A)){
      loop <- FALSE
    }
  } # end of while loop
  
  return(list(p, swap, swap_r))
  
} # end of function


# Main function to Solve equation (3x3)

solve_equation <- function (A, b){
  
  # Validate input parameters, A, b, and whether row(A)==length(b)
  if(missing(A)){
    stop("Need to specify matrix A in equation Ax = b.")
  }
  
  if(missing(b)){
    stop("Needs to specify vector b in equation Ax =b.")
  }
  if(nrow(A) != ncol(A)){
    stop("Needs A to be square matrix.")
  }
  if(nrow(A) != length(b)){
    stop("Needs to have number of rows of A same as dimension of b.")
  }
  
  # store nrow and ncol for matrix A for reference
  num_rows <- nrow(A)
  num_col <- ncol(A)

  # store Matrix and Vector in temporary variables
  U <- A
  w <- b

  #-----------------#  
  # Build Upper-Triangular Matrix 
  
  # Initialize colum cursor
  c <- 1
  
  while (c <= num_col){
    
    # Initialize row cursor
    r <- c
    
    # find pivot point
    l_result <- find_pivot (U, r, c)
    
    p <- unlist(l_result[1])
    swap_indicator <- unlist(l_result[2])
    swap_row <- unlist(l_result[3])
    
    # Validate that pivot point is found
    if (p==0){
      text <- "No pivot point found in column"
      txt_col <- as.character(c)
      msg <- paste(text,txt_col)
      stop(msg)
    }
    
    # if pivot point found, check for required swap
    if(swap_indicator){
      U <- swap_rows(U,r,swap_row)
      w <- swap_elements (w, r, swap_row)
    }
    
    # Proceed with elimination for each rows
    i <- r + 1
    
    while (i <= num_rows){
      
      # Determine multiplier
      m = U[i,c]/p
      
      U[i, ] <- U[i, ] - m*U[r, ]
      w[i] <- w[i] - m*w[r]
      
      i <- i + 1
    } # end of for while for rows
    
    c <- c + 1
    
  } # end of while loop for columns
  #------------# 
  
  # Solve equation from last row up 
  
  v_equation <- vector()
  
  i <- num_col
  
  v_equation[i] <- w[i]/U[i,i]
  
  z<- w[3]/U[3,3]
  
  y<- (w[2] - U[2,3]*z)/U[2,2]
  
  x<- (w[1] - U[1,2]*y - U[1,3]*z)/U[1,1]
  
  result <- c(round(x, digits = 2), round(y, digits = 2), round(z, digits = 2))
  
  return(result)
}

# -------- Invoquing function ----------#

M = matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow=3, ncol=3)
v = c(1, 2, 6)

result_equation <- solve_equation(M,v)

result_equation
## [1] -1.55 -0.32  0.95