News for hackers. Stuff that matters.
Re:Random walk in 2 lines of J by mgraczyk on Saturday October 15, 2022 @08:26PM (#33219951)
I think that specific example is very easy

    walks = np.random.choice([-1,1], (100, 1000))
    running_sum = np.cumsum(walks, axis=0)
    plt.plot(running_sum)

Intuitively it seems like the more complicated the problem, the bigger the advantage for a language like numpy where functions are used for most higher level abstractions. For example what if you want to change the logic to do a stopped game, where you now keep each walk at zero once it becomes zero, and count the portion of walks that stop after some fixed duration? In numpy I can always fall back to list comprehensions or for loops for something like that. Is it easy to express logic like this in J?
Re:Random walk in 2 lines of J by rak1507 on Saturday October 15, 2022 @08:39PM (#33220036)
Yes, that example is easy, although you got it wrong, I said 100 random walks of length 1000, which should be (1000, 100) not (100, 1000). However, there are still more 'moving parts' in numpy than in APL/J, you have to modify the tuple, and the axis, and other things. It all adds up when making lots of small iterations.

The modification you mentioned is straightforward in any array language. Here is what you mentioned in APL (I know APL more than J but I'm sure the J would be basically the same):

  ⊃⍤⍸⍤1⊢0=+\¯1*?100 1000⍴2
This is the number of steps to return to 0 for each of 100 random walks of length 1000. (It is then easy to analyse the frequency/average/etc)
Re:Random walk in 2 lines of J by mgraczyk on Saturday October 15, 2022 @08:50PM (#33220079)
Interesting, but I guess I'd have to learn APL or J to compare. Without understanding what those symbols mean, it seems much less like "math" than the numpy version.

    1 + np.min(np.argwhere(running_sum[1:] == 0), axis=0)
Re:Random walk in 2 lines of J by rak1507 on Saturday October 15, 2022 @08:57PM (#33220128)
⊃ is 'first', ⍸ is where, f⍤g is like function composition, and ⍤1 means apply to each rank 1 (vector), so ⊃⍤⍸⍤1 means 'the first true for each vector'
Re:Random walk in 2 lines of J by mgraczyk on Saturday October 15, 2022 @09:09PM (#33220197)
Interesting, thanks for explaining!