# \#sum

**URL:** https://discourse.julialang.org/tag/sum/699.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [A fast sum. Any downsides?](https://discourse.julialang.org/t/a-fast-sum-any-downsides/123723)

<div class="topic-metadata">

**Author:** [@nicolas](https://discourse.julialang.org/u/nicolas)\
**Replies:** 18\
**Last updated:** [December 16, 2024, 3:04pm UTC](https://discourse.julialang.org/t/a-fast-sum-any-downsides/123723 "2024-12-16T15:04:12Z")

</div>

I wrote a simple function to perform sums using indices. function sumi(f, s, range) @simd for i in range s += f(i) end return s end I was curious to measure the performance hit of using sumi for arrays instead of s…

---

## [Optimizing sums of products (dot products)](https://discourse.julialang.org/t/optimizing-sums-of-products-dot-products/119515)

<div class="topic-metadata">

**Author:** [@s-baumann](https://discourse.julialang.org/u/s-baumann)\
**Replies:** 17\
**Last updated:** [September 24, 2024, 7:40pm UTC](https://discourse.julialang.org/t/optimizing-sums-of-products-dot-products/119515 "2024-09-24T19:40:11Z")

</div>

Is there a way to avoid the intermediate allocation for a sumproduct scenario? In the following case I can redo it with a for loop which improves the memory allocation but causes it to take longer to execute (the differ…

---

## [Weird empty sum behavior in JuMP](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386)

<div class="topic-metadata">

**Author:** [@KSepetanc](https://discourse.julialang.org/u/KSepetanc)\
**Replies:** 3\
**Last updated:** [April 24, 2024, 9:36pm UTC](https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386 "2024-04-24T21:36:57Z")

</div>

I am coding basic AC OPF model. The following sum results in an error: -(Vd\[n\]^2+Vq\[n\]^2)\*sum(gs\[sh\] for sh in bus\_shunts\[n\]) LoadError: MethodError: no method matching zero(::Type{MutableArithmetics.Zero}) Closest ca…

---

## [Assistance in speeding up Array operations and summations](https://discourse.julialang.org/t/assistance-in-speeding-up-array-operations-and-summations/102989)

<div class="topic-metadata">

**Author:** [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Replies:** 12\
**Last updated:** [August 23, 2023, 12:01pm UTC](https://discourse.julialang.org/t/assistance-in-speeding-up-array-operations-and-summations/102989 "2023-08-23T12:01:00Z")

</div>

I have a problem where I have arrays of matrices that I do operations on and then sum the result. I wonder if I can optimize this part of my code as it will be used repeatedly. Please find below an MWE that reproduces my…

---

## [Check the sum of a certain array index](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552)

<div class="topic-metadata">

**Author:** [@divon](https://discourse.julialang.org/u/divon)\
**Replies:** 19\
**Last updated:** [June 20, 2023, 7:40am UTC](https://discourse.julialang.org/t/check-the-sum-of-a-certain-array-index/100552 "2023-06-20T07:40:34Z")

</div>

Hello everyone, I would like to check the sum of certain indexes in the array. For example, I have the array: a = \[0, 0, 0, 1, 0, 1, 0, 1\] b = \[1, 2, 3, 5\] I will check the sum of a\[1\], a\[2\], a\[3\], and a\[5\]. If the su…

---

## [Specifying broadcast in reduce operations](https://discourse.julialang.org/t/specifying-broadcast-in-reduce-operations/97589)

<div class="topic-metadata">

**Author:** [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Replies:** 5\
**Last updated:** [April 25, 2023, 6:43am UTC](https://discourse.julialang.org/t/specifying-broadcast-in-reduce-operations/97589 "2023-04-25T06:43:48Z")

</div>

Writing a new CircShiftedArrays package that specified its own BroadcastStyle, I have the trouble that reduce operations such as sum do not get affected by my overloaded functions. I overloaded copyto! and materialize! …

---

## [Does the julia intrinsic sum() apply fastmath by default?](https://discourse.julialang.org/t/does-the-julia-intrinsic-sum-apply-fastmath-by-default/94815)

<div class="topic-metadata">

**Author:** [@Ender\_L](https://discourse.julialang.org/u/Ender_L)\
**Replies:** 15\
**Last updated:** [February 21, 2023, 4:05am UTC](https://discourse.julialang.org/t/does-the-julia-intrinsic-sum-apply-fastmath-by-default/94815 "2023-02-21T04:05:00Z")

</div>

I noticed the output of sum() is different from custom loops julia\> function mysum(A) out = 0.0 for ii in eachindex(A) out += A\[ii\] end return out end my…

---

## [How to write a summation in Julian?](https://discourse.julialang.org/t/how-to-write-a-summation-in-julian/91346)

<div class="topic-metadata">

**Author:** [@xArcoirisXx](https://discourse.julialang.org/u/xArcoirisXx)\
**Replies:** 6\
**Last updated:** [December 7, 2022, 8:10am UTC](https://discourse.julialang.org/t/how-to-write-a-summation-in-julian/91346 "2022-12-07T08:10:18Z")

</div>

How to write and solve a summation in Julian?

---

## [Inconsistency in \`sum\` of an empty generator](https://discourse.julialang.org/t/inconsistency-in-sum-of-an-empty-generator/89638)

<div class="topic-metadata">

**Author:** [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Replies:** 5\
**Last updated:** [November 2, 2022, 3:46pm UTC](https://discourse.julialang.org/t/inconsistency-in-sum-of-an-empty-generator/89638 "2022-11-02T15:46:57Z")

</div>

On Julia 1.3, the following two empty generators both throw an error when you apply sum to them: julia\> g1 = (1 for i in Int\[\]); julia\> g2 = (x for x in \[-1\] if x \> 0); julia\> eltype(g1) Any julia\> eltype(g2) Any ju…

---

## [Sum of columns in julia dataframe and appen it as last element in the respective columns](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837)

<div class="topic-metadata">

**Author:** [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Replies:** 3\
**Last updated:** [September 6, 2022, 2:17pm UTC](https://discourse.julialang.org/t/sum-of-columns-in-julia-dataframe-and-appen-it-as-last-element-in-the-respective-columns/86837 "2022-09-06T14:17:33Z")

</div>

I have a dataframe df, using DataFrames df=DataFrame(p1 = \[1, 2, 3, 4, 5\],p2 = \[10,20,30,40,50\],p3 = \[5,5,10,20,20\]) I wanted to calculate the sum of specific columns (p1 and p2) and append it as the last row in respec…

---

## [How to multiply tuples and how to change a format of summation?](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928)

<div class="topic-metadata">

**Author:** [@Kevin\_12](https://discourse.julialang.org/u/Kevin_12)\
**Replies:** 2\
**Last updated:** [June 20, 2022, 3:03pm UTC](https://discourse.julialang.org/t/how-to-multiply-tuples-and-how-to-change-a-format-of-summation/82928 "2022-06-20T15:03:24Z")

</div>

If you have a set A = \[ (1,4,7), (2,5,3), (5,4,9), (2,8,7), (8,6,7), (2,3,5) \] .Is it possible to derive: #1 another set A' such that A' = 1.2\*A each element of A' is the scaled version of A #2 define a variable …

---

## [Is there a way to bring the range of a summation back from an array?](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937)

<div class="topic-metadata">

**Author:** [@vicentemunozlh](https://discourse.julialang.org/u/vicentemunozlh)\
**Replies:** 7\
**Last updated:** [May 31, 2022, 9:24pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-bring-the-range-of-a-summation-back-from-an-array/81937 "2022-05-31T21:24:10Z")

</div>

Hello, I am new to Julia and trying to model a relatively complicated optimization problem from other lenguage(GAMS). I am stuck in trying to apply a summation in a constraint and its depndence index to be in a range pr…

---

## [Sum elements of a struct](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559)

<div class="topic-metadata">

**Author:** [@vamp](https://discourse.julialang.org/u/vamp)\
**Replies:** 7\
**Last updated:** [May 5, 2022, 9:13pm UTC](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559 "2022-05-05T21:13:17Z")

</div>

How can I sum the elements of MyStruct? Code: struct MyStruct a::Int b::SVector{N\_lds, Int64} c::Int end import Base: - -(x::MyStruct, y::MyStruct) = MyS…

---

## [Help in sum columns](https://discourse.julialang.org/t/help-in-sum-columns/77883)

<div class="topic-metadata">

**Author:** [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Replies:** 11\
**Last updated:** [March 14, 2022, 7:16pm UTC](https://discourse.julialang.org/t/help-in-sum-columns/77883 "2022-03-14T19:16:13Z")

</div>

Hi everybody! I’m new in Julia and need a simple help. I want to sum some columns of my dataframe and input that sum in a new column, but i’m trying with this and are not working. colunasSum = \[:CancIncorreto,:DifColet…

---

## [Parallel Reductions with Zygote](https://discourse.julialang.org/t/parallel-reductions-with-zygote/75969)

<div class="topic-metadata">

**Author:** [@SEA](https://discourse.julialang.org/u/SEA)\
**Replies:** 11\
**Last updated:** [February 9, 2022, 1:05pm UTC](https://discourse.julialang.org/t/parallel-reductions-with-zygote/75969 "2022-02-09T13:05:38Z")

</div>

I am trying to speed up the gradient computation of a model of the form sum(m-\>m(x), models), where models is an array of constituent models. This is (supposed to be) easily parallelizable, but I am having trouble maki…

---

## [How to improve performance of sum()](https://discourse.julialang.org/t/how-to-improve-performance-of-sum/64928)

<div class="topic-metadata">

**Author:** [@andrewkhardy](https://discourse.julialang.org/u/andrewkhardy)\
**Replies:** 19\
**Last updated:** [July 19, 2021, 8:13pm UTC](https://discourse.julialang.org/t/how-to-improve-performance-of-sum/64928 "2021-07-19T20:13:40Z")

</div>

Hello. I recently tried out Julia because I was hoping for a speed improvement over numpy.sum(). I’m trying to benchmark a sum for a 2D integral and so far my Julia code is much slower. I’m wondering if there’s any perf…

---

## [Infinite series in Julia](https://discourse.julialang.org/t/infinite-series-in-julia/62151)

<div class="topic-metadata">

**Author:** [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Replies:** 6\
**Last updated:** [May 31, 2021, 7:56pm UTC](https://discourse.julialang.org/t/infinite-series-in-julia/62151 "2021-05-31T19:56:53Z")

</div>

What is the standard approach for computing an infinite series in Julia? Define the sequence of partial sums: s\_{N} \\equiv \\sum\_{j=1}^{N} f(j) In Julia this is: s(N) = sum(f, 1:N) The only way I know is to either com…
