Take: Difference between revisions

Jump to navigation Jump to search
1,422 bytes added ,  12:09, 18 October 2019
APL model
Miraheze>Adám Brudzewsky
Miraheze>Marshall
(APL model)
Line 72: Line 72:


When Take is called with [[Function axis|axis]], the axis determines how elements of the left argument correspond to axes of the right argument. The left argument and axis are required to have rank no more than 1 and are treated as vectors. Their lengths must match, and be less than or equal to the rank of the right argument. Then each element of the left argument applies to the right argument axis given by the corresponding element of the axis vector. Each axis may only be specified once, and unspecified axes are left unchanged.
When Take is called with [[Function axis|axis]], the axis determines how elements of the left argument correspond to axes of the right argument. The left argument and axis are required to have rank no more than 1 and are treated as vectors. Their lengths must match, and be less than or equal to the rank of the right argument. Then each element of the left argument applies to the right argument axis given by the corresponding element of the axis vector. Each axis may only be specified once, and unspecified axes are left unchanged.
=== APL model ===
The following [[dfn]] models Take as defined by [[Dyalog APL]] but with no axis specification or error checking. It is implemented by construction a [[nested]] array of indices and using these to select from the right argument, with prototypes used for out-of-range indices. It explicitly includes [[scalar rank extension]] for the right argument and the SHARP APL extension; if these extensions are not wanted those lines can be removed. [[Scalar rank extension]] of the left argument is inherited from [[Iota]] and [[scalar function]] extension.
<source class=apl>
Take ← {
  ⎕IO←0                        ⍝ For index comparisons
  r s ← ≢¨(⍴⍵)(⍺)              ⍝ Rank and number of modified axes
  (r=0)∧s>0: ⍺∇(s⍴1)⍴⍵          ⍝ Right argument scalar rank extension
  s<r: (⍺,(s-r)∇⍴⍵)∇⍵          ⍝ SHARP APL extension
  inds ← ((⍺<0)×⍺+⍴⍵)∘+¨ ⍳|⍺    ⍝ Indices to select
  sel ← {
    ∧/(0≤⍺)∧⍺<⍴⍵: (⊂⍺)⊃⍵        ⍝ In range: use Pick
    ⊃0⍴⍵                        ⍝ Otherwise, get prototype
  }
  sel∘⍵¨ inds
}
</source>
{{Works in|[[Dyalog APL]]}}
This definition could be converted to work in a [[flat]] APL with the [[Rank operator]] by using an [[odometer function]] like <code>⊢⊤(⍳×/)</code> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.


== History ==
== History ==
Anonymous user

Navigation menu