|
Math::Polynomial - Perl class for working with polynomials. |
Math::Polynomial - Perl class for working with polynomials.
This document describes Math::Polynomial version 0.04.
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.
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.
' + '.
' - '.
'*'.
'**'.
'$X'.
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()degree()size()tidy(), degree is equal to size-1.
tidy()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.
The denominator must not be the zero polynomial.
The denominator must not be the zero polynomial.
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.
/ 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 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.
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()interpolate() function.
interpolate() function.
Math::Polynomial exports nothing by default. Subroutines that can be exported on demand are:
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.
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.
Pages in category Polynomials of Wikipedia.
Currently maintained by Martin Becker <becker-cpan-mp@cozap.com>.
Originally written by Mats Kindahl <mats@kindahl.net>.
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 the perlartistic manpage.
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.
|
Math::Polynomial - Perl class for working with polynomials. |