| … | |
… | |
| 19 | # each degree can be included in the polynomial: |
19 | # each degree can be included in the polynomial: |
| 20 | # |
20 | # |
| 21 | # Context("LimitedPolynomial")->flags->set(singlePowers=>1); |
21 | # Context("LimitedPolynomial")->flags->set(singlePowers=>1); |
| 22 | # |
22 | # |
| 23 | |
23 | |
|
|
24 | =cut |
|
|
25 | |
| 24 | # |
26 | # |
| 25 | # Handle common checking for BOPs |
27 | # Handle common checking for BOPs |
| 26 | # |
28 | # |
| 27 | |
|
|
| 28 | =cut |
|
|
| 29 | |
|
|
| 30 | package LimitedPolynomial::BOP; |
29 | package LimitedPolynomial::BOP; |
| 31 | |
30 | |
| 32 | # |
31 | # |
| 33 | # Do original check and then if the operands are numbers, its OK. |
32 | # Do original check and then if the operands are numbers, its OK. |
| 34 | # Otherwise, do an operator-specific check for if the polynomial is OK. |
33 | # Otherwise, do an operator-specific check for if the polynomial is OK. |
| … | |
… | |
| 59 | LimitedPolynomial::markPowers($l); |
58 | LimitedPolynomial::markPowers($l); |
| 60 | LimitedPolynomial::markPowers($r); |
59 | LimitedPolynomial::markPowers($r); |
| 61 | my $exponents = $self->{exponents} = $r->{exponents}; |
60 | my $exponents = $self->{exponents} = $r->{exponents}; |
| 62 | delete $r->{exponents}; delete $r->{powers}; |
61 | delete $r->{exponents}; delete $r->{powers}; |
| 63 | if ($l->{exponents}) { |
62 | if ($l->{exponents}) { |
| 64 | my $single = $self->{equation}{context}->flag('singlePowers'); |
63 | my $single = $self->context->flag('singlePowers'); |
| 65 | foreach my $i (0..scalar(@{$exponents})-1) { |
64 | foreach my $i (0..scalar(@{$exponents})-1) { |
| 66 | $self->Error("A variable can appear only once in each term of a polynomial") |
65 | $self->Error("A variable can appear only once in each term of a polynomial") |
| 67 | if $exponents->[$i] && $l->{exponents}[$i] && $single; |
66 | if $exponents->[$i] && $l->{exponents}[$i] && $single; |
| 68 | $exponents->[$i] += $l->{exponents}[$i]; |
67 | $exponents->[$i] += $l->{exponents}[$i]; |
| 69 | } |
68 | } |
| … | |
… | |
| 78 | # and record the new power list |
77 | # and record the new power list |
| 79 | # |
78 | # |
| 80 | sub checkPowers { |
79 | sub checkPowers { |
| 81 | my $self = shift; |
80 | my $self = shift; |
| 82 | my ($l,$r) = ($self->{lop},$self->{rop}); |
81 | my ($l,$r) = ($self->{lop},$self->{rop}); |
| 83 | my $single = $self->{equation}{context}->flag('singlePowers'); |
82 | my $single = $self->context->flag('singlePowers'); |
| 84 | LimitedPolynomial::markPowers($l); |
83 | LimitedPolynomial::markPowers($l); |
| 85 | LimitedPolynomial::markPowers($r); |
84 | LimitedPolynomial::markPowers($r); |
| 86 | $self->{isPoly} = 1; |
85 | $self->{isPoly} = 1; |
| 87 | $self->{powers} = $l->{powers} || {}; delete $l->{powers}; |
86 | $self->{powers} = $l->{powers} || {}; delete $l->{powers}; |
| 88 | return 1 unless $r->{powers}; |
87 | return 1 unless $r->{powers}; |
| … | |
… | |
| 316 | # |
315 | # |
| 317 | # Now build the new context that calls the |
316 | # Now build the new context that calls the |
| 318 | # above classes rather than the usual ones |
317 | # above classes rather than the usual ones |
| 319 | # |
318 | # |
| 320 | |
319 | |
| 321 | $context{LimitedPolynomial} = Context("Numeric"); |
320 | $context{LimitedPolynomial} = Parser::Context->getCopy(undef,"Numeric"); |
| 322 | $context{LimitedPolynomial}->operators->set( |
321 | $context{LimitedPolynomial}->operators->set( |
| 323 | '+' => {class => 'LimitedPolynomial::BOP::add'}, |
322 | '+' => {class => 'LimitedPolynomial::BOP::add'}, |
| 324 | '-' => {class => 'LimitedPolynomial::BOP::subtract'}, |
323 | '-' => {class => 'LimitedPolynomial::BOP::subtract'}, |
| 325 | '*' => {class => 'LimitedPolynomial::BOP::multiply'}, |
324 | '*' => {class => 'LimitedPolynomial::BOP::multiply'}, |
| 326 | '* ' => {class => 'LimitedPolynomial::BOP::multiply'}, |
325 | '* ' => {class => 'LimitedPolynomial::BOP::multiply'}, |