ArithmeticTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace MathPHP\Tests\Functions;
  3. use MathPHP\Functions\Arithmetic;
  4. class ArithmeticTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @test sum
  8. * @dataProvider dataProviderForSum
  9. * @param int $x
  10. * @param int $expectedSum
  11. */
  12. public function testSum(int $x, int $expectedSum)
  13. {
  14. // Given
  15. // f(x) = x⁴ + 8x³ -13x² -92x + 96
  16. $f = function ($x) {
  17. return $x ** 4 + 8 * $x ** 3 - 13 * $x ** 2 - 92 * $x + 96;
  18. };
  19. // g(x) = x³ - 12x² + 72x + 27
  20. $g = function ($x) {
  21. return $x ** 3 - 12 * $x ** 2 + 72 * $x + 27;
  22. };
  23. // Σ(x) = f(x) + g(x) = x⁴ + 9x³ -25x² -20x + 123
  24. $adder = Arithmetic::add($f, $g);
  25. // When
  26. $sum = $adder($x);
  27. // Then
  28. $this->assertEquals($expectedSum, $sum);
  29. }
  30. /**
  31. * @return array
  32. */
  33. public function dataProviderForSum(): array
  34. {
  35. return [
  36. 'Σ(0) = 123' => [0, 123],
  37. 'Σ(5) = 1148' => [5, 1148],
  38. 'Σ(-5) = -902' => [-5, -902],
  39. 'Σ(100) = 108748123' => [100, 108748123],
  40. 'Σ(-100) = 90752123' => [-100, 90752123],
  41. ];
  42. }
  43. /**
  44. * @test multiply
  45. * @dataProvider dataProviderForMultiply
  46. * @param int $x
  47. * @param int $expectedProduct
  48. */
  49. public function testMultiply(int $x, int $expectedProduct)
  50. {
  51. // f(x) = x² + 8x - 12
  52. $f = function ($x) {
  53. return $x ** 2 + 8 * $x - 12;
  54. };
  55. // g(x) = x - 9
  56. $g = function ($x) {
  57. return $x - 9;
  58. };
  59. // Π(x) = f(x) * g(x) = x³ - x² -84x + 108
  60. $multiplier = Arithmetic::multiply($f, $g);
  61. // When
  62. $product = $multiplier($x);
  63. // Then
  64. $this->assertEquals($expectedProduct, $product);
  65. }
  66. /**
  67. * @return array
  68. */
  69. public function dataProviderForMultiply(): array
  70. {
  71. return [
  72. 'Π(0) = 108' => [0, 108],
  73. 'Π(5) = -212' => [5, -212],
  74. 'Π(-5) = 378' => [-5, 378],
  75. 'Π(100) = 981708' => [100, 981708],
  76. 'Π(-100) = -1001492' => [-100, -1001492],
  77. ];
  78. }
  79. /**
  80. * @test Multiple sums
  81. * @dataProvider dataProviderForMultipleSums
  82. * @param int $x
  83. * @param int $expectedSum
  84. */
  85. public function testMultipleSums(int $x, int $expectedSum)
  86. {
  87. // Given
  88. // f(x) = 8x³ - 13x² -92x + 96
  89. $f = function ($x) {
  90. return 8 * $x ** 3 - 13 * $x ** 2 - 92 * $x + 96;
  91. };
  92. // Σ(x) = f(x) + f(x) + f(x) + f(x) + f(x) = 5*f(x) = 40x³ - 65x² -460x + 480
  93. $adder = Arithmetic::add($f, $f, $f, $f, $f);
  94. // When
  95. $sum = $adder($x);
  96. // Then
  97. $this->assertEquals($expectedSum, $sum);
  98. }
  99. /**
  100. * @return array
  101. */
  102. public function dataProviderForMultipleSums(): array
  103. {
  104. return [
  105. 'Σ(0) = 480' => [0, 480],
  106. 'Σ(5) = 1555' => [5, 1555],
  107. 'Σ(-5) = -3845' => [-5, -3845],
  108. ];
  109. }
  110. /**
  111. * @test Multiple products
  112. * @dataProvider dataProviderForMultipleProducts
  113. * @param int $x
  114. * @param int $expectedSum
  115. */
  116. public function testMultipleProducts(int $x, int $expectedSum)
  117. {
  118. // f(x) = x - 9
  119. $f = function ($x) {
  120. return $x - 9;
  121. };
  122. // g(x) = x + 2
  123. $g = function ($x) {
  124. return $x + 2;
  125. };
  126. // h(x) = x
  127. $h = function ($x) {
  128. return $x;
  129. };
  130. // Π(x) = f(x) * g(x) * h(x) = x³ - 7x² -18x
  131. $multiplier = Arithmetic::multiply($f, $g, $h);
  132. // When
  133. $product = $multiplier($x);
  134. // Then
  135. $this->assertEquals($expectedSum, $product);
  136. }
  137. /**
  138. * @return array
  139. */
  140. public function dataProviderForMultipleProducts(): array
  141. {
  142. return [
  143. 'Π(0) = 0' => [0, 0],
  144. 'Π(5) = -140' => [5, -140],
  145. 'Π(-5) = -210' => [-5, -210],
  146. ];
  147. }
  148. /**
  149. * @test Nested arithmetic
  150. * @dataProvider dataProviderForNestedArithmetic
  151. * @param int $x
  152. * @param int $expected
  153. */
  154. public function testNestedArithmetic(int $x, int $expected)
  155. {
  156. // f(x) = x - 9
  157. $f = function ($x) {
  158. return $x - 9;
  159. };
  160. // g(x) = x + 2
  161. $g = function ($x) {
  162. return $x + 2;
  163. };
  164. // h(x) = x
  165. $h = function ($x) {
  166. return $x;
  167. };
  168. // Π(x) = $f(x) * ( g(x) + h(x) ) = (x - 9) * (2x + 2) = 2x² - 16x - 18
  169. $multiplier = Arithmetic::multiply($f, Arithmetic::add($g, $h));
  170. // When
  171. $product = $multiplier($x);
  172. // Then
  173. $this->assertEquals($expected, $product);
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function dataProviderForNestedArithmetic(): array
  179. {
  180. return [
  181. 'Π(0) = -18' => [0, -18],
  182. 'Π(5) = -48' => [5, -48],
  183. 'Π(-5) = 112' => [-5, 112],
  184. ];
  185. }
  186. }