| 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: webwork2/lib/WeBWorK.pm,v 1.100 2007/08/13 22:59:53 sh002i Exp $ |
4 | # $CVSHeader: pg/macros/contextLimitedPolynomial.pl,v 1.17 2007/10/04 16:40:48 sh002i 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 | |
137 | |
| 138 | package LimitedPolynomial; |
138 | package LimitedPolynomial; |
| 139 | |
139 | |
| 140 | # |
140 | # |
| 141 | # Mark a variable as having power 1 |
141 | # Mark a variable as having power 1 |
|
|
142 | # Mark a number as being present (when strict coefficients are used) |
| 142 | # Mark a monomial as having its given powers |
143 | # Mark a monomial as having its given powers |
| 143 | # |
144 | # |
| 144 | sub markPowers { |
145 | sub markPowers { |
| 145 | my $self = shift; |
146 | my $self = shift; |
| 146 | if ($self->class eq 'Variable') { |
147 | if ($self->class eq 'Variable') { |
| 147 | my $vIndex = LimitedPolynomial::getVarIndex($self); |
148 | my $vIndex = LimitedPolynomial::getVarIndex($self); |
| 148 | $self->{index} = $vIndex->{$self->{name}}; |
149 | $self->{index} = $vIndex->{$self->{name}}; |
| 149 | $self->{exponents} = [(0) x scalar(keys %{$vIndex})]; |
150 | $self->{exponents} = [(0) x scalar(keys %{$vIndex})]; |
| 150 | $self->{exponents}[$self->{index}] = 1; |
151 | $self->{exponents}[$self->{index}] = 1; |
|
|
152 | } elsif ($self->class eq 'Number') { |
|
|
153 | $self->{exponents} = [] if $self->context->flag("strictCoefficients"); |
| 151 | } |
154 | } |
| 152 | if ($self->{exponents}) { |
155 | if ($self->{exponents}) { |
| 153 | my $power = join(',',@{$self->{exponents}}); |
156 | my $power = join(',',@{$self->{exponents}}); |
| 154 | $self->{powers}{$power} = 1; |
157 | $self->{powers}{$power} = 1; |
| 155 | } |
158 | } |
| … | |
… | |
| 197 | my ($l,$r) = ($self->{lop},$self->{rop}); |
200 | my ($l,$r) = ($self->{lop},$self->{rop}); |
| 198 | $self->Error("Addition is allowed only between monomials") if $r->{isPoly}; |
201 | $self->Error("Addition is allowed only between monomials") if $r->{isPoly}; |
| 199 | $self->checkPowers; |
202 | $self->checkPowers; |
| 200 | } |
203 | } |
| 201 | |
204 | |
| 202 | sub checkStrict { |
|
|
| 203 | my $self = shift; |
|
|
| 204 | $self->Error("You can only use addition for the terms of a polynomial",$self->{bop}); |
|
|
| 205 | } |
|
|
| 206 | |
|
|
| 207 | ############################################## |
205 | ############################################## |
| 208 | |
206 | |
| 209 | package LimitedPolynomial::BOP::subtract; |
207 | package LimitedPolynomial::BOP::subtract; |
| 210 | our @ISA = qw(LimitedPolynomial::BOP Parser::BOP::subtract); |
208 | our @ISA = qw(LimitedPolynomial::BOP Parser::BOP::subtract); |
| 211 | |
209 | |
| 212 | sub checkPolynomial { |
210 | sub checkPolynomial { |
| 213 | my $self = shift; |
211 | my $self = shift; |
| 214 | my ($l,$r) = ($self->{lop},$self->{rop}); |
212 | my ($l,$r) = ($self->{lop},$self->{rop}); |
| 215 | $self->Error("Subtraction is allowed only between monomials") if $r->{isPoly}; |
213 | $self->Error("Subtraction is allowed only between monomials") if $r->{isPoly}; |
| 216 | $self->checkPowers; |
214 | $self->checkPowers; |
| 217 | } |
|
|
| 218 | |
|
|
| 219 | sub checkStrict { |
|
|
| 220 | my $self = shift; |
|
|
| 221 | $self->Error("You can only use subtraction between the terms of a polynomial",$self->{bop}); |
|
|
| 222 | } |
215 | } |
| 223 | |
216 | |
| 224 | ############################################## |
217 | ############################################## |
| 225 | |
218 | |
| 226 | package LimitedPolynomial::BOP::multiply; |
219 | package LimitedPolynomial::BOP::multiply; |
| … | |
… | |
| 261 | return 1; |
254 | return 1; |
| 262 | } |
255 | } |
| 263 | |
256 | |
| 264 | sub checkStrict { |
257 | sub checkStrict { |
| 265 | my $self = shift; |
258 | my $self = shift; |
| 266 | $self->Error("You can only use '%s' to form fractions",$self->{bop}) if $self->{lop}->class eq 'BOP'; |
259 | $self->Error("You can only use '%s' to form numeric fractions",$self->{bop}) if $self->{lop}->class eq 'BOP'; |
| 267 | } |
260 | } |
| 268 | |
261 | |
| 269 | ############################################## |
262 | ############################################## |
| 270 | |
263 | |
| 271 | package LimitedPolynomial::BOP::power; |
264 | package LimitedPolynomial::BOP::power; |
| … | |
… | |
| 429 | |
422 | |
| 430 | # |
423 | # |
| 431 | # A context where coefficients can't include operations |
424 | # A context where coefficients can't include operations |
| 432 | # |
425 | # |
| 433 | $context = $main::context{"LimitedPolynomial-Strict"} = $context->copy; |
426 | $context = $main::context{"LimitedPolynomial-Strict"} = $context->copy; |
| 434 | $context->flags->set(strictCoefficients=>1, singelPowers=>1, reduceConstants=>0); |
427 | $context->flags->set(strictCoefficients=>1, singlePowers=>1, reduceConstants=>0); |
| 435 | $context->functions->disable("All"); # can be re-enabled if needed |
428 | $context->functions->disable("All"); # can be re-enabled if needed |
| 436 | |
429 | |
| 437 | main::Context("LimitedPolynomial"); ### FIXME: probably should require author to set this explicitly |
430 | main::Context("LimitedPolynomial"); ### FIXME: probably should require author to set this explicitly |
| 438 | } |
431 | } |
| 439 | |
432 | |