You are here

abstract class Matrix in Recommender API 7.6

Same name and namespace in other branches
  1. 6.2 Matrix.php \Matrix

This PHP file does not require Drupal. Including both Matrix and Vector implementation. Missing data are treated as NAN. Some extra complexity comes from trying to increase memory/cpu performance Note, this implementation does check input parameters. Please make sure to pass in the correct parameters. The strange OO paradigm was due to legacy OO support for PHP4.

Hierarchy

Expanded class hierarchy of Matrix

File

classes/Matrix.php, line 11

View source
abstract class Matrix {

  // the matrix values
  protected $values = NULL;
  protected $row = NULL;
  protected $column = NULL;

  // has to use the factory method.
  protected function __construct() {
  }

  // the factory method
  static function create($type, $row, $column, $value = 0) {
    if ($type == 'SparseMatrix') {
      $matrix = new SparseMatrix();
      $matrix->values = array();
    }
    elseif ($type == 'RealMatrix') {
      $matrix = new RealMatrix();
      $matrix->values = array_fill(0, $row, array_fill(0, $column, $value));

      // #794284, need to assert $row and $column are greater than 0
    }
    else {
      trigger_error('Matrix type not recognized', E_USER_ERROR);
    }

    // $row, $column are the expected dimensions.
    // for SparseMatrix, $row[x]->count() might return 0, but the expected column number is still $column
    $matrix->row = $row;
    $matrix->column = $column;
    return $matrix;
  }

  // Note: wrap is temporarily disabled unless we find a case to use it.
  // this can make sure the array created by self::created() is always in the correct format.

  //static function wrap($type, &$values) {}

  // Note, PHP will not throw OutOfIndexException. so please make sure passing the correct value.
  function set($row, $column, $value) {
    $this->values[$row][$column] = $value;

    // we assume no OutOfIndexException.
  }

  // if not set, return NAN
  function get($row, $column) {
    return isset($this->values[$row][$column]) ? $this->values[$row][$column] : NAN;
  }
  function &raw_values() {
    return $this->values;
  }

  /**
   * Only return the row vectors that have at least one element.
   * Work for both RealMatrix and SparseMatrix
   * @return unknown_type
   */
  function row_vectors() {

    // $this could be either RealMatrix or SparseMatrix
    $type = $this instanceof RealMatrix ? 'RealVector' : 'SparseVector';
    $vectors = array();
    foreach ($this->values as $row_i => &$row_value) {
      $vectors[$row_i] = Vector::wrap($type, $row_value);

      // note, by default this is passing by reference.
    }
    return $vectors;

    // don't have to return by reference.
    // Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.
  }

  /**
   * Compute the covariance matrix for the row vectors.
   * @param $matrix Could be RealMatrix or SparseMatrix.
   * @return Matrix a new m by m covariance matrix.
   *         don't have to return by ref, because the engine will take care of it.
   *         Note that no matter what's the input matrix, the returned matrix is always a sparse matrix.
   */
  static function correlation($matrix) {
    $vectors = $matrix
      ->row_vectors();
    $m = $matrix->row;

    // dimension of the correlation matrix
    $cor_matrix = Matrix::create('SparseMatrix', $m, $m);
    for ($v1 = 0; $v1 < $m; $v1++) {
      for ($v2 = $v1; $v2 < $m; $v2++) {
        if (isset($vectors[$v1]) && isset($vectors[$v2])) {

          // note, some value (such as std) is cached, so it won't be too much performance problem.
          $cor = $vectors[$v1]
            ->correlation($vectors[$v2]);
          if (!is_nan($cor)) {
            $cor_matrix
              ->set($v1, $v2, $cor);
            $cor_matrix
              ->set($v2, $v1, $cor);
          }
        }
      }
    }
    return $cor_matrix;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Matrix::$column protected property
Matrix::$row protected property
Matrix::$values protected property
Matrix::correlation static function Compute the covariance matrix for the row vectors.
Matrix::create static function
Matrix::get function
Matrix::raw_values function
Matrix::row_vectors function Only return the row vectors that have at least one element. Work for both RealMatrix and SparseMatrix
Matrix::set function
Matrix::__construct protected function