I want to find an efficient method to generalize how the function Accumulate works.
An example should make it clear:
x=Range[10];
Accumulate[x]
Out[36]= {1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
which can be replicated by:
Plus @@@ Table[Take[x, i], {i, Length@x}]
Out[35]= {1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
More generally, for any function f, what I want is a more efficient and elegant way of doing this:
f @@@ Table[Take[x, i], {i, Length@x}]
Out[34]= {f[1], f[1, 2], f[1, 2, 3], f[1, 2, 3, 4], f[1, 2, 3, 4, 5],
f[1, 2, 3, 4, 5, 6], f[1, 2, 3, 4, 5, 6, 7],
f[1, 2, 3, 4, 5, 6, 7, 8], f[1, 2, 3, 4, 5, 6, 7, 8, 9],
f[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
It looks like something that could be handled with FoldList or ArrayFilter, but I don't see how: I want f[1,2] not f[f[1],2]. That happens to work for f = Plus, but it won't necessarily work for other functions.
For example, suppose we want to produce a list of standardized values, but we only want to use the prior data to calculate the mean and standard deviation.
RepeatedTiming[
Prepend[Last /@
Standardize @@@ Table[{Take[x, i]}, {i, 2, Length@x}], 0] // N]
Out[60]= {0.00105957, {0., 0.707107, 1., 1.1619, 1.26491, 1.33631,
1.38873, 1.42887, 1.46059, 1.4863}}
or
RepeatedTiming[
Prepend[Last /@
Standardize @@@ List /@ Rest@Rest@FoldList[Append, {}, x] // N, 0]]
Out[84]= {0.00106662, {0, 0.707107, 1., 1.1619, 1.26491, 1.33631,
1.38873, 1.42887, 1.46059, 1.4863}}
or
RepeatedTiming[
Prepend[Last /@
Standardize @@@ List /@ Reverse@NestList[Most, x, Length@x - 2] //
N, 0]]
Out[109]= {0.00105479, {0, 0.707107, 1., 1.1619, 1.26491, 1.33631,
1.38873, 1.42887, 1.46059, 1.4863}}
Can anyone improve on the syntax and/or speed of these expressions?