/** * Multiplies two matrices * * Multiply current matrix with another matrix and returns the result * matrix. * * @param ezcGraphMatrix $matrix Second factor * @return ezcGraphMatrix Result matrix */ public function multiply(ezcGraphMatrix $matrix) { $mColumns = $matrix->columns(); // We want to ensure, that the matrix stays 3x3 if ($this->columns !== $matrix->rows() && $this->rows !== $mColumns) { throw new ezcGraphMatrixInvalidDimensionsException($this->columns, $this->rows, $mColumns, $matrix->rows()); } $result = parent::multiply($matrix); // The matrix dimensions stay the same, so that we can modify $this. for ($i = 0; $i < $this->rows; ++$i) { for ($j = 0; $j < $mColumns; ++$j) { $this->set($i, $j, $result->get($i, $j)); } } return $this; }
/** * Multiplies two matrices * * Multiply current matrix with another matrix and returns the result * matrix. * * @param ezcGraphMatrix $matrix Second factor * @return ezcGraphMatrix Result matrix */ public function multiply(ezcGraphMatrix $matrix) { $mColumns = $matrix->columns(); if ($this->columns !== ($mRows = $matrix->rows())) { throw new ezcGraphMatrixInvalidDimensionsException($this->columns, $this->rows, $mColumns, $mRows); } $result = new ezcGraphMatrix($this->rows, $mColumns); for ($i = 0; $i < $this->rows; ++$i) { for ($j = 0; $j < $mColumns; ++$j) { $sum = 0; for ($k = 0; $k < $mRows; ++$k) { $sum += $this->matrix[$i][$k] * $matrix->get($k, $j); } $result->set($i, $j, $sum); } } return $result; }