Operations.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Matrix;
  3. use Matrix\Operators\Addition;
  4. use Matrix\Operators\DirectSum;
  5. use Matrix\Operators\Division;
  6. use Matrix\Operators\Multiplication;
  7. use Matrix\Operators\Subtraction;
  8. class Operations
  9. {
  10. public static function add(...$matrixValues): Matrix
  11. {
  12. if (count($matrixValues) < 2) {
  13. throw new Exception('Addition operation requires at least 2 arguments');
  14. }
  15. $matrix = array_shift($matrixValues);
  16. if (is_array($matrix)) {
  17. $matrix = new Matrix($matrix);
  18. }
  19. if (!$matrix instanceof Matrix) {
  20. throw new Exception('Addition arguments must be Matrix or array');
  21. }
  22. $result = new Addition($matrix);
  23. foreach ($matrixValues as $matrix) {
  24. $result->execute($matrix);
  25. }
  26. return $result->result();
  27. }
  28. public static function directsum(...$matrixValues): Matrix
  29. {
  30. if (count($matrixValues) < 2) {
  31. throw new Exception('DirectSum operation requires at least 2 arguments');
  32. }
  33. $matrix = array_shift($matrixValues);
  34. if (is_array($matrix)) {
  35. $matrix = new Matrix($matrix);
  36. }
  37. if (!$matrix instanceof Matrix) {
  38. throw new Exception('DirectSum arguments must be Matrix or array');
  39. }
  40. $result = new DirectSum($matrix);
  41. foreach ($matrixValues as $matrix) {
  42. $result->execute($matrix);
  43. }
  44. return $result->result();
  45. }
  46. public static function divideby(...$matrixValues): Matrix
  47. {
  48. if (count($matrixValues) < 2) {
  49. throw new Exception('Division operation requires at least 2 arguments');
  50. }
  51. $matrix = array_shift($matrixValues);
  52. if (is_array($matrix)) {
  53. $matrix = new Matrix($matrix);
  54. }
  55. if (!$matrix instanceof Matrix) {
  56. throw new Exception('Division arguments must be Matrix or array');
  57. }
  58. $result = new Division($matrix);
  59. foreach ($matrixValues as $matrix) {
  60. $result->execute($matrix);
  61. }
  62. return $result->result();
  63. }
  64. public static function divideinto(...$matrixValues): Matrix
  65. {
  66. if (count($matrixValues) < 2) {
  67. throw new Exception('Division operation requires at least 2 arguments');
  68. }
  69. $matrix = array_pop($matrixValues);
  70. $matrixValues = array_reverse($matrixValues);
  71. if (is_array($matrix)) {
  72. $matrix = new Matrix($matrix);
  73. }
  74. if (!$matrix instanceof Matrix) {
  75. throw new Exception('Division arguments must be Matrix or array');
  76. }
  77. $result = new Division($matrix);
  78. foreach ($matrixValues as $matrix) {
  79. $result->execute($matrix);
  80. }
  81. return $result->result();
  82. }
  83. public static function multiply(...$matrixValues): Matrix
  84. {
  85. if (count($matrixValues) < 2) {
  86. throw new Exception('Multiplication operation requires at least 2 arguments');
  87. }
  88. $matrix = array_shift($matrixValues);
  89. if (is_array($matrix)) {
  90. $matrix = new Matrix($matrix);
  91. }
  92. if (!$matrix instanceof Matrix) {
  93. throw new Exception('Multiplication arguments must be Matrix or array');
  94. }
  95. $result = new Multiplication($matrix);
  96. foreach ($matrixValues as $matrix) {
  97. $result->execute($matrix);
  98. }
  99. return $result->result();
  100. }
  101. public static function subtract(...$matrixValues): Matrix
  102. {
  103. if (count($matrixValues) < 2) {
  104. throw new Exception('Subtraction operation requires at least 2 arguments');
  105. }
  106. $matrix = array_shift($matrixValues);
  107. if (is_array($matrix)) {
  108. $matrix = new Matrix($matrix);
  109. }
  110. if (!$matrix instanceof Matrix) {
  111. throw new Exception('Subtraction arguments must be Matrix or array');
  112. }
  113. $result = new Subtraction($matrix);
  114. foreach ($matrixValues as $matrix) {
  115. $result->execute($matrix);
  116. }
  117. return $result->result();
  118. }
  119. }