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

View of /trunk/pg/lib/Parser/Number.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, 3 months ago) by dpvc
File size: 1641 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 Number class
    4 #
    5 package Parser::Number;
    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   return Parser::Complex->new($equation,$value,$ref) if (ref($value) eq 'ARRAY');
   14   $value = $value->value while Value::isReal($value);
   15   $value = $value + 0; # format the value as a number
   16   $num = bless {
   17     value => $value, type => $Value::Type{number}, isConstant => 1,
   18     ref => $ref, equation => $equation,
   19   }, $class;
   20   my $x = Value::Real->make($value);
   21   $num->{isOne}  = 1 if $x eq 1;
   22   $num->{isZero} = 1 if $x == 0;
   23   return $num;
   24 }
   25 
   26 #
   27 #  We know the answers to these, so no need to compute them
   28 #
   29 sub isComplex {0}
   30 sub isNumber {1}
   31 sub isRealNumber {1}
   32 
   33 #
   34 #  Return the value
   35 #
   36 sub eval {(shift)->{value}}
   37 
   38 #
   39 #  If the number is negative, factor it out and
   40 #    try using that in the reductions of the parent objects.
   41 #
   42 sub reduce {
   43   my $self = shift;
   44   if ($self->{value} < 0) {
   45     $self->{value} = -($self->{value});
   46     $self = Parser::UOP::Neg($self);
   47     $self->{op}{isOne} = 1 if Value::Real->make($self->{op}{value}) eq 1;
   48   }
   49   return $self;
   50 }
   51 
   52 #
   53 #  Call the Value::Real versions to format numbers
   54 #
   55 sub string {
   56   my $self = shift;
   57   Value::Real->make($self->{value})->string($self->{equation},@_);
   58 }
   59 sub TeX {
   60   my $self = shift;
   61   Value::Real->make($self->{value})->TeX($self->{equation},@_);
   62 }
   63 sub perl {shift->{value}}
   64 
   65 #########################################################################
   66 
   67 1;
   68 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9