rainwoodman / vast

vala and scientific numerical computation
11 stars 1 forks source link

reshape #7

Open rainwoodman opened 8 years ago

rainwoodman commented 8 years ago

https://github.com/numpy/numpy/blob/dbb70947b95b92abf3e48ef8be7bd3ab79489892/numpy/core/src/multiarray/shape.c

arteymix commented 7 years ago

As said in #12

For reshape wouldn't it be nicer to fill smallest size first? Instead of having -1 for inferred dimension, we could have something like size_t.MAX for unconstrained axes.

arr.reshape ({2, 2, size_t.MAX}); // fill 2 by 2 and as much as possible in the last axis
rainwoodman commented 7 years ago

a possible algorithm:

solve for the unknown

find the permutation of axes that moves the trivially iterable (continuous) axes to the last dimensions

transose the array

permute the new shape

assert the new shape is identical to the transposed array's shape in the non-continuous axes

modify the shape and strides of the transposed array

untranspose the transposed array.
arteymix commented 7 years ago

It could be, the only thing to keep in mind is that reshaping does not change the order of iteration: it's like reinserting existing elements in a new array.

I guess it should only be about finding a new compatible shape that fit the constraints and use a general strategy to reshape.

arteymix commented 7 years ago

Should we deal with contractions as well? LIke if the new shape is too small for the current entries?

rainwoodman commented 7 years ago
arteymix commented 7 years ago

Let say that we reshape (2, 4) into (2, 2), do we trim the 4 last elements or do we raise an error?

rainwoodman commented 7 years ago

Error!

reshape is messy and probably not all that useful afterall.

For broadcast we only need the ability to insert axes of dimension 1 at any location in the shape array. Just thought over this straight last night.

Lemma: If shape[d] == 1 then strides[d] is irrelevant. Lemma: inserting strides[] = 0 dimensions do not change flat iteration order.

In broadcast we make use of this fact to reset strides[d] = 0 on any dimensions with shape[d] = 1 then we change the dimension to shape[d] = desired shape.

Of course the length of shape array must be matched by explicitly inserting axes with shape[d] == 1 before the broadcast.

On Wed, Nov 30, 2016 at 6:50 PM, Guillaume Poirier-Morency < notifications@github.com> wrote:

Let say that we reshape (2, 4) into (2, 2), do we trim the 4 last elements or do we raise an error?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rainwoodman/vast/issues/7#issuecomment-264064494, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIbTOxRB9G-E2kxwQ17NTBq9gE6tehxks5rDjYAgaJpZM4KrrRj .