Reshape: Difference between revisions

Jump to navigation Jump to search
782 bytes added ,  22:26, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
Miraheze>Marshall
m (Text replacement - "<source" to "<syntaxhighlight")
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Primitive|⍴|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.
{{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 ==


Reshape can be used to produce an array with a given shape and ravel:
Reshape can be used to produce an array with a given shape and ravel:
<source lang=apl>
<syntaxhighlight lang=apl>
       3 4 ⍴ ⍳12
       3 4 ⍴ ⍳12
1  2  3  4
1  2  3  4
5  6  7  8
5  6  7  8
9 10 11 12
9 10 11 12
</source>
</syntaxhighlight>
It appears to exhibit a form of [[Scalar extension|scalar]] or singleton extension:
It appears to exhibit a form of [[Scalar extension|scalar]] or singleton extension:
<source lang=apl>
<syntaxhighlight lang=apl>
       3 4 ⍴ 12
       3 4 ⍴ 12
12 12 12 12
12 12 12 12
12 12 12 12
12 12 12 12
12 12 12 12
12 12 12 12
</source>
</syntaxhighlight>


In fact it repeats an argument of any length, singleton or otherwise. This repetition applies with a vector result, or a higher rank.
In fact it repeats an argument of any length, singleton or otherwise. This repetition applies with a vector result, or a higher rank.
<source lang=apl>
<syntaxhighlight lang=apl>
       12 ⍴ 'abcde'
       12 ⍴ 'abcde'
abcdeabcdeab
abcdeabcdeab
Line 26: Line 26:
eabc
eabc
deab
deab
</source>
</syntaxhighlight>


Reshape can also decrease the rank or [[bound]] of an array. One notable example is the use of an empty left argument <code>[[Zilde|⍬]]</code> to produce a [[scalar]]. The scalar is the first 0-[[cell]] of the right argument. In [[Nested array model|nested]] languages <code>⍬⍴</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 [[Zilde]] (<syntaxhighlight lang=apl inline>⍬</syntaxhighlight>) to produce a [[scalar]]. The scalar is the first 0-[[cell]] of the right argument. In [[Nested array model|nested]] languages <syntaxhighlight lang=apl inline>⍬⍴</syntaxhighlight> is like [[First]] except that it does not remove a layer of nesting.
<source lang=apl>
<syntaxhighlight lang=apl>
       9 ⍴ ∘.+⍨ 1 2 1
       9 ⍴ ∘.+⍨ 1 2 1
2 3 2 3 4 3 2 3 2
2 3 2 3 4 3 2 3 2
Line 38: Line 38:
│1 1│
│1 1│
└───┘
└───┘
</source>
</syntaxhighlight>


== Description ==
== Description ==
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 <code>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. <code>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 <syntaxhighlight lang=apl inline>shape</syntaxhighlight>, 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. <syntaxhighlight lang=apl inline>shape</syntaxhighlight> 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>
<syntaxhighlight lang=apl>
Reshape ← {
Reshape ← {
     (1/⍺) shape (×/⍺) {(0=≢⍵)∨⍺≤≢⍵:⍺↑⍵ ⋄ ⍺∇,⍨⍵} ,⍵
     (1/⍺) shape (×/⍺) {(0=≢⍵)∨⍺≤≢⍵:⍺↑⍵ ⋄ ⍺∇,⍨⍵} ,⍵
}
}
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]],[[ngn/apl]]}}
{{Works in|[[Dyalog APL]],[[ngn/apl]]}}
The above implementation performs truncation and fill element generation using [[Take]], after extending the [[Ravel|ravelled]] right argument by [[Catenate|catenating]] it with itself until it is long enough. An implementation using indices instead of structural manipulation is also possible:
The above implementation performs truncation and fill element generation using [[Take]], after extending the [[Ravel|ravelled]] right argument by [[Catenate|catenating]] it with itself until it is long enough. An implementation using indices instead of structural manipulation is also possible:
<source lang=apl>
<syntaxhighlight lang=apl>
Reshape ← {
Reshape ← {
     ⎕IO←0
     ⎕IO←0
     (1/⍺) shape ((,⍵),(⊂⊃⍵))[(1⌈×/⍴⍵)|⍳×/⍺]
     (1/⍺) shape ((,⍵),(⊂⊃⍵))[(1⌈×/⍴⍵)|⍳×/⍺]
}
}
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
Here the right argument is converted to a ravel vector by ravelling and appending the [[prototype]], then [[indexing]] to produce a vector of the correct length. The indices used are the ravel indices of the result, but they are made to wrap around using [[Residue]].
Here the right argument is converted to a ravel vector by ravelling and appending the [[prototype]], then [[Bracket indexing|indexing]] to produce a vector of the correct length. The indices used are the ravel indices of the result, but they are made to wrap around using [[Residue]].


== 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 <code>$</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 <code>{ (⍺,1↓⍴⍵)⍴⍵ }</code>, and in J the APL Reshape function can be written using the [[hook]] <code>($,)</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 <syntaxhighlight lang=apl inline>$</syntaxhighlight>. 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 <syntaxhighlight lang=apl inline>{ (⍺,1↓⍴⍵)⍴⍵ }</syntaxhighlight>, and in J the APL Reshape function can be written using the [[hook]] <syntaxhighlight lang=apl inline>($,)</syntaxhighlight> which first ravels the right argument so that its major cells are its elements.


== Notable uses ==
== Notable uses ==


Reshape can be used to produce an [[identity matrix]] by reshaping a vector which is one longer than the desired side length.
Reshape can be used to produce an [[wikipedia:identity matrix|identity matrix]] by reshaping a vector which is one longer than the desired side length.
<source lang=apl>
<syntaxhighlight lang=apl>
       4 4 ⍴ 5↑1
       4 4 ⍴ 5↑1
1 0 0 0
1 0 0 0
Line 80: Line 80:
0 0 1 0
0 0 1 0
0 0 0 1
0 0 0 1
</source>
</syntaxhighlight>
This idea might be written in a [[tacit]] style as <code>,⍨⍴1↑⍨1∘+</code> or <code>,⍨⍴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 <syntaxhighlight lang=apl inline>,⍨⍴1↑⍨1∘+</syntaxhighlight> or <syntaxhighlight lang=apl inline>,⍨⍴1,⍴∘0</syntaxhighlight>. Both functions take the side length as an argument and produce an identity matrix with that side length.


== External links ==
== External links ==
Line 88: Line 88:


* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-10-apl-functions-- APL Cultivation]
* [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 ===
=== Documentation ===


* [http://help.dyalog.com/latest/Content/Language/Primitive%20Functions/Reshape.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Reshape.htm Dyalog]
 
* [http://wiki.nars2000.org/index.php/Rho NARS2000]
* [http://wiki.nars2000.org/index.php/Rho NARS2000]
* [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 <syntaxhighlight lang=apl inline>$</syntaxhighlight> "Shape")
* [https://mlochbaum.github.io/BQN/doc/reshape.html BQN]


* [https://code.jsoftware.com/wiki/Vocabulary/dollar#dyadic J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/dollar#dyadic J NuVoc] (as <code>$</code> "Shape")
{{APL built-ins}}[[Category:Primitive functions]]
 
{{APL built-ins}}

Navigation menu