[Python-bugs-list] [Bug #110838] Inverse hyperbolic functions in cmath module (PR#231)

noreply@sourceforge.net noreply@sourceforge.net
Tue, 12 Dec 2000 12:54:11 -0800


Bug #110838, was updated on 2000-Aug-01 14:15
Here is a current snapshot of the bug.

Project: Python
Category: Modules
Status: Closed
Resolution: None
Bug Group: Feature Request
Priority: 1
Submitted by: nobody
Assigned to : tim_one
Summary: Inverse hyperbolic functions in cmath module (PR#231)

Details: Jitterbug-Id: 231
Submitted-By: nadavh@envision.co.il
Date: Fri, 10 Mar 2000 18:35:07 -0500 (EST)
Version: 1.52
OS: NT 4.0 SP4


1.  The function cmath.acosh provides the negative branch with low 
precision. For example:

>>> cmath.acosh(cmath.cosh(10.0))
(-10.0000000135+0j)

Proposed solution --- use the following formula which is precise and
avoids singularities with complex arguments:

def acosh(x):
   return 2.0*log(sqrt(x+1.0) + sqrt(x-1.0)) - log(2.0)

2.  The function cmath.sinh does not handle moderately large 
arguments. For example:

>>> cmath.asinh(cmath.sinh(20.0))
(1.#INF+0j)

Proposed solution:

Use the textbook formula:
def asinh(x):
   return log(x+sqrt(x*x+1.0))

This calculation is more limited then the acosh calculation, but
still works fine.



====================================================================
Audit trail:
Mon Apr 03 18:38:28 2000	guido	changed notes
Mon Apr 03 18:38:28 2000	guido	moved from incoming to request

Follow-Ups:

Date: 2000-Dec-12 12:54
By: gvanrossum

Comment:
I've added this feature request to PEP 42.
-------------------------------------------------------

Date: 2000-Aug-01 17:38
By: jhylton

Comment:
I think this bug should be left open, but perhaps a new bug should be created for the general feature request "re-write cmath in python." It's up to you, Tim.

-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
Might be a good idea.
Waiting for patches.

-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
From: "Tim Peters" <tim_one@email.msn.com>
Subject: RE: [Python-bugs-list] Inverse hyperbolic functions in cmath module (PR#231)
Date: Sat, 11 Mar 2000 13:47:25 -0500

[Tim]
> C doesn't define any functions on complex numbers -- cmathmodule.c
> implements these all on its own.

[David Ascher]
> As an aside, if anyone ever wants to trim the number of builtin C
> modules, I found that it was much easier to write cmath.py than to
> write cmath.java (for JPython).  The same cmath.py should work fine
> in CPython.

Yes, I don't see anything in cmathmodule.c that *needs* to be coded in C; &
coding would be much clearer in Python, using infix notation for the basic
complex binary ops.  Two possible reasons for leaving it in C:

1. Lower internal call overheads (i.e., speed).

2. Improving quality -- complex libraries are very difficult to get right
   in all cases if they're made IEEE-754 aware, and doing so requires
fiddling
   with the processor-level 754 control & status features.  But there's no
   portable way to do that now, and won't be until the next iteration of C.

> I can dig it up, but I can't swear that I used the most numerically stable
> algorithms.

I can:  you didn't <wink>.  Doesn't matter, though!  cmathmodule.c is naive
too, and achieving good accuracy across the whole domain is a major
undertaking.  That gives the best reason to write it in Python:

3. There's a long way to go to make this "industrial strength", so the
   current cmath is really just a prototype.  Everyone knows prototyping
   is much easier in Python.  QED <wink>.

> It did give the same numbers as CPython's cmath on a test set.

So ship it <wink>.




-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
From: "David Ascher" <DavidA@ActiveState.com>
Subject: RE: [Python-bugs-list] Inverse hyperbolic functions in cmath module (PR#231)
Date: Fri, 10 Mar 2000 21:49:27 -0800

> [Nadav Horesh, suggests different algorithms in cmath, for some complex
>  inverse hyperbolics]
>
> [Guido, misfires]
> > We're just using the VC++ C library.
>
> C doesn't define any functions on complex numbers -- cmathmodule.c
> implements these all on its own.  I can't make time to look at
> this now, but
> complaining to Microsoft about this will do Nadav even less good than when
> it *is* their problem <wink>.

As an aside, if anyone ever wants to trim the number of builtin C modules, I
found that it was much easier to write cmath.py than to write cmath.java
(for JPython).  The same cmath.py should work fine in CPython.  I can dig it
up, but I can't swear that I used the most numerically stable algorithms.
It did give the same numbers as CPython's cmath on a test set.

-david



-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
From: "Tim Peters" <tim_one@email.msn.com>
Subject: RE: [Python-bugs-list] Inverse hyperbolic functions in cmath module (PR#231)
Date: Fri, 10 Mar 2000 22:05:07 -0500

[Nadav Horesh, suggests different algorithms in cmath, for some complex
 inverse hyperbolics]

[Guido, misfires]
> We're just using the VC++ C library.

C doesn't define any functions on complex numbers -- cmathmodule.c
implements these all on its own.  I can't make time to look at this now, but
complaining to Microsoft about this will do Nadav even less good than when
it *is* their problem <wink>.




-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
From: "David Ascher" <DavidA@ActiveState.com>
Subject: RE: [Python-bugs-list] Inverse hyperbolic functions in cmath module (PR#231)
Date: Fri, 10 Mar 2000 16:47:23 -0800

> We're just using the VC++ C library.  I suggest you send your bug
> report to Microsoft.

FWIW: the Perl folks are more and more (it seems to me) redoing things
themselves if the C library tends to be broken or slow.  I'm not suggesting
that it's a good decision, just commenting.

--david



-------------------------------------------------------

Date: 2000-Aug-01 14:15
By: nobody

Comment:
From: Guido van Rossum <guido@python.org>
Subject: Re: [Python-bugs-list] Inverse hyperbolic functions in cmath module (PR#231)
Date: Fri, 10 Mar 2000 18:44:01 -0500

> Full_Name: Nadav Horesh
> Version: 1.52
> OS: NT 4.0 SP4
> Submission from: (NULL) (212.25.119.223)
> 
> 
> 1.  The function cmath.acosh provides the negative branch with low 
> precision. For example:
> 
> >>> cmath.acosh(cmath.cosh(10.0))
> (-10.0000000135+0j)
> 
> Proposed solution --- use the following formula which is precise and
> avoids singularities with complex arguments:
> 
> def acosh(x):
>    return 2.0*log(sqrt(x+1.0) + sqrt(x-1.0)) - log(2.0)
> 
> 2.  The function cmath.sinh does not handle moderately large 
> arguments. For example:
> 
> >>> cmath.asinh(cmath.sinh(20.0))
> (1.#INF+0j)
> 
> Proposed solution:
> 
> Use the textbook formula:
> def asinh(x):
>    return log(x+sqrt(x*x+1.0))
> 
> This calculation is more limited then the acosh calculation, but
> still works fine.

We're just using the VC++ C library.  I suggest you send your bug
report to Microsoft.

--Guido van Rossum (home page: http://www.python.org/~guido/)


-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=110838&group_id=5470