6
$\begingroup$

My current implementation for a totally antisymmetric function is like this:

g[a__] := Signature[{a}] (g @@ Sort@{a}) /; !OrderedQ[{a}];
g[a__] := 0 /; ! Unequal[a];

So I take the list of arguments of f and calculate the signature of the permutation, and put that factor out the front. I then sort to pick a canonical ordering which I put into f.

so I want that eg. f[1,3,2] = -f[1,2,3] and f[1,2,3] = f[2,3,1], and my code above achieves this. The second line sets any f with two arguments the same to be zero.

Then you could use this f for a wedge product or a minor of a matrix for example.

My code works, but is very slow when doing many rearrangments, as it needs to keep sorting and unioning the list of arguments.

Does anyone have an idea for a faster implementation?

$\endgroup$
12
  • 1
    $\begingroup$ You might want to add the exact mathematical definition of what you want to achieve. Otherwise, people will have to do their own research or reverse-engineer your code before they can help you. $\endgroup$
    – Felix
    Commented Feb 16, 2017 at 0:07
  • 2
    $\begingroup$ Can you provide an example where your code is slow? $\endgroup$ Commented Feb 16, 2017 at 12:47
  • 1
    $\begingroup$ Just a few things that won't dramatically speed up your code though (I think): f[Sequence @@ Sort @ List @ a] can be simplified to (f @@ Sort @ List @ a) and I'd generally write List@a as {a}. Also the Union can be avoided with Unequal[a] (Unequal doesn't only check for equality between consecutive pairs, but between all pairs, so it ensures all values are different.) For Sort @ {a} =!= {a} you can use OrderedQ, or ! Less[a]. $\endgroup$ Commented Feb 16, 2017 at 13:26
  • 1
    $\begingroup$ @Joe I don't think OrderedQ sorts the list (at least I really hope it doesn't). I'm pretty sure its implementation would be similar to my ! Less[a] suggestion which should run in linear time. You are probably sorting twice to determine Signature though. I'm not sure there's a way to avoid that, short of writing your own Signature that returns the sorted permutation along the way. (Probably based on Ordering.) (Also, if you put an @ in front of my name, I'll actually get a notification for your comments.) $\endgroup$ Commented Feb 17, 2017 at 8:59
  • 1
    $\begingroup$ If your function will be used for numeric arguments, or an "easily enumerable" set of symbolic expressions, than maybe you could take advantage of packed arrays and compilation. What is "typical" number of arguments that you're passing to your function? Unequal stays unevaluated for "most" of symbolic expressions whether they contain duplicates or not e.g. Unequal[a, b, b], Unequal[a, b, c], so your current g function will remain unevaluated in some cases for which it could give 0 e.g. g[a, b, b]. $\endgroup$
    – jkuczm
    Commented Feb 17, 2017 at 12:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.