Search
Categories
Documents
Math::Polynomial - Perl class for working with polynomials. (Displayed) README
|
Math::Polynomial - Perl class for working with polynomials.
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.
- 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.
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'.
- 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 Euclidian algorithm for division, hence we have a
complexity of O(n*m) where n and m are the degrees of the
polynomials.
- verbose(bool)
-
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.
clone()
-
This method will clone the polynomial and return a copy of it.
- coeff(degree)
-
This method returns the coefficient for degree 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.
- 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
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.
- 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 * 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.
- 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.
- 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.
- 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.
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
interpolation package.
- div1c(c)
-
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.
Information
|
This site is currently in testing, it is not yet operating using the full database. Until it is officially launched you may wish to visit Help-Site Computer Manuals. After launch, this site (HelpSpy) will replace Help-Site. Information about the spider which is currently trawling the Internet looking for links to add to this directory can be found here. |
|
|