您只需将代码修改为C++(而不是C)BOOST_PYTHON_MODULE(Spline)
{
import_array();
boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
class_("Spline", init<>())
.def("spline", &Spline::spline)
;
}
因此在Python中可以使用:
^{pr2}$
Cpp等级:
^{3}$
然后在boost::python::list Spline::spline(numeric::array& x_val, numeric::array& y_val, double p)函数中得到:PyArrayObject* x_pyArr = (PyArrayObject*)PyArray_FROM_O(x_val.ptr());
PyArrayObject* y_pyArr = (PyArrayObject*)PyArray_FROM_O(y_val.ptr());
int size = *(x_pyArr->dimensions), i , j;
double* data_x = (double*)x_pyArr->data;
double* data_y = (double*)y_pyArr->data;
double h[NUMBER_OF_SAMPLES], a, b, c, d, s[NUMBER_OF_SAMPLES] = { 0 }, F[NUMBER_OF_SAMPLES], f[NUMBER_OF_SAMPLES], x[NUMBER_OF_SAMPLES], m[NUMBER_OF_SAMPLES][NUMBER_OF_SAMPLES] = { 0 }, temp;
for (int i = 0; i < size; i++)
{
x[i] = *(data_x + i);
f[i] = *(data_y + i);
}
依此类推。我在Spline::spline函数中返回一个python列表:boost::python::list return_val;
// ....
return_val.append(sum);
return_val.append(result);
return return_val;