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.Tue, 12 Mar 2019 11:26:15 +0100Obtaining a poset from Posets(n)https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/I have defined function that takes a poset $P$ as input and outputs the major index generating polynomial over all linear extensions of $P$. Here, the major index of a certain linear extension of $P$ is given by the major index of the corresponding permutation, see e.g. http://mathworld.wolfram.com/LinearExtension.html for an example of this correspondence.
def major_index(pos):
poly = 0
lst = pos.linear_extensions()
for le in lst:
perm = Permutation(x.element for x in le)
maj = perm.major_index()
poly += x^maj
return poly
If I write, for example
P = Poset(([1,2,3,4], [[1,3],[1,4],[2,3]]), linear_extension=True, facade=False)
major_index(P)
Then I get
x^4 + x^3 + x^2 + x + 1
which is the correct output. I would like to use Posets(n) to generate all posets (up to isomorphism) with $n$ vertices. However, when I write
lst = Posets(3)
P = lst[3]
major_index(P)
I get the error:
Traceback (click to the left of this block for traceback)
...
AttributeError: 'sage.rings.integer.Integer' object has no attribute
'element'
Clearly the error is due to the line
perm = Permutation(x.element for x in le)
But I don't understand how Posets(n) works and why I cannot just write lst[3] to obtain a poset.Sun, 10 Mar 2019 14:53:21 +0100https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/Answer by FrédéricC for <p>I have defined function that takes a poset $P$ as input and outputs the major index generating polynomial over all linear extensions of $P$. Here, the major index of a certain linear extension of $P$ is given by the major index of the corresponding permutation, see e.g. <a href="http://mathworld.wolfram.com/LinearExtension.html">http://mathworld.wolfram.com/LinearEx...</a> for an example of this correspondence.</p>
<pre><code>def major_index(pos):
poly = 0
lst = pos.linear_extensions()
for le in lst:
perm = Permutation(x.element for x in le)
maj = perm.major_index()
poly += x^maj
return poly
</code></pre>
<p>If I write, for example</p>
<pre><code>P = Poset(([1,2,3,4], [[1,3],[1,4],[2,3]]), linear_extension=True, facade=False)
major_index(P)
</code></pre>
<p>Then I get </p>
<pre><code>x^4 + x^3 + x^2 + x + 1
</code></pre>
<p>which is the correct output. I would like to use Posets(n) to generate all posets (up to isomorphism) with $n$ vertices. However, when I write </p>
<pre><code>lst = Posets(3)
P = lst[3]
major_index(P)
</code></pre>
<p>I get the error: </p>
<pre><code>Traceback (click to the left of this block for traceback)
...
AttributeError: 'sage.rings.integer.Integer' object has no attribute
'element'
</code></pre>
<p>Clearly the error is due to the line </p>
<pre><code>perm = Permutation(x.element for x in le)
</code></pre>
<p>But I don't understand how Posets(n) works and why I cannot just write lst[3] to obtain a poset.</p>
https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/?answer=45747#post-id-45747This is because all these posets are "facade". Try something like that :
sage: P = Posets(4)
sage: Q = next(iter(P))
sage: Q._is_facade
True
sage: Permutation(x+1 for x in Q.linear_extension())
[4, 3, 2, 1]Sun, 10 Mar 2019 16:55:30 +0100https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/?answer=45747#post-id-45747Comment by joakim_uhlin for <p>This is because all these posets are "facade". Try something like that :</p>
<pre><code>sage: P = Posets(4)
sage: Q = next(iter(P))
sage: Q._is_facade
True
sage: Permutation(x+1 for x in Q.linear_extension())
[4, 3, 2, 1]
</code></pre>
https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/?comment=45769#post-id-45769This partially solved my issues. But I would, if possible, like to use the major_index-function on the posets of Poset(n). So I would like to write something like
`P = Posets(3)
Q = next(iter(P))
major_index(Q)`
but this gives me a similar error.Tue, 12 Mar 2019 11:26:15 +0100https://ask.sagemath.org/question/45746/obtaining-a-poset-from-posetsn/?comment=45769#post-id-45769