Pyrex - Numeric demo doesn't build in 0.7.2

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Wed May 14 14:04:59 EDT 2003


On Wed, 14 May 2003 06:52:24 +0200, Fernando Perez wrote:

[...]
> And here is my corresponding pyrex version:
> 
> import cmath
> from Numeric import zeros,Complex
> from math import sin,pi,sqrt
> 
> def quantum_cat_pyrex(int N,float kappa):
> 
>     # type declarations
>     cdef int k,l
>     cdef double alpha, kap_al,sqrt_N_inv
> 
>     # First initialize complex matrix with NxN elements mat =
>     zeros((N,N), Complex)
>     # precompute a few things outside the loop sqrt_N_inv = 1.0/sqrt(N)
>     alpha = 2.0*pi/N
>     kap_al = kappa/alpha
>     
>     # now we fill each element
>     exp = cmath.exp
>     I = complex(0,1)
>     for k from 0 <= k < N:
>         for l from  0 <= l < N:
>             mat[k,l] = sqrt_N_inv * \
>                        exp(I*(alpha*(k*k-k*l+l*l) +
>                        kap_al*sin(alpha*l)))
>     return(mat)
> 
> 

Possible hints :

- do with 'sin' what you have done with 'exp' :
  sin = math.sin # local lookup on symbol instead of global

- use math.h "sin" instead of python's one :

    from math import pi,sqrt

    cdef extern from "math.h":
        cdef double sin(double x)

-- 
Pedro




More information about the Python-list mailing list