You are here

function RecommenderMatrixTestCase::testMatrix in Recommender API 6.2

File

./recommender.test, line 65

Class

RecommenderMatrixTestCase

Code

function testMatrix() {
  $mat1 = Matrix::create('RealMatrix', 3, 5, 0);
  $mat1
    ->set(0, 1, 2);
  $mat1
    ->set(0, 3, 3);
  $mat1
    ->set(1, 0, 1);
  $mat1
    ->set(1, 2, 4);
  $mat1
    ->set(1, 3, 1);
  $mat1
    ->set(2, 0, 2);
  $mat1
    ->set(2, 2, 5);
  $mat1
    ->set(2, 4, 1);
  $raw_values = $mat1
    ->raw_values();
  $this
    ->assertTrue($raw_values === $mat1
    ->raw_values());
  $this
    ->assertEqual(4, $raw_values[1][2]);
  $this
    ->assertEqual(0, $raw_values[2][3]);
  $mat2 = Matrix::correlation($mat1);
  $this
    ->assertEqualX(1, $mat2
    ->get(1, 1));
  $this
    ->assertEqualX(1, $mat2
    ->get(0, 0));
  $this
    ->assertEqualX(-0.3227, $mat2
    ->get(0, 1));
  $this
    ->assertEqualX(-0.6820000000000001, $mat2
    ->get(2, 0));
  $this
    ->assertEqualX(0.9098000000000001, $mat2
    ->get(2, 1));
  $this
    ->assertTrue(is_nan($mat2
    ->get(3, 1)));
  $mat3 = Matrix::create('SparseMatrix', 3, 5, 0);
  $mat3
    ->set(0, 1, 2);
  $mat3
    ->set(0, 3, 3);
  $mat3
    ->set(1, 0, 1);
  $mat3
    ->set(1, 2, 4);
  $mat3
    ->set(1, 3, 1);
  $mat3
    ->set(2, 0, 2);
  $mat3
    ->set(2, 2, 5);
  $mat3
    ->set(2, 4, 1);
  $mat4 = Matrix::correlation($mat3);
  $this
    ->assertEqualX(1, $mat4
    ->get(0, 0));
  $this
    ->assertTrue(is_nan($mat4
    ->get(0, 1)));
  $this
    ->assertTrue(is_nan($mat4
    ->get(0, 2)));
  $this
    ->assertTrue(is_nan($mat4
    ->get(0, 1)));
  $this
    ->assertEqualX(1, $mat4
    ->get(1, 2));

  // note: comprehensive matrix test will be at testCorrelationRecommenderMemory();
}