1. 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=u%*%v
cat("The dot product of u and v is:",dot.u.v,"\n")
## The dot product of u and v is: -0.5

2) What are the lengths of u and v?

length.u<-sqrt(u%*%u)
cat("The length of u is:",length.u,"\n")
## The length of u is: 0.7071068
length.v<-sqrt(v%*%v)
cat("The length of v is:",length.v,"\n")
## The length of v is: 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?

dot.u.v=u%*%v
length.u<-sqrt(u%*%u)
length.v<-sqrt(v%*%v)
dot.length.u.v=length.u%*%length.v
angle.in.radians=acos(dot.u.v/dot.length.u.v)
cat("The angle between u and v in radians is:",angle.in.radians,"\n")
## The angle between u and v in radians is: 1.712693
angle.in.degrees=(180*angle.in.radians/pi)
cat("The angle between u and v in radians is:",angle.in.degrees,"\n")
## The angle between u and v in radians is: 98.1301

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.

# This function creates an augmented matrix from the 2 input matrices with the co-efficients and the constraints
augmentMatrix = function(matrixA,matrixb) {
 r = dim(matrixA)[1]
 c = dim(matrixb)[2]+dim(matrixA)[2]
 matrixB = matrix(c(matrixA,matrixb), ncol=c, nrow=r)
return(matrixB)
}
# This function modifies the matrix and brings all zero rows to the bottom
modifyMatrix=function(matrixA) {
  numberOfRows=as.numeric(dim(matrixA)[1])
  n=0
  z=0
  zeroRoWIndex<-c()
  nonZeroRoWIndex<-c()
  matrixZ<-c()
  matrixN<-c()
  rowIndex<-as.numeric(0)
  r=0
# matrixB<-matrixA[order(matrixA[,1], decreasing = TRUE),]
  for(rowIndex in 1:numberOfRows) {
    if(all(matrixA[rowIndex,]== 0)) {
    zeroRoWIndex<-c(zeroRoWIndex,rowIndex)
    matrixZ<-rbind(matrixZ,matrixA[rowIndex,])
    z=z+1
    }
    else {
      nonZeroRoWIndex<-c(nonZeroRoWIndex,rowIndex)
      matrixN<-rbind(matrixN,matrixA[rowIndex,])
      n=n+1
      }
    }
  matrixC<-rbind(matrixN,matrixZ)
  return(matrixC)
}
# This is the main function. It converts the input matrices to Reduced Row Echelon Form
reduceMatrix<-function(matrixA, matrixb) {
  matrixB<-augmentMatrix(A,b)
  matrixB<-modifyMatrix(matrixB)
  numberOfRows=(dim(matrixB)[1])
  numberOfColumns=(dim(matrixB)[2])
  swap_row=c()
  colIndex=0
  rowIndex=0
  r=0
  #for(colIndex in seq_along(numberOfColumns)) 
  while(colIndex<=numberOfColumns) {
    r=colIndex+1
    rowIndex=r
    colIndex=colIndex+1
 # cat("Processing Col index: ",colIndex,"\n")
    k=0
 # cat("r: ",r,"\n")
 # cat("Row index: ",rowIndex,"\n") 
   
    while(rowIndex<=numberOfRows) {
      if (all(matrixB[(1:numberOfRows),colIndex]== 0)) {
  #     cat("All rows in this column :",colIndex,"are 0 \n")
        rowIndex=numberOfRows
        rowIndex=rowIndex+1
        }
          
       else if (matrixB[rowIndex,colIndex]!=0)   {
       # rowIndex=min(which(matrixB[(r+1:numberOfRows),colIndex]!=0))
       # cat("Row index: ",rowIndex,"\n")
       # cat("r: ",r,"\n")
         swap_row<-matrixB[r,]
         matrixB[r,]=matrixB[rowIndex,]
         matrixB[rowIndex,]=swap_row
     #   print(matrixB)
         matrixB[r,]=(matrixB[r,]/matrixB[r,colIndex])
    
          for(k in 1:numberOfRows) {
            if(k==r){
              next
            }
            else {
        #     cat("Processing row ",k,"for col ",colIndex,"\n")
              matrixB[k,]=matrixB[k,]-(matrixB[k,colIndex]/matrixB[r,colIndex]*matrixB[r,])
              }
        # print(matrixB)
          }
          rowIndex=numberOfRows
          rowIndex=rowIndex+1
        }
      
      else if (matrixB[rowIndex,colIndex]==0) {
       # cat("cell in row :",rowIndex ,"and col :",colIndex, "has 0. Nothing to do \n")
         rowIndex=rowIndex+1
        }
    }
  }
   # print(matrixB)
    return(matrixB)
}

Test the function by calling it on an input for 3 equations and 3 variables, as shown below:

A<-matrix(c(1,2,1,-1,1,1,2,1,0), nrow=3, ncol=3)
b<-matrix(c(1,8,5),nrow=3, ncol=1)
rrefMatrix=reduceMatrix(A,b)
rrefMatrix
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    1    3
## [2,]    0    1   -1    2
## [3,]    0    0    0    0