Martin Becker >
Math-Polynomial >
Math::Polynomial::Generic

Module Version: 1.008
Math::Polynomial::Generic - syntactical sugar coating Math::Polynomial

This documentation refers to version 1.008 of Math::Polynomial::Generic.

This module has always been declared as experimental and its interface as not to be taken for granted. The intention was to give users more expressive flexibility through a kind of polynomial object that is not bound to a particular coefficient space.

This experiment is now considered a failure. Coefficient spaces need to be specified at one point, and delaying this part of proper initialization seems do do more harm than good in terms of clarity.

Therefore,
Math::Polynomial::Generic is now scheduled for deprecation.
Starting with the first version after January 1,
2016,
using the module will trigger a deprecation warning unless the special symbol `:legacy`

appears on the import list.
At some later time,
the module will be discontinued altogether.
Currently,
importing `:legacy`

is optional and does nothing.

use Math::Polynomial::Generic qw(X C); $p = X**2 - 3 * X + 5; $q = $p * X; $r = (X - $some_value) * (X - $some_other_value); $s = C($some_value) * X**2 + C($some_other_value);

Math::Polynomial::Generic allows to create Math::Polynomial objects in a more descriptive way than with basic constructors. It offers a symbol *X* that will act as a polynomial when used as the variable in a polynomial expression. Another one-letter symbol *C* turns constants into constant polynomials.

*X*-
`X`

is different from`Math::Polynomial->new(0, 1)`

in that it is not bound to a particular coefficient space.`X`

can be coupled in expressions with polynomials of arbitrary coefficient types, such as complex numbers, big rationals, square matrices, etc. The coefficients actually used determine the coefficient space of the whole expression. Incompatible coefficient types must not be mixed in a single expression, of course.The mechanism to make this work is based on a kind of generic object (hence the name) which will be cast to a proper polynomial when it is used in a binary operation together with something already bound to a coefficient space: either another polynomial or a plain coefficient.

*C*-
`C($coeff)`

creates a constant polynomial from a given coefficient value`$coeff`

.Coefficients other than simple numerical values should be turned into polynomials to prevent perl from carrying out the overloaded operator in the coefficient class rather than the polynomial class (see below).

Expressions containing

*X*but lacking any coefficient values will produce generic polynomial objects. These must not be mistaken for proper polynomials, nor should Math::Polynomial methods be invoked on them.In order to turn an otherwise generic expression into a regular polynomial object, add a

`C()`

-wrapped zero value.

$c = Math::BigRat->new('2/3'); # some coefficient value $p = Math::Polynomial->new(1); # some regular polynomial $q = X; # wrong (generic) $q = X * X - X; # wrong (generic) $q = X * X - X + C(0); # OK $q = $p + X; # OK $q = X + $p; # OK $q = $p - X; # OK $q = X - $p; # OK $q = $p * X; # OK $q = $p / X; # OK $q = $p % X; # OK $q = $c + X; # wrong (operand types) $q = X + $c; # wrong (operand types) $q = C($c) + X; # OK $q = X + C($c); # OK $q = C($c) * X**2; # OK $q = X / X + C(0); # wrong (generic division)

*divmod**div**mod*-
Currently, division of generic objects by generic objects (like

`X/X`

) is not implemented. The methods*divmod*,*div*and*mod*are overriden to guard against such cases.

*_is_generic*-
Boolean true for generic objects, false for regular polynomials.

*_cast*-
`$p->_cast($q)`

generates a regular polynomial from a generic object`$p`

, the result sharing the coefficient space with`$q`

.

By default, nothing is exported into the caller's namespace. The polynomial generators X and C can be explicitly imported, however.

The special symbol `:legacy`

can be imported without any consequences. In future versions of the library, it can be used to suppress deprecation warnings.

As this module is scheduled for deprecation, existing applications should be migrated to no longer use it. This, fortunately, is rather easy. Instead of the symbol `X`

, we recommend to use a variable `$X`

that is initialized as `Math::Polynomial->new($zero, $one)`

with appropriate coefficient values `$zero`

and `$one`

.

`C($coeff)`

can be replaced by `Math::Polynomial->new($coeff)`

or it can be defined locally as a small wrapper for the same.

Math::Polynomial

Martin Becker, <becker-cpan-mp@cozap.com>

Copyright (c) 2009-2015 by Martin Becker. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6 or, at your option, any later version of Perl 5 you may have available.

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.

syntax highlighting: