ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 03 Mar 2019 02:03:52 -0600Iterator: amazing behaviorhttp://ask.sagemath.org/question/45609/iterator-amazing-behavior/ I came across the following [example:](https://treyhunner.com/2018/06/how-to-make-an-iterator-in-python/)
def count(start=0):
num = start
while True:
yield num
num += 1
When I try to apply it (Sage 8.6), the result is different than I expected:
print [count().next() for i in range(18)]
print next(count())
print next(count())
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] <br>
0 <br>
0 <br>
Can someone please elucidate why the output is not equal to:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] <br>
0 <br>
1 <br>
rolandSun, 03 Mar 2019 02:03:52 -0600http://ask.sagemath.org/question/45609/How to make "zip" work faster?http://ask.sagemath.org/question/26790/how-to-make-zip-work-faster/ I have these two finite sets $A$ and $B$ where the size of $A$ is typically much larger than the size of $B$. (typically $|A| is 200-500$ and $|B| is 10-50$) I am trying to enumerate all possible maps from $B$ to $A$ using the following idea - but this turns out to be very slow.
- Is there a way to speed this up?
- Without the over all "for i" loop can I access any one of the "k"s?
(for every i each $l$ is a list of tuples)
How can I just pick out any one such "k" list without wanting to wait for the whole code to run.
S = []
from itertools import product
for i in product(A,repeat = len (B)):
k = zip(B,i)
S.append(k)
show(S)PhoenixSun, 10 May 2015 16:42:04 -0500http://ask.sagemath.org/question/26790/Iterator for conjugacy classes of Snhttp://ask.sagemath.org/question/7794/iterator-for-conjugacy-classes-of-sn/Hello,
I would like to iterate through elements of a conjugacy classes of the symmetric group Sn. In other words, I'm looking for an algorithm which given an integer partition p = [p1,...,pk] of n provides an iterator over permutations with cycle decomposition whose length of cycles is exactly given by p.
There is one way which uses GAP, but as I have to iterate through conjugacy classes of S(12) and it is infinitely slow inside Sage. On the other hand, there is a useful efficient way to iterate through all permutations of Sn : there exists a "Gray code" for which two consecutive permutations differ by a swap (ie exchange of images between two elements). Such a method is implemented in cython in sage.combinat.permutation_cython (thanks Tom Boothby!).
- Do there exist algorithms for iteration through conjugacy classes of the symmetric group which is as close as possible as a Gray code ?
- Does there exist a better algorithm if we consider partitions of given length (the number k above) ? In other words, not iterating through conjugacy classes but through permutations with fixed number of cycles in their cycle decomposition.
- Is there something yet implemented in softwares included in Sage ?
Thanks,
VincentvdelecroixTue, 07 Dec 2010 09:48:05 -0600http://ask.sagemath.org/question/7794/How to slice an unknown length iteratorhttp://ask.sagemath.org/question/24033/how-to-slice-an-unknown-length-iterator/ I'm trying to take chunks of an iterator, for which I don't know the length of. The underlying goal is that I have a generator function which makes piles and piles of graphs (via a generator/iterator) that I want to try and test for a property in parallel (because serially testing each element is fast, but the list is really long).
Here's the code I'm working with right now:
#Slice Size:
slsize=500
grpgen=graphs.nauty_geng("10 -c 33:39 -d6")
start=0
stop=slsize
grplst=list()
loopcnt=0
loopt=True
while loopt:
grplst.append(itertools.islice(grpgen,start,stop))
#print loopcnt
try:
g=grplst[loopcnt].next()
print g
except StopIteration:
loopt=False
print "got except"
start+=slsize+1
stop+=slsize
loopcnt+=1
print loopcnt
#print grplst
This snippet will run ok. But as soon as I uncomment the "print grplst" at the end, it raises a strange 'resource unavailable' error. The error is raised on the 'try command' (g=grplst[loopcnt].next()) .... not when I actually print the grplst. And, it happens while _preparsing_ my file. (I'm loading this from a separate file).
When I leave it commented out though, it's clearly going through the 'try' command several times successfully before correctly catching the exception. It just seems that when I want to USE the list I've been creating of iterators, then it doesn't like it!Pelonza1Thu, 04 Sep 2014 17:37:34 -0500http://ask.sagemath.org/question/24033/How to find instances where $d(a,b) = p^2$ for $p$ a primehttp://ask.sagemath.org/question/10140/how-to-find-instances-where-dab-p2-for-p-a-prime/Suppose I have a dimension formula (for a Lie algebra representation) given by
$\mathrm{dim}_{a,b} = {(a+1)(b+1)(a+b+2) \over 2}$. I now would like to find pairs $(a,b)$ where $\dim_{a,b} = p^2$ for $p$ a prime? What are some techniques for accomplishing this? Should I first filter out a list of primes using `isprime` and then check possible pairs $(a,b)$ for each prime $p < N$, say $1000$.
JoshIzzardThu, 23 May 2013 07:12:39 -0500http://ask.sagemath.org/question/10140/How do I iterate through factors of a monomial?http://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/I am (very naively) trying to implement a certain non-linear map of polynomial rings. I can get the monomials in a polynomial by simply iterating through a polynomial:
>>> R.<x,y,z>=QQ[]
>>> list(x*y^3 + 2*x*y)
[(1, x*y^3), (2, x*y)]
How do I now iterate through the factors of each monomial, i.e. convert x*y^3 into something like [(x, 1), (y, 3)] or even just [x, y, y, y]?
Also, what is the correct way to do the opposite conversion, i.e. [x, y, y, y] to x*y^3?
I hope you won't be offended by such newbie questions. I couldn't find this in any of the tutorials easily (did I miss the right one?) Thank you! IlyaSun, 10 Jun 2012 04:46:35 -0500http://ask.sagemath.org/question/9055/How to decrease the iteration step from 1 to 0.1?http://ask.sagemath.org/question/9008/how-to-decrease-the-iteration-step-from-1-to-01/ @interact
def _(a=((0.1)..(5.00))):
...
show(pl1+pl2+pl3+pl4);
I would like 'a' to be iterated by the step 0.1, and not 1.
How can I achieve this?dnizeticSun, 27 May 2012 13:43:01 -0500http://ask.sagemath.org/question/9008/iterating over a combinatorial classhttp://ask.sagemath.org/question/8144/iterating-over-a-combinatorial-class/It is somewhat annoying that `len` and `next` don't do what you think with combinatorial classes. Is there any more Pythonic (e.g. list comprehension) way to do the following?
prof = [1,3,2,0,0,3]
L = []
P = Permutations(3)
Q = P.__iter__()
for j in range(P.cardinality()):
tp = Q.next()
for i in range(prof[j]):
L.append(tp)
Part of the issue is that I would really rather not use `Q`, since it is "used up" when I'm done. I really want something that is succinct, but I just could not find a way to do it.kcrismanThu, 02 Jun 2011 05:35:37 -0500http://ask.sagemath.org/question/8144/