You can think of vectors representing many dimensions of related information. For instance, Netflix might store all the ratings a user gives to movies in a vector. This is clearly a vector of very large dimensions (in the millions) and very sparse as the user might have rated only a few movies. Similarly, Amazon might store the items purchased by a user in a vector, with each slot or dimension representing a unique product and the value of the slot, the number of such items the user bought. One task that is frequently done in these settings is to find similarities between users. And, we can use dot-product between vectors to do just that. As you know, the dot-product is proportional to the length of two vectors and to the angle between them. In fact, the dot-product between two vectors, normalized by their lengths is called as the cosine distance and is frequently used in recommendation engines.
# Create matrices u and v (will be promoted to matrices by %*%)
u <- c(0.5, 0.5)
v <- c(3, -4)
# Calculate dot product
u %*% v
## [,1]
## [1,] -0.5
# Calculate length of u
sqrt(u %*% u)
## [,1]
## [1,] 0.7071068
# Calculate length of v
sqrt(v %*% v)
## [,1]
## [1,] 5
# Calculate 3u - 2v
(3 * u) - (2 * v)
## [1] -4.5 9.5
angle <- acos((u %*% v) / (sqrt(u %*% u) * sqrt(v %*% v)))
# In degrees
(180 * angle) / pi
## [,1]
## [1,] 98.1301
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.
Please test it with the system below and it should produce a solution x = [-1.55, -0.32, 0.95]
# Used this system of 3 equations from Strang text to built function
ex1 <- matrix(c(2, 4, -2, 4, 9, -3, -2, -3, 7, 2, 8, 10), nrow = 3, ncol = 4)
ex1
## [,1] [,2] [,3] [,4]
## [1,] 2 4 -2 2
## [2,] 4 9 -3 8
## [3,] -2 -3 7 10
# Function which solves for x
# I have it solving x, y and z and returning them in that order as a vector
findx <- function(mx) {
# mx is 3x4 matrix, start by finding R2C1 / R1C1 (first pivot)
p1 <- mx[2, 1] / mx[1, 1]
# take p1 * R1 and subtract from R2
elim1 <- matrix(c(mx[1, ], mx[2, ] - (p1 * mx[1, ]), mx[3, ]),
ncol = 4, nrow = 3, byrow = TRUE)
# find R3C1 / R1C1 (2nd pivot)
p2 <- elim1[3, 1] / elim1[1, 1]
# take p2 * R1 and subtract from R3
elim2 <- matrix(c(elim1[1, ], elim1[2, ], elim1[3, ] - (p2 * elim1[1, ])),
ncol = 4, nrow = 3, byrow = TRUE)
# find R3C2 / R2C2 (3rd pivot)
p3 <- elim2[3, 2] / elim2[2, 2]
# take p3 * R2 and subtract from R3
elim3 <- matrix(c(elim2[1, ], elim2[2, ], elim2[3, ] - (p3 * elim2[2, ])),
ncol = 4, nrow = 3, byrow = TRUE)
# Solve for z, y, and then x
z <- elim3[3, 4] / elim3[3, 3]
y <- (elim3[2, 4] - (elim3[2, 3] * z)) / elim3[2, 2]
x <- (elim3[1, 4] - ((elim3[1, 3] * z) + (elim3[1, 2] * y))) / elim3[1, 1]
return(c(x, y, z))
}
# Test function on ex1 (matrix from book used to build function)
findx(ex1)
## [1] -1 2 2
# Create 2nd example matrix; is the test matrix from assignment
ex2 <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4, 1, 2, 6), nrow = 3, ncol = 4)
ex2
## [,1] [,2] [,3] [,4]
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
# Test function on ex2
findx(ex2)
## [1] -1.5454545 -0.3181818 0.9545455