# Is there a nice way to specify sums or products with excluded indices?

**URL:** <https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 14, 2026, 8:54pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198 "2026-07-14T20:54:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ducksoverip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ducksoverip/32/31967_2.png) [@ducksoverip](https://discourse.julialang.org/u/ducksoverip)\
**Post date:** [July 14, 2026, 8:54pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/1 "2026-07-14T20:54:36Z")

</div>

Say I need to calculate something like

y\_i = \sum\_{i \neq j} \frac{1}{x\_i - x\_j},

Where the summation excludes identical indices. In Julia, the only way I know how to do this is with nested loops and an `if` statement, eg:

```julia
x = [1 2 3 4 5]
y = zeros(5)

for i in eachindex(x), j in eachindex(x)
    if i != j
        y[i] += 1/(x[i] - x[j])
    end
end

```

This is functional, but rather clunky to write. Is there a more concise/elegant/idiomatic way to write “hey, sum/multiply these terms over all indices, but exclude this special case”? (Note: the function above is a good example of where you might encounter this excluded index summation, since it avoids a divide-by-zero error, but that’s hardly the only application.)

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [July 14, 2026, 9:22pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/2 "2026-07-14T21:22:30Z")

</div>

You can use InvertedIndices.jl:

```julia-auto
using InvertedIndices
x = [1, 2, 3, 4, 5]
x[Not(1)] # --> [2, 3, 4, 5]
x[1] .- x[Not(1)] # --> [-1, -2, -3, -4]
sum(inv, x[1] .- x[Not(1)] # --> -2.0833333

y = map(i -> sum(inv, x[i] .- x[Not(i)]), eachindex(x))

```

Whether it’s more elegant is up to you. I think the loop is actually fine.

---

<div class="post-metadata">

**Author:** ![ducksoverip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ducksoverip/32/31967_2.png) [@ducksoverip](https://discourse.julialang.org/u/ducksoverip)\
**Post date:** [July 14, 2026, 9:23pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/3 "2026-07-14T21:23:08Z")

</div>

Ok, so this didn’t appear when I searched it (figures), but _did_ appear in the “Related Posts” section after I posted this topic. Turns out the answer was, in fact, pretty simple: [How to sum every element but one - #3 by greg\_plowman](https://discourse.julialang.org/t/how-to-sum-every-element-but-one/49771/3)

---

<div class="post-metadata">

**Author:** ![ducksoverip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ducksoverip/32/31967_2.png) [@ducksoverip](https://discourse.julialang.org/u/ducksoverip)\
**Post date:** [July 14, 2026, 9:25pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/4 "2026-07-14T21:25:03Z")

</div>

That’s pretty slick! I might use it. Thanks for showing me!

---

<div class="post-metadata">

**Author:** ![parn4q](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/parn4q/32/222376_2.png) [@parn4q](https://discourse.julialang.org/u/parn4q)\
**Post date:** [July 14, 2026, 9:26pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/5 "2026-07-14T21:26:12Z")

</div>

To be honest, your original code was readable and made sense to me.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 14, 2026, 10:43pm UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/6 "2026-07-14T22:43:50Z")

</div>

> [@penelopeysm](#):
>
> `y = map(i -> sum(inv, x[i] .- x[Not(i)]), eachindex(x))`

Note that this allocates a temporary array for each `sum` call.

An allocation-free one-liner is:

```julia-auto
y = map(i -> sum(j -> (j != i) * inv(x[i]-x[j]), eachindex(x)), eachindex(x))

```

Note that this works because `false` is a “strong zero” in Julia:

```julia-auto
julia> false * Inf
0.0

julia> false * NaN
0.0

```

Beware that the more obvious code `j != i ? inv(x[i]-x[j]) : 0` is type-unstable (while using `0.0` for the other branch assumes `Float64` precision rather than inferring the precision from `x`).

All-in-all, the “one-liner” solutions seem less clear than a nested loop, though they do have the advantage that `sum` uses [pairwise summation](https://en.wikipedia.org/wiki/Pairwise_summation) for improved accuracy. `map` also infers the result type for you, whereas if you use `zeros` approach you would need to do a type computation like `y = zeros(typeof(inv(oneunit(eltype(x)))), length(x))` if you want the code to be type-generic.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [July 15, 2026, 2:58am UTC](https://discourse.julialang.org/t/is-there-a-nice-way-to-specify-sums-or-products-with-excluded-indices/138198/7 "2026-07-15T02:58:04Z")

</div>

As an intermediate form where it’s basically a nested loop but put into a single line, you could do

```julia-auto
x = [1, 2, 3, 4, 5]
y = [sum(inv(x[i] - x[j]) for j ∈ eachindex(x) if j ≠ i) for i ∈ eachindex(x)]

```
