PHP code example of jstewmc / piecewise-fx

1. Go to this page and download the library: Download jstewmc/piecewise-fx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jstewmc / piecewise-fx example snippets


use Jstewmc\Fx\{Constant, Linear};
use Jstewmc\Interval\Interval;
use Jstewmc\PiecewiseFx;

// define our sub-functions...
//     y = 1     | 1 <= x <= 3
//     y = x - 2 | 3 < x <= 6
//     y = 4     | 6 < x <= 9
//
$subFxs = [
    new SubFx(
        new Interval('[1, 3]'),
        new Constant(1)
    ),
    new SubFx(
        new Interval('(3, 6]'),
        new Linear(1, -2)
    ),
    new SubFx(
        new Interval('(6, 9]'),
        new Constant(4)
    )
];

// define our piecewise fx
$fx = new PiecewiseFx($subFxs);

$fx(0);   // returns null
$fx(1);   // returns 1
$fx(2);   // returns 1
$fx(3);   // returns 1
$fx(4);   // returns 2
$fx(5);   // returns 3 
$fx(6);   // returns 4
$fx(7);   // returns 4
$fx(8);   // returns 4
$fx(9);   // returns 4
$fx(10);  // returns null