This would greatly help in what I am trying to do… This expression can be calculated using loops but in what I am writing end-users would have to calculate a lot of expressions like this one and I want to give them an easy way to do that.
I’m sorry if there is a library that does this that I have missed.
There is the problem that this A is not composed of numbers. So, for instance, in the first example, what I am really doing is saying for i=1:length(I) for j=1:length(J), if I,J are two sets and then if j=i+1 . But I have to do calculations where I would need something like: for s="new-york":"topeka" and then calculate C[s] (which I can do using NamedArrays) but I would not like to do all my calculations using NamedArrays but rather do all the calculations using normal Arrays and then name them (having named the original sets beforehand).
I think there are a couple of other problems as well, but I will have to think about them some more. For example, in the first example, for calculating Z, I would have to write something like (correct me if there is an obvious shortcut)
for i=1:n
if i!=1
for t=1:m
Z[i,t] = ...
end
end
end
(I wrote it a bit more generally to include any condition on i …)
and I would like to avoid so many loops because some people get easily confused when they have to write them
edit: However, you are right that comprehensions inside sums are very useful in what I am doing and I will use them more.
I = ["i1","i2","i3","i4"]
J = ["j1","j2","j3","j4"]
T = ["t1","t2","t3","t4"]
iter = ["1","2","3","4"]
A = zeros((length(I),length(J)))
B = zeros((length(I),length(J)))
C = zeros((length(I),length(J)))
for i=1:length(I)
for j=1:length(J)
A[i,j] = i+j
B[i,j] = i*j
C[i,j] = A[i,j] + B[i,j]
end
end
for c=1:length(iter)
for i=1:length(I)
if i!=1
for t=1:length(T)
Z[i,t] = sum((C[i,j]+0.1*C[i-1,j] for j=i-2 for j=1:length(J)))
+c*log(t+exp(-c))
end
end
end
for j=1:length(J)
for t=1:length(T)
if t!=1 && t==c
Y[j,t] = sum(C[i,j] for i=1:length(I)) + c*log(t-1+sqrt(c))
end
end
end
end
Yes, I guess you are right. I don’t really want to avoid loops altogether, I was just thinking that there might be a library that uses friendly syntax for this kind of thing. Anyway, thank you for answering, and generator expressions in sums helps quite a lot.
You may be interested in Tortilla.jl, it’s a work-in-progress by Peter Ahrens, see his talk at JuliaCon 2018. Also, there is Einsum.jl, TensorOperations.jl, and some other packages, but I didn’t personally use.