Library for (algebraically) complicated sums (and other expressions)?


#1

I was wondering if anyone knows of a library that easily handles expressions like this:


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.


#2

I’m not sure what exact syntax you’re looking for but it’s easy enough to just do

julia> a = randn(3,3)
julia> sum((a[i,j] for i=1:2 for j=i+1))

#3

Yes but this expression could be pretty much anything… The general form that I am looking for is this:


#4

But you do have to specify A in some way. Are you not satisfied with things like for i=1:2 for j=i+1?


#5

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 :pensive:

edit: However, you are right that comprehensions inside sums are very useful in what I am doing and I will use them more.


#6

Please make an MWE. Gradually revealing specifications for a question makes it difficult to help you.


#7

Ok,

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

This is a simple yet typical example…


#8

It is not clear to me how you expect to have something simpler than this. If you really want to avoid loops, you can do

Z(i,t) = i == 1 ? NaN : sum(...)
Zarr = [Z(i,t) for i=1:n for t=1:n]) # or Z.(1:n, (1:n)')

#9

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.


#10

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.