Timeline for Convert list of lists to cumulative join
Current License: CC BY-SA 3.0
6 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 1, 2013 at 19:07 | comment | added | Leonid Shifrin | This is nice, +1. For very large list of lists, you may want to memoize the partial linked lists instead, and then Flatten them and use a single Join when the function is later called for a particular n. This would make the initial call very fast (linear in the number of lists), for the price of some (not very significant) added overhead for the subsequent calls for specific n. As of now, your initial call is O(m^2), where m is the total number of elements in all lists. | |
| Jul 1, 2013 at 12:29 | comment | added | Theo Tiger | You are right, your version exactly matches the structures being asked for. However, rather than fulfilling that specific structure, I would take a wild guess and say to the questioner that list and cumulativeList being regular Lists is more likely to be what he wants. This is why I interpreted his question like that in first place, but I will ask him. | |
| Jul 1, 2013 at 12:27 | comment | added | Theo Tiger | FoldList will also avoid re-evaluation of previous terms, as does memoization. Yep, you either need to clean up after computation or use something like Module[{comulativeList},...] in order to re-use this function for a different list and to release memory consumed by the down-values. However, I somehow feel this is too complicated for this rather straight-forward problem, what do you think? | |
| Jul 1, 2013 at 12:15 | comment | added | rm -rf♦ | @TheoTiger What do you mean this will "re-evaluate"? The explicit purpose of memoization is to compute once and store the result, so as to avoid re-evaluation, so I'm not sure why you think this does. Also, what do you mean "variable localization" and "memory overhead"? This is the exact structure that the OP specifically asked for in the question, with the addition of down-values for 0 and n_ (which can be removed after computing, if it bothers you). | |
| Jul 1, 2013 at 12:06 | comment | added | Theo Tiger | hmm, although I like the concept of memoization, I think this is not a good example of it. FoldList is specifically made for this purpose and it will not re-evaluate. Also, intuitively, I would not do this with recursive code, but this might be something personal, as I tend to use recursive programming in Mathematica only on very rare occasions (same for iterative programming). Moreover, I believe memoization would require variable localization and it produces memory overhead, so in summary I don't feel this is as clean a code as with FoldList. | |
| Jul 1, 2013 at 11:44 | history | answered | rm -rf♦ | CC BY-SA 3.0 |