Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9e7be8c
BUG: Overflow in tan and tanh for large complex values
ewmoore Feb 22, 2013
f442ead
STY: npymath: tabs -> spaces, long lines
ewmoore Mar 1, 2013
d62c590
ENH: Check for, and use all C99 complex functions if available
ewmoore Mar 6, 2013
af5ff55
ENH: npymath: Use a better complex division algorithm.
ewmoore Mar 8, 2013
e6b99c9
MAINT: umath: generate the ldexp and frexp ufuncs like all the others.
ewmoore Mar 9, 2013
0fdaa54
MAINT: umath/npymath: Add ldexp and frexp to npymath
ewmoore Mar 9, 2013
2d33f7c
ENH: npymath: handle clog edge cases more carefully.
ewmoore Mar 10, 2013
50d6720
ENH: evaluate c99 complex funcs at build time
ewmoore Oct 1, 2013
d504ea1
BUG: NPY_LOGE2 is log(2)
ewmoore Oct 2, 2013
5362b35
BUG: Test aginst +/-TANH_HUGE
ewmoore Oct 2, 2013
e61e74f
TST: check for tanh(1000+0j) == 1 + 0j etc.
ewmoore Oct 2, 2013
852cd10
BUG: Fix up some corner cases in npy_cexp
ewmoore Oct 2, 2013
0f57b42
MAINT: remove debug print statement
ewmoore Oct 2, 2013
eb06557
MAINT: fix two typos in test_c99complex.c
ewmoore Oct 4, 2013
77705ab
ENH: Be more careful with large real parts in npy_cexp
ewmoore Oct 4, 2013
e574e29
ENH: Import the ccosh/ccos implementation from FreeBSD
ewmoore Oct 4, 2013
3710481
ENH: Import the csinh/csin implementation from FreeBSD
ewmoore Oct 4, 2013
a837853
ENH: Import the catanh/catan implemenation from FreeBSD
ewmoore Oct 9, 2013
36e0d8b
BUG: printf requires a literal for the first argument.
ewmoore Oct 9, 2013
330b54a
BUG: Py3 '-2j' is (NZERO - 2j) causes test failures
ewmoore Oct 9, 2013
def6327
ENH: Import the cacos,casin,cacosh,casinh implemenation from FreeBSD
ewmoore Oct 10, 2013
c8f13ee
TST: Enable signed zero branch cut tests
ewmoore Oct 10, 2013
eab5228
BUG: fix cpow tests to match what npy_cpow does.
ewmoore Oct 10, 2013
2b5f1ad
STY: long lines, whitespace
ewmoore Oct 10, 2013
1fb1b74
MAINT: remove gccisms and unbreak MSVC build.
ewmoore Oct 11, 2013
be42cae
MAINT: fix ldexp/frexp changes to compile with MSVC.
ewmoore Oct 11, 2013
ac075b2
MAINT: npy_a(exp,log,sqrt,fabs) don't really exist.
ewmoore Oct 15, 2013
79afefd
ENH: Run the test_c99complex.c tests with the test suite too.
ewmoore Oct 16, 2013
cfe9fcc
BUG: Fix tests to compile under MSVC
ewmoore Oct 21, 2013
cbc97fa
MAINT: pick sunmath.h from npy_math.h
ewmoore Oct 21, 2013
863d97d
MAINT: Update note about FreeBSD.
ewmoore Oct 22, 2013
2348023
MAINT: make sure npy_math builds when long double is double double
ewmoore Oct 24, 2013
5771dae
MAINT: move npy_(get,clear)_floatstatus() to their own file.
ewmoore Nov 7, 2013
566c15e
BUG Use the floating point status functions in build time tests
ewmoore Nov 7, 2013
503047b
MAINT: move test_c99complex.c to npymath
ewmoore Nov 7, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: Overflow in tan and tanh for large complex values
gh-2321 (trac 1726).

np.tanh(1000+0j) gave nan+nan*j, should be 1.0+0j.  The
same bug was present in np.tan(0+1000j).

Bug fixed by replacing our complex tan and tanh implementation
with one from FreeBSD.
  • Loading branch information
ewmoore committed Feb 26, 2014
commit 9e7be8cfe8f8e734ed3af3cbd50d9caa2ad45dc9
9 changes: 9 additions & 0 deletions numpy/core/include/numpy/npy_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ npy_cdouble npy_csqrt(npy_cdouble z);

