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 – twiddling bits


We will go over three tricks—checking whether the signs of integers are different, checking whether a number is a power of 2, and calculating the modulus of a number that is a power of 2. We will show an operators only notation and one using the corresponding NumPy functions:

  1. Checking signs: The first trick depends on the XOR or ^ operator. The XOR operator is also called the inequality operator; so, if the sign bit of the two operands is different, the XOR operation will lead to a negative number. ^ corresponds to the bitwise_xor function. < corresponds to the less function.

    x = numpy.arange(-9, 9)
    y = -x
    print "Sign different?", (x ^ y) < 0
    print "Sign different?", numpy.less(numpy.bitwise_xor(x, y), 0)

    The result is shown as follows:

    Sign different? [ True  True  True  True  True  True  True  True  True False  True  True  True  True  True  True  True  True]Sign different? [ True  True  True  True  True  True  True  True  True False  True  True  True...