Grando 1 Homework

options(width = 120)
# This is a standard setup I include so that my working directory is set correctly whether I work on
# one of my windows or linux machines.
if (Sys.info()["sysname"] == "Windows") {
    setwd("~/Masters/DATA605/Week1/HW")
} else {
    setwd("~/Documents/Masters/DATA605/Week1/HW")
}

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

v1a = 0.5
v1b = 0.5
v2a = 3
v2b = -4

The simple way:

(dp <- unlist(list(c(v1a, v1b) %*% c(v2a, v2b))))
## [1] -0.5

The hard way:

hypf <- function(x, y) {
    x <- sqrt(x^2 + y^2)
    return(x)
}

angledf <- function(x, y) {
    x <- y - x
    x <- ifelse(x > 2 * pi, x - 2 * pi, ifelse(x < 0, x + 2 * pi, x))
    return(x)
}

hyp1 = hypf(v1a, v1b)
angle1 <- acos(v1b/hyp1)
hyp2 = hypf(v2a, v2b)
angle2 <- acos(v2b/hyp2)
adf <- angledf(angle1, angle2)
(dp2 <- hyp1 * hyp2 * cos(adf))
## [1] -0.5

Answer:

Using the simple way, the dot product is -0.5. Using the hard way, the dot product is -0.5.

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

v1l = sqrt(unlist(list(c(v1a, v1b) %*% c(v1a, v1b))))
v2l = sqrt(unlist(list(c(v2a, v2b) %*% c(v2a, v2b))))

Answer:

The mathematical length (magnitude) is 0.71 for v1 and 5 for v2. This can also be computed as the hypotenuse of the components for each vector (v1 = 0.71, v2 = 5)

The computer science term for lengths is the number of dimenstions (i.e. 2)

1.3 What is the linear combination: 3u − 2v?

Answer:

v1 = c(0.5, 0.5)
v2 = c(3, -4)
comb = 3 * v1 - 2 * v2
print(comb)
## [1] -4.5  9.5

1.4 What is the angle between u and v

Answer:

This was computed above (1.1) and found to be 1.71 radians (e.g. -4.57 radians)

2.1 Matrix Solution Function

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.

solve_matrix <- function(A, C) {
    mtx <- cbind(A, C)
    fixrow = 0
    for (col in 1:as.integer(dim(mtx)[2] - 1)) {
        for (row in 1:as.integer(dim(mtx)[1])) {
            if (row == col & mtx[row, col] == 0) {
                for (subrow in (row + 1):as.integer(dim(mtx)[1])) {
                  if (subrow > dim(mtx)[1]) {
                    stop("A more complex matrix solver is necessary")
                  }
                  if (mtx[subrow, col] != 0) {
                    tmprow = mtx[row, ]
                    mtx[row, ] = mtx[subrow, ]
                    mtx[subrow, ] = tmprow
                    fixrow = 1
                  }
                }
                if (fixrow == 0) {
                  stop("A more complex matrix solver is necessary asfas")
                }
            }
            if (row == col) {
                mtx[row, ] = mtx[row, ]/mtx[row, col]
            }
            if (row > col) {
                mtx[row, ] <- mtx[col, ] * (mtx[row, col]/mtx[col, col]) - mtx[row, ]
            }
        }
    }
    X_mtx <- matrix(c(rep(0, dim(mtx)[1])), nrow = dim(mtx)[1])
    for (row in as.integer(dim(mtx)[1]):1) {
        tmp_val <- 0
        for (col in as.integer(dim(mtx)[2] - 1):row) {
            if (row < col) {
                tmp_val <- mtx[row, col] * X_mtx[col, ] + tmp_val
            }
            if (row == col) {
                X_mtx[row] = (mtx[row, dim(mtx)[2]] - tmp_val)/mtx[row, col]
            }
        }
    }
    return(X_mtx)
}
A_mtx <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow = 3)
C_mtx <- matrix(c(1, 2, 6), nrow = 3)
X_mtx <- solve_matrix(A_mtx, C_mtx)
print(X_mtx)
##            [,1]
## [1,] -1.5454545
## [2,] -0.3181818
## [3,]  0.9545455

Example with zero values in the pivot points.

A_mtx <- matrix(c(0, 1, -4, 1, 0, -1, 1, 1, 0), nrow = 3)
C_mtx <- matrix(c(1, 2, 6), nrow = 3)
X_mtx <- solve_matrix(A_mtx, C_mtx)
print(X_mtx)
##      [,1]
## [1,]   -1
## [2,]   -2
## [3,]    3