# How to sum a vector in accumulated form to another vector?

Hi. I have been struggling trying to figure it out how to sum a vector. For example, I got those numers:

``````a= Float64
0.143816064
0.130130156
0.117746635
0.106541561
0.096402791
0.087228852
0.07892793
0.071416944
0.064620723
0.058471248
0.052906973
``````

And In another vector I want the sum of the rows in this way:

``````row 1 = 0.143816064
row 2 = 0.273946221 (row 1  + row 2).
row 3 = 0.391692855 (row 1 + row 2 + row3)
row 4... = 0.498234416 (row 1 + row 2 + row 3 + row 4...)
... row 11 = 1.008209877 (row 1+... + row 10)
``````

Any help would be really appreciated

1 Like
``````julia> a
10-element Array{Float64,1}:
0.14606632510509954
0.1756703572398095
0.7302286092293604
0.8201673548813444
0.47004672453621676
0.01988181685994972
0.6152573987998071
0.36977676750224187
0.7434864001324983
0.9583002695417104

julia> cumsum(a)
10-element Array{Float64,1}:
0.14606632510509954
0.32173668234490904
1.0519652915742694
1.8721326464556138
2.342179370991831
2.3620611878517805
2.9773185866515877
3.3470953541538293
4.090581754286328
5.048882023828038
``````

Edit: just noticed there is `cumsum` in base 1 Like

Thank you very much! But the think I want to do is something like this:

For example I want in
row 1 = 0.143816064.
row 2 = 0.273946221 (row 1 + row 2).
row 3 = 0.391692855 (row 1 + row 2 + row3)
row 4 = 0.498234416 (row 1 + row 2 + row 3 + row 4)

This until row 11, which total sum must be 1.008209877

Yes, I understood. This is exactly what `cumsum` does 1 Like

While in this case there does happen to be a standard-library function (`cumsum`) that does exactly what you want, if you are “struggling” with this problem then I suspect that you may be approaching Julia in the wrong way.

In some other languages, numerical programming is an exercise in “mining” a set of “standard” libraries, since these are the only fast operations, so even doing something very basic can be a struggle if you don’t know exactly which library function to call.

In Julia, re-using mature library code is still a good thing to do! But it is not your only choice — there is absolutely nothing wrong with writing your own loops. And implementing a trivial function like `cumsum` should not be a struggle unless you are an absolute beginner at programming. For example:

``````function my_cumsum(a)
s = copy(a)
for i = firstindex(s)+1:lastindex(s)
s[i] += s[i-1]
end
return s
end
``````

is a perfectly reasonable implementation. It is a bit slower than `Base.cumsum`, but most of that difference can be made up by adding `@inbounds` to the `s[i] += s[i-1]` line. If this were performance-critical for you (unlikely), the rest of the difference could be made up by avoiding the separate `copy` step (combining the `copy` with the sum):

``````function my_cumsum_faster(a)
s = similar(a)
isempty(s) && return s
sum = first(a)
for i = firstindex(s)+1:lastindex(s)
@inbounds s[i] = (sum += a[i])
end
return s
end
``````

This loop is also less accurate than `Base.cumsum`, because the latter uses a recursive algorithm called pairwise summation that accumulates roundoff errors much more slowly. Taking advantage of non-obvious algorithms like that written by experienced developers is indeed an advantage of utilizing mature libraries!

However, in a real application you are probably not just doing `cumsum`. Probably you are doing additional operations on the data before or after the summation. In that case, there is often a real advantage in writing your own loop, in order to avoid multiple passes over the same data or, worse, allocating lots of temporary arrays (one for each “vectorized” operation). So, there is a tradeoff, even for using optimized libraries, compared to writing your own code.

Programming is hard for complicated problems! But if you find yourself struggling to code a simple problem, then maybe you are trying too hard to shoehorn your problem into an existing library function rather than just writing your own loop (or recursion, or …). Languages like R, Python, and Matlab train you to “never” write your own inner loops, but in Julia you should unlearn that lesson.

7 Likes

Thank you very much for your answer!

Actually I am new into programming world. I have not ever program a simple code before. Indeed, Julia is my first approach to. I really appreciated your words and your tips. I must say that it has been very difficult to me.

Anyways, thanks for your help. I was even trying to do a loop but with no success but with those tips from the user before and yours I have learned alot.

2 Likes

In that case, welcome and enjoy your journey! You will probably want to go through some introductory programming tutorials — for example, Think Julia is a new book targeted at absolute beginners, which might be useful for you. And learning to write a function like `cumsum` yourself is a good exercise.

5 Likes

no need to feel uncomfortable asking ‘dumb’ question here, this is what community for, after all. Really happy to see people are trying out Julia as their first language.

4 Likes

I wish my first language was Julia Welcome @Halor and feel free to ask questions 2 Likes