david
david

Reputation: 103

inverse of symmetric matrix is not symmetric in Julia

I am using Julia version 0.6.2 and I am facing this problem.

mat = zeros(6, 6)
for i = 1 : 6
    for j = 1 : 6
        mat[i, j] = exp(-(i - j)^2)
    end
end
issymmetric(mat)
issymmetric(inv(mat))

And the output is

Main> issymmetric(mat)
true
Main> issymmetric(inv(mat))
false

I also tried the following Matlab code

mat = zeros(6, 6);
for i = 1 : 6
    for j = 1 : 6
        mat(i, j) = exp(-(i - j)^2);
    end
end
issymmetric(mat)
issymmetric(inv(mat))

And the output is

logical 1
logical 1

Upvotes: 2

Views: 637

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

Apart from manually making the matrix symmetric as you propose, e.g. taking the average of matrix and its transpose like

A = inv(mat)
(A+A.')/2

probably a cleaner way is

smat = Symmetric(mat)
B = inv(smat)

now B (as well as smat) passes issymmetric. Moreover, the fact that it is symmetric is ensured on type level (Symmetric) - some functions might take advantage of this additional information. This is exactly what inv does for smat.

EDIT: the question was also posted on Discourse, where you can find additional discussion about the performance of Symmetric.

Upvotes: 5

Related Questions