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

**URL:** <https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810>\
**Category:** General Usage\
**Created:** [November 21, 2018, 11:25am UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810 "2018-11-21T11:25:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rotsoc](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rotsoc](https://discourse.julialang.org/u/rotsoc)\
**Post date:** [November 21, 2018, 11:25am UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/1 "2018-11-21T11:25:44Z")

</div>

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

 ![example](https://global.discourse-cdn.com/julialang/original/3X/3/2/32171cf2da9bebeba967dd29f3c31db30c0c07ae.jpeg)  
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.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [November 21, 2018, 11:31am UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/2 "2018-11-21T11:31:50Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![rotsoc](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rotsoc](https://discourse.julialang.org/u/rotsoc)\
**Post date:** [November 21, 2018, 11:47am UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/3 "2018-11-21T11:47:55Z")

</div>

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

 ![example2](https://global.discourse-cdn.com/julialang/original/3X/7/5/750e7415423b79ba3da5a07bc9eee077389aa872.jpeg)

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [November 21, 2018, 11:56am UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/4 "2018-11-21T11:56:04Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![rotsoc](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rotsoc](https://discourse.julialang.org/u/rotsoc)\
**Post date:** [November 21, 2018, 12:13pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/5 "2018-11-21T12:13:41Z")

</div>

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)

```julia
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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 21, 2018, 12:51pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/6 "2018-11-21T12:51:44Z")

</div>

> [@rotsoc](#):
>
> There is the problem that this A is not composed of numbers.

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

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![rotsoc](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rotsoc](https://discourse.julialang.org/u/rotsoc)\
**Post date:** [November 21, 2018, 1:25pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/7 "2018-11-21T13:25:15Z")

</div>

Ok,

```julia
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…

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [November 21, 2018, 1:46pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/8 "2018-11-21T13:46:16Z")

</div>

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

```julia
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)')

```

---

<div class="post-metadata">

**Author:** ![rotsoc](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rotsoc](https://discourse.julialang.org/u/rotsoc)\
**Post date:** [November 21, 2018, 1:53pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/9 "2018-11-21T13:53:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [November 21, 2018, 2:44pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/10 "2018-11-21T14:44:20Z")

</div>

You may be interested in `Tortilla.jl`, it’s a work-in-progress by Peter Ahrens, [see his talk at JuliaCon 2018.](https://youtu.be/Rp7sTl9oPNI?t=769) Also, there is `Einsum.jl`, `TensorOperations.jl`, and some other packages, but I didn’t personally use.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 15, 2019, 2:08pm UTC](https://discourse.julialang.org/t/library-for-algebraically-complicated-sums-and-other-expressions/17810/11 "2019-05-15T14:08:29Z")

</div>

Anyone knows where I can get Tortilla.jl? I could not find it on Github.
