[SciPy-user] numarray complex comparisons

Perry Greenfield perry at stsci.edu
Wed Dec 11 15:24:15 EST 2002


> def clip(a,low,hi):
>     a = asarray(a) # allows lists or scalars as input
>     res = where(a < low,low,a)
>     res = where(res > hi,hi,res)
>     return res
>    
> def clip2(a,low,hi):
>     a = asarray(a)
>     if a.typecode() in ['F','D']:
>         reals = a.real
>     else:
>         reals = a
>     if low is complex:
>         rlow = low.real
>     else:
>         rlow = low
>     if hi is complex:
>         rhi = hi.real
>     else:
>         rhi = hi
>               
>     res = where(reals < low,low,reals)
>     res = where(res > hi,hi,res)
>     return res.astype(a.typecode()) 
> 
How about a third approach?

def clip3(a,low, hi):
	a = asarray(a)
	res = where(a.real < low, low, a)
	res = where(res.real > hi, hi, res)
	return res

This is as simple as clip, is explicit about the
comparison behavior, and handles complex arrays
in the manner desired. It's true that .real doesn't
currently work on non-complex arrays, but it could,
or we could add a real function that just returns all
non-complex arrays as is and returns the real part of
complex arrays. 

Perry



More information about the SciPy-User mailing list