4 ms·
I have only passing experience with numpy, but I suspect the following "shift a matrix in all 8 directions", used in the famous APL/J game of life solution [0][
by jonahx 6y ago
I have only passing experience with numpy, but I suspect the following "shift a matrix in all 8 directions", used in the famous APL/J game of life solution [0][1], would require multiple statements as well as being more verbose in numpy (I'm guessing you'd need to use "roll" multiple times?):
(>,{;~i:1) |. i.4 4
Try it online: https://tio.run/##y/r/PzU5I19Bw06n2rou08pQU6FGTyFTz0TB5P9/AA https://tio.run/##y/r/PzU5I19Bw06n2rou08pQU6FGTyFTz0TB5P9/AA
[0] https://www.youtube.com/watch?v=a9xAKttWgP4&t=5s https://www.youtube.com/watch?v=a9xAKttWgP4&t=5s
[1] https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/NWJtjbVNbLU0o6209TXsdKqt6zKtDDXVavT@a3Jx5drqWSpYKqgANRgCMTo0ROMZYlVFF8jFlZqcka9gra9Qraaup6WukBZnpZGpZ6mpkPsfAA https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/NWJtjb...
- worc3131 6y agoThis 100 character version is probably the cleanest. M = np.reshape(np.arange(16),(4,4)) np.array([np.roll(M,(i,j),(0,1)) for i in [1,0,-1] for j in [1,0,-1]])
- jonahx 6y agoThat requires you to break out of the array paradigm and drop back down to procedural looping. Which is what you'd expect since numpy is array operations bolted onto python as a library and J is array operations baked into the language itself.