vec.u <- c(0,4,-1) #create vector U
dim(vec.u) <- c(3,1) #convert vector into column matrix
l1u <- norm(vec.u, type = "1") #calculate magnitude of "vec" matrix (Manhattan norm / L1)
l2u <- norm(vec.u, type = "2") #calculate L2 norm of "vec" matrix (Euclidean norm)
The 1st Normal vector of U is 5
The 2nd Normal vector of U is 4.1231056
Creating a vector in the opposite direction first requires we find the magnitude. We can then multiply our original vector by the negative inverse.
opp.vec.u <- (-1/l2u) * vec.u
A unit vector in the opposite direction of our original vec.u is [0, -0.9701425, 0.2425356].
Computing the cosine angle between two vectors U and V:
First we create the vector V and calcuate it’s L1 and L2 normal values
vec.v <- c(-2,3,7) #create vector V
dim(vec.v) <- c(3,1) #convert vector into column matrix
l1v <- norm(vec.v, type = "1") #calculate magnitude of "vec" matrix (Manhattan norm / L1)
#This is the same as summing the magnitude of each component
l2v <- norm(vec.v, type = "2") #calculate L2 norm of "vec" matrix (Euclidean norm)
#This is the same as sqauring the square roots of each component
Then we find the dot product of our two vectors U and V
dot.vectors <- c(0,4,-1) %*% c(-2,3,7)
Now we can calculate the cosine of the angle by dividing the dot product by the product of each vector’s magnitude
cosine.uv <- dot.vectors / (l1u * l1v)
The Cosine of the angle between vec.u & vec.v is 0.0833333.
Simply knowing we have a value for the cosine of the angle between the two vectors does not mean we know whether or not they are orthogonal (i.e. 90 degrees from each other). But knowing that either their dot product is zero of that the angle theta is 90 will confirm it. We will calculate it both ways.
inv.cosine <- acos(cosine.uv)
Since the Cosine of the angle between vec.u & vec.v is 0.0833333. Then we know theta = inverse cosine (0.0833333) or 1.4873662 degrees
Alternatively we can simply see if the dot product of the two original vectors equals zero
dot.vectors <- c(0,4,-1) %*% c(-2,3,7)
The dot product of our two vectors in question equals 5, not zero.
This means that by two measures our vectors are not orthogonal to each other
l1u <- norm(vec.u, type = "1") #calculate magnitude of "vec" matrix (Manhattan norm / L1)
The L1 norm is 5
Computing the dot products of vectors UV and VA, then comparing them yields:
dot.vectors.UV <- c(0,4,-1) %*% c(-2,3,7)
dot.vectors.VA <- c(-2,3,7) %*% c(0,4,-1)
The dot product of UV is 5 and the dot product of VA is 5
Since their difference is 0, we can conclude the dot product of two matrices are commutative
We might expect to see the same results when computing the matrix product of AB and BA, but let’s check:
Matrix A| 2 | 0 | 1 |
| 3 | -1 | 4 |
| -2 | 5 | 0 |
| 4 | -2 | 7 |
| 0 | 2 | -1 |
| 4 | 0 | 3 |
| 12 | -4 | 17 |
| 28 | -8 | 34 |
| -8 | 14 | -19 |
| -12 | 37 | -4 |
| 8 | -7 | 8 |
| 2 | 15 | 4 |
This means the order does indeed matter and matrix multiplication is not commutative
Now let’s compute the Trace of both dot product matrices AB & BA above, then compare
#Trace of Dot Product of Matrix AB
tr.prod.matrix.AB <- prod.matrix.AB[1,1] + prod.matrix.AB[2,2] + prod.matrix.AB[3,3]
#Trace of Dot Product of Matrix BA
tr.prod.matrix.BA <- prod.matrix.BA[1,1] + prod.matrix.BA[2,2] + prod.matrix.BA[3,3]
We can see the Trace of the dot product matrix of AB is -15
And the Trace of the dot product matrix of BA is -15
Which means the trace of the dot product of two matrices is commutative, despite the dot products not being commutative themselves
Let’s manually compute the product of a series of row and column vectors, then compare those calculations to the dot product of AB & BA
#Define our row and column vectors
a <- c(2,3,-2)
b <- c(4,-2,7)
c <- c(0,-1,5)
d <- c(0,2,-1)
e <- c(1,4,0)
f <- c(4,0,3)
manual.test <- (a %*% b) + (c %*% d) + (e %*% f)
Interesting that we see our manual.test variable (manual dot product) = -15 which is also the Trace of dot product AB & BA.
The relationship between the calculations in #8 and the dot product of B & A is illustrated below:
The first two elements are -12 (which is also the BA[1,1] element of BA)
The second two elements are -7 (which is also the BA[2,2] element of BA)
And the final two elements are 4 (which is also the BA[3,3] element of BA)
Meaning, the calculations as listed are the columns of A dotted with the rows of B, or the dot product of matrices B & A.
In addition, the sum is also the Trace of our two square matrices AB & BA