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

Skip to content
Closed
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
Prev Previous commit
Next Next commit
ENH: Import the ccosh/ccos implementation from FreeBSD
The code from FreeBSD was lightly adapted to fit with the numpy style
and to correct an Annex G failure. (ccosh(Inf + 0j) should be (Inf + 0),
and symmetric).

With this commit, npy_ccosh(f) and npy_ccos(f) pass all of the tests in
test_c99complex.c.
  • Loading branch information
ewmoore committed Feb 26, 2014
commit e574e299a781394a83cbcde0f9282fd0fecacafc
142 changes: 131 additions & 11 deletions numpy/core/src/npymath/npy_math_complex.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,20 @@ static NPY_INLINE @ctype@ cmuli@c@(@ctype@ a)
*
* This is based on the technique used in FreeBSD's __frexp_exp and
* __ldexp_(c)exp functions by David Schultz.
*
* SCALED_CEXP_LOWER = log(FLT_MAX)
* SCALED_CEXP_UPPER = log(2) + log(FLT_MAX) - log(FLT_TRUE_MIN),
* where FLT_TRUE_MIN is the smallest possible subnormal number.
*/

#define SCALED_CEXP_LOWERF 88.722839f
#define SCALED_CEXP_UPPERF 192.69492
#define SCALED_CEXP_LOWER 710.47586007394386
#define SCALED_CEXP_UPPERF 192.69492f
#define SCALED_CEXP_LOWER 710.47586007394386
#define SCALED_CEXP_UPPER 1454.9159319953251
#define SCALED_CEXP_LOWERL 11357.216553474703895L
#define SCALED_CEXP_UPPERL 22756.021937783004509L

