[Matrix-SIG] nevermind! (no round() in NumPy?)

jhauser@ifm.uni-kiel.de jhauser@ifm.uni-kiel.de
Tue, 20 Apr 1999 13:46:46 +0200 (CEST)


I have this function here to round like python does. (Hopefully)

__Janko

## cut here ###
def around(m, signif=0):
    """
    Should round in the way Python builtin round does it. Presume
    that this is the right way to do it.
    """
    m = Numeric.asarray(m)
    s = sign(m)
    if signif:
        m = Numeric.absolute(m*10.**signif)
    else:
        m = Numeric.absolute(m)
    rem = m-Numeric.asarray(m).astype(Numeric.Int)
    m = Numeric.where(Numeric.less(rem,0.5), Numeric.floor(m), Numeric.ceil(m))
    # convert back
    if signif:
        m = m*s/(10.**signif)
    else:
        m = m*s
    return m
    
def sign(m):
    """
    Gives an array with shape of m. Where array less than 0 a=-1,
    where m greater null a=1, elsewhere a=0.
    """
    m = Numeric.asarray(m)

    if ((type(m) == type(1.4)) or (type(m) == type(1))):
	return m-m-Numeric.less(m,0)+Numeric.greater(m,0)
    else:
	return Numeric.zeros(Numeric.shape(m))-Numeric.less(m,0)+Numeric.greater(m,0)

Just van Rossum writes:
 > Sorry, I must be having a bad day. I somehow remembered floor() going
 > towards zero, instead of going down *always*. So adding 0.5 and the
 > floor()-ing does work after all... Sorry for wasting your time. (Still, a
 > round() function woud be nice, no?)
 > 
 > Just
 > 
 > 
 > 
 > _______________________________________________
 > Matrix-SIG maillist  -  Matrix-SIG@python.org
 > http://www.python.org/mailman/listinfo/matrix-sig
 >