Simple math functions

v1.0.0 2016-08-13 22:50 UTC

This package is auto-updated.

Last update: 2024-04-29 03:12:41 UTC


README

Simple math functions.

use Jstewmc\Fx;

(new Constant(1))(3);         // returns 1
(new Equality())(3);          // returns 3
(new Linear(1, 2))(3);        // returns 5 (1 * 3 + 2)
(new Quadratic(1, 2, 3))(4);  // returns 27 (1 * 4 ^ 2 + 2 * 4 + 3)
(new Power(1, 2))(3);         // returns 9 (1 * 3 ^ 2)
(new Exponential(1))(2);      // returns 1 (1 ^ 2)

This library supports the following functions:

  • constant, c = x
  • equality, y = x
  • linear, y = mx + b
  • quadratic, y = ax2 + bx + c
  • power, y = cxp
  • exponential, y = bx

Constant

A constant function where c = x:

use Jstewmc\Fx

$fx = new Constant(1);

$fx(1);  // returns 1
$fx(2);  // returns 1
$fx(3);  // returns 1

Equality

An equality where y = x:

use Jstewmc\Fx;

$fx = new Equality();

$fx(1);  // returns 1
$fx(2);  // returns 2
$fx(3);  // returns 3

Linear

A linear function is y = mx + b, where m is the slope and b is the y-intercept:

use Jstewmc\Fx;

$fx = new Linear(1, 2);

$fx(1);  // returns 3 (1 * 1 + 2)
$fx(2);  // returns 4 (1 * 2 + 2)
$fx(3);  // returns 5 (1 * 3 + 2)

Quadratic

A univariate, standard-form quadratic function is y = ax2 + bx + c, where a, b, and c are constants (aka, the quadratic coefficient, the linear coefficient, and the constant, respectively):

use Jstewmc\Fx;

$fx = new Quadratic(1, 2, 3);

$fx(1);  // returns 5 (1 * 1 ^ 2 + 2 * 1 + 3)
$fx(2);  // returns 11 (1 * 2 ^ 2 + 2 * 2 + 3)
$fx(3);  // returns 18 (1 * 3 ^ 2 + 2 * 3 + 3)

Power

A power function is y = cxp, where c is a constant, and p is the power:

use Jstewmc\Fx;

$fx = new Power(1, 2);

$fx(1);  // returns 1 (1 * 1 ^ 2)
$fx(2);  // returns 4 (1 * 2 ^ 2)
$fx(3);  // returns 9 (1 * 3 ^ 2)

Exponential

An exponential function is y = bx, where b is a constant:

use Jstewmc\Fx;

$fx = new Exponential(2);

$fx(1);  // returns 2 (2 ^ 1)
$fx(2);  // returns 4 (2 ^ 2)
$fx(3);  // returns 8 (2 ^ 3)

That's it!

License

MIT

Author

Jack Clayton

Version

1.0.0, August 13, 2016

  • Major release
  • Update composer.json

0.3.1, August 13, 2016

  • Update Fx parent class to be an interface

0.3.0, August 6, 2016

  • Add Fx parent class

0.2.0, August 6, 2016

  • Update README examples
  • Add Equality function
  • Add Constant function

0.1.0, July 30, 2016

  • Initial release