Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context flag multiplyRightToLeft #481

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions macros/contextPermutation.pl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ =head1 USAGE
multiplied by itself that many times (the absolute value of the
power).

The order in which permutations are multiplied is left-to-right by
default (e.g., (1 2 3)(1 2) = (2 3) using left-to-right multiplication).
But, right-to-left multiplication (e.g., (1 2 3)(1 2) = (1 3) using right-
to-left multiplication) can be used instead by setting

Context()->flags->set(multiplyRightToLeft => 1);

Note: Right-to-left multiplication is consistent with viewing permutations
as functions that are composed from right-to-left.

There are times when you might not want to allow inverses to be
computed automatically. In this case, set

Expand Down Expand Up @@ -181,6 +191,7 @@ sub _contextPermutation_init {
noPowers => 0, # allow powers of cycles and permutations?
noInverses => 0, # allow negative powers to mean inverse?
noGroups => 0, # allow parens for grouping (for powers)?
multiplyRightToLeft => 0, # default is left to right multiplication
);

$context->{error}{msg}{"Entries in a Cycle must be of the same type"} =
Expand Down Expand Up @@ -240,16 +251,30 @@ sub make {
#
sub mult {
my ($self,$l,$r,$other) = Value::checkOpOrderWithPromote(@_);
if ($l->isReal) {
$l = $l->value;
Value->Error("Can't multiply %s by a non-integer value",$self->showType) unless $l == int($l);
Value->Error("Can't multiply %s by a negative value",$self->showType) if $l < 0;
my $n = $self->{P}{$l}; $n = $l unless defined $n;
return $self->Package("Real")->make($n);
if (!$self->getFlag('multiplyRightToLeft')) {
if ($l->isReal) {
$l = $l->value;
Value->Error("Can't multiply %s by a non-integer value",$self->showType) unless $l == int($l);
Value->Error("Can't multiply %s by a negative value",$self->showType) if $l < 0;
my $n = $self->{P}{$l}; $n = $l unless defined $n;
return $self->Package("Real")->make($n);
} else {
Value->Error("Can't multiply %s by %s",$l->showType,$r->showType)
unless $r->classMatch("Cycle","Permutation");
return $self->Package("Permutation")->new($l,$r);
}
} else {
Value->Error("Can't multiply %s by %s",$l->showType,$r->showType)
unless $r->classMatch("Cycle","Permutation");
return $self->Package("Permutation")->new($l,$r);
if ($r->isReal) {
$r = $r->value;
Value->Error("Can't multiply %s by a non-integer value",$self->showType) unless $r == int($r);
Value->Error("Can't multiply %s by a negative value",$self->showType) if $r < 0;
my $n = $self->{P}{$r}; $n = $r unless defined $n;
return $self->Package("Real")->make($n);
} else {
Value->Error("Can't multiply %s by %s",$l->showType,$r->showType)
unless $l->classMatch("Cycle","Permutation");
return $self->Package("Permutation")->new($l,$r);
}
}
}

Expand Down Expand Up @@ -375,7 +400,7 @@ sub new {
}

#
# Find the internal representation of the permutation
# Find the internal representation of the cycle
# (a hash representing where each element goes)
#
sub makeP {
Expand Down Expand Up @@ -431,6 +456,7 @@ sub new {
sub makeP {
my $self = shift; my $p = $self->{data};
my $P = {}; my %N;
$p = [reverse(@$p)] if $self->getFlag('multiplyRightToLeft');
foreach my $x (@$p) {map {$N{$_} = 1} (keys %{$x->{P}})} # get all elements used
foreach my $i (keys %N) {
my $j = $i;
Expand Down Expand Up @@ -592,4 +618,3 @@ sub TeX {
###########################################################

1;