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

View of /trunk/pg/lib/Parser/BOP/underscore.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: 3377 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 #  Implement vector and matrix element extraction.
    4 #
    5 package Parser::BOP::underscore;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::BOP);
    8 
    9 #
   10 #  Check that the operand types are OK
   11 #
   12 sub _check {
   13   my $self = shift;
   14   return if ($self->checkStrings());
   15   my ($ltype,$rtype) = $self->promotePoints();
   16   if ($ltype->{name} =~ m/Vector|Matrix|List/) {
   17     if ($rtype->{name} =~ m/Number|Vector/ ||
   18   ($rtype->{name} eq 'List' && $rtype->{entryType}{name} eq 'Number')) {
   19       $self->{type} = {%{$ltype}};
   20       $self->{type}{length} = $rtype->{length};
   21     } else {$self->Error("Right-hand operand of '_' must be a Number or List of numbers")}
   22   } else {$self->Error("Entries can be extracted only from Vectors, Matrices, or Lists")}
   23 }
   24 
   25 #
   26 #  Perform the extraction.
   27 #
   28 sub _eval {
   29   shift; my $M = shift; my $i = shift;
   30   $i = $i->data if Value::isValue($i);
   31   $i = [$i] unless ref($i) eq 'ARRAY';
   32   my $n = $M->extract(@{$i});
   33   return $n if ref($n);
   34   return Value::List->new() if $n eq '';
   35   return $n;
   36 }
   37 
   38 #
   39 #  If the right-hand side is constant and the left is a list
   40 #    extact the given coordinate(s).  Return empty lists
   41 #    if we run past the end of the coordinates.  Return
   42 #    a simpler extraction if a portion of the extraction
   43 #    can be performed.
   44 #
   45 sub _reduce {
   46   my $self = shift; my $equation = $self->{equation};
   47   my $parser = $equation->{context}{parser};
   48   return $self unless $self->{rop}->{isConstant} && $self->{lop}{coords};
   49   my $index = $self->{rop}->eval; my $M = $self->{lop};
   50   $index = $index->data if Value::isValue($index);
   51   $index = [$index] unless ref($index) eq 'ARRAY';
   52   my @index = @{$index};
   53   while (scalar(@index) > 0) {
   54     unless ($M->{coords}) {
   55       return $parser->{Value}->new($equation,Value::List->new())
   56         unless $M->type =~ m/Point|Vector|Matrix|List/;
   57       return $parser->{BOP}->new($equation,$self->{bop},
   58           $M,$parser->{Value}->new($equation,@index))
   59     }
   60     my $i = shift(@index); $i-- if $i > 0;
   61     $self->Error("Can't extract element number '$i' (index must be an integer)")
   62       unless $i =~ m/^-?\d+$/;
   63     $M = $M->{coords}[$i];
   64     return $parser->{Value}->new($equation,Value::List->new()) unless $M;
   65   }
   66   return $M;
   67 }
   68 
   69 #
   70 #  Brace the index for TeX.  (Not really good for multiple indices.)
   71 #
   72 sub TeX {
   73   my ($self,$precedence,$showparens,$position,$outerRight) = @_;
   74   my $TeX; my $bop = $self->{def};
   75   my $addparens =
   76       defined($precedence) &&
   77       ($showparens eq 'all' || $precedence > $bop->{precedence} ||
   78       ($precedence == $bop->{precedence} &&
   79         ($bop->{associativity} eq 'right' || $showparens eq 'same')));
   80   my $outerRight = !$addparens && ($outerRight || $position eq 'right');
   81 
   82   my $symbol = (defined($bop->{TeX}) ? $bop->{TeX} : $bop->{string});
   83   $TeX = $self->{lop}->TeX($bop->{precedence},$bop->{leftparens},'left',$outerRight).
   84     $symbol.'{'.$self->{rop}->TeX.'}';
   85 
   86   $TeX = '\left('.$TeX.'\right)' if $addparens;
   87   return $TeX;
   88 }
   89 
   90 #
   91 #  Perl used extract method of the Value::List object.
   92 #
   93 sub perl {
   94   my ($self,$precedence,$showparens,$position) = @_;
   95   my $bop = $self->{def};
   96   $self->{lop}->perl($bop->{precedence},$bop->{leftparens},'left').
   97     '->extract('.$self->{rop}->perl.')';
   98 }
   99 
  100 #########################################################################
  101 
  102 1;
  103 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9