App-Chart

 view release on metacpan or  search on metacpan

devel/ema-omitted-old.pl  view on Meta::CPAN

#!/usr/bin/perl -w

# Copyright 2007, 2008, 2009, 2010 Kevin Ryde

# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart 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.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart.  If not, see <http://www.gnu.org/licenses/>.


use 5.010;
use strict;
use warnings;
use Memoize;
use List::Util qw(min max);
use List::MoreUtils;
use Math::BigRat try => 'GMP';
use lib '/home/gg/perl/math-polynomial/004-modified';
use Math::Polynomial;

use constant DEBUG => 0;
$| = 1;


#------------------------------------------------------------------------------
Math::Polynomial->verbose(1);
Math::Polynomial->configure (VARIABLE => '$x');

package Math::Polynomial;
use List::Util qw(min max);

use overload '==' => \&equal, '!=' => \&not_equal;
sub not_equal { return ! equal(@_); }
sub equal {
  my ($left, $right) = @_;

  unless (ref($right) && $right->isa('Math::Polynomial')) {
    $right = [ $right ];
  }
  foreach my $i (0 .. max ($#$left, $#$right)) {
    unless (($i < @$left ? $left->[$#$left-$i] : 0)
            ==
            ($i < @$right ? $right->[$#$right-$i] : 0)) {
      return 0;
    }
  }
  return 1;
}

my %CONFIG = (PLUS => ' + ',
	      MINUS => ' - ',
	      TIMES => '*',
	      POWER => '**',
	      VARIABLE => '$x');

sub to_string_for_eval {
  my ($poly) = @_;
  my $i = $poly->degree;
  if ($i < 0) {
    return '0';
  }

  # build in @ret to avoid O(N^2) string copying from a ".=" append
  my @ret = (undef);
  my $need_parens = 0;
  my $open_parens = 0;
  my $times = '';
  my $xpow = 0;

  {
    my $coeff = $poly->coeff($i);
    if ($coeff == 1 && $i > 0) {
      # just "$X" to start
    } elsif ($coeff == -1 && $i > 0) {
      my $minus = $CONFIG{MINUS};
      $minus =~ tr/ //d; # no spaces on leading '-$X'
      push @ret, $minus;
    } else {
      push @ret, "$coeff";  # stringize
      $times = $CONFIG{TIMES};
    }
  }

  for (;;) {
    $i--;
    my $coeff;
    if ($i >= 0) {
      $xpow++;
      $coeff = $poly->coeff($i);
      if ($coeff == 0) {
        next;
      }
    }



( run in 2.375 seconds using v1.01-cache-2.11-cpan-2398b32b56e )