sebiniemann / ArmadilloJava

Pure Java-based linear algebra library
armadillojava.org
8 stars 8 forks source link

Test cases for scalar/vector-valued functions of vectors/matrices #56

Closed sebiniemann closed 10 years ago

sebiniemann commented 11 years ago

The project is currently missing test cases for scalar/vector-valued functions of vectors/matrices, which is split in statistic and logic operations.

Both cases seem to be well parameterisable but clearly with some differences. A blank test class can already be found at test/Arma/arma/TestScalarVectorValuedFunctionsOfVectorsMatrices.java, which should be split accordingly into to separate test classes, one for statistic and one for logic operations.

My suggestion would be to split it into test/Arma/arma/TestScalarVectorValuedFunctionsOfVectorsMatricesStatistic.java and test/Arma/arma/TestScalarVectorValuedFunctionsOfVectorsMatricesLogic.java.

Tasks:

For the test data, I would like to check the following values:

Tasks continued: The expected results of these tests should be pre-calculated and saved to compare always with the same pre-calculated values. Because of the different results for vectors and matrices and the additional parameters, a bunch of different cases need to be precalculated. In the vector case, the matrix should be computed per column.

An example for the generation of pre-calculated expected results with native java functions:

public class TestExpectedValues {

  public static void main(String[] args) throws FileNotFoundException {
    testScalarVectorValuedFunctionsOfVectorsMatricesStatistic();
  }

  protected static void testScalarVectorValuedFunctionsOfVectorsMatricesStatistic() throws FileNotFoundException {
    Mat testData = (Mat) TestScalarVectorValuedFunctionsOfVectorsMatricesStatistic.getTestData().iterator().next()[0];
    Mat expected =  new Mat(testData.n_rows, 1);

    // Arma.min(...)
    // Arma.minMat(..., 0)
    for(int j = 0; j < testData.n_cols; j++) {
      Mat column = testData.col(j);

      double minimum = column.at(0);
      for(int n = 1; n < column.n_elem; n++) {
        minimum = Math.min(minimum, column.at(n));
      }

      expected.at(j, Op.EQUAL, minimum);
    }
    expected.save("test/data/Arma/TestScalarVectorValuedFunctionsOfVectorsMatrices/Statistic.min.d0.mat");

    // Arma.minMat(..., 1)
    for(int i = 0; i < testData.n_cols; i++) {
      Mat column = testData.row(i);

      double minimum = column.at(0);
      for(int n = 1; n < column.n_elem; n++) {
        minimum = Math.min(minimum, column.at(n));
      }

      expected.at(i, Op.EQUAL, minimum);
    }
    expected.save("test/data/Arma/TestScalarVectorValuedFunctionsOfVectorsMatrices/Statistic.min.d1.mat");

    /* generation of other expected results for the same parameterised test class */
  }

  /* generation of other expected results for another parameterised test class */
}

The example code given above should be inserted in test/util/arma/TestExpectedValues.java.

Tasks continued:

Continued example of the actual test case:

@Test
public void testMin() {
  Mat expected = new Mat();
  expected.load("test/data/Arma/TestScalarVectorValuedFunctionsOfVectorsMatrices/Statistic.min.d0.mat");

  for(int j = 0; j < _testData.n_cols; j++) {
    assertEquals(expected.at(j), Arma.min(_testData.col(j)), TestUtil.NUMERIC_TOLERANCE);
  }
}

@Test
public void testMinMat() {
  Mat expected = new Mat();
  expected.load("test/data/Arma/TestScalarVectorValuedFunctionsOfVectorsMatrices/Statistic.min.d0.mat");

  assertMatEquals(expected, Arma.minMat(_testData),  TestUtil.NUMERIC_TOLERANCE);
  assertMatEquals(expected, Arma.minMat(_testData, 0),  TestUtil.NUMERIC_TOLERANCE);

  expected.load("test/data/Arma/TestScalarVectorValuedFunctionsOfVectorsMatrices/Statistic.min.d1.mat");

  assertMatEquals(expected, Arma.minMat(_testData, 1),  TestUtil.NUMERIC_TOLERANCE);
}

Tasks continued: