Add the dependency:
$ composer update and you're done.
Equation Operating System
This class makes it incredibly easy to use and parse/solve equations in
your own applications. __NOTE__ ALL of the functions within
these classes are static. It is also important to note that these
classes throw exceptions if running in to errors, please read the beginning
Math.php file for the defines of the exceptions thrown. Exceptions
includes a descriptive message of the error encountered and within
also typically include the full equation used.
This class has one important function,
Parser::solve() which does all the legwork,
so we'll start there and end with examples.
To use this function:
$value = Parser::solve($eq, $vars);
Is simply a standard equation with variable support.
The parser has good implied multiplication.
The variables are fairly simple to understand. If it contains a scalar (ie
a non-array value) _every_ variable within the equation will be replaced with
that number. If it contains an array, there will be a by-variable replacement -
note that the array MUST be in the format of
'variable' => value
'x' => 2,
'y' => 3
Given the equation:
If this is called by:
It will equal '20', as every variable is replaced by 2. However, if called like:
'x' => 2,
'y' => 3));
You will get the result of '40' as it would equate to
5*2^3, as expected.
This is the fun class that can create graphs.
The image will default to 640x480, to initialize a different size use:
$height are the values used for the image size.
graph($eq, $xLow, $xHigh, [$xStep, $xyGrid, $yGuess, ...])
This method will generate the graph for the equation (
$eq) with a min and max
x range that it will parse through. All Variables explained:
The Standard Equation to use. _Must_ have a variable in it. (ie
The starting point for the calculations - the left side of the graph.
The last point calculated for the variable - the right side of the graph.
Stepping point for the variable. Set to null/false to use the smart xStep feature within the graph class.
$xyGrid = false
x/y gridlines on the graph. Defaults to false. Each grid line is set at an integer, with a max of 30 lines, so it will calculate the stepping for it. When the grid is show, the lines are labeled along the top and left side of the image.
$yGuess = true
Guess the Lower and Upper
y-bounds (The bottom and top of the image
respectively.) This will set the the bounds to the lowest
encountered for the
$yLow, and the largest
y value for
$yLow = null
Lower bound for
y. Will be reset if a lower value for
y is found if
$yGuess is true.
$yHigh = null
Upper bound for
y. Will be reset if a larger
y value is found if
$yGuess is true.
If you don't want the axis' labeled with their numbers, you can turn off the default behavior with:
Graph::$labelAxis = false;
- Allow user-defined colors for all aspects of the graph.
To set up a graph with a
21x21 window (ie
-10 to 10) for the equation
sin(x) and output as PNG, would use as:
Graph::graph('sin(x)', -10, 10, 0.01, true, false, -10, 10);
It would look like: