Math::...nomial(3pm)

🌸 NAME 🌸

okay so bestie, welcome to the Math::Cephes::Polynomial page β€” yeah, the 3pm one β€” and it's literally just a ✨Perl interface to the cephes math polynomial routines✨. polynomials!! we love her πŸ’–


✨ SYNOPSIS ✨

here's the vibe in code form, no cap:

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";

(psst β€” yes, that my $b = poly([4, 5, 6, 7]; line is missing a paren, that's literally what the page says, i'm not touching it πŸ™ˆ)


πŸ’… DESCRIPTION πŸ’…

so like, this module is a layer sitting on top of the basic routines in the cephes math library, except its whole personality is polynomials. πŸ¦‹ and every time you make one you do:

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

where $arr_ref is a reference to an array, and that array can be any one of these iconic things:

  • 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 max degree of the polynomials she can handle is set by default to 256 β€” but bestie you can change that whenever by setting $Math::Cephes::Polynomial::MAXPOL. slay. πŸ‘‘

Wanna copy a Math::Cephes::Polynomial object? easy:

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

and if you want a string representation of the polynomial (for reading, showing your friends, whatever πŸ’–) you get it through:

print $p->as_string;

πŸŽ€ Methods πŸŽ€

the following methods are available, and yes we're going through all of them:

πŸ’… 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. (full reset, no crumbs 🧹)

πŸ’… 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. so like, don't expect infinite precision, she has a ceiling 😭

πŸ’… 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. (translating: near-duplicate roots get messy after the first one shows up πŸ’€)


πŸ› BUGS πŸ›

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


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.


and that's the whole page bestie!! this one's from perl v5.42.3, dated 2026-08-19 πŸ’– go forth and polynomial, you've got this 🫢✨

πŸ“¦ where this page comes from