1
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ You could try the Python map function to vectorize the operation. Alternatively, try cython. $\endgroup$ – Biswajit Banerjee Dec 10 '15 at 20:50
  • 3
    $\begingroup$ 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. $\endgroup$ – nluigi Dec 10 '15 at 21:40
  • 1
    $\begingroup$ @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. $\endgroup$ – physicsGuy Dec 11 '15 at 10:40
  • $\begingroup$ @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. $\endgroup$ – physicsGuy Dec 11 '15 at 10:41
  • $\begingroup$ 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. $\endgroup$ – nluigi Dec 11 '15 at 11:05
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.