NAME
Math::Polynomial - Perl class for working with polynomials.
VERSION
This document describes Math::Polynomial version 0.04.
SYNOPSIS
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);
DESCRIPTION
This module implements single variable polynomials using arrays. It also implements some useful functionality when working with polynomials, such as adding, multiplication, etc.
CONSTRUCTOR
The following constructors exist to create new polynomials.
- new(coefficient, ...)
-
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.
CLASS METHODS
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.
- configure(variable => value, ...)
-
Configure various things regarding the class. Following is a list of variables used by the class.
- PLUS
-
The string inserted as a plus sign between terms. Default is
' + '
. - MINUS
-
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
' - '
. - TIMES
-
The string inserted as multiplication between the coefficients and the variables. Default is
'*'
. - POWER
-
The string inserted as power between the variable and the power. Default is
'**'
. - VARIABLE
-
The string used as variable in the polynom. Default is
'$X'
.
- verbose(bool)
-
If verbose is turned on, string conversion will return a string for the polynomial, otherwise a list of coefficients will be returned.
OBJECT METHODS
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.
- clone()
-
This method will clone the polynomial and return a copy of it.
- coeff(degree)
-
This method returns the coefficient for x to the power of 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. For zero polynomials, -1 is returned.
- eval(value)
-
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.
- size()
-
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 zero coefficients where all higher degree coefficients are also zero.
This method is never called automatically, since it is assumed that the programmer knows best when to tidy the polynomial.
OPERATORS
There is a set of operators defined for polynomials.
- polynomial + polynomial
-
Adds two polynomials together, returning the sum. The operation is O(n), where n is the maximum of the degrees of the polynomials.
- polynomial - polynomial
-
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.
- - polynomial
-
Negates a polynomial. The operation is O(n) where n is the degree of the polynomial.
- polynomial * polynomial
-
Multiplies two polynomials together, returning the product. The operation is O(n*m), where n and m are the degrees of the polynomials respectively.
- polynomial / polynomial
-
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.
The denominator must not be the zero polynomial.
- polynomial % polynomial
-
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.
The denominator must not be the zero polynomial.
- String conversion.
-
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.If verbose is turned off, a parenthesised, $"-separated list will be returned.
SUBROUTINES
- quotrem(numerator,denominator)
-
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 standard long division algorithm for polynomials, with a complexity of O(n*m) where n and m are the degrees of the polynomials.
- interpolate(x => y, ...)
-
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.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.
INTERNAL METHODS
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!
- dump()
-
Returns a compact, but human readable, string representing the object.
- mul1c(c)
-
Multiply the polynomial by (x - c). Used internally by the interpolate() function.
- div1c(c)
-
Divide the polynomial by (x - c). Used internally by the interpolate() function.
EXPORTS
Math::Polynomial exports nothing by default. Subroutines that can be exported on demand are:
- quotrem
- interpolate
DIAGNOSTICS
Division and modulus operators as well as quotrem() will die on zero polynomials as right hand operand.
The coeff() method will die on exponents outside the range from zero up to the current internal size of the coefficient vector minus one. The range of allowed exponents will always include the polynomial degree, though.
All other methods are supposed to always be successful.
CAVEATS
Most methods do not actively check their parameters. Arithmetic is carried out using Perl's builtin numeric data types and therefore prone to rounding errors and occasional floating point exceptions.
SEE ALSO
Pages in category Polynomials of Wikipedia.
AUTHORS
Currently maintained by Martin Becker <becker-cpan-mp@cozap.com>.
Originally written by Mats Kindahl <mats@kindahl.net>.
COPYRIGHT AND LICENCE
Copyright (c) 2007 Martin Becker <becker-cpan-mp@cozap.com>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
This module is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.