| 1 |
Notes for the numpy/tools/swig directory |
| 2 |
======================================== |
| 3 |
|
| 4 |
This set of files is for developing and testing file numpy.i, which is |
| 5 |
intended to be a set of typemaps for helping SWIG interface between C |
| 6 |
and C++ code that uses C arrays and the python module NumPy. |
| 7 |
|
| 8 |
Documentation |
| 9 |
------------- |
| 10 |
Documentation for how to use numpy.i, as well as for the testing |
| 11 |
system used here, can be found in the NumPy reference guide. |
| 12 |
|
| 13 |
Testing |
| 14 |
------- |
| 15 |
The tests are a good example of what we are trying to do with numpy.i. |
| 16 |
The files related to testing are in the test subdirectory:: |
| 17 |
|
| 18 |
Vector.h |
| 19 |
Vector.cxx |
| 20 |
Vector.i |
| 21 |
testVector.py |
| 22 |
|
| 23 |
Matrix.h |
| 24 |
Matrix.cxx |
| 25 |
Matrix.i |
| 26 |
testMatrix.py |
| 27 |
|
| 28 |
Tensor.h |
| 29 |
Tensor.cxx |
| 30 |
Tensor.i |
| 31 |
testTensor.py |
| 32 |
|
| 33 |
SuperTensor.h |
| 34 |
SuperTensor.cxx |
| 35 |
SuperTensor.i |
| 36 |
testSuperTensor.py |
| 37 |
|
| 38 |
Flat.h |
| 39 |
Flat.cxx |
| 40 |
Flat.i |
| 41 |
testFlat.py |
| 42 |
|
| 43 |
The header files contain prototypes for functions that illustrate the |
| 44 |
wrapping issues we wish to address. Right now, this consists of |
| 45 |
functions with argument signatures of the following forms. Vector.h:: |
| 46 |
|
| 47 |
(type IN_ARRAY1[ANY]) |
| 48 |
(type* IN_ARRAY1, int DIM1) |
| 49 |
(int DIM1, type* IN_ARRAY1) |
| 50 |
|
| 51 |
(type INPLACE_ARRAY1[ANY]) |
| 52 |
(type* INPLACE_ARRAY1, int DIM1) |
| 53 |
(int DIM1, type* INPLACE_ARRAY1) |
| 54 |
|
| 55 |
(type ARGOUT_ARRAY1[ANY]) |
| 56 |
(type* ARGOUT_ARRAY1, int DIM1) |
| 57 |
(int DIM1, type* ARGOUT_ARRAY1) |
| 58 |
|
| 59 |
Matrix.h:: |
| 60 |
|
| 61 |
(type IN_ARRAY2[ANY][ANY]) |
| 62 |
(type* IN_ARRAY2, int DIM1, int DIM2) |
| 63 |
(int DIM1, int DIM2, type* IN_ARRAY2) |
| 64 |
|
| 65 |
(type INPLACE_ARRAY2[ANY][ANY]) |
| 66 |
(type* INPLACE_ARRAY2, int DIM1, int DIM2) |
| 67 |
(int DIM1, int DIM2, type* INPLACE_ARRAY2) |
| 68 |
|
| 69 |
(type ARGOUT_ARRAY2[ANY][ANY]) |
| 70 |
|
| 71 |
Tensor.h:: |
| 72 |
|
| 73 |
(type IN_ARRAY3[ANY][ANY][ANY]) |
| 74 |
(type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) |
| 75 |
(int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) |
| 76 |
|
| 77 |
(type INPLACE_ARRAY3[ANY][ANY][ANY]) |
| 78 |
(type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) |
| 79 |
(int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) |
| 80 |
|
| 81 |
(type ARGOUT_ARRAY3[ANY][ANY][ANY]) |
| 82 |
|
| 83 |
SuperTensor.h:: |
| 84 |
|
| 85 |
(type IN_ARRAY4[ANY][ANY][ANY][ANY]) |
| 86 |
(type* IN_ARRAY4, int DIM1, int DIM2, int DIM3, int DIM4) |
| 87 |
(int DIM1, int DIM2, int DIM3, int DIM4, type* IN_ARRAY4) |
| 88 |
|
| 89 |
(type INPLACE_ARRAY4[ANY][ANY][ANY][ANY]) |
| 90 |
(type* INPLACE_ARRAY4, int DIM1, int DIM2, int DIM3, int DIM4) |
| 91 |
(int DIM1, int DIM2, int DIM3, int DIM4, type* INPLACE_ARRAY4) |
| 92 |
|
| 93 |
(type ARGOUT_ARRAY4[ANY][ANY][ANY][ANY]) |
| 94 |
|
| 95 |
Flat.h:: |
| 96 |
(type* INPLACE_ARRAY_FLAT, int DIM_FLAT) |
| 97 |
|
| 98 |
These function signatures take a pointer to an array of type "type", |
| 99 |
whose length is specified by the integer(s) DIM1 (and DIM2, and DIM3, |
| 100 |
and DIM4, or DIM_FLAT). |
| 101 |
|
| 102 |
The objective for the IN_ARRAY signatures is for SWIG to generate |
| 103 |
python wrappers that take a container that constitutes a valid |
| 104 |
argument to the numpy array constructor, and can be used to build an |
| 105 |
array of type "type". Currently, types "signed char", "unsigned |
| 106 |
char", "short", "unsigned short", "int", "unsigned int", "long", |
| 107 |
"unsigned long", "long long", "unsigned long long", "float", and |
| 108 |
"double" are supported and tested. |
| 109 |
|
| 110 |
The objective for the INPLACE_ARRAY signatures is for SWIG to generate |
| 111 |
python wrappers that accept a numpy array of any of the above-listed |
| 112 |
types. |
| 113 |
|
| 114 |
The source files Vector.cxx, Matrix.cxx, Tensor.cxx, SuperTensor.cxx |
| 115 |
and Flat.cxx contain the actual implementations of the functions |
| 116 |
described in Vector.h, Matrix.h Tensor.h, SuperTensor.h and Flat.h. |
| 117 |
The python scripts testVector.py, testMatrix.py testTensor.py, |
| 118 |
testSuperTensor.py and testFlat.py test the resulting python wrappers |
| 119 |
using the unittest module. |
| 120 |
|
| 121 |
The SWIG interface files Vector.i, Matrix.i, Tensor.i, SuperTensor.i |
| 122 |
and Flat.i are used to generate the wrapper code. The |
| 123 |
SWIG_FILE_WITH_INIT macro allows numpy.i to be used with multiple |
| 124 |
python modules. If it is specified, then the %init block found in |
| 125 |
Vector.i, Matrix.i Tensor.i, SuperTensor.i and Flat.i are required. |
| 126 |
The other things done in Vector.i, Matrix.i, Tensor.i, SuperTensor.i |
| 127 |
and Flat.i are the inclusion of the appropriate header file and |
| 128 |
numpy.i file, and the "%apply" directives to force the functions to |
| 129 |
use the typemaps. |
| 130 |
|
| 131 |
The setup.py script is a standard python distutils script. It defines |
| 132 |
_Vector, _Matrix, _Tensor, _SuperTensor, _Flat extension modules and Vector, |
| 133 |
Matrix, Tensor, SuperTensor and Flat python modules. The Makefile |
| 134 |
automates everything, setting up the dependencies, calling swig to |
| 135 |
generate the wrappers, and calling setup.py to compile the wrapper |
| 136 |
code and generate the shared objects. Targets "all" (default), "test", |
| 137 |
"doc" and "clean" are supported. The "doc" target creates HTML |
| 138 |
documentation (with make target "html"), and PDF documentation |
| 139 |
(with make targets "tex" and "pdf"). |
| 140 |
|
| 141 |
To build and run the test code, simply execute from the shell:: |
| 142 |
|
| 143 |
$ make test |