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

View of /trunk/pg/lib/Parser/Complex.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2625 - (download) (as text) (annotate)
Mon Aug 16 18:35:12 2004 UTC (15 years, 4 months ago) by dpvc
File size: 2344 byte(s)
Added string comparison to all Value object classes (to compare the
string value of an object to another string).

Overloaded perl '.' operator to do dot product when the operands are
formulas returning vectors.  (Part of the auto-generation of
formulas).

A few improvements to real and complex class output results.

Made Union class slightly more robust and removed need for makeUnion
method other than in the Union itself.

    1 #########################################################################
    2 #
    3 #  Implements the Complex class
    4 #
    5 package Parser::Complex;
    6 use strict; use vars qw(@ISA);
    7 @ISA = qw(Parser::Item);
    8 
    9 sub new {
   10   my $self = shift; my $class = ref($self) || $self;
   11   my $equation = shift; my $num;
   12   my ($value,$ref) = @_;
   13   $value = [$value,0] unless ref($value) eq 'ARRAY';
   14   $value->[1] = 0 unless defined($value->[1]);
   15   $equation->Error("Complex Numbers must have real and complex parts",$ref)
   16     if (scalar(@{$value}) != 2);
   17   $num = bless {
   18     value => $value, type => $Value::Type{complex}, isConstant => 1,
   19     ref => $ref, equation => $equation,
   20   }, $class;
   21   my $z = Value::Complex->make(@{$value});
   22   $num->{isOne}  = 1 if $z cmp 1;
   23   $num->{isZero} = 1 if $z == 0;
   24   return $num;
   25 }
   26 
   27 #
   28 #  We know the answer to these, so no need to compute them.
   29 #
   30 sub isComplex {1}
   31 sub isNumber {1}
   32 sub isRealNumber {0}
   33 
   34 #
   35 #  Use Value.pm to evaluate these
   36 #
   37 sub eval {
   38   my $self = shift;
   39   return Value::Complex->make(@{$self->{value}});
   40 }
   41 
   42 #
   43 #  Factor out a common negative.
   44 #
   45 sub reduce {
   46   my $self = shift; my ($a,$b) = @{$self->{value}};
   47   if ($a <= 0 && $b <= 0 && ($a != 0 || $b != 0)) {
   48     $self->{value} = [-$a,-$b];
   49     $self = Parser::UOP::Neg($self);
   50     $self->{isOne} = 1 if Value::Complex->make(-$a,-$b) == 1;
   51   }
   52   return $self;
   53 }
   54 
   55 #
   56 #  Use Value::Complex to format the number
   57 #  Add parens if the parent oparator has higher precedence
   58 #    than addition (and there IS an addition or subtraction).
   59 #
   60 sub string {
   61   my $self = shift; my $precedence = shift;
   62   my $plus = $self->{context}{operators}{'+'}{precedence};
   63   my $z = Value::Complex->make(@{$self->{value}})->string($self->{equation});
   64   $z = "(".$z.")" if defined($precedence) && $precedence > $plus && $z =~ m/[-+]/;
   65   return $z;
   66 }
   67 
   68 sub TeX {
   69   my $self = shift; my $precedence = shift;
   70   my $plus = $self->{context}{operators}{'+'}{precedence};
   71   my $z = Value::Complex->make(@{$self->{value}})->TeX($self->{equation});
   72   $z = '\left('.$z.'\right)' if defined($precedence) && $precedence > $plus && $z =~ m/[-+]/;
   73   return $z;
   74 }
   75 
   76 sub perl {
   77   my $self = shift; my $parens = shift;
   78   my $perl = Value::Complex->make(@{$self->{value}})->perl;
   79   $perl = '('.$perl.')' if $parens;
   80   return $perl;
   81 }
   82 
   83 #########################################################################
   84 
   85 1;
   86 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9