[SciPy-dev] Uploading attachments in scipy.org

Sturla Molden sturla@molden...
Wed Nov 18 06:21:15 CST 2009

Ramon Crehuet skrev:
> Hi all,
> I few days ago I posted a question and I have not received any answer. I
> have modified the Performance Python page, introducing new examples with
> fortran 90.

I think the Performance Python page needs a Cython version as well.

Sturla Molden

import numpy as np
cimport numpy as np
cimport cython
import laplace

cdef extern from "math.h":
    double sqrt(double)

class LaplaceSolver(laplace.LaplaceSolver):

    def cythonTimeStep(self, dt=0.0):

        cdef np.ndarray[double, ndim=2, mode="c"] u
        cdef double dx2, dy2, dnr_inv, diff, tmp, err
        cdef int nx, ny, i, j

        g = self.grid
        u = g.u
        nx = u.shape[0]
        ny = u.shape[1]
        dx2 = g.dx**2
        dy2 = g.dy**2
        dnr_inv = 0.5/(dx2 + dy2)

        err = 0.0
        for i in range(1, nx-1):
            for j in range(1, ny-1):
                tmp = u[i,j]
                u[i,j] = ((u[i-1, j] + u[i+1, j])*dy2 +
                    (u[i, j-1] + u[i, j+1])*dx2)*dnr_inv
                diff = u[i,j] - tmp
                err += diff*diff

        return float(sqrt(err))

More information about the Scipy-dev mailing list