Problem Set I

(1) Calculate the dot product \(\vec{u} \cdot \vec{v}\) where \(\vec{u} = [0.5; 0.5]\) and \(\vec{v} = [3;-4]\)

u <- matrix(c(0.5, 0.5), nrow = 2)
v <- matrix(c(3, -4), nrow = 2)

crossprod(u,v)
##      [,1]
## [1,] -0.5

(2) What are the lengths of \(\vec{u}\) and \(\vec{v}\)? Please note that the mathematical notion of the length of a vector is not the same as a computer science definition.

\(\left \| \vec{u} \right \| = \sqrt{0.5^2 + 0.5^2} = 0.7071068\)

\(\left \| \vec{v} \right \| = \sqrt{3^2 + (-4)^2} = 5\)

What is the linear combination: \(3\vec{u} - 2\vec{v}\)?

3*u - 2*v
##      [,1]
## [1,] -4.5
## [2,]  9.5

(4) What is the angle between \(\vec{u}\) and \(\vec{v}\)?

\(cos\theta = \frac{\vec{u} \cdot \vec{v}}{\left \| \vec{u} \right \| \cdot \left \| \vec{v} \right \|} = \frac{-0.5}{3.5355339} = -0.1414214\)

\(\theta = acos(-0.1414214) = 1.7126934\) radians \(= 98.1301165\) degrees.

II Solve Sytem of Equations

A <- matrix(c(
    1, 1, 3,
    2, -1, 5,
    -1, -2, 4),
    byrow = TRUE, nrow = 3)

b <- matrix(c(
    1,
    2,
    6),
    byrow = TRUE, nrow = 3)

i_dim = nrow(A)
j_dim = ncol(A)

# via Gauss Jordan Elimination -
# The following loop produces the unfactored
# identity matrix
for (i in 1:i_dim) {
    for (j in 1:j_dim) {
        if (i == j) {
            for (k in 1:i_dim){
                if (k != i & A[k,j] != 0) {
                    multi <- A[i,j] / A[k,j]
                    A[k,] <- A[k,] * multi - A[i,]
                    b[k] <- b[k] * multi - b[i]
                }
            }
        }
    }
}

# Loop will create factored identity matrix
for (l in 1:i_dim) {
    b[l] <- b[l]/A[l,l]
    A[l,l] <- A[l,l]/A[l,l]
}

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