/** * @return Matrix */ private function getTargetsMatrix() { if (is_array($this->targets[0])) { return new Matrix($this->targets); } return Matrix::fromFlatArray($this->targets); }
/** * @param Matrix $matrix * * @return Matrix * * @throws InvalidArgumentException */ public function multiply(Matrix $matrix) { if ($this->columns != $matrix->getRows()) { throw InvalidArgumentException::inconsistentMatrixSupplied(); } $product = []; $multiplier = $matrix->toArray(); for ($i = 0; $i < $this->rows; ++$i) { $columns = $matrix->getColumns(); for ($j = 0; $j < $columns; ++$j) { $product[$i][$j] = 0; for ($k = 0; $k < $this->columns; ++$k) { $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j]; } } } return new self($product, false); }
public function testCrossOutMatrix() { $matrix = new Matrix([[3, 4, 2], [4, 5, 5], [1, 1, 1]]); $crossOuted = [[3, 2], [1, 1]]; $this->assertEquals($crossOuted, $matrix->crossOut(1, 1)->toArray()); }