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
Prev Previous commit
Next Next commit
MAINT: umath: generate the ldexp and frexp ufuncs like all the others.
  • Loading branch information
ewmoore committed Feb 26, 2014
commit e6b99c946c411b3ae4e8b1dbdab2149ed341fa19
38 changes: 36 additions & 2 deletions numpy/core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
class FullTypeDescr(object):
pass

class FuncNameSuffix(object):
"""Stores the suffix to append when generating function names.
"""
def __init__(self, suffix):
self.suffix = suffix

class TypeDescription(object):
"""Type signature for a ufunc.

Attributes
----------
type : str
Character representing the nominal type.
func_data : str or None or FullTypeDescr, optional
func_data : str or None or FullTypeDescr or FuncNameSuffix, optional
The string representing the expression to insert into the data array, if
any.
in_ : str or None, optional
Expand Down Expand Up @@ -795,6 +801,30 @@ def english_upper(s):
None,
TD(flts),
),
'ldexp' :
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.ldexp'),
None,
[TypeDescription('e', None, 'ei', 'e'),
TypeDescription('f', None, 'fi', 'f'),
TypeDescription('e', FuncNameSuffix('long'), 'el', 'e'),
TypeDescription('f', FuncNameSuffix('long'), 'fl', 'f'),
TypeDescription('d', None, 'di', 'd'),
TypeDescription('d', FuncNameSuffix('long'), 'dl', 'd'),
TypeDescription('g', None, 'gi', 'g'),
TypeDescription('g', FuncNameSuffix('long'), 'gl', 'g'),
],
),
'frexp' :
Ufunc(1, 2, None,
docstrings.get('numpy.core.umath.frexp'),
None,
[TypeDescription('e', None, 'e', 'ei'),
TypeDescription('f', None, 'f', 'fi'),
TypeDescription('d', None, 'd', 'di'),
TypeDescription('g', None, 'g', 'gi'),
],
)
}

if sys.version_info[0] >= 3:
Expand Down Expand Up @@ -854,7 +884,7 @@ def make_arrays(funcdict):
thedict = chartotype1 # one input and one output

for t in uf.type_descriptions:
if t.func_data not in (None, FullTypeDescr):
if t.func_data not in (None, FullTypeDescr) and not isinstance(t.func_data, FuncNameSuffix):
funclist.append('NULL')
astype = ''
if not t.astype is None:
Expand All @@ -880,6 +910,10 @@ def make_arrays(funcdict):
tname = english_upper(chartoname[t.type])
datalist.append('(void *)NULL')
funclist.append('%s_%s_%s_%s' % (tname, t.in_, t.out, name))
elif isinstance(t.func_data, FuncNameSuffix):
datalist.append('(void *)NULL')
tname = english_upper(chartoname[t.type])
funclist.append('%s_%s_%s' % (tname, name, t.func_data.suffix))
else:
datalist.append('(void *)NULL')
tname = english_upper(chartoname[t.type])
Expand Down
62 changes: 62 additions & 0 deletions numpy/core/code_generators/ufunc_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3312,3 +3312,65 @@ def add_newdoc(place, name, doc):
array([0, 0, 0, 0, 1])

""")

add_newdoc('numpy.core.umath', 'ldexp',
"""
Scale floating point value by a power of 2, element-wise.

Equivalent to ``x1 * 2**x2``.

Parameters
----------
x1 : array_like
Input array to be scaled.
x2 : array_like
Input array of exponents.

Returns
-------
y: ndarray
The corresponding scaled values.

See Also
--------
frexp : Extract significand and exponent from floating point number

Examples
--------
>>> np.ldexp(0.5, np.arange(-1,4))
array([ 0.25, 0.5 , 1. , 2. , 4. ])

""")

add_newdoc('numpy.core.umath', 'frexp',
"""
Extract significand and exponent from a floating point number.

All floating point numbers are of the form: ``y1 * 2**y2`` where `y1`,
the significand, is a number between 0.5 and 1, or 0 and `y2`, the
exponent, is an integer.

Parameters
----------
x: array_like
Input array.

Returns
-------
y1 : ndarray
The normalized significands from `x`.
y2 : ndarray
The exponents from `x`.

See Also
--------
ldexp : Scale floating point number by power of 2

Examples
--------
>>> np.frexp([0.25, 0.5, 1, 2, 4])
(array([ 0.5, 0.5, 0.5, 0.5, 0.5]),
array([-1, 0, 1, 2, 3], dtype=int32))

""")

100 changes: 5 additions & 95 deletions numpy/core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,99 +201,6 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
** SETUP UFUNCS **
*****************************************************************************
*/

/* Less automated additions to the ufuncs */

