Take: Difference between revisions

Jump to navigation Jump to search
279 bytes added ,  11:02, 11 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 9: Line 9:
       ¯3 ↑ 5 4 3 2 1
       ¯3 ↑ 5 4 3 2 1
3 2 1
3 2 1
</source>
</syntaxhighlight>
The left argument to length specifies a length, and not an index. It does not depend on [[index origin]].
The left argument to length specifies a length, and not an index. It does not depend on [[index origin]].
=== Overtaking ===
=== Overtaking ===
Line 18: Line 18:
       ¯8 ↑ 5 4 3 2 1
       ¯8 ↑ 5 4 3 2 1
0 0 0 5 4 3 2 1
0 0 0 5 4 3 2 1
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}
=== Truncation ===
=== Truncation ===
Line 29: Line 29:
│4 1│4 2│4 3│
│4 1│4 2│4 3│
└───┴───┴───┘
└───┴───┴───┘
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]]}}
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]]}}
=== Leading axes ===
=== Leading axes ===
Line 40: Line 40:
│4 1│4 2│4 3│4 4│4 5│
│4 1│4 2│4 3│4 4│4 5│
└───┴───┴───┴───┴───┘
└───┴───┴───┴───┴───┘
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}
=== Function axis ===
=== Function axis ===
Line 55: Line 55:
│4 4│4 5│
│4 4│4 5│
└───┴───┘
└───┴───┘
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
If the [[Rank operator]] is available then <source lang=apl inline>¯2↑⍤1⍳4 5</source> is an equivalent expression.
If the [[Rank operator]] is available then <source lang=apl inline>¯2↑⍤1⍳4 5</syntaxhighlight> is an equivalent expression.


== Description ==
== Description ==


In the expression <source lang=apl inline>X↑Y</source>, <source lang=apl inline>X</source> may be any array, and <source lang=apl inline>Y</source> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <source lang=apl inline>Y</source>. Many APLs require the length to be exactly equal; however, an extension by [[SHARP APL]] to allow a shorter left argument has been widely adopted by recent APLs. <source lang=apl inline>X</source> may also be a scalar, in which case it is treated as a one-element vector in an instance of [[scalar rank extension]]. In some APLs, <source lang=apl inline>Y</source> is also subject to [[scalar rank extension]]: if it is scalar then it will be extended so its rank is the length <source lang=apl inline>≢X</source>.
In the expression <source lang=apl inline>X↑Y</syntaxhighlight>, <source lang=apl inline>X</syntaxhighlight> may be any array, and <source lang=apl inline>Y</syntaxhighlight> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <source lang=apl inline>Y</syntaxhighlight>. Many APLs require the length to be exactly equal; however, an extension by [[SHARP APL]] to allow a shorter left argument has been widely adopted by recent APLs. <source lang=apl inline>X</syntaxhighlight> may also be a scalar, in which case it is treated as a one-element vector in an instance of [[scalar rank extension]]. In some APLs, <source lang=apl inline>Y</syntaxhighlight> is also subject to [[scalar rank extension]]: if it is scalar then it will be extended so its rank is the length <source lang=apl inline>≢X</syntaxhighlight>.


Elements of <source lang=apl inline>X</source> are matched with axes of <source lang=apl inline>Y</source> with the same [[index]], that is, the left argument corresponds to [[Leading axis theory|leading axes]] of the right. The trailing axes of <source lang=apl inline>Y</source> which are not matched in this way are unchanged by Take; this may also be modelled by extending <source lang=apl inline>X</source> using the lengths of those axes.
Elements of <source lang=apl inline>X</syntaxhighlight> are matched with axes of <source lang=apl inline>Y</syntaxhighlight> with the same [[index]], that is, the left argument corresponds to [[Leading axis theory|leading axes]] of the right. The trailing axes of <source lang=apl inline>Y</syntaxhighlight> which are not matched in this way are unchanged by Take; this may also be modelled by extending <source lang=apl inline>X</syntaxhighlight> using the lengths of those axes.


For each modified axis the result length along that axis is equal to the corresponding element of <source lang=apl inline>|X</source>. If the original element in <source lang=apl inline>X</source> is positive then the result is aligned with the argument at the beginning of that axis, and if that element is negative they are aligned at the end. If it is zero then the result is empty, so both are true. Elements from the right argument are used in the result until the result is filled. If the argument axis is shorter than the result axis then [[Fill element|fills]] are used once it is exhausted. If the result is empty, its [[prototype]] is the same as the right argument's.
For each modified axis the result length along that axis is equal to the corresponding element of <source lang=apl inline>|X</syntaxhighlight>. If the original element in <source lang=apl inline>X</syntaxhighlight> is positive then the result is aligned with the argument at the beginning of that axis, and if that element is negative they are aligned at the end. If it is zero then the result is empty, so both are true. Elements from the right argument are used in the result until the result is filled. If the argument axis is shorter than the result axis then [[Fill element|fills]] are used once it is exhausted. If the result is empty, its [[prototype]] is the same as the right argument's.


If the result is no larger than the right argument along each axis (equivalently, no fills are used, or <source lang=apl inline>(|X)≤(≢X)↑⍴Y</source>), then the result is a [[subarray]] of <source lang=apl inline>Y</source>. Not all subarrays can be produced in this way: only those which have the same rank and align with one boundary of the argument along each axis.
If the result is no larger than the right argument along each axis (equivalently, no fills are used, or <source lang=apl inline>(|X)≤(≢X)↑⍴Y</syntaxhighlight>), then the result is a [[subarray]] of <source lang=apl inline>Y</syntaxhighlight>. Not all subarrays can be produced in this way: only those which have the same rank and align with one boundary of the argument along each axis.


