Skip to content
Prev 275227 / 398506 Next

symmetric matrix multiplication

On 23-Oct-11 07:00:07, Daniel Nordlund wrote:
In addition to Dan's comment, let me point out that you
can convert your very nearly symmetric matrix C to an
exactly (even by R's finite-precision standards) symmetric
matrix by using (C + t(C))/2. The result will differ from
the original matrix C by similar "for all intents and purposes
zero" amounts. Here is an example, using 4x4 matrices:

##[1]: The symmetric matrix B:
B <- matrix( c(
  1.1, 1.2, 1.3, 1.4,
  1.2, 2.2, 2.3, 2.4,
  1.3, 2.3, 3.3, 3.4,
  1.4, 2.4, 3.4, 4.4), byrow=TRUE, nrow=4 )

##[2]: The non-symmetric matrix B:
A <- matrix( c(
  1.1, 1.2, 1.3, 1.4,
  2.1, 2.2, 2.3, 2.4,
  3.1, 3.2, 3.3, 3.4,
  4.1, 4.2, 4.3, 4.4), byrow=TRUE, nrow=4 )

##[3]: An allegedly symmetric matrix C1 (constructed
##     like your C):
C1 <- t(A)%*%B%*%A

##[4]: But it isn't exactly symmetric:
max(abs(C1 - t(C1)))
# [1] 5.684342e-14

##[5]: So construct an exactly symmetric version:
C2 <- (C1 + t(C1))/2

##[6]: Check that it is exactly symmetric:
max(abs(C2 - t(C2)))
# [1] 0

##[7]: And check how close it is to the original C1:
max(abs(C2 - C1))
# 1] 5.684342e-14

Hoping this helps!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Oct-11                                       Time: 08:43:18
------------------------------ XFMail ------------------------------