Homework 01

Problem Set 1
(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 = 2)
v <- matrix(c(3, -4), nrow = 2)

cp <- crossprod(u, v)
print (cp)
##      [,1]
## [1,] -0.5
(2) What are the lenghts 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.
length_u <- sqrt(sum(u * u))
length_v <- sqrt(sum(v * v))

print(length_u)
## [1] 0.7071068
print(length_v)
## [1] 5
(3) What is the linear combination: 3u - 2v?
lc <- 3*u - 2*v
print (lc)
##      [,1]
## [1,] -4.5
## [2,]  9.5
(4) What is the angle between u and v

\[ \cos \theta = \frac{u \cdot v}{||u|| \ ||v||} \]

theta <- acos( crossprod(u, v) / ( length_u * length_v ) )
print (theta)
##          [,1]
## [1,] 1.712693
Problem Set 2
# Function to determine the solution of System of Linear Equations
eliminateAndSolve <- function(A, b) {
  
  # Step 1: Construct an Augmented Matrix M by applying cbind on both Matrix A and Vector b
  M = cbind(A, b)

  # Step 2: Use Gauss Jordan Elimination method on the Augmented matrix M
  for (i in 1:nrow(M)) {
    for (j in 1:ncol(M)) {
      if (i == j) {
        for (k in 1:nrow(M)) {
          if (M[k,j] != 0 & k != i) {
            multiplier <- M[i,j] / M[k,j]
            M[k,] <- M[k,] * multiplier - M[i,]
          }
        }
      }
    }
  }
  
  # Step 3: Loop thru the Augmented matrix to create an identity matrix by dividing each row with either M[1, 1] or M[2, 2] or M[3, 3]
  for (i in 1:nrow(M)) {
    M[i,] <- M[i,] * (1/M[i,i])
  }
  
  # Step 4: Obtain values for variables
  x1 <- M[1, 4]
  x2 <- M[2, 4]
  x3 <- M[3, 4]
  X <- matrix(c(x1, x2, x3), nrow = 3)
}

equation    <- matrix(c(1, 1, 3, 2, -1, 5, -1, -2, 4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(1, 2, 6), nrow = 3)

# Display the equation Matrix
print (equation)
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    2   -1    5
## [3,]   -1   -2    4
# Display the constraint vector
print (constraints)
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    6
solution = round (eliminateAndSolve(equation, constraints), 2)
print (solution)
##       [,1]
## [1,] -1.55
## [2,] -0.32
## [3,]  0.95
Test Cases: The function is also tested with the following equations and it successfully returns the solutions.
Test Case 01:
TestCase01
equation    <- matrix(c(-1, -5, -5, 4, -5, 4, 1, 5, -1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(2, 19, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -2
## [2,]   -3
## [3,]    3
Test Case 02:
TestCase02
equation    <- matrix(c(-4, -5, -1, -2, -5, -2, -2, 5, 2), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(18, 12, 4), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -4
## [2,]    0
## [3,]   -2
Test Case 03:
TestCase03
equation    <- matrix(c(-1, -5, 1, -5, -5, 5, 2, 5, -3), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(17, 5, -10), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -1
## [2,]   -4
## [3,]   -4
Test Case 04:
TestCase04
equation    <- matrix(c(4, 4, 1, 2, -4, 1, 5, -4, -5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(24, 0, 12), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    4
## [2,]    2
## [3,]    0
Test Case 05:
TestCase05
equation    <- matrix(c(4, -4, 4, 4, 1, -2, -3, -3, -4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-4, 5, -16), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    1
## [2,]    3
## [3,]    1
Test Case 06:
TestCase06
equation    <- matrix(c(1, -6, 4, 1, 1, -4, 2, 2, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-12, 12, -15), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    0
## [2,]    0
## [3,]   -3
Test Case 07:
TestCase07
equation    <- matrix(c(1, -1, -2, 3, 2, 0, -4, 1, -1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-6, -25, 12), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -5
## [2,]   -5
## [3,]    3
Test Case 08:
TestCase08
equation    <- matrix(c(5, 5, 5, 4, 3, 3, -4, 3, 3), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-20, -6, 9), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    6
## [2,]  Inf
## [3,] -Inf
Test Case 09:
TestCase09
equation    <- matrix(c(-6, 5, 2, -2, 1, 4, 4, -5, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-11, -9, -4), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    4
## [2,]    3
## [3,]   -1
Test Case 10:
TestCase10
equation    <- matrix(c(-6, -2, 2, 3, -2, -4, 6, -2, -6), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-8, 8, -18), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]  Inf
## [2,] -Inf
## [3,]  Inf
Test Case 11:
TestCase11
equation    <- matrix(c(5, -4, 2, -1, -5, 6, -1, -4, 5), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(21, -24, -21), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    5
## [2,]   -1
## [3,]   -4
Test Case 12:
TestCase12
equation    <- matrix(c(6, -1, 3, 5, 5, -5, 3, -1, 4), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-9, 20, -5), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -1
## [2,]    6
## [3,]    1
Test Case 13:
TestCase13
equation    <- matrix(c(-3, -1, -3, -5, 3, 6, -6, -4, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-8, -4, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    2
## [2,]    2
## [3,]    0
Test Case 14:
TestCase14
equation    <- matrix(c(-5, 3, 6, -3, 1, 5, -4, 2, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(4, -5, 13), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]   -2
## [2,]    4
## [3,]   -3
Test Case 15:
TestCase15
equation    <- matrix(c(3, -3, 4, 1, 2, -3, 4, -1, 1), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-23, 25, 25), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]  Inf
## [2,]  Inf
## [3,]  Inf
Test Case 16:
TestCase16
equation    <- matrix(c(-6, -2, -1, 5, 1, -6, -4, -6, -6), nrow = 3, ncol = 3, byrow = TRUE)
constraints <- matrix(c(-17, 19, -20), nrow = 3)
print(round (eliminateAndSolve(equation, constraints), 2))
##      [,1]
## [1,]    2
## [2,]    3
## [3,]   -1