Skip to content

Instantly share code, notes, and snippets.

@justin2004
Last active August 25, 2025 15:28
Show Gist options
  • Select an option

  • Save justin2004/ece4495e32bd783b7a4c29ecf5ca7989 to your computer and use it in GitHub Desktop.

Select an option

Save justin2004/ece4495e32bd783b7a4c29ecf5ca7989 to your computer and use it in GitHub Desktop.
numpy broadcasting in APL
⎕IO←0
m←2 3 4 3⍴⍳72
⍝ the goal is to reproduce this numpy broadcasting behavior in APL
⍝ m/m.sum(axis=0, keepdims=True)
normalize_and_keepdims←{⍵÷(⍴⍵)⍴(⍺⌷⍴⍵)/[(0⌈⍺-1)]+/[⍺]⍵}
0 normalize_and_keepdims m
0 0.02631578947 0.05
0.07142857143 0.09090909091 0.1086956522
0.125 0.14 0.1538461538
0.1666666667 0.1785714286 0.1896551724
0.3333333333 0.3421052632 0.35
0.3571428571 0.3636363636 0.3695652174
0.375 0.38 0.3846153846
0.3888888889 0.3928571429 0.3965517241
0.4 0.4032258065 0.40625
0.4090909091 0.4117647059 0.4142857143
0.4166666667 0.4189189189 0.4210526316
0.4230769231 0.425 0.4268292683
0.6 0.5967741935 0.59375
0.5909090909 0.5882352941 0.5857142857
0.5833333333 0.5810810811 0.5789473684
0.5769230769 0.575 0.5731707317
0.5714285714 0.5697674419 0.5681818182
0.5666666667 0.5652173913 0.5638297872
0.5625 0.5612244898 0.56
0.5588235294 0.5576923077 0.5566037736
0.7142857143 0.7093023256 0.7045454545
0.7 0.6956521739 0.6914893617
0.6875 0.6836734694 0.68
0.6764705882 0.6730769231 0.6698113208
1 normalize_and_keepdims m
0 0.02564102564 0.04761904762
0.06666666667 0.08333333333 0.09803921569
0.1111111111 0.1228070175 0.1333333333
0.1428571429 0.1515151515 0.1594202899
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.6666666667 0.641025641 0.619047619
0.6 0.5833333333 0.568627451
0.5555555556 0.5438596491 0.5333333333
0.5238095238 0.5151515152 0.5072463768
0.25 0.2517006803 0.2533333333
0.2549019608 0.2564102564 0.2578616352
0.2592592593 0.2606060606 0.2619047619
0.2631578947 0.2643678161 0.2655367232
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.3333333333 0.3333333333 0.3333333333
0.4166666667 0.4149659864 0.4133333333
0.4117647059 0.4102564103 0.4088050314
0.4074074074 0.4060606061 0.4047619048
0.4035087719 0.4022988506 0.4011299435
2 normalize_and_keepdims m
0 0.04545454545 0.07692307692
0.1666666667 0.1818181818 0.1923076923
0.3333333333 0.3181818182 0.3076923077
0.5 0.4545454545 0.4230769231
0.1818181818 0.1857142857 0.1891891892
0.2272727273 0.2285714286 0.2297297297
0.2727272727 0.2714285714 0.2702702703
0.3181818182 0.3142857143 0.3108108108
0.2105263158 0.2118644068 0.2131147541
0.2368421053 0.2372881356 0.237704918
0.2631578947 0.2627118644 0.262295082
0.2894736842 0.2881355932 0.2868852459
0.2222222222 0.2228915663 0.2235294118
0.2407407407 0.2409638554 0.2411764706
0.2592592593 0.2590361446 0.2588235294
0.2777777778 0.2771084337 0.2764705882
0.2285714286 0.2289719626 0.2293577982
0.2428571429 0.2429906542 0.2431192661
0.2571428571 0.2570093458 0.2568807339
0.2714285714 0.2710280374 0.2706422018
0.2325581395 0.2328244275 0.2330827068
0.2441860465 0.2442748092 0.2443609023
0.2558139535 0.2557251908 0.2556390977
0.2674418605 0.2671755725 0.2669172932
>>> m = np.arange(72).reshape(2, 3, 4, 3)
>>> m/m.sum(axis=0, keepdims=True)
array([[[[0. , 0.02631579, 0.05 ],
[0.07142857, 0.09090909, 0.10869565],
[0.125 , 0.14 , 0.15384615],
[0.16666667, 0.17857143, 0.18965517]],
[[0.2 , 0.20967742, 0.21875 ],
[0.22727273, 0.23529412, 0.24285714],
[0.25 , 0.25675676, 0.26315789],
[0.26923077, 0.275 , 0.2804878 ]],
[[0.28571429, 0.29069767, 0.29545455],
[0.3 , 0.30434783, 0.30851064],
[0.3125 , 0.31632653, 0.32 ],
[0.32352941, 0.32692308, 0.33018868]]],
[[[1. , 0.97368421, 0.95 ],
[0.92857143, 0.90909091, 0.89130435],
[0.875 , 0.86 , 0.84615385],
[0.83333333, 0.82142857, 0.81034483]],
[[0.8 , 0.79032258, 0.78125 ],
[0.77272727, 0.76470588, 0.75714286],
[0.75 , 0.74324324, 0.73684211],
[0.73076923, 0.725 , 0.7195122 ]],
[[0.71428571, 0.70930233, 0.70454545],
[0.7 , 0.69565217, 0.69148936],
[0.6875 , 0.68367347, 0.68 ],
[0.67647059, 0.67307692, 0.66981132]]]])
>>> m/m.sum(axis=1, keepdims=True)
array([[[[0. , 0.02564103, 0.04761905],
[0.06666667, 0.08333333, 0.09803922],
[0.11111111, 0.12280702, 0.13333333],
[0.14285714, 0.15151515, 0.15942029]],
[[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333]],
[[0.66666667, 0.64102564, 0.61904762],
[0.6 , 0.58333333, 0.56862745],
[0.55555556, 0.54385965, 0.53333333],
[0.52380952, 0.51515152, 0.50724638]]],
[[[0.25 , 0.25170068, 0.25333333],
[0.25490196, 0.25641026, 0.25786164],
[0.25925926, 0.26060606, 0.26190476],
[0.26315789, 0.26436782, 0.26553672]],
[[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333]],
[[0.41666667, 0.41496599, 0.41333333],
[0.41176471, 0.41025641, 0.40880503],
[0.40740741, 0.40606061, 0.4047619 ],
[0.40350877, 0.40229885, 0.40112994]]]])
>>> m/m.sum(axis=2, keepdims=True)
array([[[[0. , 0.04545455, 0.07692308],
[0.16666667, 0.18181818, 0.19230769],
[0.33333333, 0.31818182, 0.30769231],
[0.5 , 0.45454545, 0.42307692]],
[[0.18181818, 0.18571429, 0.18918919],
[0.22727273, 0.22857143, 0.22972973],
[0.27272727, 0.27142857, 0.27027027],
[0.31818182, 0.31428571, 0.31081081]],
[[0.21052632, 0.21186441, 0.21311475],
[0.23684211, 0.23728814, 0.23770492],
[0.26315789, 0.26271186, 0.26229508],
[0.28947368, 0.28813559, 0.28688525]]],
[[[0.22222222, 0.22289157, 0.22352941],
[0.24074074, 0.24096386, 0.24117647],
[0.25925926, 0.25903614, 0.25882353],
[0.27777778, 0.27710843, 0.27647059]],
[[0.22857143, 0.22897196, 0.2293578 ],
[0.24285714, 0.24299065, 0.24311927],
[0.25714286, 0.25700935, 0.25688073],
[0.27142857, 0.27102804, 0.2706422 ]],
[[0.23255814, 0.23282443, 0.23308271],
[0.24418605, 0.24427481, 0.2443609 ],
[0.25581395, 0.25572519, 0.2556391 ],
[0.26744186, 0.26717557, 0.26691729]]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment