-
Notifications
You must be signed in to change notification settings - Fork 1
/
numpy_interface.cc
110 lines (88 loc) · 2.83 KB
/
numpy_interface.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <gnucap/m_matrix.h>
#include <gnucap/m_wave.h>
#include "numpy_interface.h"
#include <Python.h>
#include <numpy/arrayobject.h>
void init_numpy() {
import_array();
}
PyObject *to_double_array(double *data, int len) {
npy_intp dims[1] = {len};
return PyArray_SimpleNewFromData(1, dims, PyArray_DOUBLE, (void *)(data+1));
}
PyObject *get_complex_array(COMPLEX *data, int len) {
npy_intp dims[1] = {len};
PyObject *arr = PyArray_SimpleNew(1, dims, PyArray_COMPLEX128);
npy_complex128 *outdata = (npy_complex128 *) PyArray_DATA(arr);
// copy data
for(int i=0; i < len; i++) {
outdata[i].real = data[i+1].real();
outdata[i].imag = data[i+1].imag();
}
return arr;
}
void set_complex_array(COMPLEX *data, PyObject *srcarray) {
if( !PyArray_Check(srcarray) ||
( PyArray_TYPE(srcarray ) != PyArray_COMPLEX128 ) ||
( PyArray_NDIM(srcarray ) != 1 ))
throw Exception("Source array has incorrect type");
npy_complex128 *indata = (npy_complex128 *) PyArray_DATA(srcarray);
for(int i=0; i < PyArray_DIM(srcarray, 0); i++)
data[i] = COMPLEX(indata[i].real, indata[i].imag);
}
PyObject *wave_to_arrays(WAVE *wave) {
PyObject *x, *y;
int i, nrows = 0;
if(wave == NULL)
throw Exception("wave_to_arrays got NULL instead of WAVE pointer");
// Count number of elements
// FIXME, WAVE should provide a method for this
for(WAVE::const_iterator wi = wave->begin(); wi < wave->end(); wi++)
nrows++;
// Create arrays
npy_intp dims[1] = {nrows};
x = PyArray_SimpleNew(1, dims, PyArray_DOUBLE);
y = PyArray_SimpleNew(1, dims, PyArray_DOUBLE);
// Copy data
double *xdata = (double *) PyArray_DATA(x);
double *ydata = (double *) PyArray_DATA(y);
i=0;
for(WAVE::const_iterator wi = wave->begin(); wi < wave->end(); wi++, i++) {
xdata[i] = wi->first;
ydata[i] = wi->second;
}
// Return tuple of x,y
PyObject *pTuple = PyTuple_New(2); // new reference
PyTuple_SetItem(pTuple, 0, x);
PyTuple_SetItem(pTuple, 1, y);
return pTuple;
}
PyObject *bsmatrix_to_array_d(BSMATRIX<double> &A) {
PyObject *Aarray;
int i,j;
// Create arrays
npy_intp dims[2] = {A.size(), A.size()};
Aarray = PyArray_SimpleNew(2, dims, PyArray_DOUBLE);
// Copy data
double *ptr = (double *) PyArray_DATA(Aarray);
for(i = 1; i <= A.size(); i++)
for(j = 1; j <= A.size(); j++)
*ptr++ = A.s(i,j);
return Aarray;
}
PyObject *bsmatrix_to_array_c(BSMATRIX<COMPLEX> &A) {
PyObject *Aarray;
int i,j;
// Create arrays
npy_intp dims[2] = {A.size(), A.size()};
Aarray = PyArray_SimpleNew(2, dims, PyArray_COMPLEX128);
// Copy data
npy_complex128 *ptr = (npy_complex128 *) PyArray_DATA(Aarray);
for(i = 1; i <= A.size(); i++)
for(j = 1; j <= A.size(); j++) {
ptr->real = A.s(i,j).real();
ptr->imag = A.s(i,j).imag();
ptr++;
}
return Aarray;
}