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?