Inline typecasting weaving (python)

best wishes 2013.

I use SciPy, inserting inline with some C ++ code to transfer huge matrices (about 200,000 x 15). It works like a charm, but I have a question about type casting:

My input matrix is ​​read from a file, separated by a comma, etc., so all records are strings, not floats ("0.551", not 0.551). This does not affect the operation of the transpose function, but later I have to convert certain strings to numpy floating point arrays, so I was wondering if this can be done in C ++ code. Let me explain using code:

def transpose(lines, N, x): code = """ py::list matrix; for(int i = 0; i < x; i++) { py::list line; if(i == 1) { continue; } for(int j = 0; j < N; j++) { line.append(lines[j][i]); } matrix.append(line); } return_val = matrix; """ return weave.inline(code, ['lines', 'N', 'x']) matrix = [['0.5','0.1'],['0.2','0.2']] matrixT = transpose(matrix, len(matrix), len(matrix[0])) final_result = np.array(matrixT[0], dtype=float) 

In the example, my small matrix will be transposed, and the result of my example will be the first row of the transposed matrix, converted to a numpy dtype float array. Can this be done in C ++ code? I tried using double x = (double) lines[j][i] and the like, but it somehow does not work to add to the py :: list object.

+4
source share
1 answer

Below you can do the following:

 def transpose(lines): code = """ for(int i = 0; i < x; i++) { for(int j = 0; j < N; j++) { out[j + i * N] = atof(lines[j][i]); // OUT2(i, j) = atof(lines[j][i]); } } """ N = len(lines) x = len(lines[0]) out = np.empty((x, N), dtype=np.float64) weave.inline(code, ['lines', 'N', 'x', 'out']) return out >>> matrix = [['0.5', '0.1', '0.7'],['0.2','0.2', '0.4']] >>> matrix [['0.5', '0.1', '0.7'], ['0.2', '0.2', '0.4']] >>> transpose(matrix) array([[ 0.5, 0.2], [ 0.1, 0.2], [ 0.7, 0.4]]) 

Besides constant forgetting ; after something like 6 years without writing C, I had a lot of problems finding out that out turned into C ++ code, and in the end it is a pointer to the data, not PyArrayObject , as indicated in the documentation . There are two variables defined by weaving that are available for use, out_array and py_out , which are of type PyArrayObject* and PyObject* respectively.

I left an alternative version of the assigned comment: weave automatically determines the macros <VAR>1 , <VAR>2 , <VAR>3 and <VAR>4 to access the elements of arrays of the corresponding number of dimensions.

+1
source

All Articles