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.Sun, 30 Aug 2020 15:52:38 +0200Iteration over a Combinations(alist, 5).list()https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/ Dear all,
I have a big set built as : Combinations([1,2,3,4,5,6,7], 5).list() through which I need to iterate. In real life, the list [1,2,3,4,5,6,7] has 72 components. I don't need this set of combinations, only to iterate through it.
I did not find the proper way to do that with the combinat package, but I have the feeling I overlooked something.
Many thanks if you have infos on this issue!
Best, O. Olivier R.Sun, 30 Aug 2020 15:52:38 +0200https://ask.sagemath.org/question/53220/Need help converting python code to sage compatiblehttps://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/ I have a list of 262144 elements in a list created through "itertools.product". Now I have to loop over these elements and multiply it with all other elements, which is taking too much time. (I don't have any issue of memory / cpu)
elements = []
for e in itertools.product(range(4), repeat=9):
elements.append(e)
for row in elements:
for col in elements:
do_calculations(row, col)
def do_calculations(ro, co):
t = {}
t[0] = [multiply(c=ro[0], r=co[0])]
for i in range(1, len(ro)):
_t = []
for j in range(i+1):
_t.append(multiply(c=ro[j], r=co[i-j]))
t[i] = _t
for vals in t.values():
nx = len(vals)
_co = ro[nx:]
_ro = co[nx:]
for k in range(len(_ro)):
vals.append(multiply(c=_co[k], r=_ro[k]))
_t = []
for k in t.values():
s = k[0]
for j in range(1, len(k)):
s = addition(c=s, r=k[j])
_t.append(s)
return _t
def addition(c, r) -> int:
__a = [[0, 3, 1, 2],
[3, 2, 0, 1],
[0, 3, 2, 1],
[1, 0, 2, 3]]
return __a[c][r]
def multiply(c, r) -> int:
__m = [[0, 0, 0, 0],
[0, 1, 2, 3],
[0, 3, 1, 2],
[0, 2, 3, 1]]
return __m[c][r]
it is taking too much time to process single col with rows....
can any one help me in this?
regardsabu safaTue, 02 Jul 2019 12:58:33 +0200https://ask.sagemath.org/question/47035/Words avoiding patternshttps://ask.sagemath.org/question/43249/words-avoiding-patterns/I'm trying to write variations on the "words" generator from http://doc.sagemath.org/html/en/reference/combinat/sage/combinat/tutorial.html. The code there is
def words(alphabet,l):
if l == 0:
yield []
else:
for word in words(alphabet, l-1):
for a in alphabet:
yield word + [a]
which produces words in "alphabet" of length l. (Parenthetical remark: Where I've written "for a in alphabet", the original
has "for l in alphabet", which looks confusing given that l is also the name of the input length.)
I'd like to modify this generator in various ways, for example to produce words
in which the same "letter" does not appear twice in a row. What I've tried is the following:
def non_rep_words(alphabet,l):
if l == 0:
yield []
elif l==1:
yield alphabet
else:
for word in words(alphabet, l-1):
for a in alphabet:
if word[-1] != a:
yield w+[a]
The idea is to take a word w of length l-1 and for each element a of the alphabet, test if it agrees with the end of w,
and if it doesn't, then tack it on to w.
This seems to work for l=0,1,2, but fails at l=3. Here's what my terminal looks like:
sage: list(non_rep_words(['a','b'],2))
[['a', 'b'], ['b', 'a']]
sage: list(non_rep_words(['a','b'],3))
[['a', 'a', 'b'], ['a', 'b', 'a'], ['b', 'a', 'b'], ['b', 'b', 'a']]
Clearly I am misunderstanding something basic, and I would appreciate any advice.
Eventually, I'd like to do other kinds of pattern avoidance. For example, elements of my "alphabet" might come in pairs, a yin and a yang version, and I might want to make words in which a_yin does not follow a_yang, and conversely.PoloniusSat, 04 Aug 2018 11:05:49 +0200https://ask.sagemath.org/question/43249/Iterating over list of tupleshttps://ask.sagemath.org/question/36727/iterating-over-list-of-tuples/I am attempting to index into the y-coordinates of each tuple to count how many tuples have a y-coordinate of 0, 1, or 2 but am receiving an error message which looks like this:
Error in lines 1-1
Traceback (most recent call last):
File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
TypeError: 'sage.rings.integer.Integer' object is not iterable
My code is below:
zero_count, one_count, two_count = 0
for i in range(len([list])):
if list[i][1] == 0:
zero_count += 1
elif list[i][1] == 1:
one_count += 1
else:
two_count += 1collegesistaSat, 25 Feb 2017 01:29:42 +0100https://ask.sagemath.org/question/36727/make class iterablehttps://ask.sagemath.org/question/9381/make-class-iterable/How do I make a class iterable?
I want to create a class, which is a set of objects that I created, that has attributes of its own.
The user experience I am thinking of is the following:
(let's say A, B, and C are Objects in memory)
sage: D = Box((A,B,C))
I want to be able to say `for i in D: ....`
But I can't.
Any tips?
SLOtoSFWed, 03 Oct 2012 03:14:12 +0200https://ask.sagemath.org/question/9381/TypeError: 'int' object is not iterablehttps://ask.sagemath.org/question/9125/typeerror-int-object-is-not-iterable/Hello,
For a given matrix M (m by n) and vector V of size n, my goal is to do the following:
1. Take the $\text{i}^{\text{th}}$ column of M and convert them as keywords for a dictionary and associate the keywords to the $\text{i}^{\text{th}}$ value of the vector V.
2. I do (1) in MatVec(M,V) in the code below by creating a dictionary for each keyword and value. Finally I try to merge all the dictionaries.
Here is my code:
def DictionaryMerge(*args):
import collections
super_dict = collections.defaultdict(set)
for d in args:
for k, v in d.iteritmes():
super_dict[k].add(v)
return super_dict
def MatVec(M,V):
from sets import Set
Vec = V.list()
MatCols = M.ncols()
D2 = {}
for i in range(MatCols):
ColumnVec = M.column(i).list()
CVec = list()
for k in range(len(ColumnVec)):
CVec.append(int(ColumnVec[k]))
for j in CVec:
dict = {j:Set(int(Vec[i]))}
D2 = DictionaryMerge(dict,D2)
return dict
M = matrix([[(i+1)*(j+2) for i in range(5)] for j in range(6)])
V = vector([int(i+100) for i in range(5)])
MatVec(M,V)
The error I get is:
Traceback (most recent call last): return super_dict
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 29, in <module>
exec compile(u'MatVec(M,V)
File "", line 1, in <module>
File "/tmp/tmp4m6HiR/___code___.py", line 22, in MatVec
dict = {j:Set(int(Vec[i]))}
File "/home/usr111/sage/local/lib/python/sets.py", line 414, in __init__
self._update(iterable)
File "/home/usr111/sage/local/lib/python/sets.py", line 368, in _update
for element in iterable:
TypeError: 'int' object is not iterable
I am not so sure why I am getting this error. MaelstromYamatoTue, 03 Jul 2012 13:03:11 +0200https://ask.sagemath.org/question/9125/