[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 5907 Revision 5912
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.14 2008/09/12 21:31:43 dpvc Exp $ 4# $CVSHeader: pg/macros/parserFormulaUpToConstant.pl,v 1.15 2008/09/12 21:53:52 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}->inherit($l)) == $r}; # a closure to access local variables 179 $main::{_cmp_} = sub {return $l->adapt == $r}; # a closure to access local variables
180 my $equal = main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents errors with large adaptive parameters 180 my $equal = main::PG_restricted_eval('&{$main::{_cmp_}}'); # prevents errors with large adaptive parameters
181 delete $main::{_cmp_}; # remove temprary function 181 delete $main::{_cmp_}; # remove temprary function
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 #
187 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol"); 187 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol");
188}
189
190#
191# Return the {adapt} formula with test points adjusted
192#
193sub adapt {
194 my $self = shift;
195 my $adapt = $self->{adapt}->inherit($self); delete $adapt->{adapt};
196 return $self->adjustInherit($self->{adapt});
197}
198
199#
200# Inherit from the main FormulaUpToConstant, but
201# adjust the test points to include the constants
202#
203sub adjustInherit {
204 my $self = shift;
205 my $f = shift->inherit($self);
206 delete $f->{adapt}; delete $f->{constant};
207 foreach my $id ('test_points','test_at') {
208 if (defined $f->{$id}) {
209 $f->{$id} = $f->{$id}->value if Value::isValue($f->{$id});
210 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY';
211 $f->{$id} = [map {[$_]} @{$f->{$id}}] unless ref($f->{$id}[0]) eq 'ARRAY';
212 $f->{$id} = $self->addConstants($f->{$id});
213 }
214 }
215 return $f;
216}
217
218#
219# Insert dummy values for the constants for the test points
220# (These are supposed to be +C, so the value shouldn't matter?)
221#
222sub addConstants {
223 my $self = shift; my $points = shift;
224 my @names = $self->context->variables->variables;
225 my $variables = $self->context->{variables};
226 my $Points = [];
227 foreach my $p (@{$points}) {
228 my @P = (.1) x scalar(@names); my $j = 0;
229 foreach my $i (0..scalar(@names)-1) {
230 if (!$variables->{$names[$i]}{arbitraryConstant}) {
231 $P[$i] = $p->[$j] if defined $p->[$j]; $j++;
232 }
233 }
234 push (@{$Points}, \@P);
235 }
236 return $Points;
188} 237}
189 238
190################################################## 239##################################################
191# 240#
192# Here we override part of the answer comparison 241# Here we override part of the answer comparison
236# 285#
237# Remove the constant and return a Formula object 286# Remove the constant and return a Formula object
238# 287#
239sub removeConstant { 288sub removeConstant {
240 my $self = shift; 289 my $self = shift;
241 main::Formula($self->substitute($self->{constant}=>0))->reduce->inherit($self); 290 return $self->adjustInherit(main::Formula($self->substitute($self->{constant}=>0))->reduce);
242} 291}
243 292
244# 293#
245# Override the differentiation so that we always return 294# Override the differentiation so that we always return
246# a Formula, not a FormulaUpToConstant (we don't want to 295# a Formula, not a FormulaUpToConstant (we don't want to

Legend:
Removed from v.5907  
changed lines
  Added in v.5912

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9