3
$\begingroup$

I have a function f whose input is two lists. I want to define a replacement rule which brings to input to a canonical ordering. I have tried the following

CanonicalOrdering = {f[a__, b__] ->Signature@a Signature@b f[Sort@a, Sort@b]}

This seems to do what I want just fine, for example

In[2]:= f[{2, 1}, {1, 2}] /. CanonicalOrdering
Out[2]= -f[{1, 2}, {1, 2}]

However I get some warnings when I first evaluate the function

In[1]:= CanonicalOrdering = {f[a__, b__] -> Signature@a Signature@b f[Sort@a, Sort@b]}

During evaluation of In[1]:= Signature::normal: Nonatomic expression 
expected at position 1 in Signature[a].

During evaluation of In[1]:= Signature::normal: Nonatomic expression 
expected at position 1 in Signature[b].

During evaluation of In[1]:= Sort::normal: Nonatomic expression expected at 
position 1 in Sort[a].

During evaluation of In[1]:= Sort::normal: Nonatomic expression expected at 
position 1 in Sort[b].

Out[1]= {f[a__, b__] -> f[Sort[a], Sort[b]] Signature[a] Signature[b]}

I'm worried that while everything is fine for simple examples issues will arise eventually. How should my code be modified to avoid any potential issues, i.e. to avoid the warnings?

$\endgroup$
5
$\begingroup$

It seems I need to use a delayed replacement rule.

CanonicalOrdering = {f[a__, b__] :>Signature@a Signature@b f[Sort@a, Sort@b]}

This works perfectly (and makes sense).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.