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

Annotation of /trunk/pg/lib/Parser/BOP/power.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2678 - (view) (download) (as text)

1 : sh002i 2558 #########################################################################
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 : dpvc 2678 my $self = shift; my $equation = $self->{equation};
37 :     my $parser = $equation->{context}{parser};
38 :     return $parser->{Number}->new($equation,1)
39 : sh002i 2558 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 : dpvc 2678 $self = $parser->{BOP}->new($equation,'/',
43 :     $parser->{Number}->new($equation,1),$self->{lop});
44 : sh002i 2558 $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) = @_;
54 :     my $TeX; my $bop = $self->{def};
55 :     my $symbol = (defined($bop->{TeX}) ? $bop->{TeX} : $bop->{string});
56 :     $self->{lop}->TeX($bop->{precedence},$bop->{leftparens},'left').
57 :     $symbol.'{'.$self->{rop}->TeX.'}';
58 :     }
59 :    
60 :     #########################################################################
61 :    
62 :     1;
63 :    

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9