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? regards
Exactly 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.
You should first remove the functions
addition
andmultiply
. That is to say, define your matrices__a
and__m
inside the functiondo_calculations
and replace the calladdition(c=s, r=k[j])
by__a[s][k[j]]
(and similarly with multiply).i 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 doing
is it possible to convert above code and use it in sage? does sage do iterations fast?
This question deserves a new tag, such as
Nine-billion-names-of-god
, but I don't know how to retag a question...