-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
chrisjordansquire
wants to merge
6
commits into
numpy:master
from
chrisjordansquire:ufuncdoc_addition
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7ea38b
DOCS: New ufunc creation docs
3fdc4cc
Changed to follow PEP 7
0a10f1a
ENH: Docs now Py3k compliant
2b16b97
style changes and bugs removed from docs
7f262fd
Py3K compliant, extra comments added
4adb151
npspam changed to npufunc
File filter
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
Py3K compliant, extra comments added
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,7 +139,7 @@ the module. | |
/* This initiates the module using the above definitions. */ | ||
#if PY_VERSION_HEX >= 0x03000000 | ||
static struct PyModuleDef moduledef = { | ||
PyModuleDef HEAT_INIT, | ||
PyModuleDef_HEAD_INIT, | ||
"spam", | ||
NULL, | ||
-1, | ||
|
@@ -150,7 +150,7 @@ the module. | |
NULL | ||
}; | ||
|
||
PyObject *PyInit__npspam(void) | ||
PyObject *PyInit_npspam(void) | ||
{ | ||
PyObject *m; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
m = PyModule_Create(&moduledef); | ||
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed one ;)