Skip to content

Commit

Permalink
prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
rezazadeh committed Jan 4, 2014
1 parent e9bd6cb commit cdff9fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
9 changes: 5 additions & 4 deletions mllib/src/main/scala/org/apache/spark/mllib/linalg/SVD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ object SVD {
// Construct jblas A^T A locally
val ata = DoubleMatrix.zeros(n, n)
for (entry <- emits.toArray) {
ata.put(entry._1._1-1, entry._1._2-1, entry._2)
ata.put(entry._1._1 - 1, entry._1._2 - 1, entry._2)
}

// Since A^T A is small, we can compute its SVD directly
Expand All @@ -158,12 +158,12 @@ object SVD {
MatrixEntry(i + 1, j + 1, V.get(i,j)) }.flatten)

val retS = sc.makeRDD(Array.tabulate(sigma.length){
x => MatrixEntry(x + 1,x + 1, sigma(x))})
x => MatrixEntry(x + 1, x + 1, sigma(x))})

// Compute U as U = A V S^-1
// turn V S^-1 into an RDD as a sparse matrix
val vsirdd = sc.makeRDD(Array.tabulate(V.rows, sigma.length)
{ (i,j) => ((i + 1, j + 1), V.get(i,j)/sigma(j)) }.flatten)
{ (i,j) => ((i + 1, j + 1), V.get(i,j) / sigma(j)) }.flatten)

// Multiply A by VS^-1
val aCols = data.map(entry => (entry.j, (entry.i, entry.mval)))
Expand All @@ -178,10 +178,11 @@ object SVD {

def main(args: Array[String]) {
if (args.length < 8) {
println("Usage: SVD <master> <matrix_file> <m> <n>" +
println("Usage: SVD <master> <matrix_file> <m> <n> " +
"<k> <output_U_file> <output_S_file> <output_V_file>")
System.exit(1)
}

val (master, inputFile, m, n, k, output_u, output_s, output_v) =
(args(0), args(1), args(2).toInt, args(3).toInt,
args(4).toInt, args(5), args(6), args(7))
Expand Down
34 changes: 17 additions & 17 deletions mllib/src/test/scala/org/apache/spark/mllib/linalg/SVDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ class SVDSuite extends FunSuite with BeforeAndAfterAll {
// Return jblas matrix from sparse matrix RDD
def getDenseMatrix(matrix:RDD[MatrixEntry], m:Int, n:Int) : DoubleMatrix = {
val ret = DoubleMatrix.zeros(m, n)
matrix.toArray.map(x => ret.put(x.i-1, x.j-1, x.mval))
matrix.toArray.map(x => ret.put(x.i - 1, x.j - 1, x.mval))
ret
}

def assertMatrixEquals(a:DoubleMatrix, b:DoubleMatrix) {
assert(a.rows == b.rows && a.columns == b.columns, "dimension mismatch")
val diff = DoubleMatrix.zeros(a.rows, a.columns)
Array.tabulate(a.rows, a.columns){(i,j) =>
diff.put(i,j,
Math.min(Math.abs(a.get(i,j)-b.get(i,j)),
Math.abs(a.get(i,j)+b.get(i,j)))) }
Array.tabulate(a.rows, a.columns){(i, j) =>
diff.put(i, j,
Math.min(Math.abs(a.get(i, j) - b.get(i, j)),
Math.abs(a.get(i, j) + b.get(i, j)))) }
assert(diff.norm1 < EPSILON, "matrix mismatch: " + diff.norm1)
}

test("full rank matrix svd") {
val m = 10
val n = 3
val data = sc.makeRDD(Array.tabulate(m,n){ (a,b)=>
MatrixEntry(a+1,b+1, (a+2).toDouble*(b+1)/(1+a+b)) }.flatten )
val data = sc.makeRDD(Array.tabulate(m,n){ (a, b) =>
MatrixEntry(a + 1, b + 1, (a + 2).toDouble * (b + 1) / (1 + a + b)) }.flatten )

val decomposed = SVD.sparseSVD(data, m, n, n)
val u = decomposed.U
Expand All @@ -75,9 +75,9 @@ class SVDSuite extends FunSuite with BeforeAndAfterAll {
val densea = getDenseMatrix(data, m, n)
val svd = Singular.sparseSVD(densea)

val retu = getDenseMatrix(u,m,n)
val rets = getDenseMatrix(s,n,n)
val retv = getDenseMatrix(v,n,n)
val retu = getDenseMatrix(u, m, n)
val rets = getDenseMatrix(s, n, n)
val retv = getDenseMatrix(v, n, n)

// check individual decomposition
assertMatrixEquals(retu, svd(0))
Expand Down Expand Up @@ -106,9 +106,9 @@ class SVDSuite extends FunSuite with BeforeAndAfterAll {
val densea = getDenseMatrix(data, m, n)
val svd = Singular.sparseSVD(densea)

val retu = getDenseMatrix(u,m,retrank)
val rets = getDenseMatrix(s,retrank,retrank)
val retv = getDenseMatrix(v,n,retrank)
val retu = getDenseMatrix(u, m, retrank)
val rets = getDenseMatrix(s, retrank, retrank)
val retv = getDenseMatrix(v, n, retrank)

// check individual decomposition
assertMatrixEquals(retu, svd(0).getColumn(0))
Expand All @@ -123,7 +123,7 @@ class SVDSuite extends FunSuite with BeforeAndAfterAll {
val m = 10
val n = 3
val data = sc.makeRDD(Array.tabulate(m,n){ (a, b) =>
MatrixEntry(a + 1, b + 1, (a + 2).toDouble*(b + 1)/(1 + a + b)) }.flatten )
MatrixEntry(a + 1, b + 1, (a + 2).toDouble * (b + 1)/(1 + a + b)) }.flatten )

val k = 1 // only one svalue above this

Expand All @@ -136,9 +136,9 @@ class SVDSuite extends FunSuite with BeforeAndAfterAll {
val densea = getDenseMatrix(data, m, n)
val svd = Singular.sparseSVD(densea)

val retu = getDenseMatrix(u,m,retrank)
val rets = getDenseMatrix(s,retrank,retrank)
val retv = getDenseMatrix(v,n,retrank)
val retu = getDenseMatrix(u, m, retrank)
val rets = getDenseMatrix(s, retrank, retrank)
val retv = getDenseMatrix(v, n, retrank)

assert(retrank == 1, "rank returned not one")

Expand Down

0 comments on commit cdff9fc

Please sign in to comment.