What should specialized-array-reshape do?
Bradley Lucier 12 May 2020 22:22 UTC
I've been looking at numpy's resizing code.
Basically, numpy tries to resize an array in place with what it calls a
"view", and if that doesn't work, it automatically copies the contents
of the old array to a newly allocated array.
Here is what I've considered might happen if you ask to resize an array
and it can't happen:
1. Do the numpy thing, resize it in place if some sufficient condition
is met, otherwise copy it to a new array. The choice is made silently.
The code that determines whether a numpy array can be shared in place is
found here:
https://github.com/numpy/numpy/blob/7f836a9aca57de7fcae188b66ee1d8b60c6fc7b1/numpy/core/src/multiarray/shape.c#L400
but I don't understand it yet.
2. Offer a function array-can-be-reshaped-in-place?, so users can check
themselves, and have specialized-array-reshape throw an error if asked
to do something impossible.
3. Have specialized-array-reshape return either a resized array (in
place) or #f. Then the user must, in general, check the result before
going on and using the result, otherwise there will be a
difficult-to-debug problem later on.
4. Until now, I've been thinking of array-copy as a reshape operator
for both generalized arrays and specialized arrays, with in-place
resizing only for specialized array (it doesn't make sense for
generalized arrays). Perhaps we could just have array-reshape, with
copying the default for generalized arrays, and the possibility of
reshaping in place for specialized arrays (extend the NumPy idea).
None of these seem very Schemely.
Any suggestions?
Brad