コード例 #1
0
ファイル: ArithmeticTest.php プロジェクト: scrtree/jenkins
 /**
  * 割り算の検証。10/5=2となることを検証する
  */
 public function testDivision()
 {
     $this->assertEquals(2, $this->arithmetic->division(10, 5));
 }
コード例 #2
0
ファイル: Polynomial.php プロジェクト: markrogoyski/math-php
 /**
  * When a polynomial is being evaluated at a point x₀, build a callback
  * function and return the value of the callback function at x₀
  * Example: $polynomial = new Polynomial([1, -8, 12, 3]);
  *          echo $polynomial(4);
  *          // prints -13
  *
  * @param number $x₀ The value at which we are evaluting our polynomial
  *
  * @return number The result of our polynomial evaluated at $x₀
  */
 public function __invoke($x₀) : float
 {
     // Set object parameters as local variables so they can be used with the use function
     $degree = $this->degree;
     $coefficients = $this->coefficients;
     // Start with the zero polynomial
     $polynomial = function ($x) {
         return 0;
     };
     // Iterate over each coefficient to create a callback function for each term
     for ($i = 0; $i < $degree + 1; $i++) {
         // Create a callback function for the current term
         $term = function ($x) use($degree, $coefficients, $i) {
             return $coefficients[$i] * $x ** ($degree - $i);
         };
         // Add the new term to the polynomial
         $polynomial = Arithmetic::add($polynomial, $term);
     }
     return $polynomial($x₀);
 }
コード例 #3
0
 public function testNestedArithmetic()
 {
     // f(x) = x - 9
     $f = function ($x) {
         return $x - 9;
     };
     // g(x) = x + 2
     $g = function ($x) {
         return $x + 2;
     };
     // h(x) = x
     $h = function ($x) {
         return $x;
     };
     // Π(x) = $f(x) * ( g(x) + h(x) ) = (x - 9) * (2x + 2) = 2x² - 16x - 18
     $product = Arithmetic::multiply($f, Arithmetic::add($g, $h));
     // Π(0) = -18
     $expected = -18;
     $x = $product(0);
     $this->assertEquals($expected, $x);
     // Π(5) = -48
     $expected = -48;
     $x = $product(5);
     $this->assertEquals($expected, $x);
     // Π(-5) = 112
     $expected = 112;
     $x = $product(-5);
     $this->assertEquals($expected, $x);
 }