PHP Класс MathPHP\Functions\Polynomial

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
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__construct ( array $coefficients, $variable = "x" ) When a polynomial is instantiated, set the coefficients and degree of that polynomial as its object parameters.
__invoke ( number $x₀ ) : float 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
__toString ( ) : string When a polynomial is to be treated as a string, return it in a readable format.
add ( Polynomial $polynomial ) : Polynomial 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
differentiate ( ) : Polynomial 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
getCoefficients ( ) : array Getter method for the coefficients of a polynomial
getDegree ( ) : integer Getter method for the degree of a polynomial
getVariable ( ) : string Getter method for the dependent variable of a polynomial
integrate ( ) : Polynomial 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
multiply ( Polynomial $polynomial ) : Polynomial 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
setVariable ( string $variable ) Setter method for the dependent variable of a polynomial

Описание методов

__construct() публичный Метод

When a polynomial is instantiated, set the coefficients and degree of that polynomial as its object parameters.
public __construct ( array $coefficients, $variable = "x" )
$coefficients array An array of coefficients in decreasing powers. Example: new Polynomial([1, 2, 3]) will create a polynomial that looks like x² + 2x + 3.

__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
public __invoke ( number $x₀ ) : float
$x₀ number The value at which we are evaluting our polynomial
Результат float The result of our polynomial evaluated at $x₀

__toString() публичный Метод

Example: $polynomial = new Polynomial([1, -8, 12, 3]); echo $polynomial; prints 'x³ - 8x² + 12x + 3'
public __toString ( ) : string
Результат string A human readable representation of the polynomial

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
public add ( Polynomial $polynomial ) : Polynomial
$polynomial Polynomial The polynomial we are adding to our current polynomial
Результат Polynomial The sum of our polynomial objects, also a polynomial object

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
public differentiate ( ) : Polynomial
Результат Polynomial The derivative of our polynomial object, also a polynomial object

getCoefficients() публичный Метод

Getter method for the coefficients of a polynomial
public getCoefficients ( ) : array
Результат array The coefficients array of a polynomial object

getDegree() публичный Метод

Getter method for the degree of a polynomial
public getDegree ( ) : integer
Результат integer The degree of a polynomial object

getVariable() публичный Метод

Getter method for the dependent variable of a polynomial
public getVariable ( ) : string
Результат string The dependent variable of a polynomial object

integrate() публичный Метод

Note that this method assumes the constant of integration to be 0.
public integrate ( ) : Polynomial
Результат Polynomial The integral of our polynomial object, also a polynomial object

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
public multiply ( Polynomial $polynomial ) : Polynomial
$polynomial Polynomial The polynomial we are multiplying with our current polynomial
Результат Polynomial The product of our polynomial objects, also a polynomial object

setVariable() публичный Метод

Setter method for the dependent variable of a polynomial
public setVariable ( string $variable )
$variable string