[SciPy-user] once again about zeros() and ones()

dmitrey openopt@ukr....
Sun Jul 1 12:12:12 CDT 2007

Matthieu Brucher wrote:
> numpy.zeros((2, 3, 4), float) for instance.
> Matthieu
Ok, please try the updated file.
Maybe, it could be simplified.

from numpy import ones
def Ones(*args, **kwargs):
    if type(args[0]) in (type(()), type([])):
        return ones(*args, **kwargs)
        i, args2 = 1, [args[0]]
        while i<len(args) and type(args[i])==type(15):  
args2.append(args[i]);  i+=1
        if len(args[i:]) ==0: return ones(args2, **kwargs)
        elif len(args[i:]) ==1: return ones(args2, args[i], **kwargs)
        elif len(args[i:]) ==2: return ones(args2, args[i], args[i+1], 
        else: print 'ERROR!'

if __name__ == '__main__':
    print Ones((2,2))
    print 2*Ones([2,2])
    print 3*Ones(2,2)   
    print 4*Ones(2,2,2, dtype=float)
    print 5*Ones((2,2,2), dtype='float')
    print 6*Ones([2,2,2], dtype='int')
    print 7*Ones(3,3, dtype=int, order = 'C')
    print 8*Ones((3,3), dtype='int')
    print 9*Ones(3, dtype='int')
    print 10*Ones((3,), dtype='int')

    print 11*ones((2, 3, 4), float)
    print 12*Ones([2, 3, 4], 'float')
    print 13*Ones(2, 3, 4, float)
    print 14*Ones(2, 3, 4, int, order = 'C')
    print 15*Ones(2, 3, 4, float, 'F')

More information about the SciPy-user mailing list