# [Numpy-discussion] Args for ones, zeros, rand, eye, ones, empty (possible 1.0 change?)

Bill Baxter wbaxter at gmail.com
Sun Jul 2 20:58:47 CDT 2006

```Here's another possible now or never change:
fix rand(), eye(), ones(), zeros(), and empty() to ALL take either a tuple
argument or plain list.

I know this has been discussed before, but I really don't see why these
methods can't be overloaded to accept either one.
Here are some wrappers I cooked up that basically do the trick, with a few
exceptions which could easily be ironed out with a little input from someone
who knows python better than me.  I've got versions of rand(), eye() and
ones() below.  empty() and zeros() should basically be the same as ones().

Actually I guess it's not a "now or never change", because this should be
completely backwards compatible.  But still I find myself very frequently
typing things like ones(2,2), and I'm getting tired of it not working.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060703/9f3db2b5/attachment.html
-------------- next part --------------

import types
import numpy

def xrand(*varg):
"""xrand(d0, d1, ..., dn) -> random values

Return an array of the given dimensions which is initialized to
random numbers from a uniform distribution in the range [0,1).

xrand(d0, d1, ..., dn) -> random values
or
xrand((d0, d1, ..., dn)) -> random values
"""
if len(varg) == 0 or type(varg[0]) != types.TupleType:
return rand(*varg)
else:
if len(varg) != 1:
raise TypeError('Argument should either be a tuple or an argument list')
else:
return rand(*varg[0])

def xeye(N, *varg,**kwarg):
"""xeye(N, M=None, k=0, dtype=float64)

eye returns a N-by-M 2-d array where the  k-th diagonal is all ones,
and everything else is zeros.
"""

if hasattr(N,'__getitem__') and hasattr(N,'__len__'):
if len(N) == 0 or len(N) > 2:
raise TypeError('First tuple to eye should be length 1 or 2')
if len(N) == 1:
return numpy.eye(N[0], None, *varg, **kwarg)
else:
return numpy.eye(N[0], N[1], *varg, **kwarg)

return numpy.eye(N,*varg,**kwarg)

def xones(shape, *varg, **kwarg):
"""xones(shape, dtype=<type 'int32scalar'>, order='C')

xones(shape, dtype=int_) returns an array of the given
dimensions which is initialized to all ones.
"""

if hasattr(shape,'__getitem__'):
return numpy.ones(shape,*varg,**kwarg)

i = 0
for x in varg:
if type(x)==types.IntType:
i+=1
else:
break

tshape = (shape,)
if i>0:
tshape += tuple( varg[0:i] )
args = varg[i:]
return numpy.ones(tshape, *args, **kwarg)

def test():
xrand()
xrand(2)
xrand(2,2)
xrand((2,2))

xeye(1)
xeye(2,2)
xeye(2,2)
xeye(2,2,0,numpy.float64)
xeye(2,2,k=0,dtype=numpy.float64)
xeye((2,2),0,numpy.float64)
xeye((2,2),k=0,dtype=numpy.float64)

xones(1)
xones([1])
xones([1])
xones(2,2)
xones([2,2])
xones((2,2))
xones(numpy.array([2,2]))

xones(1,numpy.float64)
xones([1],numpy.float64)
xones([1],numpy.float64)
xones(2,2,numpy.float64)
xones([2,2],numpy.float64)
xones((2,2),numpy.float64)
xones(numpy.array([2,2]),numpy.float64)

xones(1,dtype=numpy.float64)
xones([1],dtype=numpy.float64)
xones([1],dtype=numpy.float64)
xones(2,2,dtype=numpy.float64)
xones([2,2],dtype=numpy.float64)
xones((2,2),dtype=numpy.float64)
xones(numpy.array([2,2]),dtype=numpy.float64)

```