Ask Your Question

Revision history [back]

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

click to hide/show revision 2
retagged

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