summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2008-11-05 19:40:28 +0000
committerCharles Harris <charlesr.harris@gmail.com>2008-11-05 19:40:28 +0000
commitfcd872c96eb64d36a36f9ae77f79e302c99e20b2 (patch)
tree1961ce3e74956d85830cbe4888c0d13bbd29fae6 /numpy
parent0727f28178bda93816673c80c5f8e39d4cfc8311 (diff)
downloadnumpy-fcd872c96eb64d36a36f9ae77f79e302c99e20b2.tar.gz
Add logsumexp ufunc and some small cleanups.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/code_generators/generate_umath.py5
-rw-r--r--numpy/core/src/math_c99.inc.src76
-rw-r--r--numpy/core/src/umathmodule.c.src56
3 files changed, 75 insertions, 62 deletions
diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py
index a7ebd93fb..579184645 100644
--- a/numpy/core/code_generators/generate_umath.py
+++ b/numpy/core/code_generators/generate_umath.py
@@ -326,6 +326,11 @@ defdict = {
"",
TD(inexact)
),
+'logsumexp' :
+ Ufunc(2, 1, None,
+ "",
+ TD(flts, f="logsumexp")
+ ),
'bitwise_and' :
Ufunc(2, 1, One,
docstrings.get('numpy.core.umath.bitwise_and'),
diff --git a/numpy/core/src/math_c99.inc.src b/numpy/core/src/math_c99.inc.src
index 14399f699..84acebfb0 100644
--- a/numpy/core/src/math_c99.inc.src
+++ b/numpy/core/src/math_c99.inc.src
@@ -33,7 +33,7 @@ double log1p(double x)
if (u == 1.0) {
return x;
} else {
- return log(u) * x / (u-1.);
+ return log(u) * x / (u - 1);
}
}
#endif
@@ -187,78 +187,56 @@ static int signbit_ld (long double x)
* instead test for the macro, but I am lazy to do that for now.
*/
-/*
- * One value argument function
- */
-
/**begin repeat
-
- #kind=(sin,cos,tan,sinh,cosh,tanh,fabs,floor,ceil,rint,trunc,sqrt,log10,log,exp,expm1,asin,acos,atan,asinh,acosh,atanh,log1p)*2#
- #KIND=(SIN,COS,TAN,SINH,COSH,TANH,FABS,FLOOR,CEIL,RINT,TRUNC,SQRT,LOG10,LOG,EXP,EXPM1,ASIN,ACOS,ATAN,ASINH,ACOSH,ATANH,LOG1P)*2#
- #typ=longdouble*23, float*23#
- #c=l*23,f*23#
- #C=L*23,F*23#
- #TYPE=LONGDOUBLE*23, FLOAT*23#
+ * #type = longdouble, float#
+ * #TYPE = LONGDOUBLE, FLOAT#
+ * #c = l,f#
+ * #C = L,F#
*/
+/**begin repeat1
+ * #kind = sin,cos,tan,sinh,cosh,tanh,fabs,floor,ceil,rint,trunc,sqrt,log10,
+ * log,exp,expm1,asin,acos,atan,asinh,acosh,atanh,log1p#
+ * #KIND = SIN,COS,TAN,SINH,COSH,TANH,FABS,FLOOR,CEIL,RINT,TRUNC,SQRT,LOG10,
+ * LOG,EXP,EXPM1,ASIN,ACOS,ATAN,ASINH,ACOSH,ATANH,LOG1P#
+ */
#ifndef HAVE_@KIND@@C@
#ifdef @kind@@c@
#undef @kind@@c@
#endif
-@typ@ @kind@@c@(@typ@ x)
+@type@ @kind@@c@(@type@ x)
{
- return (@typ@) @kind@((double)x);
+ return (@type@) @kind@((double)x);
}
#endif
-/**end repeat**/
+/**end repeat1**/
-/*
- * Two values arguments function
+/**begin repeat1
+ * #kind = atan2,hypot,pow,fmod#
+ * #KIND = ATAN2,HYPOT,POW,FMOD#
*/
-
-/**begin repeat
-
- #kind=(atan2,hypot,pow,fmod)*2#
- #KIND=(ATAN2,HYPOT,POW,FMOD)*2#
- #typ=longdouble*4, float*4#
- #c=l*4,f*4#
- #C=L*4,F*4#
- #TYPE=LONGDOUBLE*4,FLOAT*4#
-*/
#ifndef HAVE_@KIND@@C@
#ifdef @kind@@c@
#undef @kind@@c@
#endif
-@typ@ @kind@@c@(@typ@ x, @typ@ y)
+@type@ @kind@@c@(@type@ x, @type@ y)
{
- return (@typ@) @kind@((double)x, (double) y);
+ return (@type@) @kind@((double)x, (double) y);
}
#endif
-/**end repeat**/
-
-/*
- * One value - one pointer argument function
- */
+/**end repeat1**/
-/**begin repeat
- #kind=modf*2#
- #KIND=MODF*2#
- #c=l,f#
- #C=L,F#
- #typ=longdouble, float#
- #TYPE=LONGDOUBLE, FLOAT#
-*/
-#ifndef HAVE_@KIND@@C@
+#ifndef HAVE_MODF@C@
#ifdef modf@c@
#undef modf@c@
#endif
-@typ@ modf@c@(@typ@ x, @typ@ *iptr)
+@type@ modf@c@(@type@ x, @type@ *iptr)
{
- double nx, niptr, y;
- nx = (double) x;
- y = modf(nx, &niptr);
- *iptr = (@typ@) niptr;
- return (@typ@) y;
+ double niptr;
+ double y = modf((double)x, &niptr);
+ *iptr = (@type@) niptr;
+ return (@type@) y;
}
#endif
+
/**end repeat**/
diff --git a/numpy/core/src/umathmodule.c.src b/numpy/core/src/umathmodule.c.src
index 05197c879..00abe2282 100644
--- a/numpy/core/src/umathmodule.c.src
+++ b/numpy/core/src/umathmodule.c.src
@@ -23,26 +23,56 @@
#include "math_c99.inc"
-float degreesf(float x) {
- return x * (float)(180.0/M_PI);
-}
-double degrees(double x) {
- return x * (180.0/M_PI);
+/*
+ *****************************************************************************
+ ** FLOAT FUNCTIONS **
+ *****************************************************************************
+ */
+
+/**begin repeat
+ * #type = float, double, longdouble#
+ * #c = f, ,l#
+ * #C = F, ,L#
+ */
+
+#define PI 3.14159265358979323846264338328@c@
+
+static @type@
+degrees@c@(@type@ x) {
+ return x*(180.0@c@/PI);
}
-longdouble degreesl(longdouble x) {
- return x * (180.0L/M_PI);
+
+static @type@
+radians@c@(@type@ x) {
+ return x*(PI/180.0@c@);
}
-float radiansf(float x) {
- return x * (float)(M_PI/180.0);
+static @type@
+rad2deg@c@(@type@ x) {
+ return x*(180.0@c@/PI);
}
-double radians(double x) {
- return x * (M_PI/180.0);
+
+static @type@
+deg2rad@c@(@type@ x) {
+ return x*(PI/180.0@c@);
}
-longdouble radiansl(longdouble x) {
- return x * (M_PI/180.0L);
+
+static @type@
+logsumexp@c@(@type@ x, @type@ y)
+{
+ const @type@ tmp = x - y;
+ if (tmp > 0) {
+ return x + log1p@c@(exp@c@(-tmp));
+ }
+ else {
+ return y + log1p@c@(exp@c@(tmp));
+ }
}
+#undef PI
+
+/**end repeat**/
+
/*
*****************************************************************************
** PYTHON OBJECT FUNCTIONS **