In MATLAB, I execute something like this:
z = diag(c)*D*x;
idxN = z<0; idxP = z>=0; % logical indexing
y1 = sum(-z(idxN))+sum(arrayfun(@(x) log(exp(x)+1),z(idxN)));
I'm trying to formulate a Python one:
z = np.diagflat(c).dot(D).dot(x)
idxN = z<0, idxP = z>=0
y1 = np.sum(-z[idxN])+np.sum(math.log(np.exp(x)+1),z[idxN]);
The above snippet doesn't work as I'm guessing I've to vectorize somewhere?
Thanks for your inputs.
arrayfunisn't vectorization, it's shorthand for a loop. Just use a loop, or rewrite your code to use vectorization.