I am currently coding using Matlab. As everyone knows, for loops are slow, while Matlab does matrix multiplication very efficiently. Unfortunately, my code is full of for loops.
My code resembles this:
function FInt = ComputeF(A, B, C, D, E, F, G, H)
%A is a real column vector of size [Na, 1].
%B is a real column vector of size [Nb, 1].
%C is a real column vector of size [Nc, 1].
%D is a real column vector of size [Nd, 1].
%E, F, G are real column vectors of the same size as A.
%H is a real column vector of the same size as A.
%This function evaluates FInt, a tensor of size [Na, Nb, Nc, Nd].
%Recording the correct dimensions and initializing FInt
Na = size(A, 1);
Nb = size(B, 1);
Nc = size(C, 1);
Nd = size(D, 1);
FInt = zeros(Na, Nb, Nc, Nd);
%Computing the tensor FInt
for na = 1:Na
for nc=1:Nc
for nd=1:Nd
%Calculating intermediate values
S1 = -((B(:) - C(nc) + E(na)) ./ (2 * sin(D(nd) ./ 2))).^2;
S2 = (B(:) + C(nc) + F(na)) ./ (2 .* cos(D(nd) ./ 2));
S3 = (B(:) + C(nc) + G(na)) ./ (2 .* cos(D(nd) ./ 2));
S4 = H(na) ./ cos(D(nd) ./ 2);
%Calculating the integrand FInt
FInt(na, nc, :, nd) = exp(S1) .* (sinh(S2 + 1i * S4) + conj(sinh(S3 + 1i * S4)));
end
end
end
end
As you can see I have already tried to vectorize the process by using : for the vector B, improving at least a bit the speed of computations. (Why B? Usually it is the longest vector).
My problem is that the quantities depend on so many indexes that I have no idea how to vectorize it properly.