signum() not in math?

Dinu Gherman gherman at darwin.in-berlin.de
Sat Oct 13 06:09:34 EDT 2001


Robin Becker wrote:
> 
> that may be so, but while some write this
> 
> def sign(x):
>     if x < 0: return -1
>     elif x > 0: return 1
>     else: return 0
> 
> others might use
> 
> def sign(x):
>     if x<0: return -1
>     else: return x>0
> 
> only an insider could tell us if the latter is guaranteed always to work
> now and forever. If truth gets redefined as -1 then the second would
> fail badly.

That's one more argument for having a function with a *defined* 
behaviour! In any case this is *not* about truth values, but 
about arithmetics!

> Also there is controversy about what the sign function should actually
> be.

Not quite sure. If there is then it's going to be about the value
at x = 0. I think 0 is what math dictates while -1 or +1 is what 
is more convenient in certain cases... which can be handled with 
an optional second argument to sign().

> Indeed the matrix sign function is a true heaviside function
> (http://rkb.home.cern.ch/rkb/AN16pp/node117.html) on each element x the
> result is 2*h(x)-1 ie -1 for x<0 and 1 for x>=0.
>
> Probably python should provide both the three value signum and the two
> value one and perhaps the sign transfer operation. These are so common
> in maths, physics and engineering it seems wrong to have sinh() without
> h() et al.

One free pint! ;-) Well, yes a general step() function could 
be useful, too, maybe like this, even if you don't find it 
usually in entry-level math textbooks:

  def step(x, x0=0):
      "Generalised Heaviside step function."

      if x < x0: 
          return 0
      else:
          return 1

> In article <pcolmigtsz3.fsf at thoth.home>, Harald Hanche-Olsen
> <hanche at math.ntnu.no> writes
> >
> >>>> import math
> >>>> math.atan2(0.,-1.)
> >3.1415926535897931
> >>>> math.atan2(-0.,-1.)
> >-3.1415926535897931
> >
> perhaps we need some new type to distinguish numbers which might result
> in this behaviour. Then we could have a flame war about introducing it
> :) 

Looks like we have these JanusNumbers already. They change face
depending on their context:

>>> from math import *
>>> tan(-0. + pi/2)
16331778728383844.0
>>> tan(0. + pi/2)
16331778728383844.0

and:

>>> 0.
0.0
>>> -0.
0.0

Even-more-baffled'ly,

Dinu

-- 
Dinu C. Gherman
................................................................
"The world becomes a better place as a result of the refusal
to bend to authority and doctrine." (Noam Chomsky)



More information about the Python-list mailing list