divideinto.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/PHPMatrix)
  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 numbers to divide
  15. * @return Matrix
  16. * @throws Exception
  17. */
  18. if (!function_exists(__NAMESPACE__ . '\\divideinto')) {
  19. function divideinto(...$matrixValues): Matrix
  20. {
  21. if (count($matrixValues) < 2) {
  22. throw new Exception('Division operation requires at least 2 arguments');
  23. }
  24. $matrix = array_pop($matrixValues);
  25. $matrixValues = array_reverse($matrixValues);
  26. if (is_array($matrix)) {
  27. $matrix = new Matrix($matrix);
  28. }
  29. if (!$matrix instanceof Matrix) {
  30. throw new Exception('Division arguments must be Matrix or array');
  31. }
  32. $result = new Division($matrix);
  33. foreach ($matrixValues as $matrix) {
  34. $result->execute($matrix);
  35. }
  36. return $result->result();
  37. }
  38. }