Ask Your Question

Output too long in SAGE

asked 2017-09-23 15:17:13 -0500

maresage gravatar image

updated 2017-09-24 03:37:34 -0500

I have a program whos output is too long to be displayed in the terminal where I run sage. Is there a possiblity that the output of SAGE is automatically "copied" (as if I mark it and use ctrl+c) and then I can paste it into a text file? Or is there another way that the result can be automatically be saved into a text file or a way to make the terminal allow larger text before the text gets cut off?

Here the code Im talking about:

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(n) if P.is_lattice()]


It takes about 20 minits for my computer to finish the calculation while the calculation seems to never finish in the online sage cell

The code is from the thread )

edit: Another option would be to divide the set [format_poset(P) for P in posets(n) if P.is_lattice] into smaller pieces (lets say 5 pieces) and apply the code to those smaller pieces. The set contains 222 elements. Is there a way to apply the cod to the first 40 of those elements first and then I do it seperately for the next 40 and so on till I have all. Maybe it is possible to filter instead of posets(n) (for n=8) over just the first 40 posets of posets(n) and then the next 40 and so on.

edit retag flag offensive close merge delete


[Bug report (?)] Should P.is_lattice be P.is_lattice() in the second to last line in the code above?

fidbc gravatar imagefidbc ( 2017-09-23 19:57:22 -0500 )edit

Thanks I edited it.

maresage gravatar imagemaresage ( 2017-09-24 03:37:49 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-23 19:54:27 -0500

fidbc gravatar image

To save to a file, you may try the following.

with open('filename.txt','w') as outfile:

Searching for "write to file in python" might give some insight into this aspect of Python.

If you wish to process a list by chunks, then looking up "slicing in Python" might be useful. Given a list L, you may access the second chunk of length 40 by using L[40:80]. So using slicing within a for loop could produce the desired result.

edit flag offensive delete link more


Thank you very much!

maresage gravatar imagemaresage ( 2017-09-24 03:43:59 -0500 )edit

@fidbc Another question just for safety (I tested it for small examples and it works, but I want to make sure). If I want to have a "disjoint union" of a list L (say with 80 elements) into smaller lists I have to do it like L[0:40] and L[40:80] to get a disjoint union or? (instead of L[0:40] and L[41:80] )

maresage gravatar imagemaresage ( 2017-09-24 03:58:20 -0500 )edit

@maresageL[0:40] will output all elements in L with indices i such that 0<=i<40. Thus L[40] will not be contained in L[0:40]. However L[40:80] will contain L[40].

fidbc gravatar imagefidbc ( 2017-09-24 16:01:36 -0500 )edit

@fidbc ok, as expected. Thank you.

maresage gravatar imagemaresage ( 2017-09-24 16:16:56 -0500 )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


Asked: 2017-09-23 15:17:13 -0500

Seen: 51 times

Last updated: Sep 24