[Numpy-discussion] ndarray.fill and ma.array.filled
Tim Hochberg
tim.hochberg at cox.net
Thu Mar 23 08:12:09 EST 2006
Fernando Perez wrote:
> Tim Hochberg wrote:
>
>> My opinion is that all methods and functions should either:
>>
>> 1. Always return a copy.
>> 2. Always return a view
>> 3. Return a view if possible otherwise raise an exception.
>
>
> Well, but is copy/view the /only/ invariant worth guaranteeing? I
> think there is a valid need for functions which ensure other
> invariants, such as contiguity. There are applications (such as
> passing pointers to C/Fortran libraries which don't have striding
> mechanisms but will not modify their inputs) which require contiguous
> inputs, but where one would rather make a copy only if necessary.
This is a different case, I think. The result of this copy is not user
visible except in terms of performance. I'm only concerned with
functions that *return* copies or views depending on the input. I don't
care if a function sometimes makes a copy under the covers but doesn't
return it.
> My take on this is that we should /document/ clearly what invariants
> any given function satisfies, but I think the 'always view/always
> copy' view excludes an important usage case. There may be others
> beyond contiguity, but that's the one that pops immediately to mind.
I don't think we're in disagreement here although I'm not sure.
I will add, on the subject of continuity, that I think there should be a
function 'ascontiguous' that parallels asarray, but assures that the
result is contiguous. Although this sometimes returns a copy, I thinks
that's OK since that's it's job. I would like to see all of the implicit
copying pushed into functions like asarray and ascontiguous.
This also helps efficiency. Imagine I have some calls to functions that
require contiguous arrays and do copies under the covers if their args
are not contiguous. In that case:
a = ascontiguous(a)
param1 = computeSomethingOnContiguousData(a)
param2 = computeSomethingElseOnContiguousData(a)
# etc.
Will be much more efficient than the equivalent code without the
ascontiguous when the initial a value is not discontiguous.
Regards,
-tim
More information about the NumPy-Discussion
mailing list