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.21 2009/06/25 23:28:44 gage Exp $ |
4 | # $CVSHeader: pg/macros/parserFormulaUpToConstant.pl,v 1.22 2009/10/07 14:39:19 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. |
… | |
… | |
137 | my $n = $f->D($f->{constant}); |
137 | my $n = $f->D($f->{constant}); |
138 | Value->Error("Your formula isn't linear in the arbitrary constant '%s'",$f->{constant}) |
138 | Value->Error("Your formula isn't linear in the arbitrary constant '%s'",$f->{constant}) |
139 | unless $n->isConstant; |
139 | unless $n->isConstant; |
140 | # |
140 | # |
141 | # Make a version with adaptive parameters for use in the |
141 | # Make a version with adaptive parameters for use in the |
142 | # comparison later on. We could like n0*C, but already have $n |
142 | # comparison later on. We could like n00*C, but already have $n |
143 | # copies of C, so remove them. That way, n0 will be 0 when there |
143 | # copies of C, so remove them. That way, n00 will be 0 when there |
144 | # are no C's in the student answer during the adaptive comparison. |
144 | # are no C's in the student answer during the adaptive comparison. |
145 | # (Again, should really check that n0 is not in use already) |
145 | # (Again, should really check that n00 is not in use already) |
146 | # |
146 | # |
147 | my $n00 = $context->variables->get("n00"); |
147 | my $n00 = $context->variables->get("n00"); |
148 | $context->variables->add(n00=>'Parameter') unless $n00 and $n00->{parameter}; |
148 | $context->variables->add(n00=>'Parameter') unless $n00 and $n00->{parameter}; |
149 | my $n01 = $context->variables->get("n01"); |
149 | my $n01 = $context->variables->get("n01"); |
150 | $context->variables->add(n01=>'Parameter') unless $n01 and $n01->{parameter}; |
150 | $context->variables->add(n01=>'Parameter') unless $n01 and $n01->{parameter}; |
… | |
… | |
173 | # If constants aren't the same, substitute the professor's in the student answer. |
173 | # If constants aren't the same, substitute the professor's in the student answer. |
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 | # |
177 | # |
178 | # Compare with adaptive parameters to see if $l + n0 C = $r for some n0. |
178 | # Compare with adaptive parameters to see if $l + n00 C = $r for some n0. |
179 | # |
179 | # |
180 | my $adapt = $l->adapt; |
180 | my $adapt = $l->adapt; |
181 | my $equal = Parser::Eval(sub {$adapt == $r}); |
181 | my $equal = Parser::Eval(sub {$adapt == $r}); |
182 | $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags |
182 | $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags |
183 | $self->{adapt}{test_values} = $adapt->{test_values}; # (these two are removed by inherit) |
183 | $self->{adapt}{test_values} = $adapt->{test_values}; # (these two are removed by inherit) |
184 | $self->{adapt}{test_adapt} = $adapt->{test_adapt}; |
184 | $self->{adapt}{test_adapt} = $adapt->{test_adapt}; |
185 | $_[1]->{test_values} = $r->{test_values}; # save these in student answer for diagnostics |
185 | $_[1]->{test_values} = $r->{test_values}; # save these in student answer for diagnostics |
186 | return -1 unless $equal; |
186 | return -1 unless $equal; |
187 | # |
187 | # |
188 | # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer) |
188 | # Check that n00 is non-zero (i.e., there is a multiple of C in the student answer) |
189 | # (remember: return value of 0 is equal, and non-zero is unequal) |
189 | # (remember: return value of 0 is equal, and non-zero is unequal) |
190 | # |
190 | # |
191 | return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol"); |
191 | return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol"); |
192 | } |
192 | } |
193 | |
193 | |