Division.php 1005 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Matrix\Operators;
  3. use Matrix\Div0Exception;
  4. use Matrix\Exception;
  5. use \Matrix\Matrix;
  6. use \Matrix\Functions;
  7. class Division extends Multiplication
  8. {
  9. /**
  10. * Execute the division
  11. *
  12. * @param mixed $value The matrix or numeric value to divide the current base value by
  13. * @throws Exception If the provided argument is not appropriate for the operation
  14. * @return $this The operation object, allowing multiple divisions to be chained
  15. **/
  16. public function execute($value, string $type = 'division'): Operator
  17. {
  18. if (is_array($value)) {
  19. $value = new Matrix($value);
  20. }
  21. if (is_object($value) && ($value instanceof Matrix)) {
  22. $value = Functions::inverse($value, $type);
  23. return $this->multiplyMatrix($value, $type);
  24. } elseif (is_numeric($value)) {
  25. return $this->multiplyScalar(1 / $value, $type);
  26. }
  27. throw new Exception('Invalid argument for division');
  28. }
  29. }