[system] / trunk / pg / macros / parserDifferenceQuotient.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/parserDifferenceQuotient.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3867 - (download) (as text) (annotate)
Sat Dec 31 02:41:16 2005 UTC (14 years, 1 month ago) by dpvc
File size: 2491 byte(s)
Now that the parser accepts multi-character variables, there is no
need for the special treatment of dx.  Also, allow the context to
specify the default variable to use, and if not available, take the
LAST varaible alphabetically, not the first.  (x is more likely to be
last, and the author might have added dx as a variable so that it will
not cause an error if used in other answers in the problem, for
example.)

    1 loadMacros('Parser.pl');
    2 
    3 sub _parserDifferenceQuotient_init {}; # don't reload this file
    4 
    5 ######################################################################
    6 #
    7 #  This is a Parser class that implements an answer checker for
    8 #  difference quotients as a subclass of the Formula class.  The
    9 #  standard ->cmp routine will work for this.  The difference quotient
   10 #  is just a special type of formula with a special variable
   11 #  for 'dx'.  The checker will give an error message if the
   12 #  student's result contains a dx in the denominator, meaning it
   13 #  is not fully reduced.
   14 #
   15 #  Use DifferenceQuotient(formula) to create a difference equation
   16 #  object.  If the context has more than one variable, the last one
   17 #  alphabetically is used to form the dx.  Otherwise, you can specify
   18 #  the variable used for dx as the second argument to
   19 #  DifferenceQuotient().  You could use a variable like h instead of
   20 #  dx if you prefer.
   21 #
   22 #  Usage examples:
   23 #
   24 #      $df = DifferenceQuotient("2x+dx");
   25 #      ANS($df->cmp);
   26 #
   27 #      $df = DifferenceQuotient("2x+h","h");
   28 #      ANS($df->cmp);
   29 #
   30 #      Context()->variables->are(t=>'Real',a=>'Real');
   31 #      ANS(DifferenceQuotient("-a/[t(t+dt)]","dt")->cmp);
   32 #
   33 
   34 Context("Numeric");
   35 
   36 sub DifferenceQuotient {new DifferenceQuotient(@_)}
   37 
   38 package DifferenceQuotient;
   39 our @ISA = qw(Value::Formula);
   40 
   41 sub new {
   42   my $self = shift; my $class = ref($self) || $self;
   43   my $formula = shift; my $current = $$Value::context;
   44   my $dx = shift || $current->flag('diffQuotientVar') || 'd'.($current->variables->names)[-1];
   45   #
   46   #  Save the original context, and make a copy to which we
   47   #  add a variable for 'dx'
   48   #
   49   my $context = main::Context($current->copy);
   50   $context->variables->add($dx=>'Real') unless ($context->variables->get($dx));
   51   $q = bless $self->SUPER::new($formula), $class;
   52   $q->{isValue} = 1; $q->{isFormula} = 1; $q->{dx} = $dx;
   53   main::Context($current);  # put back the original context;
   54   return $q;
   55 }
   56 
   57 sub cmp_class {'a Difference Quotient'}
   58 
   59 sub cmp_defaults{(
   60   shift->SUPER::cmp_defaults,
   61   ignoreInfinity => 0,
   62 )}
   63 
   64 sub cmp_postprocess {
   65   my $self = shift; my $ans = shift; my $dx = $self->{dx};
   66   return if $ans->{score} == 0 || $ans->{isPreview};
   67   $main::__student_value__ = $ans->{student_value};
   68   my ($value,$err) = main::PG_restricted_eval('$__student_value__->substitute('.$dx.'=>0)->reduce');
   69   $self->cmp_Error($ans,"It looks like you didn't finish simplifying your answer")
   70     if $err && $err =~ m/division by zero/i;
   71 }
   72 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9