From: dpvc v. a. <we...@ma...> - 2007-09-20 22:53:57
|
Log Message: ----------- Adds a new context that is more limited by not allowing operations within the coefficients and exponents themselves. Access this context using Context("LimitedPolynomial-Strict"); Modified Files: -------------- pg/macros: contextLimitedPolynomial.pl Revision Data ------------- Index: contextLimitedPolynomial.pl =================================================================== RCS file: /webwork/cvs/system/pg/macros/contextLimitedPolynomial.pl,v retrieving revision 1.13 retrieving revision 1.14 diff -Lmacros/contextLimitedPolynomial.pl -Lmacros/contextLimitedPolynomial.pl -u -r1.13 -r1.14 --- macros/contextLimitedPolynomial.pl +++ macros/contextLimitedPolynomial.pl @@ -7,19 +7,29 @@ ########################################################## # - # Implements a context in which students can only - # enter (expanded) polynomials (i.e., sums of multiples - # of powers of x). + # Implements a context in which students can only enter (expanded) + # polynomials (i.e., sums of multiples of powers of x). # # Select the context using: # # Context("LimitedPolynomial"); # - # If you set the "singlePowers" flag, then only one monomial of - # each degree can be included in the polynomial: + # If you set the "singlePowers" flag, then only one monomial of each + # degree can be included in the polynomial: # # Context("LimitedPolynomial")->flags->set(singlePowers=>1); # + # There is also a strict limited context that does not allow + # operations even within the coefficients. Select it using: + # + # Context("LimitedPolynomial-Strict"); + # + # In addition to disallowing operations within the coefficients, + # this context does not reduce constant operations (since they are + # not allowed), and sets the singlePowers flag automatically. In + # addition, it disables all the functions, though they can be + # re-enabled, if needed. + # =cut @@ -38,8 +48,11 @@ my $self = shift; my $super = ref($self); $super =~ s/LimitedPolynomial/Parser/; &{$super."::_check"}($self); - return if LimitedPolynomial::isConstant($self->{lop}) && - LimitedPolynomial::isConstant($self->{rop}); + if (LimitedPolynomial::isConstant($self->{lop}) && + LimitedPolynomial::isConstant($self->{rop})) { + $self->checkStrict if $self->context->flag("strictCoefficients"); + return; + } return if $self->checkPolynomial; $self->Error("Your answer doesn't look like a polynomial"); } @@ -95,6 +108,15 @@ return 1; } +# +# Report an error when both operands are constants +# and strictCoefficients is in effect. +# +sub checkStrict { + my $self = shift; + $self->Error("Can't use '%s' between constants",$self->{bop}); +} + ################################################## package LimitedPolynomial; @@ -157,11 +179,15 @@ sub checkPolynomial { my $self = shift; my ($l,$r) = ($self->{lop},$self->{rop}); - $self->Error("Addition is allowed only between monomials") - if $r->{isPoly}; + $self->Error("Addition is allowed only between monomials") if $r->{isPoly}; $self->checkPowers; } +sub checkStrict { + my $self = shift; + $self->Error("You can only use addition for the terms of a polynomial",$self->{bop}); +} + ############################################## package LimitedPolynomial::BOP::subtract; @@ -170,11 +196,15 @@ sub checkPolynomial { my $self = shift; my ($l,$r) = ($self->{lop},$self->{rop}); - $self->Error("Subtraction is only allowed between monomials") - if $r->{isPoly}; + $self->Error("Subtraction is allowed only between monomials") if $r->{isPoly}; $self->checkPowers; } +sub checkStrict { + my $self = shift; + $self->Error("You can only use subtraction between the terms of a polynomial",$self->{bop}); +} + ############################################## package LimitedPolynomial::BOP::multiply; @@ -192,6 +222,11 @@ $self->Error("Multiplication can only be used between coefficients and variables"); } +sub checkStrict { + my $self = shift; + $self->Error("You can only use '%s' between a coefficent and a variable in a polynomial",$self->{bop}); +} + ############################################## package LimitedPolynomial::BOP::divide; @@ -210,6 +245,11 @@ return 1; } +sub checkStrict { + my $self = shift; + $self->Error("You can only use '%s' to form fractions",$self->{bop}) if $self->{lop}->class eq 'BOP'; +} + ############################################## package LimitedPolynomial::BOP::power; @@ -234,6 +274,11 @@ return 1; } +sub checkStrict { + my $self = shift; + $self->Error("You can only use powers of a variable in a polynomial"); +} + ############################################## ############################################## # @@ -366,6 +411,13 @@ # $context->reduction->set("(-x)-y"=>0); + # + # A context where coefficients can't include operations + # + $context = $main::context{"LimitedPolynomial-Strict"} = $context->copy; + $context->flags->set(strictCoefficients=>1, singelPowers=>1, reduceConstants=>0); + $context->functions->disable("All"); # can be re-enabled if needed + main::Context("LimitedPolynomial"); ### FIXME: probably should require author to set this explicitly } |