#### Overview of this book

Today's world of science and technology is all about speed and flexibility. When it comes to scientific computing, NumPy is on the top of the list. NumPy will give you both speed and high productivity. "NumPy Cookbook" will teach you all about NumPy, a leading scientific computing library. NumPy replaces a lot of the functionality of Matlab and Mathematica, but in contrast to those products, it is free and open source. "Numpy Cookbook" will teach you to write readable, efficient, and fast code that is as close to the language of Mathematics as much as possible with the cutting edge open source NumPy software library. You will learn about installing and using NumPy and related concepts. At the end of the book, we will explore related scientific computing projects. This book will give you a solid foundation in NumPy arrays and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project through examples. "NumPy Cookbook" will help you to be productive with NumPy and write clean and fast code.
NumPy Cookbook
Credits
www.PacktPub.com
Preface
Free Chapter
Winding Along with IPython
Get to Grips with Commonly Used Functions
Connecting NumPy with the Rest of the World
Audio and Image Processing
Special Arrays and Universal Functions
Profiling and Debugging
Quality Assurance
Speed Up Code with Cython
Index

## Creating a universal function

We can create a universal function from a Python function with the NumPy frompyfunc function.

### How to do it...

The following steps let us create a universal function:

1. Define the Python function.

Let's define a simple Python function that just doubles the input:

def double(a):
return 2 * a
2. Create the universal function.

Create the universal function with frompyfunc. We need to specify the number of input arguments and the number of objects returned:

import numpy

def double(a):
return 2 * a

ufunc = numpy.frompyfunc(double, 1, 1)
print "Result", ufunc(numpy.arange(4))

The code prints the following output, when executed:

Result [0 2 4 6]

### How it works...

We defined a Python function, which doubles the numbers it receives. Actually, we could also have strings as input, because that is legal in Python. We created a universal function from this Python function with the NumPy frompyfunc function.