ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 05 Jul 2019 10:44:19 -0500Need help converting python code to sage compatiblehttp://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?
regardsTue, 02 Jul 2019 05:58:33 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/Comment by Iguananaut for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47078#post-id-47078I think what we're trying to ask is ,mathematically speaking, what is the problem you're trying to solve? What result do you want to find? You can edit your question and add equations if need be. Also where did this code come from? Did you write it yourself? The double-underscore local variables like `__a` and `__m` is an unusual question, which is why I ask. I could try, by looking at the code, to figure out on my own what the problem is that you're solving, but I'm not sure the code is correct in the first place so that might be a fool's errand. In your previous comment you tried to explain what you are doing, but not *why*.Fri, 05 Jul 2019 10:44:19 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47078#post-id-47078Comment by abu safa for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47062#post-id-47062anyone please ????Thu, 04 Jul 2019 09:29:35 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47062#post-id-47062Comment by Emmanuel Charpentier for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47059#post-id-47059This question deserves a new tag, such as `Nine-billion-names-of-god`, but I don't know how to retag a question...Thu, 04 Jul 2019 02:09:22 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47059#post-id-47059Comment by abu safa for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47043#post-id-47043is it possible to convert above code and use it in sage?
does sage do iterations fast?Tue, 02 Jul 2019 13:42:37 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47043#post-id-47043Comment by abu safa for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47042#post-id-47042i am not solving anything, but generating a result from elements.
elements are in shape of list - tuple (000000000, 000000001, ........ 333333332, 333333333)
i need to multiply each element with all elements to get the result for single element with respect to all elements.
you can say, i am making a grid of element as row and columns and their results
in do_calculations, i am solving an algebric like equation.
I hope you understand what i am doingTue, 02 Jul 2019 12:50:34 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47042#post-id-47042Comment by vdelecroix for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47041#post-id-47041You should first remove the functions `addition` and `multiply`. That is to say, define your matrices `__a` and `__m` inside the function `do_calculations` and replace the call `addition(c=s, r=k[j])` by `__a[s][k[j]]` (and similarly with multiply).Tue, 02 Jul 2019 07:50:47 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47041#post-id-47041Comment by Iguananaut for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47036#post-id-47036Exactly what is the problem you're trying to solve? It could be there's already an efficient implementation for it. If nothing else, it looks like most of what you're doing could be done more efficiently using actual matrix objects and/or numpy arrays. But before offering a concrete solution it would help if you could tell us what the original problem is.Tue, 02 Jul 2019 07:17:48 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?comment=47036#post-id-47036Answer by Emmanuel Charpentier for <p>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)</p>
<pre><code>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]
</code></pre>
<p>it is taking too much time to process single col with rows....
can any one help me in this?
regards</p>
http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?answer=47058#post-id-47058I do not understand what you're trying to accomplish (computing the [nine billions names of God](https://en.wikipedia.org/wiki/The_Nine_Billion_Names_of_God) ?).
I note that you are doing nothing with the result of `do_calculations` : it's neither used (to what ends ?), printed (where ?) nor stored (where ?).
For argument's sake, let's have a look at the size of problem. your elements are all the nine-long lists whose elements belong to `[0, 1, 2, 3]`. There are `4^9`=262144 of them. Your (potential) table of results has therefore `(4^9)^2`=68719476736 elements (about seventy billions).
How much time is necessary to compute one element ? Let's see : pick two elements at random and operate on them. Using your original definitions in a Python3-based Sage :
sage: A=tuple(r.sample([0, 1, 2, 3], 9, replace=True).sage())
sage: B=tuple(r.sample([0, 1, 2, 3], 9, replace=True).sage())
sage: time(do_calculations(A,B))
CPU times: user 1.21 ms, sys: 0 ns, total: 1.21 ms
Wall time: 1.22 ms
[0, 0, 0, 0, 0, 1, 1, 1, 0]
You would need about `(((4^9)^2)*1.22e-3).n(digits=3)`=8.38e7 seconds (i. about `(((4^9)^2)*1.22e-3/(24*60*60*365.25)).n(digits=3)`=2.66 years) to perform the computations.
Storing the result (using an efficient encoding, i. e. two bits per element) but no previsible gain from string compressin algorithm (since your computation doesn't seem to have any special properties), you would need about 17.5 GB of storage.
Therefore, absent any "interesting" properties of your `addition` and `multiply` functions, inducing "interesting" symmetries (i. e. a [structure](https://en.wikipedia.org/wiki/Algebraic_structure)) on the set of your nine-letter words, the "brute-force" approach is doomed.
May I suggest that using (a part of) 2.66 years to study the possible algebraic properties of your elements and operations might be more fruitful ?Thu, 04 Jul 2019 02:03:27 -0500http://ask.sagemath.org/question/47035/need-help-converting-python-code-to-sage-compatible/?answer=47058#post-id-47058