_images/title_Python.png

Python is free, consistently and completely object oriented, and has a large number of (free) scientific toolboxes (e.g. http://www.scipy.org/). It is used by Google, NASA, and many others. Information can be found under http://www.python.org/. If you want to use Python for scientific applications, currently the best way to get started is the Python(x,y) distribution, the smaller WinPython, or the free Anaconda version from Contiuum Analytics. These distributions are all free, and contain the complete scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces, and the interactive scientific development environment Spyder. If you already have experience with Matlab, the article NumPy for Matlab Users provides an overview of the similarities and differences between the two languages.

More information about Python, and links to related material, can be found under http://work.thaslwanter.at/Stats/html/statsIntroduction.html#python

Examples

The following two Python modules give an example of how Python can handle matrix multiplications and graphics simply and efficiently:

Example 1: Define Rotation Matrices

"""
RotMat.py  Definition of 3D rotation matrices
Created on Oct 26, 2010

@author: Thomas Haslwanter
Ver. 1.0
"""
from numpy import *

def R1(psi):
    ## Rotation about the 1-axis

    # convert from degrees into radian:
    psi = psi * pi/180;

    R = array([[1, 0, 0],
            [0, cos(psi), -sin(psi)],
            [0, sin(psi), cos(psi)]])

    return R

def R2(phi):
    ## Rotation about the 2-axis

    # convert from degrees into radian:
    phi = phi * pi/180;

    R = array([[cos(phi), 0, sin(phi)],
            [0, 1, 0],
            [-sin(phi), 0, cos(phi)]])

    return R

def R3(theta):
    ## Rotation about the 3-axis

    # convert from degrees into radian:
    theta = theta * pi/180;

    R = array([[cos(theta), -sin(theta), 0],
            [sin(theta), cos(theta), 0],
            [0, 0, 1]])

    return R

Example 2: Select between two choices

''' Demonstration how Python can handle matrix multiplication and simple graphics

Created on Oct 26, 2010
@author: ThH
Ver 1.0
'''

import RotMat
import numpy as np

def rotateVector():
    ## Define vector & matrix and rotate vector
    myVector = np.array([0,1,0])
    psi = 20

    rotMat = RotMat.R1(psi)

    vectorRotated = np.dot(rotMat, myVector)
    print(vectorRotated)

def plotLine():
    ##import numpy as np
    import matplotlib.pyplot as plt
    x = np.arange(0,10,0.1)
    y = np.sin(x)
    plt.plot(x,y)
    plt.show()

def main():
    myInput = input('Do you want to "r"otate a vector, or "p"lot a line? ')
    if myInput == 'r':
        rotateVector()
    else:
        plotLine()

if __name__ == '__main__':
    main()

The figure below shows a screen-shot of this code in the Spyder development environment that comes with python (x,y). For interactive work, similar to MATLAB, the IPython console can facilitate a smooth transition from Matlab to Python.

image36

Screenshot of Spyder, a programming environment that is very similar to Matlab but leverages on IPython.

Table Of Contents

Previous topic

Callbacks

This Page