subtract.php 986 B

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