[Numpy-discussion] optional arguments to the array constructor
Todd Miller
jmiller at stsci.edu
Wed Jul 7 12:47:02 EDT 2004
On Wed, 2004-07-07 at 14:25, Philip Austin wrote:
> Todd Miller writes:
> > On Tue, 2004-07-06 at 21:42, Philip Austin wrote:
> > > (for numpy v1.0 on Mandrake 10 i686)
> >
> > My guess is you're talking about numarray here. Please be charitable if
> > I'm talking out of turn... I tend to see everything as a numarray
> > issue.
>
> Right -- I'm still working through the boost test suite for numarray, which is
> failing a couple of tests that passed (around numarray v0.3).
>
> > All this looks like a documentation problem. The numarray array()
> > signature has been tortured by Numeric backward compatibility, so there
> > has been more flux in it than you would expect. Anyway, the manual is
> > out of date. Here's the current signature from the code:
> >
> > def array(sequence=None, typecode=None, copy=1, savespace=0,
> > type=None, shape=None):
> >
>
> Actually, it seems to be a difference in the way that numeric and
> numarray treat the copy flag when typecode is specified. In numeric,
> if no change in type is requested and copy=0, then the constructor
> goes ahead and produces a view:
>
> import Numeric as nc
> test=nc.array([1,2,3],'i')
> a=nc.array(test,'i',0)
> a[0]=99
> print test
> >> [99 2 3]
>
> but makes a copy if a cast is required:
>
> test=nc.array([1,2,3],'i')
> a=nc.array(test,'F',0)
> a[0]=99
> print test
> >>> [1 2 3]
>
> Looking at numarraycore.py line 305 I see that:
>
> if type is None and typecode is None:
> if copy:
> a = sequence.copy()
> else:
> a = sequence
>
> i.e. numarray skips the check for a type match and ignores
> the copy flag, even if the type is preserved:
>
> import numarray as ny
> test=ny.array([1,2,3],'i')
> a=ny.array(test,'i',0)
> a._data is test._data
> >>> False
>
OK, I think I see what you're after and agree that it's a bug. Here's
how I'll change the behavior:
>>> import numarray
>>> a = numarray.arange(10)
>>> b = numarray.array(a, copy=0)
>>> a is b
True
>>> b = numarray.array(a, copy=1)
>>> a is b
False
One possible point of note is that array() doesn't return views for
copy=0; neither does Numeric; both return the original sequence.
Regards,
Todd
More information about the NumPy-Discussion
mailing list