Last active
October 12, 2015 16:31
-
-
Save kmader/38d92fc369177dcff4a9 to your computer and use it in GitHub Desktop.
Breeze Dot Product Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fourquant | |
import breeze.linalg._ | |
import org.scalatest.{FunSuite, Matchers} | |
import scala.util.Random | |
/** | |
* Created by mader on 10/12/15. | |
*/ | |
class BreezeTests extends FunSuite with Matchers { | |
val arrsize: Int = 40000 | |
val sparse: Boolean = true | |
for (i <- 0 to 10) { | |
val weight: DenseVector[Double] = DenseVector.rand(arrsize) | |
val x_i: DenseVector[Double] = DenseVector.rand(arrsize) | |
val std_indices = (0 until arrsize).toIndexedSeq | |
val sort_indices: IndexedSeq[Int] = Random.shuffle(std_indices) | |
test(s"Dense Dot Permutation $i") { | |
val stddot = weight(std_indices).dot(x_i(std_indices)) | |
val sortdot = weight(sort_indices).dot(x_i(sort_indices)) | |
stddot shouldBe sortdot+-1e-10 | |
} | |
test(s"Sparse Dot Permutation $i") { | |
val sparseWeight: SparseVector[Double] = SparseVector.zeros[Double](arrsize) | |
val sparseX_i: SparseVector[Double] = SparseVector.zeros[Double](arrsize) | |
for (j <- 0 until arrsize) { sparseX_i(j) = x_i(j); sparseWeight(j) = weight(j) } | |
val stddot = (sparseWeight(std_indices).dot(sparseX_i(std_indices))) | |
val sortdot = sparseWeight(sort_indices).dot(sparseX_i(sort_indices)) | |
stddot shouldBe sortdot+-1e-10 | |
} | |
test(s"Crappy Dot $i") { | |
var stddot = 0.0 | |
for(i<-std_indices) stddot += weight(i)*x_i(i) | |
var sortdot = 0.0 | |
for(i<-sort_indices) sortdot += weight(i)*x_i(i) | |
stddot shouldBe sortdot+-1e-10 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment