Permalink
Browse files

added test for identity matrix

  • Loading branch information...
1 parent 811053b commit 3ba00bb6c76e2dc616f061e0504dbbc65c50e8ee @schleichardt committed Apr 12, 2012
Showing with 20 additions and 1 deletion.
  1. +14 −0 src/main/scala/info/schleichardt/math/ValueMatrix.scala
  2. +6 −1 src/test/scala/MatrixSpec.scala
@@ -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]]) {
@@ -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
}
@@ -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.