Modules

Using modules

A module refers to a file containing Python statements, function and class definitions. The name of the modul is the same as the file without the .py extension. This name is contained in the variable __name__. Any .py file can be used as a module. You can define your most used functions in a module and import it, instead of copying their definitions into your programs.

Import

If Python is installed, we have the built-in modules. It is simple to import them. We did it a few times.

sys is a built-in module, it is installed by default. This module can access system files and variables (for example command line arguments).

The sys.path is a variable containing a list of strings that specifies the search path for modules.

It is possible to organize modules into hierarchical structure called packages. using the dot notation when a submodul is imported (see https://docs.python.org/3/tutorial/modules.html).

You can import a whole modul a submodule or only one function from a module. The most frequently used commands for importing modules or a part of them:

import <modul>
import <modul> as <name>
from <modul> import <identifier>
from <modul.identifier> import <identifier>

E.g.:

Relative import

You can import files from the current directory. Write the next code into a file named myfns.py:

def foo():
    print("FoO")

def bar():
    print("BaR")

After this we may import this modul by

import myfns

or we can import one function only by

from myfns import foo

Calling the function depends on the method of importing:

numpy basics

numpy is a module for numerical calculations. It can handle vectors, matrices, arrays and perform linear algebraic calculations, random number generation...

If you have Anaconda then it is installed by default.

You can import a module with an alternative name, just to make it shorter.

import numpy as np

The main object of numpy is ndarray, short for n-dimensional array, you can create arrays with the numpy.array() function. This construct from a list a 1D array (vector), from a list of lists a 2D array, from a list of lists of lists a 3D array... The other important function is arange(), which makes an array similarly to range(). A range can be reshape by the reshape() function:

You can perform elementwise operations (+ - * / **) between numpy.ndarray type objects if they are are compatible.

You can add a number to an array which means adding the same number to all of the elements. Same for multiplication and other operations between an array and an element.

The matrix dot product is not the * operator, but the @ operation or the .dot method:

You can use the normal indexing, but there are some other comfortable possibilities:

Or you can take certain columns:

But if you want to get a column vector (as a 2D array) use [ ]:

You can use a list of indices which slices the corresponding rows (or columns).

You can call a numpy function with an array parameter which performs (mostly) elementwise operation.

Numpy can calculate mean and standard deviation.

To make an array filled with zeros or ones you can call zeros or ones (similarly to MatLab/Octave, the matrix based languages). The function eye construct identity matrix (I pronounced as eye).

You can generate random numbers or array of numbers. The next function generate them with uniform distribution from $[0,1)$:

We get the same sequences if we set the seed:

We can generate integer sequences. E.g. let us simulate 10 dice rolls:

Plot

The matplotlib module used for plotting functions. (The %matplotlib magic function activates matplotlib interactive support. Magics are functions of the IPython distribution offering support for users of Python. jupyter uses most of them.)

A simple plot first. If you don't specify the $x$ values, the range $[0, 1, 2 \ldots]$ is used instead.

These libraries cannot calculate symbolically, like Sage, just makes a series of lines between the plotted points.

Let's plot a sine curve with np.sin function. Here we generate the $x$-values with the function linespace, which divides an interval into equal parts:

Draw with calculating sin at 11 points only:

Monte–Carlo-method

Monte–Carlo simulation is an easy but not too fast way to estimate an integral.

To calculate $\int_{-2}^2e^{-x^2}\,\mathrm{d}x$ you draw random points in the rectangle $[-2,2]\times[0,1]$ and count how many points fell under the graph: $e^{-x^2} > y$. In the array J we store the indexes of these $(x,y)$ points.

The ratio of the points under the curve times the size of the rectangle is an approximation of the area.

First let us study the function np.where(). If one arguments is given only, then it gives bach the indexes where the array is True, or is not 0. For a 2-tuple input you get a 2-tuple of arrays, the first contains the first indexes, the second contains the second indexes:

For a 1-dimensional array, you get a 1-tuple of the array of indexes:

Let us see the exact value of the integral:

This does not seem like a smart answer as $\operatorname{erf}x = \frac{2}{\sqrt\pi}\int_0^x e^{-t^2}\,\mathrm dt$. But the method n() returns a floating point approximation:

In a picture: