optional arguments to the array constructor

(for numpy v1.0 on Mandrake 10 i686) As noted on p. 25 the array constructor takes up to 5 optional arguments array(sequence=None, type=None, shape=None, copy=1, savespace=0,typecode=None) (and raises an exception if both type and typecode are set). Is there any way to make an alias (copy=0) of an array without passing keyword values? That is, specifying the copy keyword alone works: test=N.array((1., 3), "Float64", shape=(2,), copy=1, savespace=0) a=N.array(test, copy=0) a[1]=999 print test
[ 1. 999.]
But when intervening keywords are specified copy won't toggle: test=N.array((1., 3)) a=N.array(sequence=test, type="Float64", shape=(2,), copy=0) a[1]=999. print test
[ 1. 3.]
Which is also the behaviour I see when I drop the keywords: test=N.array((1., 3)) a=N.array(test, "Float64", (2,), 0) a[1]=999. print test
[ 1. 3.]
an additional puzzle is that adding the savespace parameter raises the following exception:
a=N.array(test, "Float64", (2,), 0,0) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 312, in array type = getTypeObject(sequence, type, typecode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 256, in getTypeObject rtype = _typeFromTypeAndTypecode(type, typecode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 243, in _typeFromTypeAndTypecode raise ValueError("Can't define both 'type' and 'typecode' for an array.") ValueError: Can't define both 'type' and 'typecode' for an array.
Thanks for any insights -- Phil

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.
As noted on p. 25 the array constructor takes up to 5 optional arguments
array(sequence=None, type=None, shape=None, copy=1, savespace=0,typecode=None) (and raises an exception if both type and typecode are set).
Is there any way to make an alias (copy=0) of an array without passing keyword values?
In numarray, all you have to do to get an alias is:
b = a.view()
It's an alias because:
b._data is a._data True
That is, specifying the copy keyword alone works:
test=N.array((1., 3), "Float64", shape=(2,), copy=1, savespace=0) a=N.array(test, copy=0) a[1]=999 print test
[ 1. 999.]
But when intervening keywords are specified copy won't toggle:
test=N.array((1., 3)) a=N.array(sequence=test, type="Float64", shape=(2,), copy=0) a[1]=999. print test
[ 1. 3.]
Which is also the behaviour I see when I drop the keywords:
test=N.array((1., 3)) a=N.array(test, "Float64", (2,), 0) a[1]=999. print test
[ 1. 3.]
an additional puzzle is that adding the savespace parameter raises the following exception:
a=N.array(test, "Float64", (2,), 0,0) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 312, in array type = getTypeObject(sequence, type, typecode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 256, in getTypeObject rtype = _typeFromTypeAndTypecode(type, typecode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 243, in _typeFromTypeAndTypecode raise ValueError("Can't define both 'type' and 'typecode' for an array.") ValueError: Can't define both 'type' and 'typecode' for an array.
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): Sorry about the confusion, Todd

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

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

Todd Miller writes:
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
Just to be clear -- the above is the current numarray v1.0 behavior (at least on my machine). Numeric compatibility would additonally require that import numarray a = numarray.arange(10) theTypeCode=repr(a.type()) b = numarray.array(a, theTypeCode, copy=0) print a is b b = numarray.array(a, copy=1) print a is b produce True False While currently it produces True True Having said this, I can work around this difference -- so either a note in the documentation or just removing the copy flag from numarray.array would also be ok. -- Thanks, Phil

Oops, note the change below at ---> Todd Miller writes:
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
Just to be clear -- the above is the current numarray v1.0 behavior (at least on my machine). Numeric compatibility would additonally require that import numarray a = numarray.arange(10) theTypeCode=repr(a.type()) b = numarray.array(a, theTypeCode, copy=0) print a is b b = numarray.array(a, copy=1) print a is b produce True False While currently it produces ---> False False Having said this, I can work around this difference -- so either a note in the documentation or just removing the copy flag from numarray.array would also be ok. -- Thanks, Phil
participants (2)
-
Philip Austin
-
Todd Miller