What are practical uses for STL's 'partial_sum'?

前端 未结 11 1683
耶瑟儿~
耶瑟儿~ 2021-02-04 03:47

What/where are the practical uses of the partial_sum algorithm in STL?

What are some other interesting/non-trivial examples or use-cases?

相关标签:
11条回答
  • 2021-02-04 04:41

    I often use partial sum not to sum but to calculate the current value in the sequence depending on the previous.

    For example, if you integrate a function. Each new step is a previous step, vt += dvdt or vt = integrate_step(dvdt, t_prev, t_prev+dt);.

    0 讨论(0)
  • 2021-02-04 04:42

    You know, I actually did use partial_sum() once... It was this interesting little problem that I was asked on a job interview. I enjoyed it so much, I went home and coded it up.

    The problem was: Given a sequential sequence of integers, find the shortest sub-sequence with the highest value. E.g. Given:

    Value: -1  2  3 -1  4 -2 -4  5
    Index:  0  1  2  3  4  5  6  7
    

    We would find the subsequence [1,4]

    Now the obvious solution is to run with 3 for loops, iterating over all possible starts & ends, and adding up the value of each possible subsequence in turn. Inefficient, but quick to code up and hard to make mistakes. (Especially when the third for loop is just accumulate(start,end,0).)

    The correct solution involves a divide-and-conquer / bottom up approach. E.g. Divide the problem space in half, and for each half compute the largest subsequence contained within that section, the largest subsequence including the starting number, the largest subsequence including the ending number, and the entire section's subsequence. Armed with this data we can then combine the two halves together without any further evaluation of either one. Obviously the data for each half can be computed by further breaking each half into halves (quarters), each quarter into halves (eighths), and so on until we have trivial singleton cases. It's all quite efficient.

    But all that aside, there's a third (somewhat less efficient) option that I wanted to explore. It's similar to the 3-for-loop case, only we add the adjacent numbers to avoid so much work. The idea is that there's no need to add a+b, a+b+c, and a+b+c+d when we can add t1=a+b, t2=t1+c, and t3=t2+d. It's a space/computation tradeoff thing. It works by transforming the sequence:

    Index: 0 1 2  3  4
    FROM:  1 2 3  4  5
      TO:  1 3 6 10 15
    

    Thereby giving us all possible substrings starting at index=0 and ending at indexes=0,1,2,3,4.

    Then we iterate over this set subtracting the successive possible "start" points...

    FROM:  1 3 6 10 15
      TO:  - 2 5  9 14
      TO:  - - 3  7 12
      TO:  - - -  4  9
      TO:  - - -  -  5
    

    Thereby giving us the values (sums) of all possible subsequences.

    We can find the maximum value of each iteration via max_element().

    The first step is most easily accomplished via partial_sum().

    The remaining steps via a for loop and transform(data+i,data+size,data+i,bind2nd(minus<TYPE>(),data[i-1])).

    Clearly O(N^2). But still interesting and fun...

    0 讨论(0)
  • 2021-02-04 04:44

    Partial sums are often useful in parallel algorithms. Consider the code

    for (int i=0; N>i; ++i) {
      sum += x[i];
      do_something(sum);
    }
    

    If you want to parallelise this code, you need to know the partial sums. I am using GNUs parallel version of partial_sum for something very similar.

    0 讨论(0)
  • 2021-02-04 04:51

    One thing to note about partial sum is that it is the operation that undoes adjacent difference much like - undoes +. Or better yet if you remember calculus the way differentiation undoes integration. Better because adjacent difference is essentially differentiation and partial sum is integration.

    Let's say you have simulation of a car and at each time step you need to know the position, velocity, and acceleration. You only need to store one of those values as you can compute the other two. Say you store the position at each time step you can take the adjacent difference of the position to give the velocity and the adjacent difference of the velocity to give the acceleration. Alternatively, if you store the acceleration you can take the partial sum to give the velocity and the partial sum of the velocity gives the position.

    Partial sum is one of those functions that doesn't come up too often for most people but is enormously useful when you find the right situation. A lot like calculus.

    0 讨论(0)
  • 2021-02-04 04:52

    Last time I (would have) used it is when converting a discrete probability distribution (an array of p(X = k)) into a cumulative distribution (an array of p(X <= k)). To select once from the distribution, you can pick a number from [0-1) randomly, then binary search into the cumulative distribution.

    That code wasn't in C++, though, so I did the partial sum myself.

    0 讨论(0)
提交回复
热议问题