Reshape: Difference between revisions

Jump to navigation Jump to search
20 bytes added ,  09:07, 29 October 2019
m
Text replacement - "</code>" to "</source>"
Miraheze>Adám Brudzewsky
m (Text replacement - "<code>" to "<source lang=apl inline>")
Miraheze>Adám Brudzewsky
m (Text replacement - "</code>" to "</source>")
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 <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.
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 <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.
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 <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.
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 <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.
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 ==
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 <source lang=apl inline>$</code> "Shape")  
* [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}}

Navigation menu