[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 5551 - (download) (as text) (annotate)
Tue Oct 2 20:48:05 2007 UTC (12 years, 2 months ago) by sh002i
File size: 2549 byte(s)
improved formatting for docs -- these were in pod sections but were all
formatted as verbatim sections, and i moved them into normal paragraphs,
lists, etc. should make things more readable from the web.

    1 =head1 NAME
    2 
    3 parserDifferenceQuotient.pl - An answer checker for difference quotients.
    4 
    5 =head1 DESCRIPTION
    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 =cut
   34 
   35 loadMacros('MathObjects.pl');
   36 
   37 sub _parserDifferenceQuotient_init {DifferenceQuotient::Init()}; # don't reload this file
   38 
   39 package DifferenceQuotient;
   40 our @ISA = qw(Value::Formula);
   41 
   42 sub Init {
   43   main::Context("Numeric");  ### FIXME:  probably should require author to set this explicitly
   44   main::PG_restricted_eval('sub DifferenceQuotient {new DifferenceQuotient(@_)}');
   45 }
   46 
   47 sub new {
   48   my $self = shift; my $class = ref($self) || $self;
   49   my $current = (Value::isContext($_[0]) ? shift : $self->context);
   50   my $formula = shift;
   51   my $dx = shift || $current->flag('diffQuotientVar') || 'd'.($current->variables->names)[-1];
   52   #
   53   #  Make a copy of the context to which we add a variable for 'dx'
   54   #
   55   my $context = $current->copy;
   56   $context->variables->add($dx=>'Real') unless ($context->variables->get($dx));
   57   $q = bless $context->Package("Formula")->new($context,$formula), $class;
   58   $q->{'dx'} = $dx;
   59   return $q;
   60 }
   61 
   62 sub cmp_class {'a Difference Quotient'}
   63 
   64 sub cmp_defaults{(
   65   shift->SUPER::cmp_defaults,
   66   ignoreInfinity => 0,
   67 )}
   68 
   69 sub cmp_postprocess {
   70   my $self = shift; my $ans = shift; my $dx = $self->{'dx'};
   71   return if $ans->{score} == 0 || $ans->{isPreview};
   72   $main::__student_value__ = $ans->{student_value};
   73   my ($value,$err) = main::PG_restricted_eval('$__student_value__->substitute(\''.$dx.'\'=>0)->reduce');
   74   $self->cmp_Error($ans,"It looks like you didn't finish simplifying your answer")
   75     if $err && $err =~ m/division by zero/i;
   76 }
   77 
   78 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9