[system] / trunk / pg / macros / contextPiecewiseFunction.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/contextPiecewiseFunction.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5444 Revision 5446
511 return $self->make(@cases); 511 return $self->make(@cases);
512} 512}
513 513
514# 514#
515# Substitute into each branch individually. 515# Substitute into each branch individually.
516# 516# If the function's variable is substituted, then
517# ### FIXME: only allow the variable to be substituted 517# if it is a constant, find the branch for that value
518# by another variable, and change the intervals as well. 518# and substitute into that, otherwise if it is
519# If it is a constant, then evaluate? 519# just another variable, replace the variable
520# in the inequalities as well as the formulas.
521# Otherwise, just replace in the formulas.
520# 522#
521sub substitute { 523sub substitute {
522 my $self = shift; my @cases = (); 524 my $self = shift;
525 my @cases = (); my $x = $self->{varName};
526 $self->setValues(@_); my $a = $self->{values}{$x}; $self->unsetValues(@_);
527 if (defined $a) {
528 if (!Value::isFormula($a)) {
529 my $f = $self->getFunctionFor($a);
530 die "undefined value" unless defined $f;
531 return $f->substitute(@_);
532 }
533 $x = $a->{tree}{name} if $a->{tree}->class eq 'Variable';
534 }
523 foreach my $If (@{$self->{data}}) { 535 foreach my $If (@{$self->{data}}) {
524 my ($I,$f) = @{$If}; 536 my ($I,$f) = @{$If};
537 $I = $I->copy; if ($x ne $I->{varName}) {$I->{varName} = $x; $I->updateParts}
525 push(@cases,$I->copy => $f->substitute(@_)); 538 push(@cases,$I => $f->substitute(@_));
526 } 539 }
527 push(@cases,$self->{otherwise}->substitute(@_)) if defined $self->{otherwise}; 540 push(@cases,$self->{otherwise}->substitute(@_)) if defined $self->{otherwise};
528 return $self->make(@cases); 541 return $self->make(@cases);
529} 542}
530 543
575 push(@{$self->{data}},[$D,$self->{otherwise}]); 588 push(@{$self->{data}},[$D,$self->{otherwise}]);
576 } 589 }
577 delete $self->{otherwise}; 590 delete $self->{otherwise};
578 foreach my $If (@{$self->{data}}) {$If->[0]{equation} = $If->[1]{equation} = $self} 591 foreach my $If (@{$self->{data}}) {$If->[0]{equation} = $If->[1]{equation} = $self}
579 return $self; 592 return $self;
593}
594
595#
596# Look up the function for the nth branch (or the "otherwise"
597# function if n is omitted or too big or too small).
598#
599sub getFunction {
600 my $self = shift; my $n = shift;
601 return $self->{otherwise} if !defined $n || $n < 1 || $n > $self->length;
602 return $self->{data}[$n-1][1];
603}
604
605#
606# Look up the domain for the nth branch (or the "otherwise"
607# domain if n is omitted or too big or too small).
608#
609sub getDomain {
610 my $self = shift; my $n = shift;
611 return $self->Package("Inequality")->new($self->context,
612 $self->domainR - $self->domainUnion,$self->{varName})
613 if !defined $n || $n < 1 || $n > $self->length;
614 return $self->{data}[$n-1][0];
615}
616
617#
618# Get the function for the given value of the variable
619# (or undef if there is none).
620#
621sub getFunctionFor {
622 my $self = shift; my $x = shift;
623 foreach my $If (@{$self->{data}}) {
624 my ($I,$f) = @$If;
625 return $f if $I->contains($x);
626 }
627 return $self->{otherwise};
580} 628}
581 629
582# 630#
583# Implements the <=> operator (really only handles equality ir not) 631# Implements the <=> operator (really only handles equality ir not)
584# 632#

Legend:
Removed from v.5444  
changed lines
  Added in v.5446

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9