Book Image

Learning SciPy for Numerical and Scientific Computing

By : Francisco J. Blanco-Silva
Book Image

Learning SciPy for Numerical and Scientific Computing

By: Francisco J. Blanco-Silva

Overview of this book

<p>It's essential to incorporate workflow data and code from various sources in order to create fast and effective algorithms to solve complex problems in science and engineering. Data is coming at us faster, dirtier, and at an ever increasing rate. There is no need to employ difficult-to-maintain code, or expensive mathematical engines to solve your numerical computations anymore. SciPy guarantees fast, accurate, and easy-to-code solutions to your numerical and scientific computing applications.<br /><br />"Learning SciPy for Numerical and Scientific Computing" unveils secrets to some of the most critical mathematical and scientific computing problems and will play an instrumental role in supporting your research. The book will teach you how to quickly and efficiently use different modules and routines from the SciPy library to cover the vast scope of numerical mathematics with its simplistic practical approach that's easy to follow.<br /><br />The book starts with a brief description of the SciPy libraries, showing practical demonstrations for acquiring and installing them on your system. This is followed by the second chapter which is a fun and fast-paced primer to array creation, manipulation, and problem-solving based on these techniques.<br /><br />The rest of the chapters describe the use of all different modules and routines from the SciPy libraries, through the scope of different branches of numerical mathematics. Each big field is represented: numerical analysis, linear algebra, statistics, signal processing, and computational geometry. And for each of these fields all possibilities are illustrated with clear syntax, and plenty of examples. The book then presents combinations of all these techniques to the solution of research problems in real-life scenarios for different sciences or engineering — from image compression, biological classification of species, control theory, design of wings, to structural analysis of oxides.</p>
Table of Contents (15 chapters)

Scientific visualization


At this point we would like to introduce you to another resource, which we will be using to generate graphs for the examples – the matplotlib libraries. It may be downloaded from its official web page, matplotlib.org, and installed following the usual Python motions. There is a good online documentation in the official web page, and we encourage the reader to dig deeper than the few commands that we will use in this book. For instance, the excellent monograph Matplotlib for Python Developers, Sandro Tosi, Packt Publishing, provides all we shall need and more. Other plotting libraries are available (commercial or otherwise), which aim to very different and specific applications. The degree of sophistication and ease of use of matplotlib makes it one of the best options for generation of graphics in scientific computing.

Once installed, it may be imported as usual, with import matplotlib. Among all its modules, we will focus on pyplot, which provides a comfortable interface with the plotting libraries. For example, if we desire to plot at this point a cycle of the sine function, we could execute the following code snippet:

import numpy
import matplotlib.pyplot
x=numpy.linspace(0,numpy.pi,32)
fig=matplotlib.pyplot.figure()
fig.plot(x, numpy.sin(x))
fig.savefig('sine.png')

We obtain the following plot:

Let us explain each command from the previous session. The first two commands are used to import numpy and matplotlib.pyplot as usual. We define an array x of 32 uniformly spaced floating point values from 0 to π, and define y to be the array containing the sine of the values from x. The command figure creates space in memory to store the subsequent plots, and puts in place an object of the form matplotlib.figure.Figure. The command plot(x, numpy.sin(x)) creates an object of the form matplotlib.lines.Line2D, containing data with the plot of x against numpy.sin(x), together with a set of axes attached to it, labeled according to the ranges of the variables. This object is stored in the previous Figure object. The last command in the session, savefig, saves the Figure object to whatever valid image format we desire (in this case, a Portable Network Graphics [PNG] image). If instead of saving to a file we desire to show on screen the result of the plot, we issue the fig.show() command. From now on, in any code that deals with matplotlib commands, we will leave the option of showing/saving open.

There are, of course, commands that control the style of axes, aspect ratio between axes, labeling, colors, the possibility of managing several figures at the same time (subplots), and many more options to display all sort of data. We will be discovering these as we progress with examples through the book.