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

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

Sat Aug 28 12:34:56 2004 UTC (15 years, 4 months ago) by dpvc
File size: 2527 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 exponentiation
4 #
5 package Parser::BOP::power;
6 use strict; use vars qw(@ISA);
7 @ISA = qw(Parser::BOP);
8
9 #
10 #  Check that operand types are OK.
11 #  For non-numbers, promote to Matrix and check
12 #    that the sizes are OK and that the exponents are numbers
13 sub _check {
14   my $self = shift; 15 return if ($self->checkStrings());
16   return if ($self->checkLists()); 17 return if ($self->checkNumbers());
18   my ($ltype,$rtype) = $self->promotePoints('Matrix'); 19 if ($rtype->{name} eq 'Number') {
20     if ($ltype->{name} eq 'Matrix') {$self->checkMatrixSize($ltype,$ltype)}
21     else {$self->Error("You can only raise a Number to a power")} 22 } else {$self->Error("Exponents must be Numbers")}
23 }
24
25 #
26 #  Do perl exponentiation
27 #
28 sub _eval {$_[1] **$_[2]}
29
30 #
31 #  Return 1 for power of zero or base of 1.
32 #  Return base if power is 1.
33 #  Return 1/base if power is -1.
34 #
35 sub _reduce {
36   my $self = shift; my$equation = $self->{equation}; 37 my$parser = $equation->{context}{parser}; 38 return$parser->{Number}->new($equation,1) 39 if ($self->{rop}{isZero} || $self->{lop}{isOne}); 40 return$self->{lop} if ($self->{rop}{isOne}); 41 if ($self->{rop}->isNeg && $self->{rop}->string eq '-1') { 42$self = $parser->{BOP}->new($equation,'/',
43       $parser->{Number}->new($equation,1),$self->{lop}); 44$self = $self->reduce; 45 } 46 return$self;
47 }
48
49 #
50 #  Put exponent in braces for TeX
51 #
52 sub TeX {
53   my ($self,$precedence,$showparens,$position,$outerRight) = @_; 54 my$TeX; my $bop =$self->{def};
55   my $addparens = 56 defined($precedence) &&
57       ($showparens eq 'all' ||$precedence > $bop->{precedence} || 58 ($precedence == $bop->{precedence} && 59 ($bop->{associativity} eq 'right' || $showparens eq 'same'))); 60 my$outerRight = !$addparens && ($outerRight || $position eq 'right'); 61 62 my$symbol = (defined($bop->{TeX}) ?$bop->{TeX} : $bop->{string}); 63 if ($self->{lop}->class eq 'Function' && $self->{rop}->class eq 'Number' && 64$self->{rop}{value} > 0 && int($self->{rop}{value}) ==$self->{rop}{value}) {
65     $TeX =$self->{lop}->TeX($precedence,$showparens,$position,$outerRight,
66            $symbol.'{'.$self->{rop}->TeX.'}');
67     $addparens = 0; 68 } else { 69$TeX = $self->{lop}->TeX($bop->{precedence},$bop->{leftparens},'left',$outerRight).
70       $symbol.'{'.$self->{rop}->TeX.'}';
71   }
72
73   $TeX = '\left('.$TeX.'\right)' if $addparens; 74 return$TeX;
75 }
76
77 #########################################################################
78
79 1;
80