Book Image

NumPy 1.5 Beginner's Guide

By : Ivan Idris
Book Image

NumPy 1.5 Beginner's Guide

By: Ivan Idris

Overview of this book

<p>In today's world of science and technology, the hype is all about speed and flexibility. When it comes to scientific computing, NumPy is on the top of the list. NumPy is the fundamental package needed for scientific computing with Python. NumPy will give you both speed and high productivity. Save thousands of dollars on expensive software, while keeping all the flexibility and power of your favourite programming language.<br /><br /><i>NumPy 1.5 Beginner's Guide</i> will teach you about NumPy from scratch. It includes everything from installation, functions, matrices, and modules to testing, all explained with appropriate examples. <br /><br /><i>Numpy 1.5 Beginner's Guide</i> will teach you about installing and using NumPy and related concepts.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. At the end of the book, we will explore related scientific computing projects such as Matplotlib for plotting and the SciPy project through examples.</p> <p><i>NumPy 1.5 Beginner's Guide</i> will help you be productive with NumPy and write clean and fast code.</p>
Table of Contents (18 chapters)
NumPy 1.5
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – sorting complex numbers


We will create an array of complex numbers and sort it:

  1. Generating random complex numbers : Generate five random numbers for the real part of the complex numbers and five numbers for the imaginary part. Seed the random generator to 42:

    numpy.random.seed(42)
    complex_numbers = numpy.random.random(5) + 1j * numpy.random.random(5)
    print "Complex numbers\n", complex_numbers
  2. Calling sort_complex on the random numbers: Call the sort_complex function to sort the complex numbers we generated in the previous step:

    print "Sorted\n", numpy.sort_complex(complex_numbers)

    The sorted numbers would be:

    Sorted[ 0.39342751+0.34955771j  0.40597665+0.77477433j  0.41516850+0.26221878j  0.86631422+0.74612422j  0.92293095+0.81335691j]
    

What just happened?

We generated random complex numbers and sorted them using the sort_complex function.

Pop quiz – generating random numbers

Which NumPy module deals with random numbers?

  • Randnum

  • random

  • randomutil

  • rand