Questions tagged [accumulate]

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

When we are interested only in the final result, it is equivalent to left folding; and if we keep the whole sequence of results progressively calculated, it is known as left scan.

319 questions
5
votes
2 answers

Understanding how the accumulate function works

I read the manual for using accumulate saying that it is a 2-argument function. I don't understand the given example: 1:5 %>% accumulate(`+`) #> [1] 1 3 6 10 15 If accumulate is a 2-argument function, should the first element that it outputs be…
tobinz
  • 305
  • 1
  • 7
5
votes
3 answers

Cumulative product of (1-previous_record)*current_record

The data frame contains two variables (time and rate) and 10 observations time <- seq(1:10) rate <- 1-(0.99^time) dat <- data.frame(time, rate) I need to add a new column (called new_rate). new_rate is defined as follows Note: new_rate_1 is the…
user9292
  • 1,125
  • 2
  • 12
  • 25
5
votes
1 answer

Why does g++ use movabs, and with a weird constant, for a simple reduction?

I'm compiling this simple program: #include int main() { int numbers[] = {1, 2, 3, 4, 5}; auto num_numbers = sizeof(numbers)/sizeof(numbers[0]); return std::accumulate(numbers, numbers + num_numbers, 0); } which sums up the…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
1 answer

Pythonic reduce with accumlation and arbitrary lambda function?

What would be the Pythonic way of performing a reduce with accumulation? For example, take R's Reduce(). Given a list and an arbitrary lambda function it allows for yielding a vector of accumulated results instead of only the final result by setting…
5
votes
2 answers

column vector with row means -- with std::accumulate?

In an effort to be as lazy as possible I read in a matrix as vector< vector > data ( rows, vector ( columns ) ); and try to use as many STL goodies as I can. One thing I need to do next is to compute the row means. In C-style…
alle_meije
  • 2,424
  • 1
  • 19
  • 40
4
votes
2 answers

How to accumulate data-sets?

I have vector with values between 1 and N > 1. Some values COULD occur multiple times consecutively. Now I want to have a second row which counts the consecutively entries and remove all those consecutively occuring entries, e.g.: A = [1 2 1 1 3 2 4…
tim
  • 9,896
  • 20
  • 81
  • 137
4
votes
2 answers

Conditional cumulative sum from two columns

I can't get my head around the following problem. Assuming the follwoing data: library(tidyverse) df <- tibble(source = c("A", "A", "B", "B", "B", "C"), value = c(5, 10, NA, NA, NA, 20), add = c(1, 1, 1, 2, 3, 4)) What…
deschen
  • 10,012
  • 3
  • 27
  • 50
4
votes
3 answers

Identify consecutive duplicates in R using accumulate

Let me share an example of what I'm trying to do, since the title may not be as clear as I'd like it to be. data <- tibble(week=1:10,name=c(rep("Joe",10)),value=c(.9,.89,.99,.98,.87,.89,.93,.92,.98,.9), wanted =…
4
votes
1 answer

What is the most accurate way to count a sum of vector elements using std::accumulate?

Possible Duplicate: C++ float precision question I've got a problem of determining the most precise method of the three to calculate the sum of vector elements, which can be only positive numbers, using std::accumulate. 1) double…
rightaway717
  • 2,631
  • 3
  • 29
  • 43
4
votes
2 answers

Fast way to calculate value in cell based on value in previous row (data.table)

Say I have the following dataset dt and a constant constant. dt <- structure(list(var1 = c(-92186.7470607738, -19163.5035325072, -18178.8396858014, -9844.67882723287, -16494.7802822178, -17088.0576319257 ), var2 =…
maarvd
  • 1,254
  • 1
  • 4
  • 14
4
votes
3 answers

Rolling min of a Pandas Series without window / cumulative minimum / expanding min

I'm looking for a way to calculate with Python Pandas rolling(*) min of a Series without window. Let's consider the following Series In [26]: s = pd.Series([10, 12, 14, 9, 10, 8, 16, 20]) Out[26]: 0 10 1 12 2 14 3 9 4 10 5 8 6 …
scls
  • 16,591
  • 10
  • 44
  • 55
4
votes
3 answers

Why does haskell stop when trying to add a string to the end of this string?

I am trying to add a string to the end of a string in Haskell. albumStr = "" main = do let albumStr = albumStr ++ "nothing" print albumStr Whenever i run this it just gets stuck on " in the console and i have to terminate it. Why? and how do…
Dean
  • 80
  • 7
4
votes
1 answer

What are the Requirements on accumulate's Functor?

I've written an answer here: https://stackoverflow.com/a/44481507/2642059 which uses accumulate. The functor must be binary with a signature like: Ret op(const auto& a, const auto& b) but: The signature does not need to have const & The…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
4
votes
3 answers

Cumulative sum of vectors from a point

I have a list of vectors, a list of scalars and a start point represented by a tuple. vecs = [(1,1), (2,3), (-1,1)] scalars = [2, 3, 2] start = (-5,0) I have a function to add "k times" a vector to a point. def add_vector(point, k, vec): return…
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
4
votes
1 answer

Flattening lazy list of lazy lists in Scheme via higher-order accumulation procedure

I'm trying to find an implementation which flattens a lazy list of lazy lists using interleave and lz-lst-accumulate which are procedures that I wrote. This is the code so far: (define lz-lst-accumulate (lambda (op initial lz) (if (empty? lz) …
kitsuneFox
  • 1,243
  • 3
  • 18
  • 31
1 2
3
21 22