Take: Difference between revisions
Jump to navigation
Jump to search
Miraheze>Marshall (Add References section) |
Miraheze>Adám Brudzewsky |
||
Line 4: | Line 4: | ||
Take may be used to get the first few, or last few, elements of a vector: | Take may be used to get the first few, or last few, elements of a vector: | ||
< | <source lang=apl> | ||
3 ↑ 5 4 3 2 1 | 3 ↑ 5 4 3 2 1 | ||
5 4 3 | 5 4 3 | ||
¯3 ↑ 5 4 3 2 1 | ¯3 ↑ 5 4 3 2 1 | ||
3 2 1 | 3 2 1 | ||
</ | </source> | ||
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}} | {{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}} | ||
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]]. | ||
A length which is larger than the argument length causes [[Fill element|fills]] to be inserted. The alignment remains the same: if two different positive arguments are used to take from an array, the one which is closer to zero gives a [[prefix]] of the other result. If they are both negative, it is a [[suffix]] instead. | A length which is larger than the argument length causes [[Fill element|fills]] to be inserted. The alignment remains the same: if two different positive arguments are used to take from an array, the one which is closer to zero gives a [[prefix]] of the other result. If they are both negative, it is a [[suffix]] instead. | ||
< | <source lang=apl> | ||
8 ↑ 5 4 3 2 1 | 8 ↑ 5 4 3 2 1 | ||
5 4 3 2 1 0 0 0 | 5 4 3 2 1 0 0 0 | ||
¯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> | ||
{{Works in|[[Dyalog APL]], [[ngn/APL]]}} | {{Works in|[[Dyalog APL]], [[ngn/APL]]}} | ||
A higher-[[rank]] array can be shortened by using a left argument with one element for each axis: | A higher-[[rank]] array can be shortened by using a left argument with one element for each axis: | ||
< | <source lang=apl> | ||
¯2 3↑⍳4 5 | ¯2 3↑⍳4 5 | ||
┌───┬───┬───┐ | ┌───┬───┬───┐ | ||
Line 30: | Line 30: | ||
│4 1│4 2│4 3│ | │4 1│4 2│4 3│ | ||
└───┴───┴───┘ | └───┴───┴───┘ | ||
</ | </source> | ||
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}} | {{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}} | ||
In languages with the SHARP APL extension, the left argument can be shortened. This causes leading axes of the right argument to be modified while trailing axes are ignored. | In languages with the SHARP APL extension, the left argument can be shortened. This causes leading axes of the right argument to be modified while trailing axes are ignored. | ||
< | <source lang=apl> | ||
¯2↑⍳4 5 | ¯2↑⍳4 5 | ||
┌───┬───┬───┬───┬───┐ | ┌───┬───┬───┬───┬───┐ | ||
Line 41: | Line 41: | ||
│4 1│4 2│4 3│4 4│4 5│ | │4 1│4 2│4 3│4 4│4 5│ | ||
└───┴───┴───┴───┴───┘ | └───┴───┴───┴───┴───┘ | ||
</ | </source> | ||
{{Works in|[[Dyalog APL]], [[ngn/APL]]}} | {{Works in|[[Dyalog APL]], [[ngn/APL]]}} | ||
An [[Function axis|axis]] may be specified to apply left argument elements to specific axes of the right argument. Here the last axis is specified in order to take two columns of the argument. | An [[Function axis|axis]] may be specified to apply left argument elements to specific axes of the right argument. Here the last axis is specified in order to take two columns of the argument. | ||
< | <source lang=apl> | ||
¯2↑[2]⍳4 5 | ¯2↑[2]⍳4 5 | ||
┌───┬───┐ | ┌───┬───┐ | ||
Line 56: | Line 56: | ||
│4 4│4 5│ | │4 4│4 5│ | ||
└───┴───┘ | └───┴───┘ | ||
</ | </source> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
If the [[Rank operator]] is available then <code>¯2↑⍤1⍳4 5</code> is an equivalent expression. | If the [[Rank operator]] is available then <code>¯2↑⍤1⍳4 5</code> is an equivalent expression. |