1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/bin/perl
#
#
use Carp;
use Math::Utils qw(:polynomial);
use Math::Complex;
use strict;
use warnings;
while (my $line = prompt("Numerator Polynomial: "))
{
my @polynomial = split(/[, ] */, $line);
$line = prompt("Divided by: ");
last unless ($line);
my @divisor = split(/,? /, $line);
my($q, $r) = pl_div(\@polynomial, \@divisor);
my @quotient = @$q;
my @remainder = @$r;
print "Quotient: ", rootprint(@quotient), "\n";
print "Remainder: ", rootprint(@remainder), "\n\n";
}
exit(0);
sub cartesian_format
{
my($fmt_re, $fmt_im, @numbers) = @_;
my(@cfn, $n, $r, $i);
$fmt_re ||= "%.15g"; # Provide a default real format
$fmt_im ||= " + %.15gi"; # Provide a default im format
foreach $n (@numbers)
{
$r = sprintf($fmt_re, Re($n));
if (Im($n) != 0)
{
$i = sprintf($fmt_im, Im($n));
}
else
{
$r = sprintf($fmt_re, $n);
$i = "";
}
push @cfn, $r . $i;
}
return wantarray? @cfn: $cfn[0];
}
sub rootprint
{
my @fmtlist;
foreach (@_)
{
push @fmtlist, cartesian_format(undef, undef, $_);
}
return "[ " . join(", ", @fmtlist) . " ]";
}
sub prompt
{
my $pr = shift;
print $pr;
my $inp = <>;
chomp $inp;
return $inp;
}
|