[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 6058 - (download) (as text) (annotate)
Thu Jun 25 23:28:44 2009 UTC (10 years, 6 months ago) by gage
File size: 3404 byte(s)
syncing pg HEAD with pg2.4.7 on 6/25/2009

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader$
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 parserDifferenceQuotient.pl - An answer checker for difference quotients.
   20 
   21 =head1 DESCRIPTION
   22 
   23 This is a Parser class that implements an answer checker for
   24 difference quotients as a subclass of the Formula class.  The
   25 standard ->cmp routine will work for this.  The difference quotient
   26 is just a special type of formula with a special variable
   27 for 'dx'.  The checker will give an error message if the
   28 student's result contains a dx in the denominator, meaning it
   29 is not fully reduced.
   30 
   31 Use DifferenceQuotient(formula) to create a difference equation
   32 object.  If the context has more than one variable, the last one
   33 alphabetically is used to form the dx.  Otherwise, you can specify
   34 the variable used for dx as the second argument to
   35 DifferenceQuotient().  You could use a variable like h instead of
   36 dx if you prefer.
   37 
   38 =head1 USAGE
   39 
   40   $df = DifferenceQuotient("2x+dx");
   41   ANS($df->cmp);
   42 
   43   $df = DifferenceQuotient("2x+h","h");
   44   ANS($df->cmp);
   45 
   46   Context()->variables->are(t=>'Real',a=>'Real');
   47   ANS(DifferenceQuotient("-a/[t(t+dt)]","dt")->cmp);
   48 
   49 =cut
   50 
   51 loadMacros('MathObjects.pl');
   52 
   53 sub _parserDifferenceQuotient_init {DifferenceQuotient::Init()}; # don't reload this file
   54 
   55 package DifferenceQuotient;
   56 our @ISA = qw(Value::Formula);
   57 
   58 sub Init {
   59   main::Context("Numeric");  ### FIXME:  probably should require author to set this explicitly
   60   main::PG_restricted_eval('sub DifferenceQuotient {new DifferenceQuotient(@_)}');
   61 }
   62 
   63 sub new {
   64   my $self = shift; my $class = ref($self) || $self;
   65   my $current = (Value::isContext($_[0]) ? shift : $self->context);
   66   my $formula = shift;
   67   my $dx = shift || $current->flag('diffQuotientVar') || 'd'.($current->variables->names)[-1];
   68   #
   69   #  Make a copy of the context to which we add a variable for 'dx'
   70   #
   71   my $context = $current->copy;
   72   $context->variables->add($dx=>'Real') unless ($context->variables->get($dx));
   73   $q = bless $context->Package("Formula")->new($context,$formula), $class;
   74   $q->{'dx'} = $dx;
   75   return $q;
   76 }
   77 
   78 sub cmp_class {'a Difference Quotient'}
   79 
   80 sub cmp_defaults{(
   81   shift->SUPER::cmp_defaults,
   82   ignoreInfinity => 0,
   83 )}
   84 
   85 sub cmp_postprocess {
   86   my $self = shift; my $ans = shift; my $dx = $self->{'dx'};
   87   return if $ans->{score} == 0 || $ans->{isPreview};
   88   $main::__student_value__ = $ans->{student_value};
   89   my ($value,$err) = main::PG_restricted_eval('$__student_value__->substitute(\''.$dx.'\'=>0)->reduce');
   90   $self->cmp_Error($ans,"It looks like you didn't finish simplifying your answer")
   91     if $err && $err =~ m/division by zero/i;
   92 }
   93 
   94 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9