Math::Polynomial - Perl class for working with polynomials.
use Math::Polynomial; # The polynomial 2x^2 + 3x - 2 my $P = Math::Polynomial->new(2,3,-2); # Evaluate the polynomial for x = 10 my $result = $P->eval(10); # The polynomial 3x + 4 my $Q = Math::Polynomial->new(3,4); print "$P / $Q = ", $P / $Q, "\n"; my $polynomial = Math::Polynomial::interpolate(1 => 5, 2 => 12, 3 => 6);
This module implements single variable polynomials using arrays. It also implements some useful functionality when working with polynomials, such as adding, multiplication, etc.
The following constructors exist to create new polynomials.
A new polynomial is constructed. The coefficient for the highest degree term is first in the list, while the constant (the coefficient for X**0) is the last one in the list.
Here is a list of class methods available. The methods can be applied to individual polynomials or Math::Polynomial. If it is applied to an object it will affect the entire class.
Math::Polynomial
Configure various things regarding the class. Following is a list of variables used by the class.
The string inserted as a plus sign between terms. Default is ' + '.
' + '
The string inserted as a minus sign between terms. If the first coefficient is negative, this string without spaces is used as prefix. Default is ' - '.
' - '
The string inserted as multiplication between the coefficients and the variables. Default is '*'.
'*'
The string inserted as power between the variable and the power. Default is '**'.
'**'
The string used as variable in the polynom. Default is '$X'.
'$X'
This method computes the quotient and the remainder when dividing numerator by denominator and returns a list (quotient,remainder). It is used by the operators / and %.
/
%
It uses the Euclidian algorithm for division, hence we have a complexity of O(n*m) where n and m are the degrees of the polynomials.
If verbose is turned on, string conversion will return a string for the polynomial, otherwise a list of coefficients will be returned.
Here is a list of object methods available. Object methods are applied to the object in question, in contrast with class methods which are applied to a class.
This method will clone the polynomial and return a copy of it.
This method returns the coefficient for degree degree.
This method returns the degree of the polynomial. The degree of a polynomial is the maximum of the degree of terms with non-zero coefficients.
The polynomial is evaluated for value. The evaluation is done using Horners rule, hence evaluation is done in O(n) time, where n is the degree of the polynomial.
This method returns the internal size of the polynomial, i.e. the length of the array where the coefficients are stored. After a tidy(), degree is equal to size-1.
tidy()
This method removes all terms which are redundant, i.e. the coefficients where all higher degree coefficients are zero.
This method is never called automatically, since it is assumed that the programmer knows best when to tidy the polynomial.
There is a set of operators defined for polynomials.
Adds two polynomials together, returning the sum. The operation is O(n), where n is the maximum of the degrees of the polynomials.
Substracts the right polynomial from the left polynomial, returning the difference. The operation is O(n), where n is the maximum of the degrees of the polynomials.
Multiplies two polynomials together, returning the product. The operation is O(n*m), where n and m are the degrees of the polynomials respectively.
Divides the polynomial on the left (called the numerator) with the polynomial on the right (called the denominator) and returns the quotient. If the degree of the denominator is greater than the degree of the numerator, the zero polynomial will be returned.
Divides the polynomial on the left (called the numerator) with the polynomial on the right (called the denominator) and returns the remainder of the division. If the degree of the denominator is greater than the degree of numerator, the numerator will be returned.
If verbose is turned on, the polynomial will be converted to a string where '$X' is used as the variable. If a coefficient is zero, that term will not be printed.
To change the string used as variable, use the configure class method described above.
configure
If verbose is turned off, a parenthesised, $"-separated list will be returned.
Given a set of pairs of x and y values, interpolate will return a polynomial which interpolates those values. The data points are supplied as a list of alternating x and y values.
interpolate
The degree of the resulting polynomial will be one less than the number of pairs, e.g. the polynomial in the synopsis will be of degree 2.
The interpolation is done using Lagrange's formula and the implementation runs in O(n^2), where n is the number of pairs supplied to interpolate.
Please note that it is a bad idea to use interpolation for extrapolation, i.e. if you are interpolating a polynomial for x-values in the range 0 to 10, then you may get terrible results if you try to predict y-values outside this range. This is true especially if the true function is not a polynomial.
The methods in this section are internal and should not acually be used for anything but internal stuff. They are documented here anyway, but beware that these methods may change or dissapear without notice!
Returns a compact, but human readable, string representing the object.
Multiply the polynomial by (x - c). Used internally by the interpolation package.
Divide the polynomial by (x - c). Used internally by the interpolation package.
A Perl Module for Polynomial Interpolation
Copyright 1997 Matz Kindahl. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install Math::Polynomial, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::Polynomial
CPAN shell
perl -MCPAN -e shell install Math::Polynomial
For more information on module installation, please visit the detailed CPAN module installation guide.