Last active
August 31, 2024 14:21
-
-
Save ap29600/e3b3f014235fbe48c35634a81a8f98f2 to your computer and use it in GitHub Desktop.
A BQN function performing associative reductions on paths of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# s0βΏo1 π½_associativeReduce π©: π½Β΄Β¨ on paths in π© of s steps with stride (=π©)βo1 | |
# | |
# example: | |
# m β‘ 4βΏ(Β―1βΏ1) +_associativeReduce x | |
# +-------+ +-------------+ | |
# | . . . | | . . . . . . | | |
# | y . . | | . . . d . . | | |
# +-------+ | . . c . . . | | |
# | . b . . . . | | |
# y β‘ +Β΄aβΏbβΏcβΏd | a . . . . . | | |
# +-------------+ | |
# | |
# shortened forms: | |
# s0βΏo0 π½_associativeReduce π© ββ s0βΏ(β₯o0) π½_associativeReduce π© | |
# s0 π½_associativeReduce π© ββ s0βΏ1 π½_associativeReduce π© | |
_associativeReduce { | |
sβΏoβ2βπ¨βΎ1 | |
o<β© | |
iβ2ββ1+β2ββΌs # powers of 2: step sizes | |
mβ2|βsΓ·i # base 2 representation of s | |
lβΒ»+`m/i | |
# <combine> <--trim to fit--> <select> <--length-i windows--> | |
π½Β΄ (-oΓs-l+m/i)βΒ¨(oΓl)βΒ¨ m/ (-βΈβπ½β)Λ`(<π©)βΎoΓΒ―1βi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment