Front page | perl.perl6.language.data | Postings from September 2000

## Re: RFC 148 (v2) Add reshape() for multi-dimensional array reshaping

Thread Next
From:
Jeremy Howard
Date:
September 18, 2000 21:01
Subject:
Re: RFC 148 (v2) Add reshape() for multi-dimensional array reshaping
Message ID:
001001c021ee\$37ffadd0\$0100a8c0@jeremy
```> Let's jump in. This RFC proposes a C<reshape> builtin with the following
> syntax:
>
Err... this syntax isn't what I expected at all! I thought the first
argument would define the shape of the result, like NumPy or PDL...

> When one array is passed in, it is split up. Here, the C<\$x> and C<\$y>
> determine the dimensions of the resulting lists. The C<\$i> determines
> the interleave.

This \$i definition should be removed now.
>
> So, we'll assume an input array of the form:
>
>    ( [1,2,3,4], [5,6,7,8], [9,10,11,12] )
>
> Which is called by C<reshape> with the following dimensions:
>
>    \$x,\$y   @results
>    -----   ------------------------------------------
>    -1,-1   ( 1,4,7,10,2,5,8,3,6,9 )      # simple concat

I think that a simple concat is:

reshape ([-1], @a);

since here the rank of a list is one, so the length of the first argument to
reshape is one. The -1 means 'use up the whole list'. It should be an error
to have more than one arg of -1.

>    3, -1   ( 1,2,3,5,6,7,8,9,10 )        # 3 vals from all lists

That should be a rank 2 matrix of shape (3,4), i.e.
([1,2,3],[4,5,6],[7,8,9],[10,11,12]).

>    -1, 2   ( 1,2,3,4,5,6,7,8 )           # all vals from 2 lists

That's a rank 2 matrix of shape (6,2).

>    3, 2    ( 1,2,3,4,5,6 )               # 3 vals x 2 lists
>
That's a rank 2 matrix of shape (3,2), which would discard the last 6
elements.

> Hopefully this is easy to understand. C<\$x> controls how many elements
> of each list are used, and C<\$y> controls how many lists are used. This
> is just like the splitting operation, but in reverse. Again, wildcards
> of C<-1> can be used here as well.
>
I don't think that there's 2 types of reshape(). There's one, and it takes
an array of one shape, and returns an array of a different shape. The shape
of the new array is specified by the first argument. The second argument is
a list, so it succumbs to Perl's normal list flattening behaviour.

The behaviour of reshape() should reflect:

- PDL's reshape()
- NumPy's reshape() -- which is the only one allowing '-1' in the shape
- J's '\$' verb

which all behave the same way.

Also, the examples should show reshaping into and out of arrays other than
rank 1 or 2.

Sorry Nate--I know we thought we were on the same wavelength here, but it
looks like we weren't at all! Would you like me to redraft this for you, or
create a new RFC?

```
Thread Next

nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About