From: dpvc v. a. <we...@ma...> - 2005-10-16 03:38:01
|
Log Message: ----------- In the past, when Value objects were inserted into strings, they would automatically include parentheses so that if you had $f equal to 1+x and $g equal to 1-x, then Formula("$f/$g") would mean (1+x)/(1-x) rather than 1+(x/1)-x, which is what would happen as a straing string substitution. The problem is that this would also happen for real numbers, vectors, and everything else, even when it wasn't necessary. So if $x=Real(3), then "Let x = $x" would be "Let x = (3)". I have changed the behavior of the string concatenation for Value objects so that parentheses are only added in a few cases: for Formulas, Complex numbers, and Unions. This makes the other Value objects work more like regular variables in strings, but might cause some problems with strings that are used as formulas. For example, if $a = Real(-3), then "x + 2 $a" will become "x + 2 -3", or "x-1" rather than the expected "x - 6". (The old approach would have made it "x + 2 (-3)" which would have worked properly). For the most part, it is easier to use something like "x + 2*$a" or even "x" + 2*$a in this case, so the extra trouble of having to avoid parentheses when you really meant to substitute the value into a string didn't seem worth it. Modified Files: -------------- pg/lib: Value.pm pg/lib/Value: Complex.pm Formula.pm Infinity.pm Interval.pm List.pm Matrix.pm Point.pm Real.pm Set.pm String.pm Union.pm Vector.pm Revision Data ------------- Index: Value.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value.pm,v retrieving revision 1.49 retrieving revision 1.50 diff -Llib/Value.pm -Llib/Value.pm -u -r1.49 -r1.50 --- lib/Value.pm +++ lib/Value.pm @@ -90,7 +90,7 @@ '*' => 'mult', '/' => 'div', '**' => 'power', - '.' => '_dot', # see _dot below + '.' => '_dot', # see _dot below 'x' => 'cross', '<=>' => 'compare', 'cmp' => 'compare_string', @@ -453,9 +453,9 @@ # sub _dot { my ($l,$r,$flag) = @_; - return Value::_dot($r,$l,!$flag) if ($l->promotePrecedence($r)); + return $r->_dot($l,!$flag) if ($l->promotePrecedence($r)); return $l->dot($r,$flag) if (Value::isValue($r)); - $l = $l->stringify; $l = '('.$l.')' unless $$Value::context->flag('StringifyAsTeX'); + if ($$Value::context->flag('StringifyAsTeX')) {$l = $l->TeX} else {$l = $l->pdot} return ($flag)? ($r.$l): ($l.$r); } # @@ -464,12 +464,18 @@ sub dot { my ($l,$r,$flag) = @_; my $tex = $$Value::context->flag('StringifyAsTeX'); - $l = $l->stringify; $l = '('.$l.')' if $tex; - if (ref($r)) {$r = $r->stringify; $r = '('.$l.')' if $tex} + if ($tex) {$l = $l->TeX} else {$l = $l->pdot} + if (ref($r)) {if ($tex) {$r = $r->TeX} else {$r = $r->pdot}} return ($flag)? ($r.$l): ($l.$r); } # +# Some classes override this to add parens +# +sub pdot {shift->stringify} + + +# # Compare the values of the objects # (list classes should replace this) # @@ -584,7 +590,7 @@ $message = [$message,@_] if scalar(@_) > 0; $$context->setError($message,''); $message = $$context->{error}{message}; - die $message . traceback() if $$context->{debug}; + die $message . traceback() if $$context->flags('showTraceback'); die $message . getCaller(); } Index: Real.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Real.pm,v retrieving revision 1.19 retrieving revision 1.20 diff -Llib/Value/Real.pm -Llib/Value/Real.pm -u -r1.19 -r1.20 --- lib/Value/Real.pm +++ lib/Value/Real.pm @@ -16,7 +16,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, @@ -224,4 +224,3 @@ ########################################################################### 1; - Index: Complex.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Complex.pm,v retrieving revision 1.17 retrieving revision 1.18 diff -Llib/Value/Complex.pm -Llib/Value/Complex.pm -u -r1.17 -r1.18 --- lib/Value/Complex.pm +++ lib/Value/Complex.pm @@ -13,7 +13,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, @@ -336,6 +336,13 @@ ################################################## +sub pdot { + my $self = shift; + my $z = $self->stringify; + return $z if $z !~ /[-+]/; + return "($z)"; +} + sub string {my $self = shift; Value::Complex::format(@{$self->data},'string',@_)} sub TeX {my $self = shift; Value::Complex::format(@{$self->data},'TeX',@_)} Index: List.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/List.pm,v retrieving revision 1.18 retrieving revision 1.19 diff -Llib/Value/List.pm -Llib/Value/List.pm -u -r1.18 -r1.19 --- lib/Value/List.pm +++ lib/Value/List.pm @@ -11,7 +11,7 @@ use overload '+' => sub {shift->add(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, Index: String.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/String.pm,v retrieving revision 1.8 retrieving revision 1.9 diff -Llib/Value/String.pm -Llib/Value/String.pm -u -r1.8 -r1.9 --- lib/Value/String.pm +++ lib/Value/String.pm @@ -8,7 +8,7 @@ @ISA = qw(Value); use overload - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare(@_)}, 'nomethod' => sub {shift->nomethod(@_)}, Index: Union.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Union.pm,v retrieving revision 1.23 retrieving revision 1.24 diff -Llib/Value/Union.pm -Llib/Value/Union.pm -u -r1.23 -r1.24 --- lib/Value/Union.pm +++ lib/Value/Union.pm @@ -10,7 +10,7 @@ use overload '+' => sub {shift->add(@_)}, '-' => sub {shift->sub(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, @@ -298,6 +298,8 @@ # Generate the various output formats # +sub pdot {'('.(shift->stringify).')'} + sub stringify { my $self = shift; return $self->TeX if $$Value::context->flag('StringifyAsTeX'); Index: Infinity.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Infinity.pm,v retrieving revision 1.8 retrieving revision 1.9 diff -Llib/Value/Infinity.pm -Llib/Value/Infinity.pm -u -r1.8 -r1.9 --- lib/Value/Infinity.pm +++ lib/Value/Infinity.pm @@ -8,7 +8,7 @@ @ISA = qw(Value); use overload - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, 'neg' => sub {shift->neg(@_)}, Index: Vector.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Vector.pm,v retrieving revision 1.19 retrieving revision 1.20 diff -Llib/Value/Vector.pm -Llib/Value/Vector.pm -u -r1.19 -r1.20 --- lib/Value/Vector.pm +++ lib/Value/Vector.pm @@ -15,7 +15,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, Index: Set.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Set.pm,v retrieving revision 1.9 retrieving revision 1.10 diff -Llib/Value/Set.pm -Llib/Value/Set.pm -u -r1.9 -r1.10 --- lib/Value/Set.pm +++ lib/Value/Set.pm @@ -10,7 +10,7 @@ use overload '+' => sub {shift->add(@_)}, '-' => sub {shift->sub(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, Index: Formula.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Formula.pm,v retrieving revision 1.38 retrieving revision 1.39 diff -Llib/Value/Formula.pm -Llib/Value/Formula.pm -u -r1.38 -r1.39 --- lib/Value/Formula.pm +++ lib/Value/Formula.pm @@ -17,7 +17,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => sub {shift->dot(@_)}, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, @@ -129,14 +129,16 @@ # # Make dot work for vector operands # -sub dot { +sub _dot { my ($l,$r,$flag) = @_; - if ($l->promotePrecedence($r)) {return $r->dot($l,!$flag)} + if ($l->promotePrecedence($r)) {return $r->_dot($l,!$flag)} return bop('.',@_) if $l->type eq 'Vector' && Value::isValue($r) && $r->type eq 'Vector'; - Value::_dot(@_); + $l->SUPER::_dot($r,$flag); } +sub pdot {'('.(shift->stringify).')'} + # # Call the Parser::Function call function # @@ -155,7 +157,6 @@ $formula->{context} = $self->{context}; $formula->{variables} = $self->{variables}; $formula->{tree} = $formula->{context}{parser}{UOP}->new($formula,'u-',$self->{tree}->copy($formula)); -# return $formula->eval if $formula->isConstant; return $formula; } Index: Interval.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Interval.pm,v retrieving revision 1.27 retrieving revision 1.28 diff -Llib/Value/Interval.pm -Llib/Value/Interval.pm -u -r1.27 -r1.28 --- lib/Value/Interval.pm +++ lib/Value/Interval.pm @@ -12,7 +12,7 @@ use overload '+' => sub {shift->add(@_)}, '-' => sub {shift->sub(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, Index: Matrix.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Matrix.pm,v retrieving revision 1.20 retrieving revision 1.21 diff -Llib/Value/Matrix.pm -Llib/Value/Matrix.pm -u -r1.20 -r1.21 --- lib/Value/Matrix.pm +++ lib/Value/Matrix.pm @@ -17,7 +17,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compare_string(@_)}, Index: Point.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Point.pm,v retrieving revision 1.17 retrieving revision 1.18 diff -Llib/Value/Point.pm -Llib/Value/Point.pm -u -r1.17 -r1.18 --- lib/Value/Point.pm +++ lib/Value/Point.pm @@ -15,7 +15,7 @@ '*' => sub {shift->mult(@_)}, '/' => sub {shift->div(@_)}, '**' => sub {shift->power(@_)}, - '.' => \&Value::_dot, + '.' => sub {shift->_dot(@_)}, 'x' => sub {shift->cross(@_)}, '<=>' => sub {shift->compare(@_)}, 'cmp' => sub {shift->compate_string(@_)}, |