I am trying to convert this C code I have into a python script so it's readily accessible by more people, but I am having problems understanding this one snippet.
int i, t;
for (i = 0; i < N; i++) {
t = (int)(T*drand48());
z[i] = t;
Nwt[w[i]][t]++;
Ndt[d[i]][t]++;
Nt[t]++;
}
N is a value (sum of one column from an array. Elemental corrected me).
T is just a numerical value.
z, w, and d are memory allocations created from the N array. They were created with this method.
w = ivec(N);
d = ivec(N);
z = ivec(N);
int *ivec(int n) //
{
int *x = (int*)calloc(n,sizeof(int));
assert(x);
return x;
}
Nwt & Ndt are both arrays too, with each element being a memory allocation? (Not sure). At least, each one of them was created by using the following method, passing in two different int's.
Nwt = dmat(W,T);
Ndt = dmat(D,T);
double **dmat(int nr, int nc) //
{
int N = nr*nc;
double *tmp = (double*) calloc(N,sizeof(double));
double **x = (double**)calloc(nr,sizeof(double*));
int r;
assert(tmp);
assert(x);
for (r = 0; r < nr; r++) x[r] = tmp + nc*r;
return x;
}
So looking at the first loop I posted, what are the following lines doing? I would like to accomplish the same thing in python, but since no memory allocation is needed, not sure what those three lines do, or how I would duplicate it in python.
Nwt[w[i]][t]++;
Ndt[d[i]][t]++;
Nt[t]++;
This is what I have so far:
for i in range(self.N):
t = self.T * random.random()
self.z[i] = t
//** INCORRECT BELOW **
//self.Nwt[self.N[i]] = t + 1
//self.Ndt[i] = t + 1
//self.Nt[t + 1] += 1