[system] / trunk / pg / lib / Value / Infinity.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Value/Infinity.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2800 - (download) (as text) (annotate)
Sun Sep 19 14:27:39 2004 UTC (15 years, 5 months ago) by dpvc
File size: 1923 byte(s)
Added isZero and isOne checks for Parser::Value objects (i.e., for
constants within formulas).  These now correctly handle vector and
matrices, in particular.  The isOne and isZero checks are used in the
reduce() method to simplify formulas.

    1 ###########################################################################
    2 
    3 package Value::Infinity;
    4 my $pkg = 'Value::Infinity';
    5 
    6 use strict;
    7 use vars qw(@ISA);
    8 @ISA = qw(Value);
    9 
   10 use overload
   11        '.'   => \&Value::_dot,
   12        '<=>' => \&compare,
   13        'cmp' => \&Value::cmp,
   14        'neg' => \&neg,
   15   'nomethod' => \&Value::nomethod,
   16         '""' => \&Value::stringify;
   17 
   18 #
   19 #  Create an infinity object
   20 #
   21 sub new {
   22   my $self = shift; my $class = ref($self) || $self;
   23   Value::Error("Infinity should have no parameters") if scalar(@_);
   24   bless {
   25     data => [$$Value::context->flag('infiniteWord')],
   26     isInfinite => 1, isNegative => 0,
   27   }, $class;
   28 }
   29 
   30 #
   31 #  Return the appropriate data.
   32 #
   33 sub length {0}
   34 sub typeRef {$Value::Type{infinity}}
   35 sub value {shift->{data}[0]}
   36 
   37 sub isZero {0}
   38 sub isOne {0}
   39 
   40 ##################################################
   41 
   42 #
   43 #  Return an infinity or real
   44 #
   45 sub promote {
   46   my $x = shift; $x = [$x,@_] if scalar(@_) > 0;
   47   $x = Value::makeValue($x);
   48   return $x if ref($x) eq $pkg || Value::isReal($x);
   49   Value::Error("Can't convert '$x' to Infinity");
   50 }
   51 
   52 ############################################
   53 #
   54 #  Operations on Infinities
   55 #
   56 
   57 sub neg {
   58   my $self = shift;
   59   my $neg = Value::Infinity->new();
   60   $neg->{isNegative} = !$self->{isNegative};
   61   $neg->{data}[0] = '-'.$neg->{data}[0] if $neg->{isNegative};
   62   return $neg;
   63 }
   64 
   65 sub compare {
   66   my ($l,$r,$flag) = @_;
   67   if ($l->promotePrecedence($r)) {return $r->compare($l,!$flag)}
   68   $r = promote($r); my $sgn = ($flag ? -1: 1);
   69   return 0 if $r->class ne 'Real' && $l->{isNegative} == $r->{isNegative};
   70   return ($l->{isNegative}? -$sgn: $sgn);
   71 }
   72 
   73 ############################################
   74 #
   75 #  Generate the various output formats
   76 #
   77 
   78 sub TeX {(shift->{isNegative} ? '-\infty': '\infty ')}
   79 sub perl {(shift->{isNegative} ? '-(Infinity)': '(Infinity)')}
   80 
   81 ###########################################################################
   82 
   83 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9