A coding impromptu
This post is a rolling collection of algorithms and computational ideas I like, implemented in BQN:
Table of Contents
Extrapolating Perlis' remark1, it's likely that a group of 50 individuals would devise 35 to 40 distinct solutions to even the simplest problem in BQN. Therefore, I will frequently juxtapose my implementations with those of seasoned BQNators2.
Z algorithm
This is a very efficient procedure that finds prefix strings in linear time. The imperative implementation reads:
ZI β {πs: lβΏrβΏz β 0β0 0βΏ0βΏs z β£ { v β r(β’1βΈ+β’_while_{(π©+π¨)<β s ? =Β΄β¨π©,π©+π¨β©βΒ¨<s ; 0}<βΆ({zβΛπ©-l}β-+1)βΏ0)π© r <βΆ@βΏ{π: lβ©π©-v+1 β rβ©π©} π©+v-1 z vβΎ(π©βΈβ)β© }Β¨ ββ s } ZI "abacabadabacaba"
β¨ 15 0 1 0 3 0 1 0 7 0 1 0 3 0 1 β©
Two algorithmic improvements can be made here, namely only iterate over indices where the character found is equal to the first character, and only search to extend the count if it goes up to the end of r:
ZFun β {πs: CountEq β { 1βΈ+β’_while_((β π¨)βΈβ€βΆβ¨ββπ¨β‘ββπ©,0β©) 0 } lβrβ0 β Ulr β {(rββ©π¨+π©)>r ? lβ©π¨ β π©; π©} SearchEq β β£ Ulr β’ + + CountEqβ(ββs) β’ Set β {iππ©: ((r-i) (i SearchEq 0ββ£)ββ€ (i-l)βπ©)βΎ(iβΈβ) π© } (β½1β/ββΈ=s) SetΒ΄Λ βΛβ s }
I came up with two array versions, with quadratic and cubic time complexities respectively:
ZAQ β Β―1ββ(+´·β§`β£=β βΈβ)Β¨< ZAC β (+Β΄β§`)Β¨<=βββ {Β«βπ¨π©}β< (ZAQβ‘ZAC)βΆ@βΏZAC "abacabadabacaba"
β¨ 15 0 1 0 3 0 1 0 7 0 1 0 3 0 1 β©
With further refinements, the earlier solutions can be transformed into:
ZAQβΏZAC β {(+Β΄β§`)Β¨π}Β¨ β¨β ββ=β½ββ, <=Β«β(βββ )β©
Longest increasing sub-sequence
This problem can be solved in \(O(n\log n)\) using dynamic programming. Here is an imperative implementation which is quadratic, but can be optimized:
LISI β { kβΏdp β Β―1βΏ(βΒ¨π©) {i β β§Β΄βΆ(βββ0)βΏ{π:k+β©1} dp<π© β dp π©βΎ(iβΈβ)β©}Β¨ π© +Β΄β>dp } LISIΒ¨ β¨0βΏ1βΏ0βΏ3βΏ2βΏ3, 10βΏ9βΏ2βΏ5βΏ3βΏ7βΏ101βΏ18, 7βΏ7βΏ7βΏ7βΏ7β©
β¨ 4 4 1 β©
A more elegant array solution is:
LISA β +Β΄ββ βΒ¨{π¨βΎ((βπ©βπ¨-1)βΈβ)π©}Β΄β½ LISAΒ¨ β¨0βΏ1βΏ0βΏ3βΏ2βΏ3, 10βΏ9βΏ2βΏ5βΏ3βΏ7βΏ101βΏ18, 7βΏ7βΏ7βΏ7βΏ7β©
β¨ 4 4 1 β©
Let's )explain
this optimized version, so we can truly appreciate its beauty:
+Β΄ββ βΒ¨{π¨βΎ((βπ©βπ¨-1)βΈβ)π©}Β΄β½ β β β ββ β β β β β β β β β {βΌβββββΌββΌββΌβββΌββΌβΒ΄β β β βΒ¨ β β β β β β ββ β β βββΌβββββΌββΌββΌβββΌββΌββΌβ½ β ββ ββββΌβββββΌββΌββΌβββΌββΌββ +Β΄ β β β β β β β βββ β β β β β β β β π¨-1 β β β π©βββ β β β βββ β β β ββββββββΈβ β π¨βΎββββββββββ β βββββββββββββπ© βΆβββββββββ
The full expression is structured as a two-train: we sum all finite entries from the
result of the rightmost three-train. The three-train is a right fold over the reversed
input, with an initial array of β
and the same length as the input. In each step
of the fold, we modify the right argument using under: we perform a binary search
with strict comparison to find where the next element should go.
The element is either placed at the end of the unfilled region, or it replaces
the first element that is greater than π¨
. Since BQN uses a based array model,
we pick the enclosed atom from this operation's result. So it goes3.
N-queens problem
This problem is the archetypal example of backtracking. Initially, I tried to solve it using a function to place the queens in the full board, hoping that it would lead to a more array oriented solution:
8 {((β¨βΒ΄0βΈ=)β¨(0=-βΒ΄)β¨0=+βΒ΄) π©-Β¨<βπ¨} 2βΏ3
ββ β΅ 0 1 0 1 0 1 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 β
This resulted in a more complicated algorithm, so I decided to go for the classical Wirth implementation:
NQ β {πn: VβΏP β {β£π(β’βΎ-β+)Β΄ββ’}Β¨ β¨β¨Β΄βΒ¨Λ, {1βΎ(π©βΈβ)π¨}Β¨β© {nβ π© ? +Β΄(π¨Vβ’)βΆβ¨(π©+1)πΛπ¨Pβ’,0β©β(π©ββ’)Β¨ βn ; 1 }ΛΒ΄ (0β0ΓΒ·βΒ¨β’βΎΒ·βΛ+Λ)n }
Which nicely compares with the OEIS sequence:
a000170 β 1βΏ0βΏ0βΏ2βΏ10βΏ4βΏ40βΏ92 a000170 β‘ NQΒ¨ 1+β8
1
And of course, in the implementation above I could have used a single array instead of three, but I find the resulting validation and position functions very aesthetic the way they are.
Majority element
The BoyerβMoore algorithm allows for finding the majority element (element that appears
more than βπ©Γ·2
times in the array) in linear time. If such element exists, then it is
equal to the mode of the data, and for this task we have a nice array solution. The original
implementation could be expressed as:
BM β {vβ0 β Iββ’β£=βΆ{π:v+β©1}βΏ{π:v-β©1} β 0{π:v=0}βΆβ¨I,IΛβ£β©Β΄π©} BM 6βΏ1βΏ3βΏ1βΏ3βΏ3βΏ4βΏ3βΏ3βΏ5
3
The previous fold tracks the majority element as state, a more elegant approach maintains the number of votes:
BM β {eβ@ β 0{π©=0 ? eβ©π¨β1 ; π©+Β―1βeβ’π¨}Β΄π© β e} BM 6βΏ1βΏ3βΏ1βΏ3βΏ3βΏ4βΏ3βΏ3βΏ5
3
An identity on the naturals
Some time ago, while working on performance optimization of linear algebra operations with Boolean arrays, I encountered an interesting summation property for an array \(a\) of length \(n\):
\begin{equation*} \sum_{i | a_i \neq 0} \sum_{j=i+1} f_j = \sum_{j=0} f_j \sum_{i < j | a_i \neq 0} 1 \end{equation*}It turns out that the RHS can be elegantly transformed into a scan, giving rise to a beautiful identity that applies to all natural numbers, not just Booleans as I initially thought:
(+`β‘Β·+Β΄/β€β<βΛ) β’rand.RangeΛ 1e3
1
This identity holds because βΛ
represents the indices i
of the list,
and since +Β΄(/π©)=i ββ iβπ©
, the fold sums all the elements in π©
up to i
, for
i
in the range of the length of the list. Ergo, a scan.
Depth of nested lists
Studying tree algorithms in APL, I learned about the depth vector representation. If the nested object in consideration is a string, the best approach is using boolean masks. However, when dealing with a BQN list, recursion becomes necessary to determine the depth of nested elements. Hereβs how it can be implemented:
{=βΆβ¨β0, 1+Β·βΎπΒ¨β©π©} β¨1, β¨2, β¨3β©, β¨4, β¨5, β¨6, 7β©β©β©β©, 1β©
β¨ 1 2 3 3 4 5 5 1 β©
H-index
This metric is one of the reasons for the deplorable state of modern academia, and the headaches for outsiders trying to get in. Consider that Peter Higgs has an estimated h-index of only 12. By contrast, a random professor nowadays boasts an h-index ten times as high, and exponentially less impact. Enough of ranting, let's concentrate on finding an elegant way to implement this useless thing:
HL β (+Β΄βΒ«βΛβ€+`βΎβ½)Β·/βΌβ βΈβ HS β +Β΄β¨β₯1+βΛ (HLβ‘HS)βΆ@βΏHL 14βΏ14βΏ11βΏ9βΏ5βΏ5βΏ1βΏ1βΏ1βΏ1βΏ0
5
If someone ever published that much, sorting will eventually be slower:
HLβΏHS {πβ’_timedπ©}Β¨< 1e8 β’rand.Range 1e3
β¨ 0.083824959 0.21801262700000001 β©
A testament to the idea that the simplest solution in BQN is often the most efficient:
I initially clip my citations array with {β Β¨ββ βπ©Β¨βΎ(β₯ββ βπ©βΈ/)π©}
, which is just /βΌβ βΈβ
.
Footnotes:
Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English, Alan J. Perlis (1978). From jsoftware's papers collection.
Initially, I intended to rigorously attribute all contributions, but this quickly filled the text with footnotes. I often get help streamlining my solutions from Marshall Lochbaum (the BQN creator), dzaima (the CBQN developer), and other fine folks from the BQN matrix room, thank you all! Please check the logs for more context.