Ask Your Question
1

Why does the code not work?

asked 2017-12-27 00:05:26 +0200

sagequstions gravatar image

updated 2017-12-27 14:31:22 +0200

calc314 gravatar image

Hi, I wanted to use a program from an older question from myself again and found the following strange thing:

The following code works fine:

m=3

le_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.comparability_graph().edges(labels=None)]

cover_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.hasse_diagram().edges(labels=None)]

format_pt = lambda k: "'x{0}'".format(k+1)

format_relations = lambda relations: [[format_pt(a),format_pt(b)] for a,b in relations]

format_pts = lambda P: [format_pt(a) for a in P]

format_poset = lambda P: [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]

what_you_want = lambda n: [format_poset(P) for P in [posets.BooleanLattice(m)]]

print(what_you_want(m))

The following code does not work:

m=3

le_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.comparability_graph().edges(labels=None)]

cover_relations = lambda P: [(a,b) if P.le(a,b) else (b,a) for a,b in P.hasse_diagram().edges(labels=None)]

format_pt = lambda k: "'x{0}'".format(k+1)

format_relations = lambda relations: [[format_pt(a),format_pt(b)] for a,b in relations]

format_pts = lambda P: [format_pt(a) for a in P]

format_poset = lambda P: [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]

what_you_want = lambda n: [format_poset(P) for P in posets.YoungsLattice(m)]

print(what_you_want(m))

The only difference between two codes is that in the second code I have

what_you_want = lambda n: [format_poset(P) for P in posets.YoungsLattice(m)]

instead of

what_you_want = lambda n: [format_poset(P) for P in [posets.BooleanLattice(m)]].

Entering the second code gives an error. I do not really understand why the first works and the second does not, so any help is welcome how to fix the error.

edit retag flag offensive close merge delete

Comments

1

Please give minimal code, that still catches the problem. It is also hard to see which is the "first code", and which is the "second one", when we have the full code blocks in one order, then only the problem line and its working cousin the the other order. The lambda n ... does not use an n any longer. The minimal code to compare would be:

P = posets.BooleanLattice(3)
format_poset(P)

Works. In the other code, there is no list around, and

Q = posets.YoungsLattice(3)
format_poset(Q)

fails. Is this the problem? This comes from the fact that P and Q have different classes and types (for the labels of the elements.) Let us compare:

[format_pt(a) for a in P] # ok 
[format_pt(a) for a in Q] # fails -> Q.list() is the problem
dan_fulea gravatar imagedan_fulea ( 2017-12-27 17:10:13 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-12-28 03:54:33 +0200

slelievre gravatar image

updated 2017-12-28 04:00:56 +0200

The question can be simplified quite a bit.

In short, you define two lattices:

B = posets.BooleanLattice(3)
Y = posets.YoungsLattice(3)

and you want to apply the following function

def format_pt(k):
    return "'x{0}'".format(k+1)

to each node of these lattices.

Observe however what these nodes are.

For B:

sage: for a in B: print a
0
1
2
3
4
5
6
7

For Y:

sage: for a in Y: print a
[]
[1]
[1, 1]
[1, 1, 1]
[2]
[2, 1]
[3]

So the k+1 in the definition of format_pt might work for nodes of B, which are integers, but will not work for nodes of Y, which are lists.

The error boils down to the following (not so surprising) error:

sage: [] + 1
Traceback (most recent call last)
...
TypeError: unsupported operand parent(s) for +: '<type 'list'>' and 'Integer Ring'

Maybe the k+1 was just to renumber nodes of B to start from 1 instead of from 0?

Defining format_pt in a simpler way, and the other functions as in your question:

def le_relations(P):
    return [(a, b) if P.le(a, b) else (b, a) for a, b in P.comparability_graph().edges(labels=None)]

def cover_relations(P):
    return [(a,b) if P.le(a, b) else (b, a) for a, b in P.hasse_diagram().edges(labels=None)]

def format_pt(k):
    return "'x{}'".format(k)

def format_relations(relations):
    return [[format_pt(a), format_pt(b)] for a, b in relations]

def format_pts(P):
    return [format_pt(a) for a in P]

def format_poset(P):
    return [format_pts(P), format_relations(cover_relations(P)), format_relations(le_relations(P))]

we get the following, which might be what you were expecting.

sage: format_poset(B)
[["'x0'", "'x1'", "'x2'", "'x3'", "'x4'", "'x5'", "'x6'", "'x7'"],
 [["'x0'", "'x1'"],
  ["'x0'", "'x2'"],
  ["'x0'", "'x4'"],
  ["'x1'", "'x3'"],
  ["'x1'", "'x5'"],
  ["'x2'", "'x3'"],
  ["'x2'", "'x6'"],
  ["'x3'", "'x7'"],
  ["'x4'", "'x5'"],
  ["'x4'", "'x6'"],
  ["'x5'", "'x7'"],
  ["'x6'", "'x7'"]],
 [["'x0'", "'x1'"],
  ["'x0'", "'x2'"],
  ["'x0'", "'x3'"],
  ["'x0'", "'x4'"],
  ["'x0'", "'x5'"],
  ["'x0'", "'x6'"],
  ["'x0'", "'x7'"],
  ["'x1'", "'x3'"],
  ["'x1'", "'x5'"],
  ["'x1'", "'x7'"],
  ["'x2'", "'x3'"],
  ["'x2'", "'x6'"],
  ["'x2'", "'x7'"],
  ["'x3'", "'x7'"],
  ["'x4'", "'x5'"],
  ["'x4'", "'x6'"],
  ["'x4'", "'x7'"],
  ["'x5'", "'x7'"],
  ["'x6'", "'x7'"]]]
sage: format_poset(Y)
[["'x[]'",
  "'x[1]'",
  "'x[1, 1]'",
  "'x[1, 1, 1]'",
  "'x[2]'",
  "'x[2, 1]'",
  "'x[3]'"],
 [["'x[]'", "'x[1]'"],
  ["'x[1]'", "'x[1, 1]'"],
  ["'x[1]'", "'x[2]'"],
  ["'x[1, 1]'", "'x[1, 1, 1]'"],
  ["'x[1, 1]'", "'x[2, 1]'"],
  ["'x[2]'", "'x[2, 1]'"],
  ["'x[2]'", "'x[3]'"]],
 [["'x[]'", "'x[1]'"],
  ["'x[]'", "'x[1, 1]'"],
  ["'x[]'", "'x[1, 1, 1]'"],
  ["'x[]'", "'x[2]'"],
  ["'x[]'", "'x[2, 1]'"],
  ["'x[]'", "'x[3]'"],
  ["'x[1]'", "'x[1, 1]'"],
  ["'x[1]'", "'x[1, 1, 1]'"],
  ["'x[1]'", "'x[2]'"],
  ["'x[1]'", "'x[2, 1]'"],
  ["'x[1]'", "'x[3]'"],
  ["'x[1, 1]'", "'x[1, 1, 1]'"],
  ["'x[1, 1]'", "'x[2, 1]'"],
  ["'x[2]'", "'x[2, 1]'"],
  ["'x[2]'", "'x[3]'"]]]
edit flag offensive delete link more

Comments

Note: to visualize B and Y, try B.plot() and Y.plot().

slelievre gravatar imageslelievre ( 2017-12-28 04:03:15 +0200 )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: 2017-12-27 00:05:26 +0200

Seen: 283 times

Last updated: Dec 28 '17