/** * Returns the sum of this dense matrix and the given dense matrix. * * @param object DenseMatrix $mat A dense matrix. * @return object DenseMatrix The sum. */ public function plus(IMatrix $mat) { if (!$mat instanceof self || $this->getNumRows() != $mat->getNumRows() || $this->getNumCols() != $mat->getNumCols()) { throw new ArgumentError(); } $result = new DenseMatrix($this->getNumRows(), $this->getNumCols()); for ($i = 0; $i < $this->getNumRows(); ++$i) { for ($j = 0; $j < $this->getNumCols(); ++$j) { $result[array($i, $j)] = $this[array($i, $j)] + $mat[array($i, $j)]; } } return $result; }
/** * Matrix test program. * * @param object IMatrix $mat The matrix to test. */ public static function test(IMatrix $mat) { printf("Matrix test program.\n"); try { $k = 0; for ($i = 0; $i < $mat->getNumRows(); ++$i) { for ($j = 0; $j < $mat->getNumCols(); ++$j) { $mat[array($i, $j)] = $k++; } } printf("%s\n", str($mat)); $mat = $mat->plus($mat); printf("%s\n", str($mat)); } catch (Exception $e) { printf("Caught %s\n", $e->getMessage()); } }