Reshape: Difference between revisions
Jump to navigation
Jump to search
Miraheze>Marshall |
Miraheze>Adám Brudzewsky m (Text replacement - "<code>" to "<source lang=apl inline>") |
||
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|⍬]]</code> 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>⍬⍴</code> 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</code>, 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</code> 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>$</code>. 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↓⍴⍵)⍴⍵ }</code>, and in J the APL Reshape function can be written using the [[hook]] <source lang=apl inline>($,)</code> 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∘+</code> or <source lang=apl inline>,⍨⍴1,⍴∘0</code>. Both functions take the side length as an argument and produce an identity matrix with that side length. | ||
== External links == | == External links == | ||
Line 97: | Line 97: | ||
* [http://microapl.com/apl_help/ch_020_020_470.htm APLX] | * [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 < | * [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>$</code> "Shape") | ||
{{APL built-ins}} | {{APL built-ins}} |