aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-10-18 13:16:09 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-10-18 13:16:09 -0400
commitd7d5c92da88b05871fd1fcdee06a9e0edf0c0604 (patch)
tree464b7c4a89fb164d5a7f212c4febd2a5d60de2b2 /examples
parentd88cda4278f2deffeb059c396bbf3872e9d9fef1 (diff)
Set inverses for permutation/combination index functions
Diffstat (limited to 'examples')
-rw-r--r--examples/fifty.bqn18
1 files changed, 13 insertions, 5 deletions
diff --git a/examples/fifty.bqn b/examples/fifty.bqn
index 32deb633..2a9cf317 100644
--- a/examples/fifty.bqn
+++ b/examples/fifty.bqn
@@ -105,13 +105,15 @@ InversePermutation ← ⍋ # Or ⊐⟜(↕≠) or ∾∘⊔
IndexFromPermutation‿PermutationFromIndex ← {
Rfd ← ⊢+´∘>¨¯1↓↓
Dfr ← (⍋∘⍋∾)´
- Ifp ← {⌽×`1+»↕≠𝕩} +´∘× Rfd
- Pfi ← Dfr (1+↕)⊸(⌽⊣|¯1↓·⌊∘÷`∾˜)
+ Ifp ← {𝕊⁼:𝕨Pfi𝕩; (⌽×`1+»↕≠𝕩) +´∘× Rfd𝕩}
+ Pfi ← {𝕊⁼:𝕨Ifp𝕩; w𝕊𝕩: Dfr (1+↕w) (⌽⊣|¯1↓·⌊∘÷`∾˜) 𝕩}
Ifp‿Pfi
}
! {
p ← 1‿3‿0‿7‿6‿5‿4‿9‿8‿2
- p ≡ 10 PermutationFromIndex IndexFromPermutation p
+ p ≡ 10 PermutationFromIndex i ← IndexFromPermutation p
+ p ≡ ⊢⌾(10⊸IndexFromPermutation) p
+ i ≡ ⊢⌾(10⊸PermutationFromIndex) i
}
# 22
@@ -121,10 +123,15 @@ Combinations ← {𝕨(=∨0=⊣)◶⟨(0∾˘𝕊⌾(-⟜1))∾1+𝕊⟜(-⟜1)
# 23
IndexFromCombination‿CombinationFromIndex ← {
C ← ((-˜+↕∘⊣)÷○(×´1⊸+)↕∘⊣)˘ # Combination function (APL's dyadic !)
- Ic ← {m‿n←𝕨 ⋄ ⊑-˝(m-↕m) +˝∘(C˘) n-(»1+𝕩)∾˘𝕩}
+ Ic ← {
+ 𝕊⁼: 𝕨Ci𝕩;
+ m‿n𝕊𝕩:
+ ⊑-˝(m-↕m) +˝∘(C˘) n-(»1+𝕩)∾˘𝕩
+ }
Ci ← {
+ 𝕊⁼: 𝕨Ic𝕩;
0‿n𝕊𝕩: ⟨⟩;
- m‿n←𝕨
+ m‿n𝕊𝕩:
v←+`(m-1)C(1-m)↓⌽↕n
k←(v>𝕩)⊐1
k∾(1+k)+(𝕨-1∾1+k)𝕊(𝕩-k⊏0∾v)
@@ -133,6 +140,7 @@ IndexFromCombination‿CombinationFromIndex ← {
}
! 11 ≡ 4‿6 IndexFromCombination 1‿2‿3‿5
! 1‿2‿3‿5 ≡ 4‿6 CombinationFromIndex 11
+! 11 ≡ 4‿6 CombinationFromIndex⁼ 4‿6 CombinationFromIndex 11
# 24
SymmetricArray ← ⍉⊸≡ ∧ 1⊸⍉⊸≡