I am having a difficulty vectorizing the following for loops in Python.
out = np.zeros((N, d))
dir_int = []
for i in range(N):
dir_int.append(np.random.randint(low=0, high = d))
out[i,dir_int[i]] = 1
#where:
# direct_int has shape (N, )
# u has shape (N, d)
# x has the same shape as u
# A has shape (2d, d) = [I,-I]^T, I the dxd identity
# b has shape (2d, )
bmAx = b - np.concatenate((x,-x), axis=1) #This is b-Ax has shape N x 2d
upper = np.copy(x)
lower = np.copy(x)
temp = np.zeros(2)
for i in range(len(dir_int)):
temp[0] = bmAx[i, dir_int[i]]
temp[1] = -bmAx[i, d + dir_int[i]]
upper[i, dir_int[i]] += np.amax(temp)
lower[i, dir_int[i]] += np.amin(temp)
For the first loop, dir_int
can be created as dir_int = np.random.randint(low=0, high = d, size = N)
. Then for each "row" of out
one of its columns should be 1
; this column is dir_int[row]
. Not sure how to do that in one line.
The second loop is even harder than the first. Any help is much appreaciated.