ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 05 Apr 2017 16:56:04 +0200Defining a generic symbolic function with symmetric argumentshttps://ask.sagemath.org/question/34849/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!Sun, 18 Sep 2016 20:38:17 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/Comment by Mafra for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37174#post-id-37174@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.Wed, 05 Apr 2017 12:38:25 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37174#post-id-37174Comment by mforets for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37173#post-id-37173trying 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!Wed, 05 Apr 2017 11:09:39 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37173#post-id-37173Comment by mforets for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37175#post-id-37175usually the brackets can be "removed" by preceding the statement with the operator `*`, see for instance the doc [unpacking argument lists](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists). does it work here?Wed, 05 Apr 2017 13:54:45 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37175#post-id-37175Comment by Mafra for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37177#post-id-37177@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!Wed, 05 Apr 2017 15:57:51 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37177#post-id-37177Comment by mforets for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37178#post-id-37178welcome :) 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.Wed, 05 Apr 2017 16:56:04 +0200https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=37178#post-id-37178Answer by MikeZabrocki for <p><strong>Updated question:</strong></p>
<p>I would like to define a generic symbolic function called <em>s</em> whose arguments are totally symmetric.</p>
<p>So if I type s(3,2) the output must be s(2,3), </p>
<pre><code> sage: s(3,2)
s(2,3)
</code></pre>
<p>and similarly</p>
<pre><code> sage: s(3,4,1,2)
s(1,2,3,4)
</code></pre>
<p>and in general some arbitrary product</p>
<pre><code> sage: s(3,2)*s(1,2)^2*s(5,4,1)
s(2,3)*s(1,2)^2*s(1,4,5)
</code></pre>
<p>Is it possible to have such a definition? It would be nice if there was an argument to the built-in <code>function()</code> to specify the symmetry of its arguments. In this case I would get what I want with something like</p>
<pre><code>s = function('s', sym='symmetric')
</code></pre>
<p><strong>Solution from Apr/2017:</strong></p>
<p>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!):</p>
<pre><code>def symmetric(self, *x):
L = list(x)
L0 = copy(L)
L.sort()
if L0 <= L:
pass
else:
return self(*L)
</code></pre>
<p>and define the (<em>Mandelstam invariant</em>) <code>s</code> as follows</p>
<pre><code> s = function("s", eval_func=symmetric)
</code></pre>
<p>everything works. For example</p>
<pre><code> sage: 2*s(2,1)*s(6,5,4,3)^2
2*s(3, 4, 5, 6)^2*s(1, 2)
</code></pre>
<p>So I consider this question closed!</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?answer=35324#post-id-35324Ok, 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.Tue, 01 Nov 2016 03:15:08 +0100https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?answer=35324#post-id-35324Comment by Mafra for <p>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?</p>
<p>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.</p>
<p>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.</p>
<pre><code>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]
</code></pre>
<p>Is this what you were looking for or am I mis-interpreting what you were asking?</p>
<p>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.</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35329#post-id-35329Thank 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).Tue, 01 Nov 2016 19:00:30 +0100https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35329#post-id-35329Comment by Mafra for <p>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?</p>
<p>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.</p>
<p>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.</p>
<pre><code>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]
</code></pre>
<p>Is this what you were looking for or am I mis-interpreting what you were asking?</p>
<p>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.</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35330#post-id-35330So 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)`Tue, 01 Nov 2016 19:03:39 +0100https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35330#post-id-35330Comment by MikeZabrocki for <p>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?</p>
<p>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.</p>
<p>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.</p>
<pre><code>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]
</code></pre>
<p>Is this what you were looking for or am I mis-interpreting what you were asking?</p>
<p>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.</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35369#post-id-35369It 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?Thu, 03 Nov 2016 02:52:39 +0100https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35369#post-id-35369Comment by Mafra for <p>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?</p>
<p>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.</p>
<p>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.</p>
<pre><code>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]
</code></pre>
<p>Is this what you were looking for or am I mis-interpreting what you were asking?</p>
<p>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.</p>
https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35405#post-id-35405Right, 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.Thu, 03 Nov 2016 21:20:46 +0100https://ask.sagemath.org/question/34849/defining-a-generic-symbolic-function-with-symmetric-arguments/?comment=35405#post-id-35405