static PyUFuncGenericFunction frexp_functions[] = {
#ifdef HAVE_FREXPF
HALF_frexp,
FLOAT_frexp,
#endif
DOUBLE_frexp
#ifdef HAVE_FREXPL
,LONGDOUBLE_frexp
#endif
};

static void * blank3_data[] = { (void *)NULL, (void *)NULL, (void *)NULL};
static void * blank6_data[] = { (void *)NULL, (void *)NULL, (void *)NULL,
(void *)NULL, (void *)NULL, (void *)NULL};
static char frexp_signatures[] = {
#ifdef HAVE_FREXPF
NPY_HALF, NPY_HALF, NPY_INT,
NPY_FLOAT, NPY_FLOAT, NPY_INT,
#endif
NPY_DOUBLE, NPY_DOUBLE, NPY_INT
#ifdef HAVE_FREXPL
,NPY_LONGDOUBLE, NPY_LONGDOUBLE, NPY_INT
#endif
};

#if NPY_SIZEOF_LONG == NPY_SIZEOF_INT
#define LDEXP_LONG(typ) typ##_ldexp
#else
#define LDEXP_LONG(typ) typ##_ldexp_long
#endif

static PyUFuncGenericFunction ldexp_functions[] = {
#ifdef HAVE_LDEXPF
HALF_ldexp,
FLOAT_ldexp,
LDEXP_LONG(HALF),
LDEXP_LONG(FLOAT),
#endif
DOUBLE_ldexp,
LDEXP_LONG(DOUBLE)
#ifdef HAVE_LDEXPL
,
LONGDOUBLE_ldexp,
LDEXP_LONG(LONGDOUBLE)
#endif
};

static char ldexp_signatures[] = {
#ifdef HAVE_LDEXPF
NPY_HALF, NPY_INT, NPY_HALF,
NPY_FLOAT, NPY_INT, NPY_FLOAT,
NPY_HALF, NPY_LONG, NPY_HALF,
NPY_FLOAT, NPY_LONG, NPY_FLOAT,
#endif
NPY_DOUBLE, NPY_INT, NPY_DOUBLE,
NPY_DOUBLE, NPY_LONG, NPY_DOUBLE
#ifdef HAVE_LDEXPL
,NPY_LONGDOUBLE, NPY_INT, NPY_LONGDOUBLE
,NPY_LONGDOUBLE, NPY_LONG, NPY_LONGDOUBLE
#endif
};

static void
InitOtherOperators(PyObject *dictionary) {
PyObject *f;
int num;

num = sizeof(frexp_functions) / sizeof(frexp_functions[0]);
f = PyUFunc_FromFuncAndData(frexp_functions, blank3_data,
frexp_signatures, num,
1, 2, PyUFunc_None, "frexp",
"Split the number, x, into a normalized"\
" fraction (y1) and exponent (y2)",0);
PyDict_SetItemString(dictionary, "frexp", f);
Py_DECREF(f);

num = sizeof(ldexp_functions) / sizeof(ldexp_functions[0]);
f = PyUFunc_FromFuncAndData(ldexp_functions, blank6_data, ldexp_signatures, num,
2, 1, PyUFunc_None, "ldexp",
"Compute y = x1 * 2**x2.",0);
PyDict_SetItemString(dictionary, "ldexp", f);
Py_DECREF(f);

#if defined(NPY_PY3K)
f = PyDict_GetItemString(dictionary, "true_divide");
PyDict_SetItemString(dictionary, "divide", f);
#endif
return;
}

NPY_VISIBILITY_HIDDEN PyObject * npy_um_str_out = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_um_str_subok = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_um_str_array_prepare = NULL;
Expand Down Expand Up @@ -409,15 +316,18 @@ PyMODINIT_FUNC initumath(void)
/* Load the ufunc operators into the array module's namespace */
InitOperators(d);

InitOtherOperators(d);

PyDict_SetItemString(d, "pi", s = PyFloat_FromDouble(NPY_PI));
Py_DECREF(s);
PyDict_SetItemString(d, "e", s = PyFloat_FromDouble(NPY_E));
Py_DECREF(s);
PyDict_SetItemString(d, "euler_gamma", s = PyFloat_FromDouble(NPY_EULER));
Py_DECREF(s);

#if defined(NPY_PY3K)
s = PyDict_GetItemString(d, "true_divide");
PyDict_SetItemString(d, "divide", s);
#endif

#define ADDCONST(str) PyModule_AddIntConstant(m, #str, UFUNC_##str)
#define ADDSCONST(str) PyModule_AddStringConstant(m, "UFUNC_" #str, UFUNC_##str)

Expand Down