[SciPy-user] numarray complex comparisons

eric jones eric at enthought.com
Wed Dec 11 18:31:11 EST 2002


> Perry Greenfield:
> 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.

Sold to the man with the Hubble Telescope (and Tim Hochberg who had a
similar solution)!  Or maybe it is sold to me (the man with a humble
telescope).  

Anyway, I'll buy this solution.  While I continue to prefer the other,
this has very reasonable semantics and the overhead is (probably)
minimal.  So, it is a worthy compromise.  

Hmmm. I still have a gripe though -- what if low or hi is a complex?  It
still throws an exception.  Something like the following will solve this
problem:

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

Explicit people should love this (I do not)!  Anyway, I'm still willing
to go with it.  So, version 0.2 of SciPy will keep its current behavior.
If the .real and .imag stuff are added to Numeric (which is desirable),
we can remove complex comparison from SciPy in 0.3.  

The general rule for code added to SciPy will be that functions have
defined behavior for complex array types.  This means code like above
will be used in maximum, minimum, and inequalities.

I guess the discussion was worth one last round...  Thanks Perry for
another workable solution.  SciPy will mirror numarray on complex
comparison.

(Somehow I still have a nagging feeling I'm going to regret this one...)

regards,
eric





More information about the SciPy-User mailing list