public function mult(Matrix $a = NULL, Matrix $b = NULL) { $newMatrix = []; if ($a->getMatrixSizeA() != $b->getMatrixSizeB()) { throw new IllegalArgumentException(); } for ($i = 0; $i < $a->getMatrixSizeA(); $i++) { for ($j = 0; $j < $b->getMatrixSizeB(); $j++) { for ($k = 0; $k < $a->getMatrixSizeB(); $k++) { $newMatrix[$i][$j] += $a->getMatrix()[$i][$k] * $b->getMatrix()[$k][$j]; } } } return new Matrix($newMatrix); }
public function determinant(Matrix $matrix) { $sum = 0; if ($matrix->getMatrixSizeA() == 1) { return $this->determinant1x1(); } else { if ($matrix->getMatrixSizeA() == 2) { return $this->determinant2x2(); } else { if ($matrix->getMatrixSizeA() == 3) { return $this->determinant3x3(); } } } for ($i = 0; $i < $matrix->getMatrixSizeA(); $i++) { $smaller = []; for ($a = 1; $a < $matrix->getMatrixSizeA(); $a++) { for ($b = 0; $b < $matrix->getMatrixSizeA(); $b++) { if ($b < $i) { $smaller[$a - 1][$b] = $matrix[$a][$b]; } else { if ($b > $i) { $smaller[$a - 1][$b - 1] = $matrix->getMatrix()[$a][$b]; } } } } $sum += $i % 2 == 0 ? 1 : -1 * $matrix->getMatrix()[0][$i] * $this->determinant(new Matrix($smaller)); } return $sum; }
private function mult2(Matrix $a = NULL, Matrix $b = NULL) { $n = $a->getMatrixSizeA(); $c = []; if ($n == 1) { $c[1][1] = $a->getMatrix()[1][1] * $b->getMatrix()[1][1]; } else { $c[1][1] = $this->mult($a->getMatrix()[1][1], $b->getMatrix()[1][1]) + $this->mult($a->getMatrix()[1][2], $b->getMatrix()[2][1]); $c[1][2] = $this->mult($a->getMatrix()[1][1], $b->getMatrix()[1][2]) + $this->mult($a->getMatrix()[1][2], $b->getMatrix()[2][2]); $c[2][1] = $this->mult($a->getMatrix()[2][1], $b->getMatrix()[1][1]) + $this->mult($a->getMatrix()[2][2], $b->getMatrix()[2][1]); $c[2][2] = $this->mult($a->getMatrix()[2][1], $b->getMatrix()[1][2]) + $this->mult($a->getMatrix()[2][2], $b->getMatrix()[2][2]); } return $c; }
public function isSameSize(Matrix $matrix = NULL) { if ($matrix == NULL) { throw new NullPointerException(); } if ($this->getMatrixSizeA() != $matrix->getMatrixSizeA()) { return FALSE; } foreach ($this->getMatrix() as $k => $v) { if (count($matrix->getMatrix()[$k]) != count($this->getMatrix()[$k])) { return FALSE; } } return TRUE; }
public function sub(Matrix $a = NULL, Matrix $b = NULL) { if ($a == NULL || $b == NULL) { throw new NullPointerException(); } $matrix = []; for ($i = 0; $i < $a->getMatrixSizeA(); $i++) { for ($j = 0; $j < $a->getMatrixSizeA(); $j++) { $matrix[$i][$j] = $a->getMatrix()[$i][$j] - $b->getMatrix()[$i][$j]; } } return new Matrix($matrix); }