library(geometry)
library(matlib)

Problem Set 1

1. Calculate the dot product u.v where u = [0.5; 0.5] and v = [3; -4]

u <- c(0.5, 0.5)
v <- c(3, -4)
dot(u,v)
## [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.

It is the square root of the inner-product of a vector with itself. The dot product of a vector gives the length of a vector squared (Excerpt from Lecture 1).
Length of vector u:

sqrt(0.5^2 + 0.5^2)
## [1] 0.7071068

Length of vector v:

sqrt(3^2 + (-4)^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?

Additionaly, we know that the dot product between u and v is the cosine of the angle between them.

acos( sum(u*v) / ( sqrt(sum(u * u)) * sqrt(sum(v * v)) ) )
## [1] 1.712693

Problem Set 2

1. 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.

solveByElimination3x3 <- function(A, b){
  
p <- nrow(A)
gaussJ <- cbind(A,b)

gaussJ[1,] <- gaussJ[1,]/gaussJ[1,1]

i <- 2
while (i < p+1) {
 j <- i
 while (j < p+1) {
  gaussJ[j, ] <- gaussJ[j, ] - gaussJ[i-1, ] * gaussJ[j, i-1]
  j <- j+1
 }
 while (gaussJ[i,i] == 0) {
  gaussJ <- rbind(gaussJ[-i,],gaussJ[i,])
 }
 gaussJ[i,] <- gaussJ[i,]/gaussJ[i,i]
 i <- i+1
}
for (i in p:2){
 for (j in i:2-1) {
  gaussJ[j, ] <- gaussJ[j, ] - gaussJ[i, ] * gaussJ[j, i]
 }
}
gaussJ
}

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

solveByElimination3x3(A,b)
##                     b
## [1,] 1 0 0 -1.5454545
## [2,] 0 1 0 -0.3181818
## [3,] 0 0 1  0.9545455