# Clever way to sum

**URL:** <https://discourse.julialang.org/t/clever-way-to-sum/93967>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 3, 2023, 2:06am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967 "2023-02-03T02:06:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gattu\_Mytraya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gattu_mytraya/32/45429_2.png) [@Gattu\_Mytraya](https://discourse.julialang.org/u/Gattu_Mytraya)\
**Post date:** [February 3, 2023, 2:06am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967/1 "2023-02-03T02:06:44Z")

</div>

I was wondering whether there’s an efficient way to calculate sums of the form:

Sn = a1-n + a2-n + … + an-n

Currently, I’m just doing something like:

```julia
sum(x->x^(-n), a)

```

I would require all the sums from 1 to n.  
The elements of a are complex and, in general, can be small. Is there a particular representation which can reduce floating point error?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 3, 2023, 2:26am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967/2 "2023-02-03T02:26:27Z")

</div>

Yes. The functions `sum_kbn` and `sum_oro` exported from [AccurateArithmetic.jl](https://github.com/JuliaMath/AccurateArithmetic.jl).  
For a smaller dependency (likely with slower throughput), the function `sum_kbn` is exported from [KahanSummation.jl](https://github.com/JuliaMath/KahanSummation.jl).

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 3, 2023, 2:38am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967/3 "2023-02-03T02:38:00Z")

</div>

You’ll get better answers here with more information. Some questions:

1. Is there anything special about your `a` values (e.g. roots of unity?)
2. Is the `n` in the `a_n` the same as the `n` in the power?
3. When you say “I would require all the sums from 1 to n” do you mean all the different powers from 1 to n? If so, is `n` an integer?

---

<div class="post-metadata">

**Author:** ![Gattu\_Mytraya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gattu_mytraya/32/45429_2.png) [@Gattu\_Mytraya](https://discourse.julialang.org/u/Gattu_Mytraya)\
**Post date:** [February 3, 2023, 2:48am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967/4 "2023-02-03T02:48:45Z")

</div>

Should have mentioned that. In order of your questions:

1. There’s nothing “special” about a[j]. They’re complex-valued random variables drawn from a known (but not standard) distribution.
2. That was a typo. Sorry! That’s aN (Not used to HTML).
3. Yes, I do mean that n is an integer. I have some multinomials in Si, which are known to contain at least one power of Si for i in {1,2,3,…,n}.

Thanks, @JeffreySarnoff, for your comment on reducing finite precision error.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [February 3, 2023, 2:54am UTC](https://discourse.julialang.org/t/clever-way-to-sum/93967/5 "2023-02-03T02:54:19Z")

</div>

In that case, I think this is pretty easy. Just use `DoubleFloats.jl` which will give you an extra 53 bits of accuracy, and instead of computing `S_n = sum a_i^-n`, compute `S_1 = sum inv(a_i)` and then just do `S_i = sum a_1*a_i-1`.