npy_cdouble npy_ccos(npy_cdouble z);
npy_cdouble npy_csin(npy_cdouble z);
npy_cdouble npy_ctan(npy_cdouble z);

npy_cdouble npy_ctanh(npy_cdouble z);

/*
* Single precision complex functions
Expand All @@ -435,6 +438,9 @@ npy_cfloat npy_csqrtf(npy_cfloat z);

npy_cfloat npy_ccosf(npy_cfloat z);
npy_cfloat npy_csinf(npy_cfloat z);
npy_cfloat npy_ctanf(npy_cfloat z);

npy_cfloat npy_ctanhf(npy_cfloat z);

/*
* Extended precision complex functions
Expand All @@ -450,6 +456,9 @@ npy_clongdouble npy_csqrtl(npy_clongdouble z);

npy_clongdouble npy_ccosl(npy_clongdouble z);
npy_clongdouble npy_csinl(npy_clongdouble z);
npy_clongdouble npy_ctanl(npy_clongdouble z);

npy_clongdouble npy_ctanhl(npy_clongdouble z);

/*
* Functions that set the floating point error
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/setup_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def check_api_version(apiversion, codegen_dir):
C99_COMPLEX_TYPES = ['complex double', 'complex float', 'complex long double']

C99_COMPLEX_FUNCS = ['creal', 'cimag', 'cabs', 'carg', 'cexp', 'csqrt', 'clog',
'ccos', 'csin', 'cpow']
'ccos', 'csin', 'cpow', 'ctan', 'ctanh']

def fname2def(name):
return "HAVE_%s" % name.upper()
Expand Down
123 changes: 120 additions & 3 deletions numpy/core/src/npymath/npy_math_complex.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Most of the code is taken from the msun library in FreeBSD (HEAD @ 30th June
* 2009), under the following license:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should perhaps be updated.

*
* Copyright (c) 2007 David Schultz <[email protected]>
* Copyright (c) 2007, 2011 David Schultz <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -227,6 +227,123 @@
return npy_cpack@c@(npy_sin@c@(x) * npy_cosh@c@(y), npy_cos@c@(x) * npy_sinh@c@(y));
}
#endif

#ifndef HAVE_CTAN@C@
@ctype@ npy_ctan@c@(@ctype@ z)
{
/* ctan(z) = -I * ctanh(I * z) */
z = npy_ctanh@c@(npy_cpack@c@(-npy_cimag@c@(z), npy_creal@c@(z)));
return (npy_cpack@c@(npy_cimag@c@(z), -npy_creal@c@(z)));
}
#endif

#ifndef HAVE_CTANH@C@
/*
* Taken from the msun library in FreeBSD, rev 226600.
*
* Hyperbolic tangent of a complex argument z = x + i y.
*
* The algorithm is from:
*
* W. Kahan. Branch Cuts for Complex Elementary Functions or Much
* Ado About Nothing's Sign Bit. In The State of the Art in
* Numerical Analysis, pp. 165 ff. Iserles and Powell, eds., 1987.
*
* Method:
*
* Let t = tan(x)
* beta = 1/cos^2(y)
* s = sinh(x)
* rho = cosh(x)
*
* We have:
*
* tanh(z) = sinh(z) / cosh(z)
*
* sinh(x) cos(y) + i cosh(x) sin(y)
* = ---------------------------------
* cosh(x) cos(y) + i sinh(x) sin(y)
*
* cosh(x) sinh(x) / cos^2(y) + i tan(y)
* = -------------------------------------
* 1 + sinh^2(x) / cos^2(y)
*
* beta rho s + i t
* = ----------------
* 1 + beta s^2
*
* Modifications:
*
* I omitted the original algorithm's handling of overflow in tan(x) after
* verifying with nearpi.c that this can't happen in IEEE single or double
* precision. I also handle large x differently.
*/

#define TANH_HUGE 22.0
#define TANHF_HUGE 11.0F
#define TANHL_HUGE 42.0L

