120
edits
Miraheze>Adám Brudzewsky |
m (16 revisions imported: Migrate from miraheze) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{Built-in|Reshape|⍴}} produces an array with [[shape]] given by the left argument and [[elements]] from the right argument. Elements are copied from the right argument to the result in [[ravel order]], truncating if the result has smaller [[bound]] than the right argument and repeating cyclically if it has larger bound. If the right argument is empty, [[Fill element|fills]] are used for the result elements. | ||
== Examples == | == Examples == | ||
Line 28: | Line 28: | ||
</source> | </source> | ||
Reshape can also decrease the rank or [[bound]] of an array. One notable example is the use of an empty left argument < | Reshape can also decrease the rank or [[bound]] of an array. One notable example is the use of an empty left argument <source lang=apl inline>[[Zilde|⍬]]</source> to produce a [[scalar]]. The scalar is the first 0-[[cell]] of the right argument. In [[Nested array model|nested]] languages <source lang=apl inline>⍬⍴</source> is like [[First]] except that it does not remove a layer of nesting. | ||
<source lang=apl> | <source lang=apl> | ||
9 ⍴ ∘.+⍨ 1 2 1 | 9 ⍴ ∘.+⍨ 1 2 1 | ||
Line 50: | Line 50: | ||
== APL model == | == APL model == | ||
Since Reshape itself is the fundamental way to create a multi-dimensional array in APL, the function as a whole cannot be modelled in terms of more fundamental primitives. However, we may express it in terms of a stricter reshaping function < | Since Reshape itself is the fundamental way to create a multi-dimensional array in APL, the function as a whole cannot be modelled in terms of more fundamental primitives. However, we may express it in terms of a stricter reshaping function <source lang=apl inline>shape</source>, which forms an array from its [[shape]] and [[ravel]] vectors, requiring both to have rank 1 and the number of elements in the ravel to be the product of the shape. <source lang=apl inline>shape</source> is identical to Reshape on its domain, but it has a strictly smaller domain than Reshape. The extensions required to implement Reshape are that a scalar left argument must be allowed, and that the right argument must be converted to a vector with the appropriate length, truncating or repeating its elements. | ||
<source lang=apl> | <source lang=apl> | ||
Reshape ← { | Reshape ← { | ||
Line 69: | Line 69: | ||
== J variant: Shape == | == J variant: Shape == | ||
The [[J]] language does not include a Reshape primitive. In J, the monadic [[Shape]] function is called "Shape Of" and uses the glyph < | The [[J]] language does not include a Reshape primitive. In J, the monadic [[Shape]] function is called "Shape Of" and uses the glyph <source lang=apl inline>$</source>. Its dyadic form, simply called "Shape", rearranges the [[Major cell|major cells]] of the right argument rather than its [[elements]]. The result shape is given by the left argument, followed by the shape of the right argument with the first [[axis]] length (if any) removed. In APL the J Shape function can be written <source lang=apl inline>{ (⍺,1↓⍴⍵)⍴⍵ }</source>, and in J the APL Reshape function can be written using the [[hook]] <source lang=apl inline>($,)</source> which first ravels the right argument so that its major cells are its elements. | ||
== Notable uses == | == Notable uses == | ||
Line 81: | Line 81: | ||
0 0 0 1 | 0 0 0 1 | ||
</source> | </source> | ||
This idea might be written in a [[tacit]] style as < | This idea might be written in a [[tacit]] style as <source lang=apl inline>,⍨⍴1↑⍨1∘+</source> or <source lang=apl inline>,⍨⍴1,⍴∘0</source>. Both functions take the side length as an argument and produce an identity matrix with that side length. | ||
== External links == | == External links == | ||
=== Lessons === | |||
* | * [https://chat.stackexchange.com/rooms/52405/conversation/lesson-10-apl-functions-- APL Cultivation] | ||
* [https://www.sacrideo.us/apl-a-day-4-arrays-have-elements/ Arrays have elements] (part of [https://www.sacrideo.us/tag/apl-a-day/ APL a Day]) | |||
* | === Documentation === | ||
* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Reshape.htm Dyalog] | |||
* [http://wiki.nars2000.org/index.php/Rho NARS2000] | |||
* [http://microapl.com/apl_help/ch_020_020_470.htm APLX] | |||
* [https://code.jsoftware.com/wiki/Vocabulary/dollar#dyadic J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/dollar#dyadic J NuVoc] (as <source lang=apl inline>$</source> "Shape") | |||
{{APL built-ins}} | {{APL built-ins}} |