directsum.php 880 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. *
  4. * Function code for the matrix direct sum 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\DirectSum;
  11. /**
  12. * Adds two or more matrices
  13. *
  14. * @param mixed[] $matrixValues The matrices to add
  15. * @return Matrix
  16. * @throws Exception
  17. */
  18. function directsum(...$matrixValues)
  19. {
  20. if (count($matrixValues) < 2) {
  21. throw new Exception('This operation requires at least 2 arguments');
  22. }
  23. $matrix = array_shift($matrixValues);
  24. if (!is_object($matrix) || !($matrix instanceof Matrix)) {
  25. $matrix = new Matrix($matrix);
  26. }
  27. $result = new DirectSum($matrix);
  28. foreach ($matrixValues as $matrix) {
  29. $result->execute($matrix);
  30. }
  31. return $result->result();
  32. }