Defining a generic symbolic function with symmetric arguments
Updated question:
I would like to define a generic symbolic function called s whose arguments are totally symmetric.
So if I type s(3,2) the output must be s(2,3),
sage: s(3,2)
s(2,3)
and similarly
sage: s(3,4,1,2)
s(1,2,3,4)
and in general some arbitrary product
sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
Is it possible to have such a definition? It would be nice if there was an argument to the built-in function()
to specify the symmetry of its arguments. In this case I would get what I want with something like
s = function('s', sym='symmetric')
Solution from Apr/2017:
Upon reading Sage's source code I noticed the 'eval_func' option when defining functions. So if I define (following a suggestion of mforets, thanks!):
def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
and define the (Mandelstam invariant) s
as follows
s = function("s", eval_func=symmetric)
everything works. For example
sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
So I consider this question closed!
trying your idea with non-keyworded variable length arguments
*args
, and sorting them insidesymmetric
with the help ofsort_complex_numbers_for_display
, it sent me to a sort of recursive evaluation of the constructor (don't know why). if i were you i would pull out the question from the context and ask in a Python forum such as stackoverflow. good luck!@mforets: Thank you very much for suggesting a general Python audience, I will try that later. Regarding the recursion, you have to know when to stop sorting the arguments. I updated the question with one attempt that is not general wrt the number of arguments just as an idea to try.
usually the brackets can be "removed" by preceding the statement with the operator
*
, see for instance the doc unpacking argument lists. does it work here?@mforets: Thank you for pointing that out! Now it works in general, see updated question. I can now also define an
antisymmetric
function to keep track of the parity of the arguments in order to mimic the Levi-Civita tensor. I.e, typingeps(2,1,3)
returns- eps(1,2,3)
. Good!welcome :) since this site allows you to mark your own answer as correct, please consider doing so. for someone with a similar question it will be crystal clear what is the approach you finally chose for this problem.