Open rainwoodman opened 8 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
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.
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.
Should we deal with contractions as well? LIke if the new shape is too small for the current entries?
I think my algorithm doesn't work. The number of dimensions of the new shape is different from the old shape.
It is more complicated and I need to find the time to code it up. Just got back from sickness and need to catch up with a bunch of real work that pays my salary though.
Example on contractions?
Let say that we reshape (2, 4)
into (2, 2)
, do we trim the 4 last elements or do we raise an error?
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 .
https://github.com/numpy/numpy/blob/dbb70947b95b92abf3e48ef8be7bd3ab79489892/numpy/core/src/multiarray/shape.c