Basic Vector Operations and R Program to Solve Linear Equations
Problem Set 1
You can think of vectors representing many dimensions of related information. For instance, Netix 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. (1) Calculate the dot product u.v where u = [0.5; 0.5] and v = [3;-4] (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 defnition. (3) What is the linear combination: 3u - 2v? (4) What is the angle between u and v
Load Packages
library(knitr)- 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_prod_u_v <- u %*% v
dot_prod_u_v## [,1]
## [1,] -0.5
- 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 defnition.
length_of_u <- sqrt(u[1]^2 + u[2]^2)
print(paste('length_of_u =', length_of_u))## [1] "length_of_u = 0.707106781186548"
length_of_v <- sqrt(v[1]^2 + v[2]^2)
print(paste('length_of_v =', length_of_v)) ## [1] "length_of_v = 5"
- What is the linear combination: 3u - 2v?
print("linear combination: 3u - 2v = ")## [1] "linear combination: 3u - 2v = "
3 * u - 2 * v## [1] -4.5 9.5
- What is the angle between u and v
We already computed the component values dot_prod_u_v, length_of_u, length_of_v. So, we’ll use them below:
print(paste(acos(dot_prod_u_v / (length_of_u * length_of_v)), 'Radians'))## [1] "1.71269338139906 Radians"
print(paste(acos(dot_prod_u_v / (length_of_u * length_of_v)) * 180 / pi, 'Degrees'))## [1] "98.130102354156 Degrees"
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.
Please test it with the system below and it should produce a solution x = [-1.55, -0.32, 0.95]
1.x1 + 1.x2 + 3.x3 = 1 ......................... (1)
2.x1 - 1.x2 + 5.x3 = 2
-1.x1 - 2.x2 + 4.x3 = 6
Answer: Please find below the solution to the above problem. First I wrote a function my_gauss_jordan_transformation(), which accepts two parameters–a matrix and the constraints. The comments explain the code. Right below the function, I made a call to the function, using the matrix and constraints of the given equation (1).
my_gauss_jordan_transformation <- function(m, b)
{
# Incoming parameters bound together, to form augmented matrix.
augmented_m <- cbind(m, b)
#
# Setting up variables.
#
cols <- c(1:nrow(m))
rows <- c(1:ncol(m))
dynamic_rows <- rows
#
# Processing starts here.
#
for(col in cols) {
for(row in dynamic_rows) {
if( (augmented_m[row, col]) == 0 ) { # Iterating through rows, to get to the first non-zero element of current column.
next
} else { # Else-block means first non-zero element (pivot) was found.
k <- row # Preserving the current row index.
dynamic_rows <- dynamic_rows [!dynamic_rows %in% c(k)] # Knocking off current index, to skip iterations in next column.
augmented_m[k,] <- (augmented_m[k,] / augmented_m[k, col]) # Dividing elements of current row, by pivot. Pivot becomes 1.
#
for(row in rows) { # Iterating through rows, to reduce other elements to 0.
if((row != k) & (augmented_m[row, col] != 0)) { # Don't reduce zero elements and itself.
augmented_m[row,] <- augmented_m[row,] + (-1) * augmented_m[k, col] * augmented_m[row, col] * augmented_m[k,]
}
}
break # After the pivot was found, no need to proceed further.
}
}
}
return(augmented_m)
}In the following, I made a call to the above function my_gauss_jordan_transformation, with matrix and constraints of the given equation.
m <- matrix(c(1, 2, -1, 1, -1, -2, 3, 5, 4), nrow = 3)
b <- c(1, 2, 6)
#
cat('Original augmented matrix:')## Original augmented matrix:
print(cbind(m, b))## b
## [1,] 1 1 3 1
## [2,] 2 -1 5 2
## [3,] -1 -2 4 6
#
processed_m <- my_gauss_jordan_transformation(m, b)
#
cat('Processed augmented matrix:')## Processed augmented matrix:
print(processed_m)## b
## [1,] 1 0 0 -1.5454545
## [2,] 0 1 0 -0.3181818
## [3,] 0 0 1 0.9545455
Finally, I got the following solution: x1 = -1.5454545, x2 = -0.3181818, x3 = 0.9545455
This is very close to the given result of x = [-1.55, -0.32, 0.95].
Marker: 605-01