Skip to content

Commit 96d2ecb

Browse files
author
Li Pu
committed
improve eigenvalue sorting
1 parent e1db950 commit 96d2ecb

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/EigenValueDecomposition.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,22 @@ object EigenValueDecomposition {
100100

101101
val computed = iparam(4)
102102

103-
val s = BDV(d)(0 until computed)
104-
val U = new BDM(n, computed, z)
105-
106-
val sortedEigenValuesWithIndex = s.toArray.zipWithIndex.sortBy(-1 * _._1).zipWithIndex
103+
val eigenPairs = java.util.Arrays.copyOfRange(d, 0, computed).zipWithIndex.map{
104+
r => (r._1, java.util.Arrays.copyOfRange(z, r._2 * n, r._2 * n + n))
105+
}
107106

108-
val sorteds = BDV(sortedEigenValuesWithIndex.map(_._1._1))
109-
val sortedU = BDM.zeros[Double](n, computed)
107+
val sortedEigenPairs = eigenPairs.sortBy(-1 * _._1)
110108

111109
// copy eigenvectors in descending order of eigenvalues
112-
sortedEigenValuesWithIndex.map{
110+
val sortedU = BDM.zeros[Double](n, computed)
111+
sortedEigenPairs.zipWithIndex.map{
113112
r => {
114-
sortedU(::, r._2) := U(::, r._1._2)
113+
for (i <- 0 until n) {
114+
sortedU.data(r._2 * n + i) = r._1._2(i)
115+
}
115116
}
116117
}
117118

118-
(sorteds, sortedU)
119+
(BDV(sortedEigenPairs.map(_._1)), sortedU)
119120
}
120121
}

0 commit comments

Comments
 (0)