[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 5912 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.15 2008/09/12 21:53:52 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.
174 # 174 #
175 $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};
176 # 176 #
177 # 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.
178 # 178 #
179 $main::{_cmp_} = sub {return $l->adapt == $r}; # a closure to access local variables 179 my $adapt = $l->adapt;
180 my $equal = main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents errors with large adaptive parameters 180 my $equal = $adapt->cmp_compare($r,{});
181 delete $main::{_cmp_}; # remove temprary function 181 $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags
182 return -1 unless $equal; 182 return -1 unless $equal;
183 # 183 #
184 # 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)
185 # (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)
186 # 186 #
204 my $self = shift; 204 my $self = shift;
205 my $f = shift->inherit($self); 205 my $f = shift->inherit($self);
206 delete $f->{adapt}; delete $f->{constant}; 206 delete $f->{adapt}; delete $f->{constant};
207 foreach my $id ('test_points','test_at') { 207 foreach my $id ('test_points','test_at') {
208 if (defined $f->{$id}) { 208 if (defined $f->{$id}) {
209 $f->{$id} = $f->{$id}->value if Value::isValue($f->{$id}); 209 $f->{$id} = [$f->{$id}->value] if Value::isValue($f->{$id});
210 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY'; 210 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY';
211 $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}}];
212 $f->{$id} = $self->addConstants($f->{$id}); 215 $f->{$id} = $self->addConstants($f->{$id});
213 } 216 }
214 } 217 }
215 return $f; 218 return $f;
216} 219}
223 my $self = shift; my $points = shift; 226 my $self = shift; my $points = shift;
224 my @names = $self->context->variables->variables; 227 my @names = $self->context->variables->variables;
225 my $variables = $self->context->{variables}; 228 my $variables = $self->context->{variables};
226 my $Points = []; 229 my $Points = [];
227 foreach my $p (@{$points}) { 230 foreach my $p (@{$points}) {
231 if (scalar(@{$p}) == scalar(@names)) {
232 push (@{$Points},$p);
233 } else {
228 my @P = (.1) x scalar(@names); my $j = 0; 234 my @P = (.1) x scalar(@names); my $j = 0;
229 foreach my $i (0..scalar(@names)-1) { 235 foreach my $i (0..scalar(@names)-1) {
230 if (!$variables->{$names[$i]}{arbitraryConstant}) { 236 if (!$variables->{$names[$i]}{arbitraryConstant}) {
231 $P[$i] = $p->[$j] if defined $p->[$j]; $j++; 237 $P[$i] = $p->[$j] if defined $p->[$j]; $j++;
238 }
232 } 239 }
240 push (@{$Points}, \@P);
233 } 241 }
234 push (@{$Points}, \@P);
235 } 242 }
236 return $Points; 243 return $Points;
237} 244}
238 245
239################################################## 246##################################################
248# Show hints by default 255# Show hints by default
249# 256#
250sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)}; 257sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)};
251 258
252# 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#
253# Add useful messages, if the author requested them 284# Add useful messages, if the author requested them
254# 285#
255sub cmp_postprocess { 286sub cmp_postprocess {
256 my $self = shift; my $ans = shift; 287 my $self = shift; my $ans = shift;
257 $self->SUPER::cmp_postprocess($ans); 288 $self->SUPER::cmp_postprocess($ans,@_);
258 return unless $ans->{score} == 0 && !$ans->{isPreview}; 289 return unless $ans->{score} == 0 && !$ans->{isPreview};
259 return if $ans->{ans_message} || !$self->getFlag("showHints"); 290 return if $ans->{ans_message} || !$self->getFlag("showHints");
260 my $student = $ans->{student_value}; 291 my $student = $ans->{student_value};
261 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_};
262 $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;
263 if ($result == 3) { 296 if ($result == 3) {
264 my $context = $self->context; 297 my $context = $self->context;
265 $context->flags->set(no_parameters=>0); 298 $context->flags->set(no_parameters=>0);
266 $context->variables->add(x00=>'Real'); 299 $context->variables->add(x00=>'Real');
267 my $correct = $self->removeConstant+"n01+n00x00"; # must use both parameters 300 my $correct = $self->removeConstant+"n01+n00x00"; # must use both parameters
268 $main::{_cmp_} = sub {return $correct == $student+"x00"}; # a closure to access local variables 301 $result = 1 if $correct->cmp_compare($student+"x00",{});
269 $result = 1 if main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents domain errors (and other errors)
270 delete $main::{_cmp_}; # remove temprary function
271 $context->variables->remove('x00'); 302 $context->variables->remove('x00');
272 $context->flags->set(no_parameters=>1); 303 $context->flags->set(no_parameters=>1);
273 } 304 }
274 $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;
275 $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.5912  
changed lines
  Added in v.5917

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9