@ctype@ npy_ctanh@c@(@ctype@ z)
{
@type@ x, y;
@type@ t, beta, s, rho, denom;

x = npy_creal@c@(z);
y = npy_cimag@c@(z);

/*
* ctanh(NaN + i 0) = NaN + i 0
*
* ctanh(NaN + i y) = NaN + i NaN for y != 0
*
* The imaginary part has the sign of x*sin(2*y), but there's no
* special effort to get this right.
*
* ctanh(+-Inf +- i Inf) = +-1 +- 0
*
* ctanh(+-Inf + i y) = +-1 + 0 sin(2y) for y finite
*
* The imaginary part of the sign is unspecified. This special
* case is only needed to avoid a spurious invalid exception when
* y is infinite.
*/
if (!npy_isfinite(x)) {
if (npy_isnan(x))
return npy_cpack@c@(x, (y == 0 ? y : x * y));
return npy_cpack@c@(npy_copysign@c@(1,x),
npy_copysign@c@(0, npy_isinf(y) ? y : npy_sin@c@(y) * npy_cos@c@(y)));
}

/*
* ctanh(x + i NAN) = NaN + i NaN
* ctanh(x +- i Inf) = NaN + i NaN
*/
if (!npy_isfinite(y))
return (npy_cpack@c@(y - y, y - y));

/*
* ctanh(+-huge + i +-y) ~= +-1 +- i 2sin(2y)/exp(2x), using the
* approximation sinh^2(huge) ~= exp(2*huge) / 4.
* We use a modified formula to avoid spurious overflow.
*/
if (x >= TANH@C@_HUGE) {
@type@ exp_mx = npy_exp@c@(-npy_fabs@c@(x));
return (npy_cpack@c@(npy_copysign@c@(1, x),
4 * npy_sin@c@(y) * npy_cos@c@(y) * exp_mx * exp_mx));
}

/* Kahan's algorithm */
t = npy_tan@c@(y);
beta = 1.0 + t * t; /* = 1 / cos^2(y) */
s = npy_sinh@c@(x);
rho = npy_sqrt@c@(1 + s * s); /* = cosh(x) */
denom = 1 + beta * s * s;
return (npy_cpack@c@((beta * rho * s) / denom, t / denom));
}
#undef TANH_HUGE
#undef TANHF_HUGE
#undef TANHL_HUGE
#endif
/**end repeat**/

/*==========================================================
Expand Down Expand Up @@ -254,8 +371,8 @@
/**end repeat1**/

/**begin repeat1
* #kind = cexp,clog,csqrt,ccos,csin#
* #KIND = CEXP,CLOG,CSQRT,CCOS,CSIN#
* #kind = cexp,clog,csqrt,ccos,csin,ctan,ctanh#
* #KIND = CEXP,CLOG,CSQRT,CCOS,CSIN,CTAN,CTANH#
*/
#ifdef HAVE_@KIND@@C@
@ctype@ npy_@kind@@c@(@ctype@ z)
Expand Down
34 changes: 3 additions & 31 deletions numpy/core/src/umath/funcs.inc.src
Original file line number Diff line number Diff line change
Expand Up @@ -671,42 +671,14 @@ nc_sinh@c@(@ctype@ *x, @ctype@ *r)
static void
nc_tan@c@(@ctype@ *x, @ctype@ *r)
{
@ftype@ sr,cr,shi,chi;
@ftype@ rs,is,rc,ic;
@ftype@ d;
@ftype@ xr=x->real, xi=x->imag;
sr = npy_sin@c@(xr);
cr = npy_cos@c@(xr);
shi = npy_sinh@c@(xi);
chi = npy_cosh@c@(xi);
rs = sr*chi;
is = cr*shi;
rc = cr*chi;
ic = -sr*shi;
d = rc*rc + ic*ic;
r->real = (rs*rc+is*ic)/d;
r->imag = (is*rc-rs*ic)/d;
return;
*r = npy_ctan@c@(*x);
return;
}

static void
nc_tanh@c@(@ctype@ *x, @ctype@ *r)
{
@ftype@ si,ci,shr,chr;
@ftype@ rs,is,rc,ic;
@ftype@ d;
@ftype@ xr=x->real, xi=x->imag;
si = npy_sin@c@(xi);
ci = npy_cos@c@(xi);
shr = npy_sinh@c@(xr);
chr = npy_cosh@c@(xr);
rs = ci*shr;
is = si*chr;
rc = ci*chr;
ic = si*shr;
d = rc*rc + ic*ic;
r->real = (rs*rc+is*ic)/d;
r->imag = (is*rc-rs*ic)/d;
*r = npy_ctanh@c@(*x);
return;
}

Expand Down