0
A <- structure(c(0.30490157183014, 0.379973564544057, 0.873301277763403, 
-0.893646079502174, 0.431187404805374, 0.124394961825634, 0.329289714515811, 
0.81835048248589, -0.471031603747781), .Dim = c(3L, 3L))
B <- structure(c(0.149775627871975, 0.180504771175303, 0.972103538147304, 
-0.636965365859095, 0.769591777715467, -0.0447617958613814, 0.756202607765634, 
0.612492059749103, -0.230241379323433), .Dim = c(3L, 3L))

I have two 3x3 matrices, A and B. I want to compute: AA^TBB^T.

> A %*% t(A) %*% B %*% t(B)
              [,1]          [,2]          [,3]
[1,]  1.000000e+00 -5.551115e-17 -1.665335e-16
[2,] -5.551115e-17  1.000000e+00 -2.498002e-16
[3,] -1.942890e-16 -2.498002e-16  1.000000e+00
> (A %*% t(A)) %*% (B %*% t(B))
              [,1]          [,2]          [,3]
[1,]  1.000000e+00  1.078521e-32 -1.665335e-16
[2,]  3.081488e-32  1.000000e+00 -2.498002e-16
[3,] -1.665335e-16 -2.498002e-16  1.000000e+00

Why are A %*% t(A) %*% B %*% t(B) and (A %*% t(A)) %*% (B %*% t(B)) giving me different results? Is this implying that AA^TBB^T is different from (AA^T)(BB^T)?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Adrian
  • 9,229
  • 24
  • 74
  • 132
  • Those numbers appear to be close to within machine epsilon of each other... There's a duplicate here about floating point math precision – duckmayr Jul 27 '20 at 19:39
  • In particular, matrix multiplication is just a lot of scalar multiplication and addition in a particular structured way. Computers do not do _exact_ math, so these errors can accumulate slightly differently with different operation orderings. So, while matrix multiplication is associative, you will see **small** differences in practice – duckmayr Jul 27 '20 at 19:41
  • I see. Thank you. – Adrian Jul 27 '20 at 19:43

0 Answers0