Skip main navigation

Using NumPy with Cython

NumPy arrays are the work horses of numerical computing with Python, and Cython allows one to work more efficiently with them. As discussed in week 2, when working with NumPy …

Avoiding function call overheads

Function calls in Python are relatively expensive in comparison to for example C. Cython allows one to reduce this overhead significantly. Main reason for the large function call overhead in …

Where to add types?

In complex programs it is not always straight forward to see whether you have introduced static typing in the all the relevant parts of your Cython module. Cython provides an …

Using static typing

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a …

Hands-on: creating a simple Cython extension

Simple Cython extension The code for this exercise is located under cython/simple-extension ~/hpc-python$ cd cython/simple-extension Create a simple Cython module (you can name it e.g. cyt_module.pyx)containing the following function: def …

What is Cython?

Cython is an optimising static compiler for Python that also provides its own programming language as a superset for standard Python. Cython is designed to provide C-like performance for a …

Creating Cython modules

Normally, when working with Cython one does not Cythonize the whole program but only selected modules. Let’s have a look at a very simple Python module that we could store …

Week 2 summary

We hope that you have enjoyed the second week of Python in High Performance Computing! This week, we have looked into NumPy arrays and how to use them for more …

Speeding up complex expressions with Numexpr

Complex expressions with large NumPy arrays present a bit of a catch-22 situation performance-wise. On the one hand, using a one-liner for the expression is not a good idea due …

Hands-on: Temporary arrays

In this exercise we study how NumPy uses computer memory Source code for this exercise is located in numpy/temporary-arrays/ Try different NumPy array expressions and investigate how much memory is …

Temporary arrays

In complex expressions, NumPy stores intermediate values in temporary arrays. This means that the memory consumption can be higher than expected. Consider e.g. the following example: import numpya = numpy.random.random((1024, …

Anatomy of NumPy arrays

The datatype of a NumPy array is called ndarray. If one looks into what an ndarray is actually made of, one can see that it consists of the following: one …

Hands-on: Linear algebra

In this exercise you can will explore NumPy’s in-built linear algebra routines Source code for this exercise is located in numpy/linear-algebra/ Construct two symmetric 2×2 matrices A and B.Hint: a …

Linear algebra and polynomials

Linear algebra NumPy includes linear algebra routines that can be quite handy. For example, NumPy can calculate matrix and vector products efficiently (dot,vdot), solve eigenproblems (linalg.eig, linalg.eigvals), solve linearsystems (linalg.solve), …

Random numbers

NumPy provides a wide range of functions to generate random numbers in arrays. These functions are available in the numpy.random module. The random numbers are generated using the same, excellent …