Bug in the translation of a C file to a Cython file

亡梦爱人 提交于 2019-12-12 05:16:19

问题


I've succeeded in converting a C file in cython. But the two codes are giving me different results and I really cannot find where the bug is.

The relevant C code of my script is the following:

double empirical_measure(int N, double K, double d, double T, double dt, FILE *store){

/*Define variables*/

  int kt;
  int kt_max =  (int) ((double)T/(double)dt);

  double *xt;
  xt=(double *)malloc(N*sizeof(double));

  double bruit;

  double S=0.0;
  double xtmp;
  double xtilde;
  double x_diff;
  double xi;

  int i;
  int j;
  int l;

/*initial condition*/
  for(i=0; i<N; i++){
     xt [i]=rand()/((double) RAND_MAX)*2*M_PI;
  }

/*Compute trajectories and empirical measure*/
  for(kt=0; kt<kt_max; kt++){

    for(i=0; i<N; i++){
      S = 0.0;
      xi = xt[i];

      for(j=0; j<N; j++){
        x_diff = xt[j] - xi;
        S = S + sin(x_diff);
      }

      bruit= d*sqrt(dt)*gaussian();
      xtilde = xi + ((K/N)*S)*dt + bruit;
      xt[i] = fmod(xtilde,  2.0*M_PI);
    }
 }

 return 0;
}

The gaussian function is a function which gives a random number from a Normal(0,1) distribution.

I translated into this cython code (where the output is a matrix with all the computations of xt[:,k] for all k, and not, for each k, a vector as in the C code):

def simul(int N, double K, double d, double T, double dt):
    cdef int kt_max =  int(T/dt)
    cdef double S1
    cdef double xtilde, x_diff, xtmp


    cdef double[:] bruit = d*sqrt(dt)*np.random.standard_normal(N) #bruit generator

    cdef double[:, ::1] xt = np.zeros((N, kt_max), dtype=np.float64)
    cdef int kt, i, j, k

    #initial conditions
    X=np.random.uniform(0,2*np.pi,N)
    for i in range(N):
        xt[i,0]=X[i]

    #Compute trajectories and empirical measure
    for kt in range(kt_max-1):
        for i in range(N):
            S1 = 0.0

            for j in range(N):
                x_diff = xt[j,kt] - xt[i,kt]
                S1 = S1 + sin(x_diff)

            xtilde = xt[i,kt] + ((K/N)*S1)*dt + bruit[i]
            xt[i,kt+1] = xtilde%(2*np.pi)

    return xt

The problem is that if I run the two scripts with the same values I have really different results. For example, given:

N=600
K=5
T=2
dt=0.01
d=1

I obtain for the C code, for the last k:

where from the cython code:

I really can't find the problem in the code. Where could a bug be present?

Update

If I run the code with d=0 (which means that the "bruit part" can be neglected) I still obtain different results:

Histogram for d=0

The blu is the C simulation and the other colors are three simulations from python.

This means that there's something wrong in this section:

    for kt in range(kt_max-1):
        for i in range(N):
            S1 = 0.0

            for j in range(N):
                x_diff = xt[j,kt] - xt[i,kt]
                S1 = S1 + sin(x_diff)

            xtilde = xt[i,kt] + ((K/N)*S1)*dt
            xt[i,kt+1] = xtilde%(2*np.pi)

Any ideas? Has the function sin some tricky argument to put in the code? I've also done the computation of the sin sum, getting:

sin sum simulations

(black is C code, the rest is python simulations)

来源:https://stackoverflow.com/questions/39852459/bug-in-the-translation-of-a-c-file-to-a-cython-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!