Ask Your Question
0

Accelerating for-loop

asked 2023-04-13 00:21:33 +0100

Thrash gravatar image

updated 2023-04-13 00:24:44 +0100

I have a loop like

n = 3
M = MatrixSpace(Integers(n),n)
L = []
for m in M:
    if condition:
        L += [m]

In the case $n=3$, there are $3^{3^2} = 19683$ such matrices in M, which the computers nowadays can do within a few seconds, but if $n=4$, there are already $4^{4^2} ≈ 4.3\cdot10^9$ matrices in M to check, but I expect only about 100 hits (i.e. such m for which the condition is true). Is there a faster way to make this possible, for example by parallelization?

edit retag flag offensive close merge delete

Comments

If you parallelize with, say, 10 processors, it will only gain you a speed increase by a factor of 10. That won't help much when n=5. Maybe you should instead try to restrict the set of matrices using the condition: use the condition to construct a much smaller set of matrices: maybe only the ones satisfying it, or maybe a larger set than that but much smaller than the full matrix space.

John Palmieri gravatar imageJohn Palmieri ( 2023-04-13 00:40:28 +0100 )edit

There is some documentation for parallel computing in Sage here: https://doc.sagemath.org/html/en/refe...

John Palmieri gravatar imageJohn Palmieri ( 2023-04-13 00:40:51 +0100 )edit

You are right in principle, and that would be the proper (elegant) mathematical way, but knowing the result in the case n=4 is already sufficient for me in my specific mathematical question. That's why I would like to try it via brute force as fast as possible.

Thrash gravatar imageThrash ( 2023-04-13 00:51:31 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2023-04-13 02:58:41 +0100

Max Alekseyev gravatar image

Try something like this

@parallel
def worker(m):
    return condition(m)

n = 3
M = MatrixSpace(Integers(n),n)
L = [a[0] for (a,_),c in worker(iter(M)) if c]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-04-13 00:21:33 +0100

Seen: 317 times

Last updated: Apr 13 '23