Permalink
Browse files

ENH: special: Move logit and lambertw inside the same module

  • Loading branch information...
1 parent 941351d commit a3c190c9593b16434d87e43e99d2c838891d70ed @pv committed Oct 6, 2012
@@ -9,8 +9,7 @@
#include <math.h>
#include "numpy/npy_math.h"
-#include "numpy/ndarraytypes.h"
-#include "numpy/ufuncobject.h"
+#include "_logit.h"
/*
* Inner loops for logit and expit
@@ -21,146 +20,21 @@
* #c = f,,l#
*/
-static void
-logit_loop@c@(char **args, npy_intp *dimensions,
- npy_intp* steps, void* data)
+@type@ logit@c@(@type@ x)
{
- npy_intp i;
- npy_intp n = dimensions[0];
- char *in = args[0], *out = args[1];
- npy_intp in_step = steps[0], out_step = steps[1];
-
- @type@ tmp;
-
- for (i = 0; i < n; i++) {
- tmp = *(@type@ *)in;
- tmp /= 1 - tmp;
- *((@type@ *)out) = npy_log@c@(tmp);
-
- in += in_step;
- out += out_step;
- }
+ x /= 1 - x;
+ return npy_log@c@(x);
}
-static void
-expit_loop@c@(char **args, npy_intp *dimensions,
- npy_intp* steps, void* data)
+@type@ expit@c@(@type@ x)
{
- npy_intp i;
- npy_intp n = dimensions[0];
- char *in = args[0], *out = args[1];
- npy_intp in_step = steps[0], out_step = steps[1];
-
- @type@ tmp;
-
- for (i = 0; i < n; i++) {
- tmp = *(@type@ *)in;
- if (tmp > 0) {
- tmp = npy_exp@c@(tmp);
- *((@type@ *)out) = tmp / (1 + tmp);
- }
- else{
- *((@type@ *)out) = 1 / (1 + npy_exp@c@(-tmp));
- }
- in += in_step;
- out += out_step;
+ if (x > 0) {
+ x = npy_exp@c@(x);
+ return x / (1 + x);
}
-}
-
-/**end repeat**/
-
-/*
- * Definitions for the ufuncs.
- */
-
-static PyUFuncGenericFunction expit_funcs[3] = {&expit_loopf,
- &expit_loop,
- &expit_loopl};
-
-static PyUFuncGenericFunction logit_funcs[3] = {&logit_loopf,
- &logit_loop,
- &logit_loopl};
-
-static char types[6] = {NPY_FLOAT, NPY_FLOAT,
- NPY_DOUBLE, NPY_DOUBLE,
- NPY_LONGDOUBLE, NPY_LONGDOUBLE};
-
-static void *data[3] = {NULL, NULL, NULL};
-
-/* Module definition */
-
-static PyMethodDef module_methods[] = {
- { NULL, NULL, 0, NULL }
-};
-
-#if PY_VERSION_HEX >= 0x03000000
-
-static PyModuleDef moduledef = {
- PyModuleDef_HEAD_INIT,
- "_logit",
- NULL,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
-};
-
-PyMODINIT_FUNC
-PyInit__logit()
-{
- PyObject *m, *f, *d;
- m = PyModule_Create(&moduledef);
- if (!m) {
- return NULL;
+ else {
+ return 1 / (1 + npy_exp@c@(-x));
}
-
- import_array();
- import_umath();
-
- d = PyModule_GetDict(m);
-
- f = PyUFunc_FromFuncAndData(logit_funcs,data, types, 3, 1, 1,
- PyUFunc_None, "logit",NULL , 0);
- PyDict_SetItemString(d, "logit", f);
- Py_DECREF(f);
-
- f = PyUFunc_FromFuncAndData(expit_funcs,data, types, 3, 1, 1,
- PyUFunc_None, "expit",NULL , 0);
- PyDict_SetItemString(d, "expit", f);
- Py_DECREF(f);
-
- return m;
}
-#else
-
-PyMODINIT_FUNC
-init_logit()
-{
- PyObject *m, *f, *d;
-
- m = Py_InitModule("_logit", module_methods);
- if (m == NULL) {
- return;
- }
-
- d = PyModule_GetDict(m);
-
- import_array();
- import_umath();
-
- f = PyUFunc_FromFuncAndData(logit_funcs,data, types, 3, 1, 1,
- PyUFunc_None, "logit",NULL , 0);
- PyDict_SetItemString(d , "logit", f);
- Py_DECREF(f);
-
-
- f = PyUFunc_FromFuncAndData(expit_funcs,data, types, 3, 1, 1,
- PyUFunc_None, "expit",NULL , 0);
- PyDict_SetItemString(d , "expit", f);
- Py_DECREF(f);
-}
-
-#endif
+/**end repeat**/
@@ -0,0 +1,14 @@
+#ifndef _LOGIT_H_
+#define _LOGIT_H_
+
+#include "numpy/npy_math.h"
+
+npy_float logitf(npy_float x);
+npy_double logit(npy_double x);
+npy_longdouble logitl(npy_longdouble x);
+
+npy_float expitf(npy_float x);
+npy_double expit(npy_double x);
+npy_longdouble expitl(npy_longdouble x);
+
+#endif
Oops, something went wrong. Retry.

0 comments on commit a3c190c

Please sign in to comment.