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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5373 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9