Bill Baxter wrote:
> 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.
Since a tuple seems to work fine as an argument I imagine you means
something else.
>
> 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.
Ah! You mean accepts either multiple args or a sequence.
I don't like this kind of "helpful" gimmickry. I suspect that the errors
that it masks in some inscrutable way more than make up for the minor
time that it saves one on occasion.
So mark me down as -1.
-tim
>
>
> ------------------------------------------------------------------------
>
>
> 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)
>
>
>
> ------------------------------------------------------------------------
>
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Numpy-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/numpy-discussion