[system] / trunk / pg / lib / Parser / BOP / divide.pm Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

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

Sun Jun 10 01:17:29 2007 UTC (6 years ago) by dpvc
File size: 2276 byte(s)
Update to handle the fact that the Value package now includes the
context with all MathObjects.  (This may still need some work.)


    1 #########################################################################
2 #
3 #  Implements division
4 #
5 package Parser::BOP::divide;
6 use strict;
7 our @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 my$reduce = $self->{equation}{context}{reduction}; 39 return$self->{lop} if $self->{rop}{isOne} &&$reduce->{'x/1'};
40   $self->Error("Division by zero"), return$self if $self->{rop}{isZero}; 41 return$self->{lop} if $self->{lop}{isZero} &&$reduce->{'0/x'};
42   return $self->makeNeg($self->{lop}{op},$self->{rop}) 43 if$self->{lop}->isNeg && $reduce->{'(-x)/y'}; 44 return$self->makeNeg($self->{lop},$self->{rop}{op})
45     if $self->{rop}->isNeg &&$reduce->{'x/(-y)'};
46   return $self; 47 } 48 49$Parser::reduce->{'x/1'} = 1;
50 $Parser::reduce->{'0/x'} = 1; 51$Parser::reduce->{'(-x)/y'} = 1;
52 $Parser::reduce->{'x/(-y)'} = 1; 53 54 # 55 # Use \frac for TeX version. 56 # 57 sub TeX { 58 my$self = shift;
59   my ($precedence,$showparens,$position,$outerRight) = @_;
60   my $TeX; my$bop = $self->{def}; 61 return$self->SUPER::TeX(@_) if $self->{def}{noFrac}; 62$showparens = '' unless defined($showparens); 63 my$addparens =
64       defined($precedence) && 65 ($showparens eq 'all' || ($precedence >$bop->{precedence} && $showparens ne 'nofractions') || 66 ($precedence == $bop->{precedence} && 67 ($bop->{associativity} eq 'right' || $showparens eq 'same'))); 68 69$TeX = '\frac{'.($self->{lop}->TeX).'}{'.($self->{rop}->TeX).'}';
70
71   $TeX = '\left('.$TeX.'\right)' if ($addparens); 72 return$TeX;
73 }
74
75 #########################################################################
76
77 1;