divideby.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. *
  4. * Function code for the matrix division operation
  5. *
  6. * @copyright Copyright (c) 2013-2018 Mark Baker (https://github.com/MarkBaker/PHPComplex)
  7. * @license https://opensource.org/licenses/MIT MIT
  8. */
  9. namespace Matrix;
  10. use Matrix\Operators\Division;
  11. /**
  12. * Divides two or more matrix numbers
  13. *
  14. * @param array<int, mixed> $matrixValues The matrices to divide
  15. * @return Matrix
  16. * @throws Exception
  17. */
  18. if (!function_exists(__NAMESPACE__ . '\\divideby')) {
  19. function divideby(...$matrixValues): Matrix
  20. {
  21. if (count($matrixValues) < 2) {
  22. throw new Exception('Division operation requires at least 2 arguments');
  23. }
  24. $matrix = array_shift($matrixValues);
  25. if (is_array($matrix)) {
  26. $matrix = new Matrix($matrix);
  27. }
  28. if (!$matrix instanceof Matrix) {
  29. throw new Exception('Division arguments must be Matrix or array');
  30. }
  31. $result = new Division($matrix);
  32. foreach ($matrixValues as $matrix) {
  33. $result->execute($matrix);
  34. }
  35. return $result->result();
  36. }
  37. }