math-php
|
Class: Polynomial A convenience class for one-dimension polynomials. More...
Public Member Functions | |
__construct (array $coefficients, string $variable="x") | |
Constructor: __construct When a polynomial is instantiated, set the coefficients and degree of that polynomial as its object parameters. More... | |
__toString () | |
Function: __ToString When a polynomial is to be treated as a string, return it in a readable format. More... | |
__invoke ($x₀) | |
Function: __invoke 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. More... | |
getDegree () | |
Function: getDegree Getter method for the degree of a polynomial. More... | |
getCoefficients () | |
Function: getCoefficients Getter method for the coefficients of a polynomial. More... | |
getVariable () | |
Function: getVariable Getter method for the dependent variable of a polynomial. More... | |
setVariable (string $variable) | |
Function: setVariable Setter method for the dependent variable of a polynomial. More... | |
differentiate () | |
Function: differentiate Calculate the derivative of a polynomial and return it as a new polynomial Example: $polynomial = new Polynomial([1, -8, 12, 3]); // x³ - 8x² + 12x + 3 $derivative = $polynomial->differentiate(); // 3x² - 16x + 12. More... | |
integrate () | |
Function: integrate Calculate the indefinite integral of a polynomial and return it as a new polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->integrate(); // x³ - 8x² + 12x. More... | |
add ($polynomial) | |
Frunction: add Return a new polynomial that is the sum of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->integrate(); // x³ - 8x² + 12x $sum = $polynomial->add($integral); // x³ - 5x² - 4x + 12. More... | |
subtract ($polynomial) | |
Function: subtract Return a new polynomial that is the difference of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->diferentiate(); // 6x - 16 $difference = $polynomial->subtract($derivative); // 3x² - 22x + 28. More... | |
multiply ($polynomial) | |
Function: multiply Return a new polynomial that is the product of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([2, -16]); // 2x - 16 $integral = $polynomial->integrate(); // x² - 16x $product = $polynomial->multiply($integral); // 2x³ - 48x² + 256x. More... | |
negate () | |
Function: negate Return a new polynomial that is the negated version. More... | |
roots () | |
Function: roots Calculate the roots of a polynomial. More... | |
Public Attributes | |
const | SYMBOLS = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'] |
Class: Polynomial A convenience class for one-dimension polynomials.
This class is used to encompass typical methods and features that you can extend to polynomials. For example, polynomial differentiation follows a specific rule, and thus we can build a differentiation method that returns the exact derivative for polynomials.
Input arguments: simply pass in an array of coefficients in decreasing powers. Make sure to put a 0 coefficient in place of powers that are not used.
Current features: o Print a human readable string of a polynomial of any variable (default of x) o Evaluate a polynomial at any real number o Return the degree of a polynomial o Return the coefficients of a polynomial o Return the variable of a polynomial o Set the variable of an instantiated polynomial o Polynomial differentiation (exact) o Polynomial integration (indefinite integral) o Polynomial addition o Polynomial multiplication
Examples: $polynomial = new Polynomial([1, -8, 12, 3]); echo $polynomial; // prints 'x³ - 8x² + 12x + 3' echo $polynomial(4); // prints -31 echo $polynomial->getDegree(); // prints 3 print_r($polynomial->getCoefficients()); // prints [1, -8, 12, 3] echo $polynomial->differentiate(); // prints '3x² - 16x + 12' echo $polynomial->integrate(); // prints '0.25x⁴ - 2.6666666666667x³ + 6x² + 3x' echo $polynomial->add($polynomial); // prints '2x³ - 16x² + 24x + 6' echo $polynomial->multiply($polynomial); // prints 'x⁶ - 16x⁵ + 88x⁴ - 186x³ + 96x² + 72x + 9' echo $polynomial->getVariable(); // prints 'x' $polynomial->setVariable("r"); echo $polynomial; // prints 'r³ - 8r² + 12r + 3'
MathPHP\Functions\Polynomial::__construct | ( | array | $coefficients, |
string | $variable = "x" |
||
) |
Constructor: __construct When a polynomial is instantiated, set the coefficients and degree of that polynomial as its object parameters.
array | $coefficients | An array of coefficients in decreasing powers Example: new Polynomial([1, 2, 3]) will create a polynomial that looks like x² + 2x + 3. |
string | $variable |
MathPHP\Functions\Polynomial::__invoke | ( | $x₀ | ) |
Function: __invoke 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.
number | $x₀ | The value at which we are evaluting our polynomial |
MathPHP\Functions\Polynomial::__toString | ( | ) |
Function: __ToString When a polynomial is to be treated as a string, return it in a readable format.
Example: $polynomial = new Polynomial([1, -8, 12, 3]); echo $polynomial; // prints 'x³ - 8x² + 12x + 3'
MathPHP\Functions\Polynomial::add | ( | $polynomial | ) |
Frunction: add Return a new polynomial that is the sum of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->integrate(); // x³ - 8x² + 12x $sum = $polynomial->add($integral); // x³ - 5x² - 4x + 12.
mixed | $polynomial | The polynomial or scaler we are adding to our current polynomial |
Exception |
Implements MathPHP\Number\ObjectArithmetic.
MathPHP\Functions\Polynomial::differentiate | ( | ) |
Function: differentiate Calculate the derivative of a polynomial and return it as a new polynomial Example: $polynomial = new Polynomial([1, -8, 12, 3]); // x³ - 8x² + 12x + 3 $derivative = $polynomial->differentiate(); // 3x² - 16x + 12.
MathPHP\Functions\Polynomial::getCoefficients | ( | ) |
Function: getCoefficients Getter method for the coefficients of a polynomial.
MathPHP\Functions\Polynomial::getDegree | ( | ) |
Function: getDegree Getter method for the degree of a polynomial.
MathPHP\Functions\Polynomial::getVariable | ( | ) |
Function: getVariable Getter method for the dependent variable of a polynomial.
MathPHP\Functions\Polynomial::integrate | ( | ) |
Function: integrate Calculate the indefinite integral of a polynomial and return it as a new polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->integrate(); // x³ - 8x² + 12x.
Note that this method assumes the constant of integration to be 0.
MathPHP\Functions\Polynomial::multiply | ( | $polynomial | ) |
Function: multiply Return a new polynomial that is the product of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([2, -16]); // 2x - 16 $integral = $polynomial->integrate(); // x² - 16x $product = $polynomial->multiply($integral); // 2x³ - 48x² + 256x.
mixed | $polynomial | The polynomial or scaler we are multiplying with our current polynomial |
Exception |
Implements MathPHP\Number\ObjectArithmetic.
MathPHP\Functions\Polynomial::negate | ( | ) |
Function: negate Return a new polynomial that is the negated version.
MathPHP\Functions\Polynomial::roots | ( | ) |
Function: roots Calculate the roots of a polynomial.
Closed form solutions only exist if the degree is less than 5
Exception |
MathPHP\Functions\Polynomial::setVariable | ( | string | $variable | ) |
Function: setVariable Setter method for the dependent variable of a polynomial.
string | $variable | The new dependent variable of a polynomial object |
MathPHP\Functions\Polynomial::subtract | ( | $polynomial | ) |
Function: subtract Return a new polynomial that is the difference of the current polynomial and an input polynomial Example: $polynomial = new Polynomial([3, -16, 12]); // 3x² - 16x + 12 $integral = $polynomial->diferentiate(); // 6x - 16 $difference = $polynomial->subtract($derivative); // 3x² - 22x + 28.
mixed | $polynomial | The polynomial or scaler we are subtracting from our current polynomial |
Exception |
Implements MathPHP\Number\ObjectArithmetic.