I have two matrices, A and B, and I am attempting to multiply them in the following way:
t(A%*%B)%*%A
Typically, the order of matrix multiplication matters, but in this case B is a diagonal matrix with all elements equal. Therefore, I can write B = c*I where I is the identity matrix with dimension = dim(B). Then, I should be able to obtain my matrix product by computing
t(A)%*%A%*%B
since
t(A%*%B)%*%A = t(A%*%cI)%*%A = t(A%*%I)%*%A*c = t(A)%*%A*c = t(A)%*%A*cI = t(A)%*%A%*%B
However, when I attempt to do this using my matrices, I do not get the same result using both calculations:
A <- data[, 1:4]
B <- diag(c(1 + gamma + (gamma^2)*gamma_R), nrow = 4, ncol = 4)
t(A%*%B)%*%A - t(A)%*%A%*%B
[1,] 1.421085e-14 1.136868e-13 0 0
[2,] 1.136868e-13 0.000000e+00 0 0
[3,] 0.000000e+00 0.000000e+00 0 0
[4,] 0.000000e+00 0.000000e+00 0 0
gamma and gamma_R are just coefficients obtained from a previous regression that I ran. Machine precision is 2.220446e-16, and these errors are several orders of magnitude above machine precision. Is the discrepancy due to the way in which the quantities that I multiply first are truncated due to machine precision, and these get magnified through the summing that occurs when multiplying by the final matrix? Or is there another likely reason? A is only 15X4, and B is 4X4.
If numerical precision is the reason for the discrepancy, then is there a preferred order for doing the operations? Should matrix products always be simplified in such a way that result in the fewest number of calculations?