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.
u·v = u1v1 + u2v2
u·v = 1.5 - 2
u·v = -0.5
#library(geometry)
u = c(0.5, 0.5)
v = c(3, -4)
uv_dot_prod = dot(u,v,d = NULL)
uv_dot_prod
## [1] -0.5
Length of u is:
\(\sqrt{u·u}\)
\(\sqrt{0.5^2 + 0.5^2}\)
\(\sqrt{0.5} = 0.7071068\)
||u|| = 0.7071068
Lengths of v is:
\(\sqrt{v·v}\)
\(\sqrt{3^2 + (-4)^2}\)
\(\sqrt{25} = 5\)
||v|| = 5
length(u)
## [1] 2
length(v)
## [1] 2
u3 = 3 * u
v2 = 2 * v
u3 - v2
## [1] -4.5 9.5
cosθ = (u • v) / (||u|| • ||v||)
length_u = 0.7071068
length_v = 5
rad2deg <- function(rad) {(rad * 180) / (pi)}
r = acos(uv_dot_prod/(crossprod(length_u, length_v)))
rad2deg(r)
## [,1]
## [1,] 98.1301
I used the methodology found in the following youtube video for assistance: https://www.youtube.com/watch?v=f-zQJtkgcOE
elimination_system_equations <- function(A, b){
gj <- cbind(A, b)
value <- (A[,1]/(-1*A[1,1]))
multiply <- matrix(c(1, value[2], value[3], 0, 1, 0, 0, 0, 1), nrow=3, ncol=3)
transformation_1 <- multiply %*% gj
value_2 <- -1*(transformation_1[3,2]/transformation_1[2,2])
multiply_2 <- matrix(c(1, 0, 0, 0, 1, value_2, 0, 0, 1), nrow=3, ncol=3)
upper_triangular_matrix <- multiply_2 %*% transformation_1
z = upper_triangular_matrix[3,4]/upper_triangular_matrix[3,3]
y = (upper_triangular_matrix[2,4]-(upper_triangular_matrix[2,3]*z))/upper_triangular_matrix[2,2]
x = (upper_triangular_matrix[1,4]-(upper_triangular_matrix[1,3]*z)-(upper_triangular_matrix[1,2]*y))/upper_triangular_matrix[1,1]
result <- rbind(x, y, z)
return (result)
}
matrix_3 <- matrix(
c(1, 2, -1, 1, -1, -2, 3, 5, 4),
nrow=3,
ncol=3)
constraint_3 <- matrix(
c(1, 2, 6),
nrow=3,
ncol=1)
elimination_system_equations(matrix_3, constraint_3)
## [,1]
## x -1.5454545
## y -0.3181818
## z 0.9545455
test_A <- matrix(
c(2, 4, 6, 4, -2, -4, -2, 6, 2),
nrow=3,
ncol=3)
test_b <- matrix(
c(-10, 20, 18),
nrow=3,
ncol=1)
elimination_system_equations(test_A, test_b)
## [,1]
## x 1
## y -2
## z 2