static @ctype@ _npy_scaled_cexp@c@(@type@ x, @type@ y, int expt)
static @ctype@ _npy_scaled_cexp@c@(@type@ x, @type@ y, npy_int expt)
{
#if @precision@ == 1
const npy_int k = 235;
Expand Down Expand Up @@ -291,7 +295,7 @@ static @ctype@ _npy_scaled_cexp@c@(@type@ x, @type@ y, int expt)
@type@ ax = npy_fabs@c@(npy_creal@c@(z));
@type@ ay = npy_fabs@c@(npy_cimag@c@(z));
@type@ rr, ri;

if (ax > @TMAX@/4 || ay > @TMAX@/4) {
rr = npy_log@c@(npy_hypot@c@(ax/2, ay/2)) + NPY_LOGE2@c@;
}
Expand Down Expand Up @@ -470,10 +474,8 @@ static @ctype@ _npy_scaled_cexp@c@(@type@ x, @type@ y, int expt)
#ifndef HAVE_CCOS@C@
@ctype@ npy_ccos@c@(@ctype@ z)
{
@type@ x, y;
x = npy_creal@c@(z);
y = npy_cimag@c@(z);
return npy_cpack@c@(npy_cos@c@(x) * npy_cosh@c@(y), -(npy_sin@c@(x) * npy_sinh@c@(y)));
/* ccos(z) = ccosh(I * z) */
return npy_ccosh@c@(npy_cpack@c@(-npy_cimag@c@(z), npy_creal@c@(z)));
}
#endif

Expand All @@ -493,17 +495,135 @@ static @ctype@ _npy_scaled_cexp@c@(@type@ x, @type@ y, int expt)
/* 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_CCOSH@C@
/*
* Taken from the msun library in FreeBSD, rev 226599.
*
* Hyperbolic cosine of a complex argument z = x + i y.
*
* cosh(z) = cosh(x+iy)
* = cosh(x) cos(y) + i sinh(x) sin(y).
*
* Exceptional values are noted in the comments within the source code.
* These values and the return value were taken from n1124.pdf.
*
* CCOSH_BIG is chosen such that
* spacing(0.5 * exp(CCOSH_BIG)) > 0.5*exp(-CCOSH_BIG)
* although the exact value assigned to CCOSH_BIG is not so important
*/
#if @precision@ == 1
#define CCOSH_BIG 9.0f
#define CCOSH_HUGE 1.70141183e+38f
#endif
#if @precision@ == 2
#define CCOSH_BIG 22.0
#define CCOSH_HUGE 8.9884656743115795e+307
#endif
#if @precision@ >= 3
#define CCOSH_BIG 24.0L
#define CCOSH_HUGE 5.94865747678615882543e+4931L
#endif

@ctype@ npy_ccosh@c@(@ctype@ z)
{
@type@ x, y;
@type@ x, y, h, absx;
npy_int xfinite, yfinite;

x = npy_creal@c@(z);
y = npy_cimag@c@(z);
return npy_cpack@c@(npy_cos@c@(y)*npy_cosh@c@(x), npy_sin@c@(y)*npy_sinh@c@(x));

xfinite = npy_isfinite(x);
yfinite = npy_isfinite(y);

/* Handle the nearly-non-exceptional cases where x and y are finite. */
if (xfinite && yfinite) {
if (y == 0)
return npy_cpack@c@(npy_cosh@c@(x), x * y);
absx = npy_fabs@c@(x);
if (absx < CCOSH_BIG) /* small x: normal case */
return npy_cpack@c@(npy_cosh@c@(x) * npy_cos@c@(y), npy_sinh@c@(x) * npy_sin@c@(y));

/* |x| >= 22, so cosh(x) ~= exp(|x|) */
if (absx < SCALED_CEXP_LOWER@C@) {
/* x < 710: exp(|x|) won't overflow */
h = npy_exp@c@(absx) * 0.5;
return npy_cpack@c@(h * npy_cos@c@(y), npy_copysign@c@(h, x) * npy_sin@c@(y));
} else if (absx < SCALED_CEXP_UPPER@C@) {
/* x < 1455: scale to avoid overflow */
z = _npy_scaled_cexp@c@(absx, y, -1);
return npy_cpack@c@(npy_creal@c@(z), npy_cimag@c@(z) * npy_copysign@c@(1, x));
} else {
/* x >= 1455: the result always overflows */
h = CCOSH_HUGE * x;
return npy_cpack@c@(h * h * npy_cos@c@(y), h * npy_sin@c@(y));
}
}

/*
* cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
* The sign of 0 in the result is unspecified. Choice = normally
* the same as dNaN. Raise the invalid floating-point exception.
*
* cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
* The sign of 0 in the result is unspecified. Choice = normally
* the same as d(NaN).
*/
if (x == 0 && !yfinite)
return npy_cpack@c@(y - y, npy_copysign@c@(0, x * (y - y)));

/*
* cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
*
* cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0.
* The sign of 0 in the result is unspecified.
*/
if (y == 0 && !xfinite)
return npy_cpack@c@(x * x, npy_copysign@c@(0, x) * y);

/*
* cosh(x +- I Inf) = dNaN + I dNaN.
* Raise the invalid floating-point exception for finite nonzero x.
*
* cosh(x + I NaN) = d(NaN) + I d(NaN).
* Optionally raises the invalid floating-point exception for finite
* nonzero x. Choice = don't raise (except for signaling NaNs).
*/
if (xfinite && !yfinite)
return npy_cpack@c@(y - y, x * (y - y));

/*
* cosh(+-Inf + I NaN) = +Inf + I d(NaN).
*
* cosh(+-Inf +- I Inf) = +Inf + I dNaN.
* The sign of Inf in the result is unspecified. Choice = always +.
* Raise the invalid floating-point exception.
*
* cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
*/
if (npy_isinf(x)) {
if (!yfinite)
return npy_cpack@c@(x * x, x * (y - y));
return npy_cpack@c@((x * x) * npy_cos@c@(y), x * npy_sin@c@(y));
}

/*
* cosh(NaN + I NaN) = d(NaN) + I d(NaN).
*
* cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
* Optionally raises the invalid floating-point exception.
* Choice = raise.
*
* cosh(NaN + I y) = d(NaN) + I d(NaN).
* Optionally raises the invalid floating-point exception for finite
* nonzero y. Choice = don't raise (except for signaling NaNs).
*/
return npy_cpack@c@((x * x) * (y - y), (x + x) * (y - y));
}
#undef CCOSH_BIG
#undef CCOSH_HUGE
#endif

#ifndef HAVE_CSINH@C@
Expand Down