# NAME

Math::Cephes::Polynomial - Perl interface to the cephes math polynomial routines

# SYNOPSIS

```
use Math::Cephes::Polynomial qw(poly);
# 'poly' is a shortcut for Math::Cephes::Polynomial->new
require Math::Cephes::Fraction; # if coefficients are fractions
require Math::Cephes::Complex; # if coefficients are complex
my $a = poly([1, 2, 3]); # a(x) = 1 + 2x + 3x^2
my $b = poly([4, 5, 6, 7]; # b(x) = 4 + 5x + 6x^2 + 7x^3
my $c = $a->add($b); # c(x) = 5 + 7x + 9x^2 + 7x^3
my $cc = $c->coef;
for (my $i=0; $i<4; $i++) {
print "term $i: $cc->[$i]\n";
}
my $x = 2;
my $r = $c->eval($x);
print "At x=$x, c(x) is $r\n";
my $u1 = Math::Cephes::Complex->new(2,1);
my $u2 = Math::Cephes::Complex->new(1,-3);
my $v1 = Math::Cephes::Complex->new(1,3);
my $v2 = Math::Cephes::Complex->new(2,4);
my $z1 = Math::Cephes::Polynomial->new([$u1, $u2]);
my $z2 = Math::Cephes::Polynomial->new([$v1, $v2]);
my $z3 = $z1->add($z2);
my $z3c = $z3->coef;
for (my $i=0; $i<2; $i++) {
print "term $i: real=$z3c->{r}->[$i], imag=$z3c->{i}->[$i]\n";
}
$r = $z3->eval($x);
print "At x=$x, z3(x) has real=", $r->r, " and imag=", $r->i, "\n";
my $a1 = Math::Cephes::Fraction->new(1,2);
my $a2 = Math::Cephes::Fraction->new(2,1);
my $b1 = Math::Cephes::Fraction->new(1,2);
my $b2 = Math::Cephes::Fraction->new(2,2);
my $f1 = Math::Cephes::Polynomial->new([$a1, $a2]);
my $f2 = Math::Cephes::Polynomial->new([$b1, $b2]);
my $f3 = $f1->add($f2);
my $f3c = $f3->coef;
for (my $i=0; $i<2; $i++) {
print "term $i: num=$f3c->{n}->[$i], den=$f3c->{d}->[$i]\n";
}
$r = $f3->eval($x);
print "At x=$x, f3(x) has num=", $r->n, " and den=", $r->d, "\n";
$r = $f3->eval($a1);
print "At x=", $a1->n, "/", $a1->d,
", f3(x) has num=", $r->n, " and den=", $r->d, "\n";
```

# DESCRIPTION

This module is a layer on top of the basic routines in the cephes math library to handle polynomials. In the following, a Math::Cephes::Polynomial object is created as

` my $p = Math::Cephes::Polynomial->new($arr_ref);`

where `$arr_ref`

is a reference to an array which can consist of one of

floating point numbers, for polynomials with floating point coefficients,

*Math::Cephes::Fraction*or*Math::Fraction*objects, for polynomials with fractional coefficients,*Math::Cephes::Complex*or*Math::Complex*objects, for polynomials with complex coefficients,

The maximum degree of the polynomials handled is set by default to 256 - this can be changed by setting *$Math::Cephes::Polynomial::MAXPOL*.

A copy of a *Math::Cephes::Polynomial* object may be done as

` my $p_copy = $p->new();`

and a string representation of the polynomial may be gotten through

` print $p->as_string;`

## Methods

The following methods are available.

*coef*: get coefficients of the polynomial-
`SYNOPSIS: my $c = $p->coef; DESCRIPTION:`

This returns an array reference containing the coefficients of the polynomial.

*clr*: set a polynomial identically equal to zero-
`SYNOPSIS: $p->clr($n); DESCRIPTION:`

This sets the coefficients of the polynomial identically to 0, up to $p->[$n]. If $n is omitted, all elements are set to 0.

*add*: add two polynomials-
`SYNOPSIS: $c = $a->add($b); DESCRIPTION:`

This sets $c equal to $a + $b.

*sub*: subtract two polynomials-
`SYNOPSIS: $c = $a->sub($b); DESCRIPTION:`

This sets $c equal to $a - $b.

*mul*: multiply two polynomials-
`SYNOPSIS: $c = $a->mul($b); DESCRIPTION:`

This sets $c equal to $a * $b.

*div*: divide two polynomials-
`SYNOPSIS: $c = $a->div($b); DESCRIPTION:`

This sets $c equal to $a / $b, expanded by a Taylor series. Accuracy is approximately equal to the degree of the polynomial, with an internal limit of about 16.

*sbt*: change of variables-
`SYNOPSIS: $c = $a->sbt($b); DESCRIPTION:`

If a(x) and b(x) are polynomials, then

`c(x) = a(b(x))`

is a polynomial found by substituting b(x) for x in a(x). This method is not available for polynomials with complex coefficients.

*eval*: evaluate a polynomial-
`SYNOPSIS: $s = $a->eval($x); DESCRIPTION:`

This evaluates the polynomial at the value $x. The returned value is of the same type as that used to represent the coefficients of the polynomial.

*sqt*: square root of a polynomial-
`SYNOPSIS: $b = $a->sqt(); DESCRIPTION:`

This finds the square root of a polynomial, evaluated by a Taylor expansion. Accuracy is approximately equal to the degree of the polynomial, with an internal limit of about 16. This method is not available for polynomials with complex coefficients.

*sin*: sine of a polynomial-
`SYNOPSIS: $b = $a->sin(); DESCRIPTION:`

This finds the sine of a polynomial, evaluated by a Taylor expansion. Accuracy is approximately equal to the degree of the polynomial, with an internal limit of about 16. This method is not available for polynomials with complex coefficients.

*cos*: cosine of a polynomial-
`SYNOPSIS: $b = $a->cos(); DESCRIPTION:`

This finds the cosine of a polynomial, evaluated by a Taylor expansion. Accuracy is approximately equal to the degree of the polynomial, with an internal limit of about 16. This method is not available for polynomials with complex coefficients.

*atn*: arctangent of the ratio of two polynomials-
`SYNOPSIS: $c = $a->atn($b); DESCRIPTION:`

This finds the arctangent of the ratio $a / $b of two polynomial, evaluated by a Taylor expansion. Accuracy is approximately equal to the degree of the polynomial, with an internal limit of about 16. This method is not available for polynomials with complex coefficients.

*rts*: roots of a polynomial-
`SYNOPSIS: my $w = Math::Cephes::Polynomial->new([-2, 0, -1, 0, 1]); my ($flag, $r) = $w->rts(); for (my $i=0; $i<4; $i++) { print "Root $i has real=", $r->[$i]->r, " and imag=", $r->[$i]->i, "\n"; } DESCRIPTION:`

This finds the roots of a polynomial.

*$flag*, if non-zero, indicates a failure of some kind.*$roots*in an array reference of*Math::Cephes::Complex*objects holding the real and complex values of the roots found. This method is not available for polynomials with complex coefficients.`ACCURACY:`

Termination depends on evaluation of the polynomial at the trial values of the roots. The values of multiple roots or of roots that are nearly equal may have poor relative accuracy after the first root in the neighborhood has been found.

# BUGS

Please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca>

# COPYRIGHT

The C code for the Cephes Math Library is Copyright 1984, 1987, 1989, 2002 by Stephen L. Moshier, and is available at http://www.netlib.org/cephes/. Direct inquiries to 30 Frost Street, Cambridge, MA 02140.

The perl interface is copyright 2000, 2002 by Randy Kobes. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.