public function determinant() { if ($this->u_size !== $this->u_stride) { return null; } switch ($this->u_size) { case 0: return 0.0; case 1: return $this->u_data[0]; case 2: return $this->u_data[0] * $this->u_data[3] - $this->u_data[1] * $this->u_data[2]; default: $result = 0.0; $minor = new Matrix($this->u_size - 1, $this->u_stride - 1); for ($n = 0; $n < $this->u_size; $n++) { $a = ($n + 1) % 2 == 0 ? -1 : 1; for ($i = 1; $i < $this->u_size; $i++) { $offset = $i * $this->u_stride; $minorOffset = ($i - 1) * $this->u_stride; for ($j = 0; $j < $n; $j++) { $minor->u_data[$minorOffset + $j] = $this->u_data[$offset + $j]; } for ($j = $n + 1; $j < $this->u_stride; $j++) { $minor->u_data[$minorOffset + $j - 1] = $this->u_data[$offset + $j]; } } $result += $this->u_data[$n] * $minor->determinant(); } return $result; } }
public function testDeterminantException() { $matrix = new Matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]); try { $matrix->determinant(); } catch (MatrixException $exception) { return; } $this->fail('MatrixException not raised.'); }
/** * @expectedException \RangeException */ public function testDeterminantException() { $arr1 = [[1, 2], [3, 4], [5, 6]]; $mat1 = new Matrix($arr1); $mat1->determinant(); }