## Python for scientific applications – the very basics

Lately, I’ve been studying a little about Python and the more I learn the more I become interested about this language. It was the first time I had contact with a language that uses dynamic typing and since the first time I used it, I had the impression it could be a good replace for Matlab. I like Matlab, but the fact of being proprietary and not being a general-purpose language makes Python a better solution for me.

For scientific applications, Python counts on three fundamental libraries:

- NumPy, which is the fundamental library for scientific applications in Python. It offers special types for matrices and vectors representation and also other tools for linear algebra and random numbers;
- SciPy, which depends on NumPy provides tools for working on many areas such as numerical integration, optimization and signal processing. Scipy libraries can be thought as replacements for Matlab toolboxes;
- Matplotlib, which permits to plot graphs for visualizing data.

Some NumPy functions have similar syntax of its Matlab counterpart, which makes things easier for those who use Matlab and want to migrate to Python. For example, to create a vector with 1000 elements ranging from 0 to 1, linearly spaced, you should do:

1 2 |
import numpy as np x = np.linspace(0, 1, 1000) |

In this example, `x`

will be an object with type `array`

representing the vector with 1000 elements. Although the similarities with Matlab, there are also some differences. To create a vector with the elements `[1, 2, 3]`

, for example, you should do:

1 2 |
import numpy as np x = np.array([1, 2, 3]) |

To reference the vector items, brackets must be used and the initial index is zero, i. e., it should be used `x[0]`

to reference the first item of the vector `x`

.

In this example, besides the syntax, there is another significant difference from Matlab: unlike Matlab, which represents every matrix or vector with at least two dimensions, Python allows to create one-dimension vectors. Thus, on the previous example, a call like `x[0,0]`

would return an exception opposing to the Matlab behavior, which allows the calls `x(1)`

or `x(1,1)`

for the first element of the vector `[1, 2, 3]`

. However, Python also permits to create vectors with two dimensions. The code to create a vector similar to the one from the previous example, with two dimensions is:

1 2 |
import numpy as np x = np.array([[1, 2, 3]]) |

It is worth to notice that two brackets must be used: the external one related to the matrix (in this case with 1 line and 3 columns) and the internal one related to the first line of the matrix. In this case, `x[0]`

references the first line of the matrix, i.e, the one-dimension vector `[1, 2, 3]`

. To reference the first item of this vector, due to the two-dimension representation, we must use `x[0,0]`

Another significant difference is the matrix multiplication syntax. In Python, to multiply matrices created as *arrays*, we use the function `dot`

. For example:

1 2 3 4 5 6 7 8 9 |
import numpy as np # 2x2 Matrix A = [1 2 # 3 4] A = np.array([[1, 2], [3, 4]) # 2x2 Matrix B = [5 6 # 7 8] B = np.array([[5, 6], [7, 8]) # C = A*B C = dot(A,B) |

Besides that, to perform a element-wise multiplication such as done with the `.*`

operator in Matlab, we must use a single asterisk in Python.

There are also several other differences to Matlab. On the page at http://www.scipy.org/NumPy_for_Matlab_Users, the most important are described. Furthermore, this document is also an excellent reference to learn the basic differences between Matlab and Python.

Regarding matplotlib, I haven’t done much yet but it seems that the syntax is very similar to the one used in Matlab. To plot a period of a sine wave, for example, we must run:

1 2 3 4 5 6 7 |
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 1000) y = np.sin(x) plt.plot(x,y) plt.show() |

Last, I cannot forget to mention iPython. It is an enhanced Python shell with many improvements such as auto-completion and access to the history of commands. Besides that, on the last version there are a lot of new features, as shown on SciPy 2011.

## Comments