0

I encounter a weird problem while using Breeze and I would like to know what could be the possible reasons.

I have a symmetric matrix, containing only small positive values. I need to get the eigenvalues and eigenvectors of the normalized matrix thus I am using:

val dataset = new File(getClass.getResource("/matrix.csv").getPath())
val a = breeze.linalg.csvread(dataset)
val diagA = diag(pow(sum(a(*, ::)), -0.5))
val b = diagA * a * diagA // Multiplying a symmetric matrix with a diagonal matrix should still give a diagonal matrix.
println(eigSym(b)) 

Doing this returns [error] (run-main-0) breeze.linalg.MatrixNotSymmetricException: Matrix is not symmetric.

If I compute eigSym(a) after reading a it from matrix.csv it will work thus I am sure that a is symmetric.

To find where is the error, I have tried the computation when creating random versions of a and in that case it works:

// Creation of a big symmetric matrix.
var a = DenseMatrix.rand(240, 240)
var row, col = 0
for (row <- 0 until a.rows) {
    for (col <- row until a.cols) {
        if (col == row) {
            a(row, col) = 0.0
        } else {
            a(col, row) = a(row, col)
        }
    }
}

println(eigSym(a)) // Works.

// Same diagA as before.
val diagA = diag(pow(sum(a(*, ::)), -0.5))
val b = diagA * a * diagA
println(eigSym(b)) // Also works.

What could be wrong in the original symmetric matrix I am using to make the computation fail?

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60

1 Answers1

0

Suppose the sum() is yielding a zero. Raising that to the power -0.5 produces floating-point Infinity in the diagonal matrix; the subsequent product produces a matrix containing NaN. Technically this matrix is symmetric, but the actual code to test that cannot succeed, because it will test NaN for equality with NaN, which always returns FALSE.

Regarding the random check: Note that a random matrix is not likely to yield a zero in sum().

PMar
  • 1
  • How could the sum of each row yields zero as the matrix contains small positive values everywhere (the only zeros being in the diagonal)? – Armand Grillet Jul 12 '16 at 15:37