[system] / trunk / pg / lib / Parser / BOP / divide.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/BOP/divide.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2696 - (download) (as text) (annotate)
Sat Aug 28 12:34:56 2004 UTC (15 years, 3 months ago) by dpvc
File size: 1880 byte(s)
Improved TeX and string output in a number of situations.  Improved
use of parentheses to avoid things like x+-3.  Fixed wrong parentheses
in powers.  Display (sin(x))^2 as sin^2(x) in TeX output.

    1 #########################################################################
    2 #
    3 #  Implements division
    4 #
    5 package Parser::BOP::divide;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that operand types are OK.
   11 #  Check for division by zero.
   12 #
   13 sub _check {
   14   my $self = shift;
   15   return if ($self->checkStrings());
   16   return if ($self->checkLists());
   17   $self->Error("Division by zero") if $self->{rop}{isZero};
   18   return if ($self->checkNumbers());
   19   my ($ltype,$rtype) = $self->promotePoints();
   20   if ($ltype->{name} =~ m/Vector|Matrix/ && $rtype->{name} eq 'Number') {
   21     $self->{type} = {%{$ltype}};
   22   } else {$self->Error("Division is allowed only for Numbers or a Vector and a Number")}
   23 }
   24 
   25 #
   26 #  Do the division.
   27 #
   28 sub _eval {$_[1] / $_[2]}
   29 
   30 #
   31 #  Remove division by 1.
   32 #  Error for division by zero.
   33 #  Reduce zero divided by anything (non-zero) to zero.
   34 #  Factor out negatives.
   35 #
   36 sub _reduce {
   37   my $self = shift;
   38   return $self->{lop} if ($self->{rop}{isOne});
   39   $self->Error("Division by zero"), return $self
   40     if ($self->{rop}{isZero});
   41   return $self->{lop} if ($self->{lop}{isZero});
   42   return $self->makeNeg($self->{lop}{op},$self->{rop}) if ($self->{lop}->isNeg);
   43   return $self->makeNeg($self->{lop},$self->{rop}{op}) if ($self->{rop}->isNeg);
   44   return $self;
   45 }
   46 
   47 #
   48 #  Use \frac for TeX version.
   49 #
   50 sub TeX {
   51   my ($self,$precedence,$showparens,$position,$outerRight) = @_;
   52   my $TeX; my $bop = $self->{def};
   53   my $addparens =
   54       defined($precedence) &&
   55       ($showparens eq 'all' || $precedence > $bop->{precedence} ||
   56       ($precedence == $bop->{precedence} &&
   57         ($bop->{associativity} eq 'right' || $showparens eq 'same')));
   58 
   59   $TeX = '\frac{'.($self->{lop}->TeX).'}{'.($self->{rop}->TeX).'}';
   60 
   61   $TeX = '\left('.$TeX.'\right)' if ($addparens);
   62   return $TeX;
   63 }
   64 
   65 #########################################################################
   66 
   67 1;
   68 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9