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

Diff of /trunk/pg/macros/parserFormulaUpToConstant.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5913 Revision 5917
1################################################################################ 1################################################################################
2# WeBWorK Online Homework Delivery System 2# WeBWorK Online Homework Delivery System
3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ 3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: pg/macros/parserFormulaUpToConstant.pl,v 1.16 2008/09/15 15:35:34 dpvc Exp $ 4# $CVSHeader: pg/macros/parserFormulaUpToConstant.pl,v 1.18 2008/09/16 03:23:54 dpvc Exp $
5# 5#
6# This program is free software; you can redistribute it and/or modify it under 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 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 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. 9# version, or (b) the "Artistic License" which comes with this package.
116 # Copy the context (so we can modify it) and 116 # Copy the context (so we can modify it) and
117 # replace the usual Variable object with our own. 117 # replace the usual Variable object with our own.
118 # 118 #
119 my $context = (Value::isContext($_[0]) ? shift : $self->context)->copy; 119 my $context = (Value::isContext($_[0]) ? shift : $self->context)->copy;
120 $context->{parser}{Variable} = 'FormulaUpToConstant::Variable'; 120 $context->{parser}{Variable} = 'FormulaUpToConstant::Variable';
121 $context->{diagnostics}{formulas}{showGraphs} = 0;
122 # 121 #
123 # Create a formula from the user's input. 122 # Create a formula from the user's input.
124 # 123 #
125 my $f = main::Formula($context,@_); 124 my $f = main::Formula($context,@_);
126 # 125 #
176 $r = $r->substitute($r->{constant}=>$l->{constant}) unless $r->{constant} eq $l->{constant}; 175 $r = $r->substitute($r->{constant}=>$l->{constant}) unless $r->{constant} eq $l->{constant};
177 # 176 #
178 # Compare with adaptive parameters to see if $l + n0 C = $r for some n0. 177 # Compare with adaptive parameters to see if $l + n0 C = $r for some n0.
179 # 178 #
180 my $adapt = $l->adapt; 179 my $adapt = $l->adapt;
181 $main::{_cmp_} = sub {return $adapt == $r}; # a closure to access local variables 180 my $equal = $adapt->cmp_compare($r,{});
182 my $equal = main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents errors with large adaptive parameters
183 delete $main::{_cmp_}; # remove temprary function
184 $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags 181 $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags
185 return -1 unless $equal; 182 return -1 unless $equal;
186 # 183 #
187 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer) 184 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer)
188 # (remember: return value of 0 is equal, and non-zero is unequal) 185 # (remember: return value of 0 is equal, and non-zero is unequal)
189 # 186 #
190 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol"); 187 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol");
191}
192
193#
194# Provide diagnostics based on the adapted function used to check
195# the student's answer
196#
197sub cmp_diagnostics {
198 my $self = shift;
199 $self->inherit($self->{adapt})->SUPER::cmp_diagnostics(@_);
200} 188}
201 189
202# 190#
203# Return the {adapt} formula with test points adjusted 191# Return the {adapt} formula with test points adjusted
204# 192#
216 my $self = shift; 204 my $self = shift;
217 my $f = shift->inherit($self); 205 my $f = shift->inherit($self);
218 delete $f->{adapt}; delete $f->{constant}; 206 delete $f->{adapt}; delete $f->{constant};
219 foreach my $id ('test_points','test_at') { 207 foreach my $id ('test_points','test_at') {
220 if (defined $f->{$id}) { 208 if (defined $f->{$id}) {
221 $f->{$id} = $f->{$id}->value if Value::isValue($f->{$id}); 209 $f->{$id} = [$f->{$id}->value] if Value::isValue($f->{$id});
222 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY'; 210 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY';
223 $f->{$id} = [map {[$_]} @{$f->{$id}}] unless ref($f->{$id}[0]) eq 'ARRAY'; 211 $f->{$id} = [map {
212 (Value::isValue($_) ? [$_->value] :
213 (ref($_) eq 'ARRAY'? $_ : [$_]))
214 } @{$f->{$id}}];
224 $f->{$id} = $self->addConstants($f->{$id}); 215 $f->{$id} = $self->addConstants($f->{$id});
225 } 216 }
226 } 217 }
227 return $f; 218 return $f;
228} 219}
235 my $self = shift; my $points = shift; 226 my $self = shift; my $points = shift;
236 my @names = $self->context->variables->variables; 227 my @names = $self->context->variables->variables;
237 my $variables = $self->context->{variables}; 228 my $variables = $self->context->{variables};
238 my $Points = []; 229 my $Points = [];
239 foreach my $p (@{$points}) { 230 foreach my $p (@{$points}) {
231 if (scalar(@{$p}) == scalar(@names)) {
232 push (@{$Points},$p);
233 } else {
240 my @P = (.1) x scalar(@names); my $j = 0; 234 my @P = (.1) x scalar(@names); my $j = 0;
241 foreach my $i (0..scalar(@names)-1) { 235 foreach my $i (0..scalar(@names)-1) {
242 if (!$variables->{$names[$i]}{arbitraryConstant}) { 236 if (!$variables->{$names[$i]}{arbitraryConstant}) {
243 $P[$i] = $p->[$j] if defined $p->[$j]; $j++; 237 $P[$i] = $p->[$j] if defined $p->[$j]; $j++;
238 }
244 } 239 }
240 push (@{$Points}, \@P);
245 } 241 }
246 push (@{$Points}, \@P);
247 } 242 }
248 return $Points; 243 return $Points;
249} 244}
250 245
251################################################## 246##################################################
260# Show hints by default 255# Show hints by default
261# 256#
262sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)}; 257sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)};
263 258
264# 259#
260# Provide diagnostics based on the adapted function used to check
261# the student's answer
262#
263sub cmp_diagnostics {
264 my $self = shift;
265 $self->inherit($self->{adapt})->SUPER::cmp_diagnostics(@_);
266}
267
268#
269# Make it possible to graph single-variable formulas by setting
270# the arbitrary constants to 0 first.
271#
272sub cmp_graph {
273 my $self = shift; my $diagnostics = shift;
274 my $F1 = shift; my $F2; ($F1,$F2) = @{$F1} if (ref($F1) eq 'ARRAY');
275 my %subs; my $context = $self->context;
276 foreach my $v ($context->variables->variables)
277 {$subs{$v} = 0 if ($context->variables->get($v)->{arbitraryConstant})}
278 $F1 = $F1->inherit($F1->{adapt})->substitute(%subs)->reduce;
279 $F2 = $F2->inherit($F2->{adapt})->substitute(%subs)->reduce;
280 $self->SUPER::cmp_graph($diagnostics,[$F1,$F2]);
281}
282
283#
265# Add useful messages, if the author requested them 284# Add useful messages, if the author requested them
266# 285#
267sub cmp_postprocess { 286sub cmp_postprocess {
268 my $self = shift; my $ans = shift; 287 my $self = shift; my $ans = shift;
269 $self->SUPER::cmp_postprocess($ans); 288 $self->SUPER::cmp_postprocess($ans,@_);
270 return unless $ans->{score} == 0 && !$ans->{isPreview}; 289 return unless $ans->{score} == 0 && !$ans->{isPreview};
271 return if $ans->{ans_message} || !$self->getFlag("showHints"); 290 return if $ans->{ans_message} || !$self->getFlag("showHints");
272 my $student = $ans->{student_value}; 291 my $student = $ans->{student_value};
273 my $result = $ans->{correct_value} <=> $student; # compare encodes the reason in the result 292 $main::{_cmp_} = sub {return $ans->{correct_value} <=> $student}; # compare encodes the reason in the result
293 my $result = main::PG_restricted_eval('&{$main::{_cmp_}}');
294 delete $main::{_cmp_};
274 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3; 295 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3;
275 if ($result == 3) { 296 if ($result == 3) {
276 my $context = $self->context; 297 my $context = $self->context;
277 $context->flags->set(no_parameters=>0); 298 $context->flags->set(no_parameters=>0);
278 $context->variables->add(x00=>'Real'); 299 $context->variables->add(x00=>'Real');
279 my $correct = $self->removeConstant+"n01+n00x00"; # must use both parameters 300 my $correct = $self->removeConstant+"n01+n00x00"; # must use both parameters
280 $main::{_cmp_} = sub {return $correct == $student+"x00"}; # a closure to access local variables 301 $result = 1 if $correct->cmp_compare($student+"x00",{});
281 $result = 1 if main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents domain errors (and other errors)
282 delete $main::{_cmp_}; # remove temprary function
283 $context->variables->remove('x00'); 302 $context->variables->remove('x00');
284 $context->flags->set(no_parameters=>1); 303 $context->flags->set(no_parameters=>1);
285 } 304 }
286 $self->cmp_Error($ans,"Your answer is not the most general solution") if $result == 1; 305 $self->cmp_Error($ans,"Your answer is not the most general solution") if $result == 1;
287 $self->cmp_Error($ans,"Your formula should be linear in the constant '$student->{constant}'") 306 $self->cmp_Error($ans,"Your formula should be linear in the constant '$student->{constant}'")

Legend:
Removed from v.5913  
changed lines
  Added in v.5917

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9