diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-09-10 22:56:50 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-09-10 22:56:50 -0400 |
| commit | e3ef0195a42a86ec28a48b4e16448ebf4e70cb7e (patch) | |
| tree | 2b12daad7ba5a4744662fa5d6cdecb0a3027580a /src | |
| parent | a29c6789618ab22f81d2e6f971e53b802a03e144 (diff) | |
Define utilities earlier and primitives later
Diffstat (limited to 'src')
| -rw-r--r-- | src/r.bqn | 55 |
1 files changed, 26 insertions, 29 deletions
@@ -14,10 +14,14 @@ Int←IsArray◶⟨⌊⊸=,0⟩ Nat←IsArray◶⟨0⊸≤×⌊⊸=,0⟩ +Deshape←IsArray◶{⟨𝕩⟩}‿⥊ +Pair ← {⟨𝕩⟩} ⊘ {⟨𝕨,𝕩⟩} +Box ← {⟨⟩⥊⟨𝕩⟩} +ToArray ← Box⍟(1-IsArray) # LIMITED to numeric arguments for scalar cases ≥ ← ≤˜ -< ← {⟨⟩⥊⟨𝕩⟩} ⊘ (1-≥) +< ← Box ⊘ (1-≥) > ← (1-≤) ⌊ ↩ ⌊ ⊘ (>⊑{𝕨‿𝕩}) ⌈ ← -∘⌊∘- ⊘ (<⊑{𝕨‿𝕩}) @@ -33,11 +37,18 @@ _fold←{ } ´ ← _fold -ToArray ← <⍟(1-IsArray) Cell←{(𝕨⊸+⊑𝕩˙)⌜↕(≠𝕩)-𝕨}⟜≢ ∾ ← {k←≠𝕨⋄k⊸≤◶⟨⊑⟜𝕨⋄-⟜k⊑𝕩˜⟩⌜↕k+≠𝕩} # LIMITED to two vector arguments +GetCells←(1==∘⊢)◶{ + c←1×´s←1 Cell 𝕩 + 𝕨((⥊𝕩)⊑˜c⊸×⊸+)⌜s⥊↕c +}‿{ + ⊑⟜𝕩⌜𝕨 +} +⊏ ← GetCells # LIMITED to depth-1 natural number left argument + _eachd←{ _d←{ # Equal ranks p←≢𝕨 @@ -114,14 +125,6 @@ TransposeInv←{ (↕l)(+⟜(l⊸×)⊑(⥊𝕩)˙)⌜c⥊↕1×´c }⍟(0<=) -GetCells←(1==∘⊢)◶{ - c←1×´s←1 Cell 𝕩 - 𝕨((⥊𝕩)⊑˜c⊸×⊸+)⌜s⥊↕c -}‿{ - ⊑⟜𝕩⌜𝕨 -} -⊏ ← GetCells # LIMITED to depth-1 natural number left argument - Reverse←{ "⌽𝕩: 𝕩 must have rank at least 1" ! 1≤=𝕩 l←≠𝕩 @@ -280,7 +283,7 @@ _onAxes_←{ ∧ ← 0 _sort ⊘ (× _perv) ∨ ← 1 _sort ⊘ ((+-×) _perv) × ↩ (0⊸(<->) ⊘ ×) _perv -< ↩ {⟨⟩⥊⟨𝕩⟩} ⊘ ((1-≥) _perv) +< ↩ Box ⊘ ((1-≥) _perv) > ↩ Merge ⊘ ((1-≤) _perv) ≠ ↩ ≠ ⊘ ((1-=) _perv) = ↩ = ⊘ (= _perv) @@ -291,7 +294,6 @@ _onAxes_←{ ¬ ← 1+- identity ← (0⊑⟨"´: Identity not found"!0˜⟩) {(0⊑𝕨){𝕗=𝕩}◶𝕩‿(1⊑𝕨)}´ ⟨+‿0,-‿0,×‿1,÷‿1,⋆‿1,√‿1,∧‿1,∨‿0,¬‿1,|‿0,⌊‿∞,⌈‿¯∞,<‿0,≤‿1,=‿1,≥‿1,>‿0,≠‿0⟩ -Deshape←IsArray◶{⟨𝕩⟩}‿⥊ Reshape←{ "𝕨⥊𝕩: 𝕨 must have rank at most 1" ! 1≥=𝕨 s←Deshape 𝕨 @@ -321,10 +323,6 @@ Range←{ IsArray◶I‿M 𝕩 } - -Pair ← {⟨𝕩⟩} ⊘ {⟨𝕨,𝕩⟩} - -≍ ← >∘Pair _ranks ← {⟨2⟩⊘⟨1,0⟩((⊣-1+|)˜⟜≠⊑¨<∘⊢)⥊∘𝔽} _depthOp_←{ neg←0>n←𝕨𝔾_ranks𝕩 ⋄ F←𝔽 @@ -334,7 +332,6 @@ _depthOp_←{ } 𝕨 n _d 𝕩 } -⚇ ← _depthOp_ _rankOp_←{ k←𝕨(Pair○= (0≤⊢)◶⟨⌊⟜-,0⌈-⟩¨ 𝔾_ranks)𝕩 Enc←{ @@ -345,7 +342,6 @@ _rankOp_←{ Enc↩(>⟜0×1+≥⟜=)◶⟨<⊢,Enc,<⌜⊢⟩ > ((0⊑k)Enc𝕨) 𝔽¨ ((1-˜≠)⊸⊑k)Enc𝕩 } -⎉ ← _rankOp_ _insert←{ "˝: 𝕩 must have rank at least 1" ! 1≤=𝕩 𝕨 𝔽´ <˘𝕩 @@ -363,9 +359,6 @@ JoinTo←∨○(1<=)◶(∾○⥊)‿{ (⟨l⟩∾0⊑c)⥊𝕨∾○⥊𝕩 } -↑ ← Prefixes ⊘ Take -↓ ← Suffixes ⊘ Drop - Rep ← Indices⊸⊏ Replicate ← IsArray∘⊣◶{ "/: Amounts to replicate must be natural numbers" ! Nat 𝕨 @@ -376,8 +369,8 @@ Replicate ← IsArray∘⊣◶{ 𝕨 Rep 𝕩 } _onAxes_ (1-0=≠) -⊏ ← (<0)⊸GetCells ⊘ (ToArray⊸(SelSub _onAxes_ 1)) - +↑ ← Prefixes ⊘ Take +↓ ← Suffixes ⊘ Drop ↕ ↩ Range ⊘ Windows ⌽ ← Reverse ⊘ (Rot _onAxes_ 0) / ← Indices ⊘ Replicate @@ -405,9 +398,6 @@ GroupGen←{ (1≠≠)◶(S _group 0⊸⊑)‿(S⌜ ·+⌜⌜´ (⌽×`⟨1⟩∾⌽1↓l) × ⊢_group⌜) 𝕨 } -∾ ↩ Join ⊘ JoinTo -⊔ ← GroupInds ⊘ GroupGen - Pick1←{ "𝕨⊑𝕩: Indices in compound 𝕨 must be lists" ! 1==𝕨 "𝕨⊑𝕩: Index length in 𝕨 must match rank of 𝕩" ! 𝕨=○≠s←≢𝕩 @@ -465,6 +455,8 @@ _bins←{ (≠𝕨)⊸{B⟜𝕩 _binSearch 𝕨}⌜ (×´sx) × ⥊⟜(↕×´)⊑⟜(≢𝕩)⌜↕cx } +⚇ ← _depthOp_ +⎉ ← _rankOp_ ⍋ ← Cmp _grade_ 0 ⊘ (Cmp _bins) ⍒ ← Cmp˜ _grade_ 1 ⊘ (Cmp˜ _bins) @@ -494,9 +486,11 @@ Find←{ 𝕨 ≡⎉r (≢𝕨) ↕⎉r 𝕩 } +≍ ← >∘Pair +∾ ↩ Join ⊘ JoinTo +⊔ ← GroupInds ⊘ GroupGen ⊐ ← SelfClas ⊘ (1 _search) ∊ ← ⊢_self ⊘ (0 _search˜) -⍷ ← ∊⊸/ ⊘ Find ReorderChk←{ "𝕨⍉𝕩: 𝕨 must have rank at most 1" ! 1≥=𝕨 @@ -524,8 +518,8 @@ ReorderAxesInv←{ OccurrenceCount ← ⊐˜(⊢-⊏)⍋∘⍋ ProgressiveIndexOf ← {𝕨⊐○(≍˘⟜OccurrenceCount𝕨⊸⊐)𝕩} -⊒ ← OccurrenceCount⊘ ProgressiveIndexOf +⊏ ← (<0)⊸GetCells ⊘ (ToArray⊸(SelSub _onAxes_ 1)) ⊑ ↩ (0⊑⥊) ⊘ Pick ◶ ↩ {𝕨((𝕨𝔽𝕩)⊑𝕘){𝔽}𝕩} # Same definition, new Pick @@ -580,9 +574,12 @@ _repeat_←{ neg←(𝕨𝔽⁼⊢)_p-l (|⊑<⟜0⊑pos‿neg˙)⚇0 n } -⍟ ↩ _repeat_ ReshapeT ← ⟨∘,⌊,⌽,↑⟩⊑∘⊐< + +⍟ ↩ _repeat_ ⥊ ↩ Deshape ⊘ Reshape ⌜ ↩ {𝔽⌜○ToArray} ⌾ ← _under_ +⊒ ← OccurrenceCount⊘ ProgressiveIndexOf +⍷ ← ∊⊸/ ⊘ Find |
