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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2558 - (download) (as text) (annotate)
Wed Jul 28 20:32:33 2004 UTC (15 years, 5 months ago) by sh002i
File size: 1744 byte(s)
merged changes from rel-2-1-a1 -- stop using that branch.

    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;
   37   return Parser::Number->new($self->{equation},1)
   38    if ($self->{rop}{isZero} || $self->{lop}{isOne});
   39   return $self->{lop} if ($self->{rop}{isOne});
   40   if ($self->{rop}->isNeg && $self->{rop}->string eq '-1') {
   41     $self = Parser::BOP->new($self->{equation},'/',
   42        Parser::Number->new($self->{equation},1),$self->{lop});
   43     $self = $self->reduce;
   44   }
   45   return $self;
   46 }
   47 
   48 #
   49 #  Put exponent in braces for TeX
   50 #
   51 sub TeX {
   52   my ($self,$precedence,$showparens,$position) = @_;
   53   my $TeX; my $bop = $self->{def};
   54   my $symbol = (defined($bop->{TeX}) ? $bop->{TeX} : $bop->{string});
   55   $self->{lop}->TeX($bop->{precedence},$bop->{leftparens},'left').
   56     $symbol.'{'.$self->{rop}->TeX.'}';
   57 }
   58 
   59 #########################################################################
   60 
   61 1;
   62 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9