8000 DOCS: New ufunc creation docs by chrisjordansquire · Pull Request #132 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOCS: New ufunc creation docs #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed t 8000 o load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Py3K compliant, extra comments added
  • Loading branch information
Chris Jordan-Squire committed Aug 22, 2011
commit 7f262fd044edde09168e7085826a1f98e13ab402
81 changes: 55 additions & 26 deletions doc/source/user/c-info.ufunc-tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ the module.
/* This initiates the module using the above definitions. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed one ;)

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef HEAT_INIT,
PyModuleDef_HEAD_INIT,
"spam",
NULL,
-1,
Expand All @@ -150,7 +150,7 @@ the module.
NULL
};

PyObject *PyInit__npspam(void)
PyObject *PyInit_npspam(void)
{
PyObject *m;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyObject *m, *logit, *d;

m = PyModule_Create(&moduledef);
Expand Down Expand Up @@ -180,13 +180,18 @@ directory.

'''
setup.py file for spammodule.c

Calling
$python setup.py build
will build the extension library in a file
that looks like ./build/lib*, where lib* is
a file that begins with lib.
$python setup.py build_ext --inplace
will build the extension library in the current file.

Calling
$python setup.py build
will build a file that looks like ./build/lib*, where
lib* is a file that begins with lib. The library will
be in this file and end with a C library extension,
such as .so

Calling
$python setup.py install
will install the module in your site-packages file.
Expand All @@ -211,11 +216,25 @@ directory.
Once the spam module is imported into python, you can call logit
via spam.logit. Note that the function used above cannot be applied
as-is to numpy arrays. To do so we must call numpy.vectorize on it.
For example:
For example, if a python interpreter is opened in the file containing
the spam library or spam has been installed, one can perform the
following commands:

>>> import numpy as np
>>> import spam
>>> spam.logit(0)
-inf
>>> spam.logit(1)
inf
>>> spam.logit(0.5)
0.0
>>> x = np.linspace(0,1,10)
>>> spam.logit(x)
TypeError: only length-1 arrays can be converted to Python scalars
>>> f = np.vectorize(spam.logit)
>>> f(x)
array([ -inf, -2.07944154, -1.25276297, -0.69314718, -0.22314355,
0.22314355, 0.69314718, 1.25276297, 2.07944154, inf])

THE RESULTING LOGIT FUNCTION IS NOT FAST! numpy.vectorize simply
loops over spam.logit. The loop is done at the C level, but the numpy
Expand Down Expand Up @@ -305,7 +324,7 @@ the primary thing that must be changed to create your own ufunc.

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef HEAT_INIT,
PyModuleDef_HEAD_INIT,
"npspam",
NULL,
-1,
Expand All @@ -316,9 +335,9 @@ the primary thing that must be changed to create your own ufunc.
NULL
};

PyObject *PyInit__npspam(void)
PyObject *PyInit_npspam(void)
{
PyObject *m;
PyObject *m, *logit, *d;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
Expand Down Expand Up @@ -375,11 +394,16 @@ or installed to site-packages via python setup.py install.
we use numpy.distutils instead of
distutils from the python standard library.

Calling
$python setup.py build_ext --inplace
will build the extension library in the current file.

Calling
$python setup.py build
will build the extension library in a file
that looks like ./build/lib*, where lib* is
a file that begins with lib.
$python setup.py build
will build a file that looks like ./build/lib*, where
lib* is a file that begins with lib. The library will
be in this file and end with a C library extension,
such as .so

Calling
$python setup.py install
Expand Down Expand Up @@ -579,7 +603,7 @@ the primary thing that must be changed to create your own ufunc.

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef HEAT_INIT,
PyModuleDef_HEAD_INIT,
"npspam",
NULL,
-1,
Expand All @@ -590,9 +614,9 @@ the primary thing that must be changed to create your own ufunc.
NULL
};

PyObject *PyInit__npspam(void)
PyObject *PyInit_npspam(void)
{
PyObject *m;
PyObject *m, *logit, *d;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
Expand Down Expand Up @@ -648,12 +672,17 @@ or installed to site-packages via python setup.py install.
Note that since this is a numpy extension
we use numpy.distutils instead of
distutils from the python standard library.


Calling
$python setup.py build_ext --inplace
will build the extension library in the current file.

Calling
$python setup.py build
will build the extension library in a file
that looks like ./build/lib*, where lib* is
a file that begins with lib.
$python setup.py build
will build a file that looks like ./build/lib*, where
lib* is a file that begins with lib. The library will
be in this file and end with a C library extension,
such as .so

Calling
$python setup.py install
Expand Down Expand Up @@ -797,7 +826,7 @@ as well as all other properties of a ufunc.

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef HEAT_INIT,
PyModuleDef_HEAD_INIT,
"npspam",
NULL,
-1,
Expand All @@ -808,9 +837,9 @@ as well as all other properties of a ufunc.
NULL
};

PyObject *PyInit__npspam(void)
PyObject *PyInit_npspam(void)
{
PyObject *m;
PyObject *m, *logit, *d;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
Expand Down
0