I created this program which runs with no error, but I'd like to have some comments if it is possible to optimize it, since I need to run the program with N = 3200. In this case it takes a lot of time to end. I left it running during the night to get the answer.

The math notation of the program:

\$ A = (a_{ij}) \space \text{is an} \space N \times N \space \text{matrix} \\ B = \text{max_prod(A)} \space \text{is an} \space N \times N \space \text{matrix where} \\ \space \space \space \space \space b_{ij} = \min\{\max(a_{ik}, b_{kj}); k = 1, \dots,N\} \$

import numpy as np

def min_max(mat1, mat2, i, j):
    ''' this function takes 2 matrices and 2 indexes
    and return the minimum of the element wise maximum 
    between row i of mat1 and col j of mat2 '''
    return min(np.maximum(mat1[i,:], mat2[:,j]))

def max_prod(mat1,mat2):
    ''' this function takes 2 matrices and return a new matrix
    where position (i,j) is min_max(mat1, mat2, i, j) '''
    n = len(mat1)
    my_prod = np.zeros((n,n), dtype=float)
    for i in range(n):
        for j in range(i,n):
            my_prod[i,j] = min_max(mat1,mat2,i,j)
            my_prod[j,i] = min_max(mat2,mat1,j,i)
    return my_prod

N = 5
A = np.random.randint(1,20, size=(N,N))

print max_prod(A,A)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.