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

Conversation

chrisjordansquire
Copy link
Contributor

A much expanded version of the ufunc creation docs. It contains example code for
*a non-numpy C extension that numpy.vectorize can be called on
*a ufunc for a single dtype
*a ufunc for multiple dtypes
*a ufunc with multiple arguments and return values

Additionally, a few glitches in the existing ufunc documentation were fixed.

The hope is that these expanded documents will make it very easy for numpy and scipy users to make their own ufuncs regardless of their previous experience with the python-C API or the numpy-C API. They barely even need to know C in order to copy and paste and then modify the code examples given.

Also, a mistake the npy_half docs was fixed and a minor bug in numpy.testing.utils related to npy_half was fixed.

#include <Python.h>
#include <math.h>

/*
Copy link
Member

Choose a reason for hiding this comment

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

Might as well conform to the numpy c style here. That is, multi-line comments like so:

/*
 * blah
 * blah
 */

@charris
Copy link
Member

charris commented Aug 13, 2011

Looks good except for the C coding style which I think should conform more closely with the numpy conventions. Thanks for adding this, it should be very helpful to other folks down the road.

@charris
Copy link
Member

charris commented Aug 13, 2011

I should also note that I believe Mark will add support for masks at the loop level at some point, so that is worth keeping an eye on.

@mwiebe
Copy link
Member

mwiebe commented Aug 13, 2011

Chuck's review looks pretty thorough, not much to add! In the master branch, there is support for masks at the loop level already. It works transparently with the existing inner loops like in these examples, but one day it will be worth adding some loops specialized for performance like I described here:

http://projects.scipy.org/numpy/ticket/1901

You can see the exposed details here:

https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ufuncobject.h#L17
https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ufuncobject.h#L145

@chrisjordansquire
Copy link
Contributor Author

Thanks charris! I only just read PEP 7 last weekend; hadn't realized I was violated python c code conventions.

Are there any numpy C code conventions beyond those in PEP 7? I looked online at the numpy docs and googled a bit, but I couldn't find anything.

I've pushed changes I knew how to make and responded inline.

@charris
Copy link
Member

charris commented Aug 15, 2011

The C conventions aren't really written down at this point, they mostly consist of me complaining about new code. Because I reformatted a good bit of the code back when, they are sort of de facto conventions ;). I should probably write something up.

@chrisjordansquire
Copy link
Contributor Author

I think I made it Py3K compliant, based on the examples charris mentioned. But I'm not sure at all. Help/insight would be appreciated.


void *data[1] = {NULL};

#if defined(NPY_PY3K)
Copy link
Member

Choose a reason for hiding this comment

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

Might as well move this down into the next #if.

Could also use

#if PY_VERSION_HEX >= 0x03000000

Which might be a bit more explanatory in this context.

@charris
Copy link
Member

charris commented Aug 16, 2011

Getting close, it looks much better.

I'll check this with python 3 if you don't want to do that.

@chrisjordansquire
Copy link
Contributor Author

Thanks for all the checking!

I just pushed a new version. I think I got everything you mentioned.

If you could do the Python 3 check that'd expedite things; I'm traveling for the next week, and won't have the time to setup a python 3 virtual env on my computer, load everything up, and check it until I get back.

/* This initiates the module using the above definitions. */
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
PyModuleDef HEAT_INIT,
Copy link
Member

Choose a reason for hiding this comment

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

PyModuleDef_HEAD_INIT


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;

@charris
Copy link
Member

charris commented Aug 21, 2011

With the modifications, the module compiles and runs under python 3 (with a deprecation warning).
Impressions:

  • npspam is an unfortunate choice of name
  • a small test/demo program would be useful
  • you should indicate where the resulting module ends up so newbies don't lose it.

@chrisjordansquire
Copy link
Contributor Author

Danke! I changed the errors you caught, and thank you very much for checking.

I also added the comment in the setup.py file for how to build the library in place. In general, there are comments in the setup.py file for how to use setp.py and where the results will be located.

There are small demos inside each section. Hopefully I'm not being too dense, but I'm not really sure why adding something would be useful. They can just cut, paste, and build the libraries as they are to play around with it themselves. The code is specifically structured so that can be done with a minimum of fuss.

And I agree that npspam is not the best name. I can't think of anything I like a lot better, though, that makes any sense in the context of the tutorial. pretty_unicorn and walrus_guano would be awesome names, yet likely inappropriate for a logit function. Statisticians don't have that much humor. Perhaps for a package containing modified chebyschev polynomials or rayleigh-ritz method algorithms. I'm open to suggestions for an alternative name to npspam.

@charris
Copy link
Member

charris commented Aug 22, 2011

I'd just use something dull and uninspired, like npufunc.

@chrisjordansquire
Copy link
Contributor Author

Dull and uninspired works for me. For public libraries, at least.

@charris
Copy link
Member

charris commented Aug 23, 2011

Pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants