Reshape: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Marshall
(Created page with "Reshape (<code>⍴</code>) produces an array with shape given by the left argument and elements from the right argument. Elements are copied from the right argument to...")
 
Miraheze>Marshall
No edit summary
Line 1: Line 1:
Reshape (<code>⍴</code>) 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.
Reshape (<code>⍴</code>) 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.
== Description ==
The left argument of Reshape must be a valid [[shape]], or [[vector]] of nonnegative integers, after [[scalar rank extension]] (that is, a scalar is treated as a one-element vector). The right argument may be any array. The result array is an array of the given shape, and its elements in ravel order are taken from the right argument in ravel order. If the right argument's ravel is too short, they are repeated starting at the beginning again as many times as necessary.


== Examples ==
== Examples ==
Line 31: Line 27:
deab
deab
</source>
</source>
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>
      9 ⍴ ∘.+⍨ 1 2 1
2 3 2 3 4 3 2 3 2
      3 ⍴ 'Samantha'
Sam
      ⍬ ⍴ ⍳8 8
┌───┐
│1 1│
└───┘
</source>
== Description ==
The left argument of Reshape must be a valid [[shape]], or [[vector]] of nonnegative integers, after [[scalar rank extension]] (that is, a scalar is treated as a one-element vector). The right argument may be any array. The result array is an array of the given shape, and its elements in [[ravel order]] are taken from the right argument in ravel order. If the right argument's ravel is too short, they are repeated starting at the beginning again as many times as necessary.
An [[Empty array|empty]] right argument will cause the result array to be composed of [[Fill element|fill elements]]. Reshape is similar to [[Take]] in this case—in fact, Take with an empty right argument is always identical to Reshape unless it results in an error.
The ravelled result is either a [[prefix]] of the ravelled argument, or a [[suffix]].


== APL model ==
== APL model ==
Line 50: Line 66:
{{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 [[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 ==
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.
== Notable uses ==
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>
      4 4 ⍴ 5↑1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
</source>
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.


== Documentation ==
== Documentation ==

Revision as of 15:01, 21 October 2019

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, fills are used for the result elements.

Examples

Reshape can be used to produce an array with a given shape and ravel:

      3 4 ⍴ ⍳12
1  2  3  4
5  6  7  8
9 10 11 12

It appears to exhibit a form of scalar or singleton extension:

      3 4 ⍴ 12
12 12 12 12
12 12 12 12
12 12 12 12

In fact it repeats an argument of any length, singleton or otherwise. This repetition applies with a vector result, or a higher rank.

      12 ⍴ 'abcde'
abcdeabcdeab
      3 4 ⍴ 'abcde'
abcd
eabc
deab

Reshape can also decrease the rank or bound of an array. One notable example is the use of an empty left argument to produce a scalar. The scalar is the first 0-cell of the right argument. In nested languages ⍬⍴ is like First except that it does not remove a layer of nesting.

      9 ⍴ ∘.+⍨ 1 2 1
2 3 2 3 4 3 2 3 2
      3 ⍴ 'Samantha'
Sam
      ⍬ ⍴ ⍳8 8
┌───┐
│1 1│
└───┘

Description

The left argument of Reshape must be a valid shape, or vector of nonnegative integers, after scalar rank extension (that is, a scalar is treated as a one-element vector). The right argument may be any array. The result array is an array of the given shape, and its elements in ravel order are taken from the right argument in ravel order. If the right argument's ravel is too short, they are repeated starting at the beginning again as many times as necessary.

An empty right argument will cause the result array to be composed of fill elements. Reshape is similar to Take in this case—in fact, Take with an empty right argument is always identical to Reshape unless it results in an error.

The ravelled result is either a prefix of the ravelled argument, or a suffix.

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 shape, 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. shape 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.

Reshape ← {
    (1/⍺) shape (×/⍺) {(0=≢⍵)∨⍺≤≢⍵:⍺↑⍵ ⋄ ⍺∇,⍨⍵} ,⍵
}

The above implementation performs truncation and fill element generation using Take, after extending the ravelled right argument by catenating it with itself until it is long enough. An implementation using indices instead of structural manipulation is also possible:

Reshape ← {
    ⎕IO←0
    (1/⍺) shape ((,⍵),(⊂⊃⍵))[(1⌈×/⍴⍵)|⍳×/⍺]
}
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.

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 $. Its dyadic form, simply called "Shape", rearranges the 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 { (⍺,1↓⍴⍵)⍴⍵ }, and in J the APL Reshape function can be written using the hook ($,) which first ravels the right argument so that its major cells are its elements.

Notable uses

Reshape can be used to produce an identity matrix by reshaping a vector which is one longer than the desired side length.

      4 4 ⍴ 5↑1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

This idea might be written in a tacit style as ,⍨⍴1↑⍨1∘+ or ,⍨⍴1,⍴∘0. Both functions take the side length as an argument and produce an identity matrix with that side length.

Documentation

Dyalog

J function Shape: Dictionary, NuVoc

Other resources

APL Cultivation