| 1 |
<?php |
| 2 |
|
| 3 |
namespace Matrix\Operators; |
| 4 |
|
| 5 |
use \Matrix\Matrix; |
| 6 |
use \Matrix\Functions; |
| 7 |
use Matrix\Exception; |
| 8 |
|
| 9 |
class Division extends Multiplication |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Execute the division |
| 13 |
* |
| 14 |
* @param mixed $value The matrix or numeric value to divide the current base value by |
| 15 |
* @throws Exception If the provided argument is not appropriate for the operation |
| 16 |
* @return $this The operation object, allowing multiple divisions to be chained |
| 17 |
**/ |
| 18 |
public function execute($value) |
| 19 |
{ |
| 20 |
if (is_array($value)) { |
| 21 |
$value = new Matrix($value); |
| 22 |
} |
| 23 |
|
| 24 |
if (is_object($value) && ($value instanceof Matrix)) { |
| 25 |
try { |
| 26 |
$value = Functions::inverse($value); |
| 27 |
} catch (Exception $e) { |
| 28 |
throw new Exception('Division can only be calculated using a matrix with a non-zero determinant'); |
| 29 |
} |
| 30 |
|
| 31 |
return $this->multiplyMatrix($value); |
| 32 |
} elseif (is_numeric($value)) { |
| 33 |
return $this->multiplyScalar(1 / $value); |
| 34 |
} |
| 35 |
|
| 36 |
throw new Exception('Invalid argument for division'); |
| 37 |
} |
| 38 |
} |
| 39 |
|