简体   繁体   English

如何将C代码转换成Python?

[英]How do I convert this C code into Python?

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. 我正在尝试将我拥有的C代码转换为python脚本,以便更多人可以轻松访问它,但是我在理解这一片段时遇到了问题。

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). N是一个值(数组中一列的总和。元素校正了我)。

T is just a numerical value. T只是一个数值。

z, w, and d are memory allocations created from the N array. z,w和d是从N数组创建的内存分配。 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? Nwt和Ndt都是数组,每个元素都是一个内存分配吗? (Not sure). (不确定)。 At least, each one of them was created by using the following method, passing in two different int's. 至少,每个人都是通过以下方法创建的,传入了两个不同的int。

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. 我想在python中完成相同的操作,但是由于不需要内存分配,因此不确定这三行是做什么的,或者不确定如何在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

A suggestion for the Python part of things is to use numpy arrays to represent the matrices (and possibly the arrays too). Python方面的建议是使用numpy数组来表示矩阵(也可能是数组)。 But to be honest, you should not be concerned with that right now. 但老实说,您现在不应该担心这一点。 That C-code looks ugly. 那个C代码看起来很难看。 Apart from that, different languages use different approaches to achieve the same thing. 除此之外,不同的语言使用不同的方法来实现同一件事。 That is what makes such conversions hard. 这就是使这种转换变得困难的原因。 Try to get an understanding of the algorithm it implements (supposing that is what it does) and write that down in a language-agnostic way. 尝试了解它所实现的算法(假设就是它所做的),并以与语言无关的方式将其写下来。 Then think how you would implement that in Python. 然后考虑如何在Python中实现它。

Nwt and Ndt are 2-dimensional arrays. NwtNdt二维数组。 These lines: 这些行:

Nwt[w[i]][t]++;
Ndt[d[i]][t]++;

Increment by 1 the value at one of the locations in each of the arrays. 将每个数组中一个位置的值加1。 If you think of the addressing as array[column][row] , then the column is chosen based on the value in some other one-dimensional array w and d (respectively) for the index i. 如果将地址视为array[column][row] ,则根据索引i的其他一维数组wd的值分别选择列。 t seems to be some random index. t似乎是一些随机索引。

You don't show what dmat function is doing, so hard to break that one down. 您没有显示dmat函数的功能,因此很难分解其中的一个。

(Can't help you on the Python side, hopefully this helps clarify the C) (无法在Python方面为您提供帮助,希望这有助于弄清C语言)

Okay you seem to have a few ideas wrong. 好的,您似乎有一些错误的主意。 N is the size of the array. N是数组的大小。

dmat returns a matrix like thing which is represented by nr row(s) - where each row is an 'array' of nc doubles dmat返回一个由nr行表示的类似矩阵的矩阵-其中每一行都是nc double的“数组”

ivec returns an 'array' of n integer elements. ivec返回n个整数元素的“数组”。

So w[] and d[] represent indexes to the array of doubles. 因此w []和d []代表双精度数组的索引。

The loop that you are having trouble with is used to increment certain elements of the matrices. 您遇到问题的循环用于增加矩阵的某些元素。 One index appears pre-stored in the w and d arrays and the other generated randomly I suspect - with out knowing what the intent of the code is it is a bit difficult to understand the semantics. 一个索引显示为预存储在w和d数组中,而另一个索引是随机生成的,我怀疑-在不知道代码的意图的情况下,理解语义有点困难。

Specifically it might help to know: Nwt[x][y]++ means increment (add 1) the matrix element at row x col y 具体来说,可能会有助于了解:Nwt [x] [y] ++表示在行x col y处增加(加1)矩阵元素

Also must mention that this C code is ugly - no useful naming and no comments, fearless use C's nastiest syntax, really difficult to follow. 还必须提及的是,此C代码很丑陋-没有有用的命名和注释,无畏地使用C的最讨厌的语法,确实很难遵循。

In your translation, the first thing I would worry about is making sensical variable names, particularly for those arrays. 在您的翻译中,我首先要担心的是创建有意义的变量名,尤其是对于那些数组。 Regardless, much of that translates directly. 无论如何,其中大部分直接翻译。

Nwt and Ndt are 2D arrays, Nt is a one dimensional array. Nwt和Ndt是2D数组,Nt是一维数组。 It looks like you're looping over all the 'columns' in the z array, and generating a random number for each one. 看来您正在遍历z数组中的所有“列”,并为每个数组生成一个随机数。 Then you increment whichever column was picked in Nwt (row w[i]), Ndt (row d[i]) and Nt. 然后,您递增在Nwt(行w [i]),Ndt(行d [i])和Nt中选择的列。 The actual random value is stashed in z. 实际随机值存放在z中。

#Literal translation
for i in range(N):
    t = Random.randint(0,T) #Not sure on this... but it seems likely.
    z[i] = t
    Nwt[w[i]][t] += 1
    Ndt[d[i]][t] += 1
    Nt[t] += 1

#In place of w= ivec(N);
w = [0]*N
d = [0]*N
z = [0]*N

#In place of Nwt = dmat(W,T)
Nwt = [[0.0] * T] * W
Ndt = [[0.0] * T] * D

EDIT: corrected w/d/z initialization from "n" to "N" 编辑:将w / d / z初始化从“ n”更正为“ N”

Note that there are still some things wrong here, since it looks like N must equal W, and D... so tread carefully. 请注意,这里仍然存在一些错误,因为看起来N必须等于W,而D ...则应谨慎行事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM