[Numpy-discussion] optional arguments to the array constructor

Philip Austin paustin at eos.ubc.ca
Wed Jul 7 11:26:11 EDT 2004


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

It look like there might have been a comment about this
in the docstring, but it got clipped at some point?:

array() constructs a NumArray by calling NumArray, one of its
    factory functions (fromstring, fromfile, fromlist), or by making a
    copy of an existing array.  If copy=0, array() will create a new
    array only if

    sequence             specifies the contents or storage for the array

Thanks, Phil





More information about the NumPy-Discussion mailing list