[system] / trunk / pg / lib / Polynomial.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Polynomial.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2558 - (download) (as text) (annotate)
Wed Jul 28 20:32:33 2004 UTC (15 years, 7 months ago) by sh002i
File size: 8532 byte(s)
merged changes from rel-2-1-a1 -- stop using that branch.

    1 #! /usr/local/bin/perl
    2 
    3 package Polynomial;
    4 
    5 #@ISA = qw(Function);
    6 
    7 sub givenVariableScaleAndRoots {
    8   my ($class, $variable,$scale,@roots) = @_;
    9 # print @roots,"\n";
   10   my @coefficients;
   11   my $degree = scalar(@roots);
   12   foreach my $i (0..$degree-1) {$roots[$i] *= -1;}
   13   foreach my $i (0..$degree) {
   14     $coefficients[$i] = $scale * symmetric_function($degree-$i, \@roots);
   15   }
   16 # print @coefficients, "\n";
   17   my $self = {'degree' => $degree,
   18         'variable' => $variable,
   19         'scale' => $scale,
   20         'roots' => \@roots,
   21         'coefficients' => \@coefficients,
   22         };
   23   bless $self, $class;
   24 # print "OK\n";
   25   return($self);
   26 }
   27 
   28 sub givenVariableScaleVTranslateAndRoots {
   29   my ($class, $variable,$scale,$vtranslate,@roots) = @_;
   30 # print @roots,"\n";
   31   my @coefficients;
   32   my $degree = scalar(@roots);
   33   foreach my $i (0..$degree) {
   34     $coefficients[$i] = $scale * symmetric_function($degree-$i, \@roots);
   35   }
   36   $coefficients[0] += $vtranslate;
   37 # print @coefficients, "\n";
   38   my $self = {'degree' => $degree,
   39         'variable' => $variable,
   40         'scale' => $scale,
   41         'roots' => \@roots,
   42         'coefficients' => \@coefficients,
   43         };
   44   bless $self, $class;
   45 # print "OK\n";
   46   return($self);
   47 }
   48 
   49 sub givenVariableAndCoefficients { # Give Coefficients from a_0 to a_n
   50   my ($class, $variable, @coefficients) = @_;
   51   my $degree = scalar(@coefficients) -1;
   52   while ($degree >= 0 and $coefficients[$degree] == 0)  {
   53     $degree--;
   54   }
   55   my $self = {'degree' => $degree,
   56         'variable' => $variable,
   57         'scale' => 1,
   58         'roots' => [],
   59         'coefficients' => \@coefficients,
   60         };
   61   bless $self, $class;
   62   return($self);
   63 }
   64 
   65 
   66 
   67 sub degree {
   68   my $self=shift;
   69   if (@_) {$self -> {'degree'} = shift;}
   70   return ($self -> {'degree'});
   71 }
   72 
   73 sub coefficients {
   74   my $self = shift;
   75   if (@_) {$self -> {'coefficients'} = shift;}
   76   return ($self -> {'coefficients'});
   77 }
   78 
   79 sub variable {
   80   my $self = shift;
   81   if (@_) {$self->{'variable'} = shift;}
   82   return($self ->{'variable'});
   83 }
   84 
   85 sub TeXizeHighFirst {
   86   my $self = shift;
   87   my $degree = $self->degree();
   88   my $variable = $self->variable();
   89   my $coefficients = $self->coefficients();
   90   my $found = 0;
   91   my $string = "";
   92   my $coef;
   93   my $jth;
   94   foreach my $i (0..$degree-1) {
   95     $jth = $degree - $i;
   96     $vterm = ($jth == 1) ? "$variable" : "$variable^{${jth}}";
   97     if (($coef = $coefficients -> [$jth]) != 0)
   98       {if ($found) {
   99         $string .=
  100           ($coef == 1) ? " + $vterm"
  101           : ($coef > 0) ? " + $coef $vterm"
  102             : ($coef == -1) ? " - $vterm"
  103               : "  $coef $vterm";
  104         }
  105       else {
  106         $found=1;
  107         $string .=
  108           ($coef == 1) ? " $vterm"
  109           : ($coef > 0) ? " $coef $vterm"
  110             : ($coef == -1) ? " - $vterm"
  111               : "  $coef $vterm";
  112         }
  113     }
  114   }
  115   $jth = 0;
  116   if (($coef = $coefficients -> [$jth]) != 0)
  117       {if ($found) {
  118         $string .= ($coef > 0) ? " + $coef "
  119           : "  $coef " ;
  120         }
  121       else {
  122         $found=1;
  123         $string .= ($coef > 0) ? " $coef "
  124           : "  $coef " ;
  125         }
  126   }
  127   return($string);
  128 }
  129 
  130 sub TeXizeLowFirst {
  131   my $self = shift;
  132   my $degree = $self->degree();
  133   my $variable = $self->variable();
  134   my $coefficients = $self->coefficients();
  135   my $found = 0;
  136   my $string = "";
  137   my $coef;
  138   my $jth;
  139   $jth = 0;
  140   if (($coef = $coefficients -> [$jth]) != 0) {
  141       $found=1;
  142       $string .= $coef;
  143   }
  144   foreach my $i (1..$degree) {
  145     $jth = $i;
  146     $vterm = ($jth == 1) ? "$variable" : "$variable^{${jth}}";
  147     if (($coef = $coefficients -> [$jth]) != 0)
  148       {if ($found) {
  149         $string .=
  150           ($coef == 1) ? " + $vterm"
  151           : ($coef > 0) ? " + $coef $vterm"
  152             : ($coef == -1) ? " - $vterm"
  153               : "  $coef $vterm";
  154         }
  155       else {
  156         $found=1;
  157         $string .=
  158           ($coef == 1) ? " $vterm"
  159           : ($coef > 0) ? " $coef $vterm"
  160             : ($coef == -1) ? " - $vterm"
  161               : "  $coef $vterm";
  162         }
  163     }
  164   }
  165   return($string);
  166 }
  167 
  168 
  169 sub perlizeHighFirst {
  170   my $self = shift;
  171   my $degree = $self->degree();
  172   my $variable = $self->variable();
  173   my $coefficients = $self->coefficients();
  174   my $found = 0;
  175   my $string = "";
  176   my $coef;
  177   my $jth;
  178   foreach my $i (0..$degree-1) {
  179     $jth = $degree - $i;
  180     $vterm = ($jth == 1) ? "$variable" : "$variable**(${jth})";
  181     if (($coef = $coefficients -> [$jth]) != 0)
  182       {if ($found) {
  183         $string .=
  184           ($coef == 1) ? " + $vterm"
  185           : ($coef > 0) ? " + $coef * $vterm"
  186             : ($coef == -1) ? " - $vterm"
  187               : "  $coef * $vterm";
  188         }
  189       else {
  190         $found=1;
  191         $string .=
  192           ($coef == 1) ? " $vterm"
  193           : ($coef > 0) ? " $coef* $vterm"
  194             : ($coef == -1) ? " - $vterm"
  195               : "  $coef * $vterm";
  196         }
  197     }
  198   }
  199   $jth = 0;
  200   if (($coef = $coefficients -> [$jth]) != 0)
  201       {if ($found) {
  202         $string .= ($coef > 0) ? " + $coef "
  203           : "  $coef " ;
  204         }
  205       else {
  206         $found=1;
  207         $string .= ($coef > 0) ? " $coef "
  208           : "  $coef " ;
  209         }
  210   }
  211   return($string);
  212 }
  213 
  214 sub ith_symmetric_function {
  215   my ($K, $i, $arrayref) = @_;
  216   my $N = scalar(@$arrayref);
  217   if ($K < 0 or $i < 0 or $K > $N or $i > $N - $K )
  218     {return(0);}
  219   elsif ($K == 0)
  220     {return(1);}
  221   elsif ($K == 1)
  222     {return($arrayref -> [$i]);}
  223   else {
  224     my $sum = 0;
  225     foreach my $j ($i+1 .. $N-($K-1)) {
  226       $sum += ith_symmetric_function($K-1, $j, $arrayref);
  227     }
  228     return(($arrayref -> [$i]) * $sum);
  229   }
  230 }
  231 
  232 sub symmetric_function {
  233   my ($K, $arrayref) = @_;
  234   my $N = scalar(@$arrayref);
  235   if ($K < 0 or $K > $N) {return(0);}
  236   if ($K == 0) {return(1);}
  237   my $sum=0;
  238   foreach my $i (0..$N-$K) {
  239     $sum += ith_symmetric_function($K, $i, $arrayref);
  240   }
  241   return($sum);
  242 }
  243 
  244 sub changeVar {
  245   my $self = shift;
  246   my $newVar = shift;
  247   if (!defined($newVar)) {return(undef());}
  248   my $coefficients=$self->coefficients;
  249   my $newP = givenVariableAndCoefficients Polynomial($newVar, @{$coefficients});
  250   return($newP);
  251 }
  252 
  253 sub scalarMult {
  254   my $self = shift;
  255   my $scalar = shift;
  256   if (!(defined($scalar))) {return(undef());}
  257   my $variable = $self -> variable;
  258   my $coefficients = $self -> coefficients;
  259   my $degree = $self -> degree;
  260   my @scalarMultCoeffs = ();
  261   foreach my $i (0..$degree) {
  262     $scalarMultCoeffs[$i] = $scalar * $coefficients->[$i];
  263   }
  264   my $scalarM = givenVariableAndCoefficients Polynomial($variable, @scalarMultCoeffs);
  265   return($scalarM);
  266 }
  267 
  268 sub polyAddLike {
  269   my $self = shift;
  270   my $other = shift;
  271   if (!(ref($other) eq "Polynomial")) { return(undef());}
  272   if (($self -> variable) ne ($other -> variable)) {return(undef());}
  273   my $variable = $self->variable;
  274   my $mycoefficients = $self->coefficients;
  275   my $othercoefficients = $other -> coefficients;
  276   my $myDegree = $self -> degree;
  277   my $otherDegree = $other -> degree;
  278   my $degree = ($myDegree >= $otherDegree) ? $myDegree : $otherDegree;
  279   my @coefficients;
  280   foreach my $i (0..$degree) {
  281     $coefficients[$i] = ( (defined($mycoefficients->[$i])) ? $mycoefficients->[$i] : 0 )
  282                       + ( (defined($othercoefficients->[$i])) ? $othercoefficients->[$i] : 0 );
  283   }
  284   my $sum = givenVariableAndCoefficients Polynomial($variable, @coefficients);
  285   return($sum);
  286 }
  287 
  288 
  289 sub polyMultLike {
  290   my $self = shift;
  291   my $other = shift;
  292   if (!(ref($other) eq "Polynomial")) { return(undef());}
  293   if (($self -> variable) ne ($other -> variable)) {return(undef());}
  294   my $variable = $self->variable;
  295   my $mycoefficients = $self->coefficients;
  296   my $othercoefficients = $other -> coefficients;
  297   my $myDegree = $self -> degree;
  298   my $otherDegree = $other -> degree;
  299   my $degree = $myDegree+$otherDegree;
  300   my @coefficients;
  301   foreach my $i (0..$degree) {
  302     $coefficients[$i]=0;
  303     my $iB = ($i > $otherDegree ) ? $i-$otherDegree : 0;
  304     my $iT = ($i > $myDegree ) ? $myDegree : $i;
  305     foreach my $j ($iB .. $iT) {
  306       $coefficients[$i] += $mycoefficients->[$j] * $othercoefficients->[$i-$j];
  307     }
  308   }
  309   my $polyM = givenVariableAndCoefficients Polynomial($variable, @coefficients);
  310   return($polyM);
  311 }
  312 
  313 
  314 sub derivative {
  315   my $self = shift;
  316   my $variable = shift;
  317   if (!(defined($variable))) { $variable = $self -> variable;}
  318   my $degree = $self -> degree;
  319   my $coefficients = $self->coefficients;
  320   my @derivativeCoeffs = ();
  321   foreach my $i (1..$degree) {
  322     $derivativeCoeffs[$i-1] = $i * $coefficients->[$i];
  323   }
  324   my $deriv = givenVariableAndCoefficients Polynomial($variable, @derivativeCoeffs);
  325   return($deriv);
  326 }
  327 
  328 
  329 sub antiDerivative {
  330   my $self = shift;
  331   my $variable = shift;
  332   if (!(defined($variable))) { $variable = $self -> variable;}
  333   my $degree = $self -> degree;
  334   my $coefficients = $self->coefficients;
  335   my @antiDerivativeCoeffs = ();
  336   $antiDerivativeCoeffs[0] = 0;
  337   foreach my $i (0..$degree) {
  338     $antiDerivativeCoeffs[$i+1] =  ($coefficients->[$i])/($i+1);
  339   }
  340   my $antiDeriv = givenVariableAndCoefficients Polynomial($variable, @antiDerivativeCoeffs);
  341   return($antiDeriv);
  342 }
  343 
  344 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9