# A Table of Sums, but faster

I'm trying to calculate the following lists a_j and b_j from some predefined values. (this is not code, it's just supposed to be a mathematical expression)

a = Table[Sum[f[k_i, v_i, ma_j],{i,1,j-1}],{j,lvls}]
b = Table[Sum[f[k_i, v_i, mb_j],{i,j,lvls}], {j, lvls}]

f is a compiled function, k and v are pairs in an Association, and ma and mb are indices of lists. lvls is the length of ma, mb, k, and v.

I've tried a lot of things, with every permutation I could imagine of KeyValueMaps, Totals, Sums, Tables, Map, MapIndexeds, etc.... and the following code has been the fastest. Unfortunately it still needs to be way quicker. k and v are defined with Keys and Values earlier on from the AssociationList, and that's fast enough that I can spare doing it.

f = (the compiled function, already plenty fast)
varhelper[x_Real,n_Integer,m_Integer]:=
Total[Map[f[x,#[[1]],#[[2]]]&,Transpose[{k[[n;;m]],v[[n;;m]]}]]];
Table[varhelper[means[[i]],1,i-1],{i,lvls}];

I feel like there's some way to do this really quickly and nicely with sequencefoldlist, but I am not sure how to implement that. Earlier I ran this code with the profiler, and found that the map in varhelper is the bottleneck. Does anyone have ideas for how to make this run quicker?

• Use Accumulate and as to b you need also Reverse – Coolwater Apr 21 '17 at 15:28
• Caution: The notation k_i does NOT define a variable but rather a pattern (see reference.wolfram.com/language/guide/Patterns.html). – yohbs Apr 21 '17 at 16:17
• Yeah, sorry, that wasn't meant to be code as much as show what I'm trying to do :) little bit of latex leaked in – laudiacay Apr 21 '17 at 22:21
• Also, @Coolwater, I think that doesn't work because the indices of the means are indices in the table, rather than in the sum? – laudiacay Apr 21 '17 at 22:24