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)

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?

  • 1
    $\begingroup$ Use Accumulate and as to b you need also Reverse $\endgroup$ – Coolwater Apr 21 '17 at 15:28
  • 2
    $\begingroup$ Caution: The notation k_i does NOT define a variable but rather a pattern (see reference.wolfram.com/language/guide/Patterns.html). $\endgroup$ – yohbs Apr 21 '17 at 16:17
  • $\begingroup$ 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 $\endgroup$ – laudiacay Apr 21 '17 at 22:21
  • $\begingroup$ Also, @Coolwater, I think that doesn't work because the indices of the means are indices in the table, rather than in the sum? $\endgroup$ – laudiacay Apr 21 '17 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.