| example1() | Here is a simple example where we pass a numpy array called x to weave. |
Documentation for mmf.examples.inline_example.
Here we give some concrete examples of the use of scipy.weave for including C++ code in your python code. First, the documentation:
inline – a function for including C/C++ code within Python blitz – a function for compiling Numeric expressions to C++ ext_tools – a module that helps construct C/C++ extension modules. accelerate – a module that inline accelerates Python functions
Inline C/C++ code within Python scripts.
inline() compiles and executes C/C++ code on the fly. Variables in the local and global Python scope are also available in the C/C++ code. Values are passed to the C/C++ code by assignment much like variables passed are passed into a standard Python function. Values are returned from the C/C++ code through a special argument called return_val. Also, the contents of mutable objects can be changed within the C/C++ code and the changes remain after the C code exits and returns to Python.
inline has quite a few options as listed below. Also, the keyword arguments for distutils extension modules are accepted to specify extra information needed for compiling.
| Parameters : | code : string
arg_names : [str], optional
local_dict : dict, optional
global_dict : dict, optional
force : {0, 1}, optional
compiler : str, optional
verbose : {0,1,2}, optional
support_code : str, optional
headers : [str], optional
customize : base_info.custom_info, optional
type_converters : [type converters], optional
auto_downcast : {1,0}, optional
newarr_converter : int, optional
|
|---|---|
| Other Parameters: | |
Relevant :mod:`distutils` keywords. These are duplicated from Greg Ward’s : :class:`distutils.extension.Extension` class for convenience: : sources : [string]
include_dirs : [string]
define_macros : [(name
undef_macros : [string]
library_dirs : [string]
libraries : [string]
runtime_library_dirs : [string]
extra_objects : [string]
extra_compile_args : [string]
extra_link_args : [string]
export_symbols : [string]
swig_opts : [string]
depends : [string]
language : string
|
|
See also
Here we demonstrate some key features.
Here is a simple example where we pass a numpy array called x to weave. Inspecting the generated code (listed below) we obtain the following information:
This is the type of code generated by weave:
py::object return_val;
int exception_occured = 0;
PyObject *py__locals = NULL;
PyObject *py__globals = NULL;
PyObject *py_x;
py_x = NULL;
/* argument conversion code */
py_x = get_variable("x", raw_locals, raw_globals);
PyArrayObject* x_array = convert_to_numpy(py_x, "x");
// Number of dimensions
int Dx = x_array->nd;
npy_intp* Nx = x_array->dimensions;
npy_intp* Sx = x_array->strides;
#define X1(i) (*((double*)(x_array->data + (i)*Sx[0])))
#define X2(i,j) (*((double*)(x_array->data + (i)*Sx[0] + (j)*Sx[1])))
#define X3(i,j,k) (*((double*)(x_array->data + (i)*Sx[0] + (j)*Sx[1] + (k)*Sx[2])))
#define X4(i,j,k,l) (*((double*)(x_array->data + (i)*Sx[0] + (j)*Sx[1] + (k)*Sx[2] + (l)*Sx[3])))
double* x = (double*) x_array->data;
/* inline code */
/* NDARRAY API VERSION 1000009 */
/* hash(support_code) = 831520531. This is used to force",
a recompile if the support_code changes. */
Py_BEGIN_ALLOW_THREADS
return_val = norm(x.data, x->dimensions);
Py_END_ALLOW_THREADS /*I would like to fill in changed locals and globals here...*/
}
catch(...)
{
return_val = py::object();
exception_occured = 1;
}
/* cleanup code */
Py_XDECREF(py_x);
#undef X1
#undef X2
#undef X3
#undef X4
Examples
Here is the square root of the sum of the squares of the integers from 1 to 24 inclusive computed using a custom norm function.
>>> example1()
70.0