Ask Your Question
4

Defining a generic symbolic function with symmetric arguments

asked 2016-09-18 20:38:17 +0100

Mafra gravatar image

updated 2017-04-05 15:53:50 +0100

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!

edit retag flag offensive close merge delete

Comments

1

trying your idea with non-keyworded variable length arguments *args, and sorting them inside symmetric with the help of sort_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 gravatar imagemforets ( 2017-04-05 11:09:39 +0100 )edit

@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.

Mafra gravatar imageMafra ( 2017-04-05 12:38:25 +0100 )edit
1

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 gravatar imagemforets ( 2017-04-05 13:54:45 +0100 )edit

@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, typing eps(2,1,3) returns - eps(1,2,3). Good!

Mafra gravatar imageMafra ( 2017-04-05 15:57:51 +0100 )edit
1

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.

mforets gravatar imagemforets ( 2017-04-05 16:56:04 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2016-11-01 03:15:08 +0100

MikeZabrocki gravatar image

updated 2016-11-03 02:54:13 +0100

Ok, it sounds like you want to work in the ring of symmetric functions (however you didn't mention that there might be a + or * or take linear combinations, but these features don't seem to interfere with what you are asking for). Are you allowed repeated entries?

If I am not mistaken, even if this is not precisely what you are looking for, it seems like this algebra should be your model.

You seem to be asking for a vector space whose coefficients are integers and whose index set is a sorted list of non-negative integers (I'm adding the non-negative integer condition for convenience). If this is the case, then lets call them 'partitions.' I hope you don't mind that they will display in weakly decreasing order rather than increasing order, but I don't see that option built in so it will take slightly more than a couple of lines of programming.

sage: Sym = SymmetricFunctions(ZZ)
sage: h = Sym.complete()
sage: h._prefix='s'
sage: def s(lst):
....:     return h.mul(h([a]) for a in lst)
....: 
sage: s([2,1,2,3])
s[3, 2, 2, 1]
sage: 2*s([2,4])+s([1,2,3])
s[3, 2, 1] + 2*s[4, 2]

Is this what you were looking for or am I mis-interpreting what you were asking?

P.S. calling the complete basis 's' kind of goes against my nature, because we use that for Schur functions normally. But the line "s._prefix='s'" makes sure that the result is displayed with an 's' instead of the default 'h' just to follow closely the notation you were looking for.

edit flag offensive delete link more

Comments

1

Thank you for your answer, but this is not really what I am looking for. The name of the function in the input must be the same as the output; ie I want to type A = s(3,2)*s(1,2) + 2*s(4,2)*s(1,3) and obtain A = s(2,3)*s(1,2) + 2*s(2,4)*s(1,3). If I am allowed to use a different name in the input I already know a solution (just replace def s(*m) by def ss(*m) in my original question and type A = ss(3,2)*ss(1,2) + ... instead).

Mafra gravatar imageMafra ( 2016-11-01 19:00:30 +0100 )edit

So ideally one would have the choice of defining a symmetric function via an argument to the function() method, for example function('f', nargs=2, sym=symmetric) would define a function 'f' that takes two arguments and is symmetric in the sense that f(3,1) = f(1,3)

Mafra gravatar imageMafra ( 2016-11-01 19:03:39 +0100 )edit
2

It is possible to muck with the names so that the name in the input and output are the same, so that is not the real problem with this algebra (I will edit the answer so that it reflects this). I don't think that you've stated all the rules that your algebra satisfies. You didn't mention if you are allowed repeated parts entries or not. That is, does s(1,1) make sense? Now you are mentioning that there is a product on this algebra. Is the algebra commutative? Are there other relations? Does s(2,3)s(1,2) = s(1,2)s(2,3) or not?

MikeZabrocki gravatar imageMikeZabrocki ( 2016-11-03 02:52:39 +0100 )edit

Right, s(1,1) makes sense but it is zero. So in general one can restrict the labels to be distinct. Furthermore, s(i,j) is commutative. In Mathematica terms I am looking for the equivalent of using something like SetAttributes[s, Orderless] and nothing more.

Mafra gravatar imageMafra ( 2016-11-03 21:20:46 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-09-18 20:38:17 +0100

Seen: 984 times

Last updated: Apr 05 '17