# How to use compiled python packages for matrix initialization

Assume I have an expression for an matrix initialization, for example the following:

A[i,i-2*j+k] = B[i-k] * C[i] * D[i+j+k]

In order to execute such a process, I could loop over all i, j and k. The problem is that loops are slow in python, compared to e.g. loops in C.

Is there some way to use compiled packages for operations such as the one above? This should improve performance a lot I would guess.

• You could try the Python map function to vectorize the operation. Alternatively, try cython. Dec 10, 2015 at 20:50
• I am curious; how computationally expensive is this initialization compared to the rest of the computations? Is this step really the bottleneck; usually it happens only once at the start of the code. Premature optimization is the root of all evil. Dec 10, 2015 at 21:40
• @nluigi Basically I have a differential equation and in order to calculate my derivatives I need to calculate the matrix above in every step. That's also the crucial part of my code. But I totally agree, that one has to be carful with spending to much time with optimising the wrong parts of the code. Dec 11, 2015 at 10:40
• @BiswajitBanerjee That's probably the best solution then. I thought that maybe something like an Einstein sum convention would be available for cases like mine. Dec 11, 2015 at 10:41
• Ah ok i see the need for optimization then; see my answer. I would review your ODE's aswell and check if you can't simplify this matrix initialization somehow by e.g. removing any constants (matrices) from the loop. Dec 11, 2015 at 11:05

## 1 Answer

Considering your last comment take a look at numpy.einsum; I used it some time ago to do a complicated multidimensional matrix multiplication. It takes a bit to get used to but if you follow the examples you should be able to work it out.

This module is implemented in C much like the rest of Numpy so you should get decent performance.