Skip to content

Commit

Permalink
added test for identity matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
schleichardt committed Apr 12, 2012
1 parent 811053b commit 3ba00bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/scala/info/schleichardt/math/ValueMatrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import collection.immutable.IndexedSeq

object ValueMatrix {
def apply(input: Seq[Double]*) = new ValueMatrix(input)
def identityMatrixForSize(size: Int) = {
val seq: Seq[Seq[Double]] =
for (line <- 0 until size) yield {
for (column <- 0 until size) yield {
if (line == column)
1.0
else
0.0
}
}
new ValueMatrix(seq)
}
}

class ValueMatrix(val content: Seq[Seq[Double]]) {
Expand Down Expand Up @@ -79,4 +91,6 @@ class ValueMatrix(val content: Seq[Seq[Double]]) {
}
new ValueMatrix(seq)
}

lazy val isIdentityMatrix: Boolean = columnCount == content.length && ValueMatrix.identityMatrixForSize(columnCount) == this
}
7 changes: 6 additions & 1 deletion src/test/scala/MatrixSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class MatrixSpec extends Specification with JUnit with ScalaTest {
matrixWith3Rows * matrixWith2Rows must throwA[IllegalArgumentException]
matrixWith2Rows * matrixWith3Rows must not(throwA[IllegalArgumentException])
}

"can be a identity matrix" in {
val identityMatrix = ValueMatrix(Seq(1, 0, 0), Seq(0, 1, 0), Seq(0, 0, 1))
val notIdentityMatrix = ValueMatrix(Seq(1, 0, 0), Seq(0, 1, 0), Seq(0, 1, 0))
identityMatrix.isIdentityMatrix must be_==(true)
notIdentityMatrix.isIdentityMatrix must be_==(false)
}
}
}

0 comments on commit 3ba00bb

Please sign in to comment.