Math-Cephes
view release on metacpan or search on metacpan
lib/Math/Cephes/Polynomial.pm view on Meta::CPAN
package Math::Cephes::Polynomial;
use strict;
use warnings;
use vars qw(@EXPORT_OK $VERSION $MAXPOL $FMAXPOL $flag $fflag);
eval {require Math::Complex; import Math::Complex qw(Re Im)};
eval {local $^W=0; require Math::Fraction;};
$MAXPOL = 256;
$flag = 0;
$FMAXPOL = 256;
$fflag = 0;
require Exporter;
*import = \&Exporter::import;
@EXPORT_OK = qw(poly);
$VERSION = '0.5308';
require Math::Cephes;
require Math::Cephes::Fraction;
require Math::Cephes::Complex;
sub new {
my ($caller, $arr) = @_;
my $refer = ref($caller);
my $class = $refer || $caller;
die "Must supply data for the polynomial"
unless ($refer or $arr);
my ($type, $ref, $data, $n);
if ($refer) {
($type, $ref, $n) =
($caller->{type}, $caller->{ref}, $caller->{n});
my $cdata = $caller->{data};
if (ref($cdata) eq 'ARRAY') {
$data = [ @$cdata ];
}
else {
my ($f, $s) = ($type eq 'fract') ? ('n', 'd') : ('r', 'i');
$data = {$f => [ @{$cdata->{$f}} ],
$s => [ @{$cdata->{$s}} ],
};
}
}
else {
($type, $ref, $data, $n) = get_data($arr);
}
bless { type => $type,
ref => $ref,
data => $data,
n => $n,
}, $class;
}
sub poly {
return Math::Cephes::Polynomial->new(shift);
}
sub coef {
return $_[0]->{data};
}
sub get_data {
my ($arr, $ref_in) = @_;
die "Must supply an array reference" unless ref($arr) eq 'ARRAY';
my $n = scalar @$arr - 1;
my $ref = ref($arr->[0]);
die "array data must be of type '$ref_in'"
if (defined $ref_in and $ref_in ne $ref);
my ($type, $data);
SWITCH: {
not $ref and do {
$type = 'scalar';
foreach (@$arr) {
die 'Found conflicting types in array data'
if ref($_);
}
$data = $arr;
set_max() unless $flag;
last SWITCH;
};
$ref eq 'Math::Cephes::Complex' and do {
$type = 'cmplx';
foreach (@$arr) {
die 'Found conflicting types in array data'
unless ref($_) eq $ref;
die "array data must be of type '$ref_in'"
if (defined $ref_in and $ref_in ne $ref);
push @{$data->{r}}, $_->r;
push @{$data->{i}}, $_->i;
}
set_max() unless $flag;
last SWITCH;
};
$ref eq 'Math::Complex' and do {
$type = 'cmplx';
foreach (@$arr) {
die 'Found conflicting types in array data'
unless ref($_) eq $ref;
die "array data must be of type '$ref_in'"
if (defined $ref_in and $ref_in ne $ref);
( run in 1.547 second using v1.01-cache-2.11-cpan-dd78ea5b424 )