Reshape: Difference between revisions

Jump to navigation Jump to search
7 bytes removed ,  11:48, 25 October 2019
class= → lang=
Miraheze>Adám Brudzewsky
No edit summary
Miraheze>Adám Brudzewsky
(class= → lang=)
Line 4: Line 4:


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 class=apl>
<source lang=apl>
       3 4 ⍴ ⍳12
       3 4 ⍴ ⍳12
1  2  3  4
1  2  3  4
Line 11: Line 11:
</source>
</source>
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 class=apl>
<source lang=apl>
       3 4 ⍴ 12
       3 4 ⍴ 12
12 12 12 12
12 12 12 12
Line 19: Line 19:


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 class=apl>
<source lang=apl>
       12 ⍴ 'abcde'
       12 ⍴ 'abcde'
abcdeabcdeab
abcdeabcdeab
Line 29: Line 29:


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 <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.
<source class=apl>
<source 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 51: Line 51:


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 <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.
<source class=apl>
<source lang=apl>
Reshape ← {
Reshape ← {
     (1/⍺) shape (×/⍺) {(0=≢⍵)∨⍺≤≢⍵:⍺↑⍵ ⋄ ⍺∇,⍨⍵} ,⍵
     (1/⍺) shape (×/⍺) {(0=≢⍵)∨⍺≤≢⍵:⍺↑⍵ ⋄ ⍺∇,⍨⍵} ,⍵
Line 58: Line 58:
{{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 class=apl>
<source lang=apl>
Reshape ← {
Reshape ← {
     ⎕IO←0
     ⎕IO←0
Line 74: Line 74:


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 [[identity matrix]] by reshaping a vector which is one longer than the desired side length.
<source class=apl>
<source lang=apl>
       4 4 ⍴ 5↑1
       4 4 ⍴ 5↑1
1 0 0 0
1 0 0 0

Navigation menu