<commit_before>package cc.factorie.la

import cc.factorie._
import cc.factorie.la._
import scala.util.Random
import org.junit._
import org.junit.Assert._

class TestTensor {
  @Test def runTest {
    val dim = 20
    val ts = Seq(new DenseTensor1(dim), new SparseTensor1(dim))
    val r = new Random
    for (i <- 0 until 10) {
      val index = math.abs(r.nextInt) % dim
      val value = r.nextDouble
      println("index="+index+" value="+value)
      ts.foreach(_.+=(index, value))
    }
    //println(ts.head.toSeq)
    //println(ts.last.toSeq)
    assert(ts.head.toSeq == ts.last.toSeq)
  }

  trait TensorCreator {
    def create(i: Int): Tensor
  }


  @Test def testPlusEqual() {
    val creators : Seq[TensorCreator] = Seq(
      new TensorCreator { def create(i: Int) = new DenseTensor1(i) },
      new TensorCreator { def create(i: Int) = new SparseTensor1(i) },
      new TensorCreator { def create(i: Int) = new DenseTensor2(1, i) },
      new TensorCreator { def create(i: Int) = new DenseLayeredTensor2(1, i, new DenseTensor1(_)) },
      new TensorCreator { def create(i: Int) = new DenseLayeredTensor2(1, i, new SparseTensor1(_)) },
      new TensorCreator { def create(i: Int) = new DenseTensor3(1, 1, i) },
      new TensorCreator { def create(i: Int) = new Dense2LayeredTensor3(1, 1, i, new DenseTensor1(_)) },
      new TensorCreator { def create(i: Int) = new Dense2LayeredTensor3(1, 1, i, new SparseTensor1(_)) }
    // TODO: add all other tensor types above here
    )
    
    def fill(t: TensorCreator) = {
      val tensor = t.create(100)
      tensor(10) = 20
      tensor(1) = 2
      tensor(2) = -5
      tensor
    }

    for (c1 <- creators; c2 <- creators) {
      val t1 = fill(c1)
      val t2 = fill(c2)
      if ((!(t1.isInstanceOf[Tensor2] && t2.isInstanceOf[Tensor3]) &&
           !(t1.isInstanceOf[Tensor3] && t2.isInstanceOf[Tensor2]))) {

        println("testing " + t1.getClass.getName + " and " + t2.getClass.getName)
        assertEquals(20.0*20 + 2*2 + 5*5, t1 dot t2, 0.001)
        t1 += (t2, 0.1)
        assertEquals(t1(10), 22, 0.01)

        t1 *= 0.5
        assertEquals(11, t1(10), 0.001)

        try {
          assertEquals(22, (t1*2)(10), 0.001)
        } catch {
          case e: Error => assert(e.getMessage.contains("Method copy not defined"))
        }
      }
    }
  }
}<commit_msg>Add tensor test to repro the SparseIndexedTensor zero() bug<commit_after>package cc.factorie.la

import cc.factorie._
import cc.factorie.la._
import scala.util.Random
import org.junit._
import org.junit.Assert._

class TestTensor {
  @Test def runTest {
    val dim = 20
    val ts = Seq(new DenseTensor1(dim), new SparseTensor1(dim))
    val r = new Random
    for (i <- 0 until 10) {
      val index = math.abs(r.nextInt) % dim
      val value = r.nextDouble
      println("index="+index+" value="+value)
      ts.foreach(_.+=(index, value))
    }
    //println(ts.head.toSeq)
    //println(ts.last.toSeq)
    assert(ts.head.toSeq == ts.last.toSeq)
  }

  trait TensorCreator {
    def create(i: Int): Tensor
  }

  def creators: Seq[TensorCreator] = Seq(
    new TensorCreator { def create(i: Int) = new DenseTensor1(i) },
    new TensorCreator { def create(i: Int) = new SparseTensor1(i) },
    new TensorCreator { def create(i: Int) = new SparseIndexedTensor2(i, 1) },
    new TensorCreator { def create(i: Int) = new DenseTensor2(1, i) },
    new TensorCreator { def create(i: Int) = new DenseLayeredTensor2(1, i, new DenseTensor1(_)) },
    new TensorCreator { def create(i: Int) = new DenseLayeredTensor2(1, i, new SparseTensor1(_)) },
    new TensorCreator { def create(i: Int) = new DenseTensor3(1, 1, i) },
    new TensorCreator { def create(i: Int) = new Dense2LayeredTensor3(1, 1, i, new DenseTensor1(_)) },
    new TensorCreator { def create(i: Int) = new Dense2LayeredTensor3(1, 1, i, new SparseTensor1(_)) }
  // TODO: add all other tensor types above here
  )

  // TODO This test is commented out because it fails right now at SparseTensor1 - need to find the bug -luke
  /*@Test*/ def testZero() {
    def fill(t: TensorCreator) = {
      val tensor = t.create(100)
      tensor(10) = 20
      tensor(1) = 2
      tensor(2) = -5
      tensor
    }
    testPairwise(fill) { (t1, t2) =>
      t1 += t2
      t1.zero()
      assert(t1.forall(0.0 ==), "Failed zero check at %s, %s" format (t1.getClass, t2.getClass))
      val t3 = t1.blankCopy
      t3 += t1
      t3 += t2
      assert((0 until t2.size).forall(i => t2(i) == t3(i)), "Failed += after zero() at %s, %s" format (t1.getClass, t2.getClass))
    }
  }

  def testPairwise(fill: TensorCreator => Tensor)(test: (Tensor, Tensor) => Unit): Unit = {
    for (c1 <- creators; c2 <- creators) {
      val t1 = fill(c1)
      val t2 = fill(c2)
      if ((!(t1.isInstanceOf[Tensor2] && t2.isInstanceOf[Tensor3]) &&
           !(t1.isInstanceOf[Tensor3] && t2.isInstanceOf[Tensor2])))
        test(t1, t2)
    }
  }

  @Test def testPlusEqual() {

    def fill(t: TensorCreator) = {
      val tensor = t.create(100)
      tensor(10) = 20
      tensor(1) = 2
      tensor(2) = -5
      tensor
    }

    testPairwise(fill) { (t1, t2) =>

      println("testing " + t1.getClass.getName + " and " + t2.getClass.getName)
      assertEquals(20.0*20 + 2*2 + 5*5, t1 dot t2, 0.001)
      t1 += (t2, 0.1)
      assertEquals(t1(10), 22, 0.01)

      t1 *= 0.5
      assertEquals(11, t1(10), 0.001)

      try {
        assertEquals(22, (t1*2)(10), 0.001)
      } catch {
        case e: Error => assert(e.getMessage.contains("Method copy not defined"))
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.14546757434611252, 'fraction_numerical': 0.04514510927982802, 'mean_word_length': 2.639504563233377, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1361869', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2033, 'n_words': 515}