Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
NumPy Essentials

You're reading from  NumPy Essentials

Product type Book
Published in Apr 2016
Publisher
ISBN-13 9781784393670
Pages 156 pages
Edition 1st Edition
Languages
Authors (3):
Leo (Liang-Huan) Chin Leo (Liang-Huan) Chin
Profile icon Leo (Liang-Huan) Chin
Tanmay Dutta Tanmay Dutta
Profile icon Tanmay Dutta
Shane Holloway Shane Holloway
Profile icon Shane Holloway
View More author details

Table of Contents (16) Chapters

NumPy Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. An Introduction to NumPy 2. The NumPy ndarray Object 3. Using NumPy Arrays 4. NumPy Core and Libs Submodules 5. Linear Algebra in NumPy 6. Fourier Analysis in NumPy 7. Building and Distributing NumPy Code 8. Speeding Up NumPy with Cython 9. Introduction to the NumPy C-API 10. Further Reading

Creating an array squared function using Python C-API


The Python function passes a reference to itself as the first argument, followed by real arguments given to the function. The PyArg_ParseTuple function is used to parse values from the Python function to local variables in the C function. In this function, we cast a value to a double, and hence we use d as the second argument. You can see a full list of strings that are accepted by this function at  https://docs.python.org/2/c-api/arg.html .

The final result of the computations is returned using Py_Buildvalue, which takes a similar type of format string to create a Python value from your answer. We use f here, which stands for float, to demonstrate that double and float are treated similarly:

/* 
Implementation of the actual C funtions 
*/ 
 
static PyObject* square_func(PyObject* self, PyObject* args) 
{ 
double value; 
double answer; 
 
/*  parse the input, from python float to c double */...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}