math-php
Public Member Functions | Public Attributes | List of all members
MathPHP\Functions\Polynomial Class Reference

Class: Polynomial A convenience class for one-dimension polynomials. More...

Inheritance diagram for MathPHP\Functions\Polynomial:
Inheritance graph
[legend]
Collaboration diagram for MathPHP\Functions\Polynomial:
Collaboration graph
[legend]

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 = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹']
 

Detailed Description

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'

https://en.wikipedia.org/wiki/Polynomial

Constructor & Destructor Documentation

◆ __construct()

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.

Parameters
array$coefficientsAn array of coefficients in decreasing powers Example: new Polynomial([1, 2, 3]) will create a polynomial that looks like x² + 2x + 3.
string$variable

Member Function Documentation

◆ __invoke()

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.

Parameters
number$x₀The value at which we are evaluting our polynomial
Returns
float The result of our polynomial evaluated at $x₀

◆ __toString()

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'

Returns
string A human readable representation of the polynomial

◆ add()

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.

Parameters
mixed$polynomialThe polynomial or scaler we are adding to our current polynomial
Returns
Polynomial The sum of our polynomial objects, also a polynomial object
Exceptions
Exception

Implements MathPHP\Number\ObjectArithmetic.

◆ differentiate()

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.

Returns
Polynomial The derivative of our polynomial object, also a polynomial object

◆ getCoefficients()

MathPHP\Functions\Polynomial::getCoefficients ( )

Function: getCoefficients Getter method for the coefficients of a polynomial.

Returns
array The coefficients array of a polynomial object

◆ getDegree()

MathPHP\Functions\Polynomial::getDegree ( )

Function: getDegree Getter method for the degree of a polynomial.

Returns
int The degree of a polynomial object

◆ getVariable()

MathPHP\Functions\Polynomial::getVariable ( )

Function: getVariable Getter method for the dependent variable of a polynomial.

Returns
string The dependent variable of a polynomial object

◆ integrate()

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.

Returns
Polynomial The integral of our polynomial object, also a polynomial object

◆ multiply()

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.

Parameters
mixed$polynomialThe polynomial or scaler we are multiplying with our current polynomial
Returns
Polynomial The product of our polynomial objects, also a polynomial object
Exceptions
Exception

Implements MathPHP\Number\ObjectArithmetic.

◆ negate()

MathPHP\Functions\Polynomial::negate ( )

Function: negate Return a new polynomial that is the negated version.

Returns
Polynomial that is negated

◆ roots()

MathPHP\Functions\Polynomial::roots ( )

Function: roots Calculate the roots of a polynomial.

Closed form solutions only exist if the degree is less than 5

Returns
array of roots
Exceptions
Exception

◆ setVariable()

MathPHP\Functions\Polynomial::setVariable ( string  $variable)

Function: setVariable Setter method for the dependent variable of a polynomial.

Parameters
string$variableThe new dependent variable of a polynomial object

◆ subtract()

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.

Parameters
mixed$polynomialThe polynomial or scaler we are subtracting from our current polynomial
Returns
Polynomial The defference of our polynomial objects, also a polynomial object
Exceptions
Exception

Implements MathPHP\Number\ObjectArithmetic.


The documentation for this class was generated from the following file: