[Numpy-svn] r5996 - in trunk/numpy: core core/code_generators core/src lib

numpy-svn@scip... numpy-svn@scip...
Mon Nov 10 18:00:14 CST 2008


Author: charris
Date: 2008-11-10 18:00:05 -0600 (Mon, 10 Nov 2008)
New Revision: 5996

Modified:
   trunk/numpy/core/code_generators/generate_umath.py
   trunk/numpy/core/setup.py
   trunk/numpy/core/src/math_c99.inc.src
   trunk/numpy/lib/scimath.py
Log:
Add log2 and exp2.
Fix scimath to use log2.
Complex versions of these functions need to be added.
MPL also defines log2 with slightly different properties.
For instance, it returns an integer value for log2(2).

Modified: trunk/numpy/core/code_generators/generate_umath.py
===================================================================
--- trunk/numpy/core/code_generators/generate_umath.py	2008-11-10 04:49:05 UTC (rev 5995)
+++ trunk/numpy/core/code_generators/generate_umath.py	2008-11-11 00:00:05 UTC (rev 5996)
@@ -467,6 +467,12 @@
           TD(inexact, f='exp'),
           TD(M, f='exp'),
           ),
+'exp2' :
+    Ufunc(1, 1, None,
+          '',
+          TD(flts, f='exp2'),
+          TD(M, f='exp2'),
+          ),
 'expm1' :
     Ufunc(1, 1, None,
           docstrings.get('numpy.core.umath.expm1'),
@@ -479,6 +485,12 @@
           TD(inexact, f='log'),
           TD(M, f='log'),
           ),
+'log2' :
+    Ufunc(1, 1, None,
+          '',
+          TD(flts, f='log2'),
+          TD(M, f='log2'),
+          ),
 'log10' :
     Ufunc(1, 1, None,
           docstrings.get('numpy.core.umath.log10'),

Modified: trunk/numpy/core/setup.py
===================================================================
--- trunk/numpy/core/setup.py	2008-11-10 04:49:05 UTC (rev 5995)
+++ trunk/numpy/core/setup.py	2008-11-11 00:00:05 UTC (rev 5996)
@@ -75,11 +75,11 @@
                 " available (the list is %s)." % str(mandatory_funcs))
 
     # Standard functions which may not be available and for which we have a
-    # replacement implementation
+    # replacement implementation. Note that some of these are C99 functions.
     # XXX: we do not test for hypot because python checks for it (HAVE_HYPOT in
     # python.h... I wish they would clean their public headers someday)
     optional_stdfuncs = ["expm1", "log1p", "acosh", "asinh", "atanh",
-                         "rint", "trunc"]
+                         "rint", "trunc", "exp2", "log2"]
 
     check_funcs(optional_stdfuncs)
 
@@ -87,7 +87,8 @@
     c99_funcs = ["sin", "cos", "tan", "sinh", "cosh", "tanh", "fabs", "floor",
                  "ceil", "rint", "trunc", "sqrt", "log10", "log", "exp",
                  "expm1", "asin", "acos", "atan", "asinh", "acosh", "atanh",
-                 "hypot", "atan2", "pow", "fmod", "modf", 'frexp', 'ldexp']
+                 "hypot", "atan2", "pow", "fmod", "modf", 'frexp', 'ldexp',
+                 "exp2", "log2"]
 
     for prec in ['l', 'f']:
         fns = [f + prec for f in c99_funcs]

Modified: trunk/numpy/core/src/math_c99.inc.src
===================================================================
--- trunk/numpy/core/src/math_c99.inc.src	2008-11-10 04:49:05 UTC (rev 5995)
+++ trunk/numpy/core/src/math_c99.inc.src	2008-11-11 00:00:05 UTC (rev 5996)
@@ -120,16 +120,28 @@
 #ifndef HAVE_TRUNC
 double trunc(double x)
 {
-    if (x < 0) {
-        return ceil(x);
-    }
-    else {
-        return floor(x);
-    }
+    return x < 0 ? ceil(x) : floor(x);
+}
+#endif
 
+#ifndef HAVE_EXP2
+#define LOG2 0.69314718055994530943
+double trunc(double x)
+{
+    return exp(LOG2*x)
 }
+#undef LOG2
 #endif
 
+#ifndef HAVE_LOG2
+#define INVLOG2 1.4426950408889634074
+double trunc(double x)
+{
+    return INVLOG2*log(x)
+}
+#undef INVLOG2
+#endif
+
 /*
  *****************************************************************************
  **                     IEEE 754 FPU HANDLING                               **
@@ -196,9 +208,9 @@
 
 /**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#
+ *         log,exp,expm1,asin,acos,atan,asinh,acosh,atanh,log1p,exp2,log2#
  * #KIND = SIN,COS,TAN,SINH,COSH,TANH,FABS,FLOOR,CEIL,RINT,TRUNC,SQRT,LOG10,
- *         LOG,EXP,EXPM1,ASIN,ACOS,ATAN,ASINH,ACOSH,ATANH,LOG1P#
+ *         LOG,EXP,EXPM1,ASIN,ACOS,ATAN,ASINH,ACOSH,ATANH,LOG1P,EXP2,LOG2#
  */
 #ifndef HAVE_@KIND@@C@
 #ifdef @kind@@c@

Modified: trunk/numpy/lib/scimath.py
===================================================================
--- trunk/numpy/lib/scimath.py	2008-11-10 04:49:05 UTC (rev 5995)
+++ trunk/numpy/lib/scimath.py	2008-11-11 00:00:05 UTC (rev 5996)
@@ -328,7 +328,7 @@
     array([ 2.+4.5324j,  3.+4.5324j,  3.+0.j    ])
     """
     x = _fix_real_lt_zero(x)
-    return nx.log(x)/_ln2
+    return nx.log2(x)
 
 def power(x, p):
     """Return x**p.



More information about the Numpy-svn mailing list