MathPHP\Functions\ArithmeticTest::testNestedArithmetic PHP Метод

testNestedArithmetic() публичный Метод

    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);
    }