=== Axis specification ===
=== Axis specification ===
Line 98: Line 98:
     sel∘⍵¨ inds
     sel∘⍵¨ inds
}
}
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}


This definition could be converted to work in a [[Flat array model|flat]] APL with the [[Rank operator]] by using an [[odometer function]] like <source lang=apl inline>⊢⊤(⍳×/)</source> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.
This definition could be converted to work in a [[Flat array model|flat]] APL with the [[Rank operator]] by using an [[odometer function]] like <source lang=apl inline>⊢⊤(⍳×/)</syntaxhighlight> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.


== History ==
== History ==
Line 107: Line 107:
In [[A Programming Language]], [[prefix]] and [[suffix]] operations were described using the syntax <math>\alpha^j/x</math> to take the first <math>j</math> elements of vector <math>x</math> and <math>\omega^j/x</math> for the last <math>j</math> elements. This combined a use of the special prefix and suffix vectors <math>\alpha^j(n)</math> and <math>\omega^j(n)</math> with [[Compress|compression]], with the length <math>n</math> inferred based on the length of <math>x</math>. The symbol <math>\uparrow</math> was used for vector [[Rotate]], while <math>\downarrow</math> rotated in the opposite direction.<ref>[[Ken Iverson|Iverson, K.E.]] (1962). A Programming Language. Wiley. ISBN 978-0-471-43014-8.</ref>
In [[A Programming Language]], [[prefix]] and [[suffix]] operations were described using the syntax <math>\alpha^j/x</math> to take the first <math>j</math> elements of vector <math>x</math> and <math>\omega^j/x</math> for the last <math>j</math> elements. This combined a use of the special prefix and suffix vectors <math>\alpha^j(n)</math> and <math>\omega^j(n)</math> with [[Compress|compression]], with the length <math>n</math> inferred based on the length of <math>x</math>. The symbol <math>\uparrow</math> was used for vector [[Rotate]], while <math>\downarrow</math> rotated in the opposite direction.<ref>[[Ken Iverson|Iverson, K.E.]] (1962). A Programming Language. Wiley. ISBN 978-0-471-43014-8.</ref>


The first version of [[APL\360]]<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> followed Iverson notation in defining prefix (<source lang=apl inline>n ⍺ j</source>) and suffix (<source lang=apl inline>n ⍵ j</source>) vectors but not Take, even though the arrow symbols were no longer used for Rotate. For a vector <source lang=apl inline>x</source>, what is now <source lang=apl inline>j↑x</source> would have been written <source lang=apl inline>((⍴x)⍺j)/x</source> while <source lang=apl inline>(-j)↑x</source> would be <source lang=apl inline>((⍴x)⍵j)/x</source>. The functions Take and Drop using arrow symbols were introduced by 1968.<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]], "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". [[IBM]], August 1968.</ref> They were implemented for arrays (not just vectors), and extended to allow the left argument to be larger than the right argument's shape (introducing overtaking and [[fill element]]s) in 1970.<ref>"[[IBM|I.B.M.]] Report". [[APL Quote-Quad]] Volume 2, Number 1. 1970-04.</ref>
The first version of [[APL\360]]<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> followed Iverson notation in defining prefix (<source lang=apl inline>n ⍺ j</syntaxhighlight>) and suffix (<source lang=apl inline>n ⍵ j</syntaxhighlight>) vectors but not Take, even though the arrow symbols were no longer used for Rotate. For a vector <source lang=apl inline>x</syntaxhighlight>, what is now <source lang=apl inline>j↑x</syntaxhighlight> would have been written <source lang=apl inline>((⍴x)⍺j)/x</syntaxhighlight> while <source lang=apl inline>(-j)↑x</syntaxhighlight> would be <source lang=apl inline>((⍴x)⍵j)/x</syntaxhighlight>. The functions Take and Drop using arrow symbols were introduced by 1968.<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]], "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". [[IBM]], August 1968.</ref> They were implemented for arrays (not just vectors), and extended to allow the left argument to be larger than the right argument's shape (introducing overtaking and [[fill element]]s) in 1970.<ref>"[[IBM|I.B.M.]] Report". [[APL Quote-Quad]] Volume 2, Number 1. 1970-04.</ref>


The [[Function axis|axis]] specification for Take was defined in [[APL2]]. It is shared by [[SHARP APL]] and [[Rationalized APL]], and continues to be supported in [[Dyalog APL]].
The [[Function axis|axis]] specification for Take was defined in [[APL2]]. It is shared by [[SHARP APL]] and [[Rationalized APL]], and continues to be supported in [[Dyalog APL]].
Line 126: Line 126:
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
|-
|-
| [[A+]], [[ngn/apl]], [[J]] (<source lang=j inline>{.</source>), [[BQN]] || {{Yes}}          || {{Yes}}        || {{No}}
| [[A+]], [[ngn/apl]], [[J]] (<source lang=j inline>{.</syntaxhighlight>), [[BQN]] || {{Yes}}          || {{Yes}}        || {{No}}
|-
|-
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}

Navigation menu