[MATRIX-SIG] umath.logical_blah vs normal python functions.

Rob Hooft R.Hooft@EuroMail.com
Thu, 20 Nov 1997 08:52:38 +0100 (MET)


>>>>> "DB" == David J C Beach <beach@verinet.com> writes:

 DB> Moore, Paul wrote:

 >> One advantage of an and/or which returns 1 or 0 is that you can
 >> play games with it, like multiply it by a constant. Consider
 >> 
 >> a * logical_and(a,b)
 >> 
 >> which gives the same values as a where b is nonzero, but zero
 >> where b is zero. This is the sort of game that gets played in APL
 >> all the time.... (OK, this isn't a very good example, but I can't
 >> make up a realistic case offhand).

 DB>  Is this the clear kind of programming style promoted by Python's
 DB> designers?

It probably is not, but I must say that when using NumPy, it is often
the only way to prevent interpreter-level loops over array elements.
It took me a long time to get used to this as well, but in the end one
can actually read ones own code! From my code, another example of the
use of boolean functions returning 0 or 1 and not any kind of value:

       min=Numeric.minimum(image0,image1)
       sum=image0+image1
       difsquare=(image0-image1)**2
       condition=Numeric.greater(difsquare,4*sum)
       # if dif^2>4*sum return 2*min, else return sum
       result=Numeric.choose(condition,(sum,2*min))

This use of a "condition" and a Numeric.choose call are very common 
in my software.

[for the interested: this example removes cosmic rays from digitized
 images by merging two measurements, taking into account that the
 measurement error in each pixel is the square-root of that pixel's
 value. I fear that for doing the same with more than two original
 images efficiently, I will need a C function. Open for suggestions.]


-- 
=====       R.Hooft@EuroMail.com   http://www.Sander.EMBL-Heidelberg.DE/rob/ ==
=====       R&D, Nonius BV, Delft  http://www.nonius.com/                    ==
===== PGPid 0xFA19277D ========================== Use Linux! ==================

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________