diff options
author | wfspotz@sandia.gov <wfspotz@sandia.gov@localhost> | 2007-03-14 19:57:23 +0000 |
---|---|---|
committer | wfspotz@sandia.gov <wfspotz@sandia.gov@localhost> | 2007-03-14 19:57:23 +0000 |
commit | df7716092b4522632b0717c7f1c00c6f7c5385da (patch) | |
tree | 1a19fb51d06f58e52a731c46e9b1f7a68cefe9b2 | |
parent | 6a23ed31cebdd16dde0500c4f532ed844c585585 (diff) | |
download | numpy-df7716092b4522632b0717c7f1c00c6f7c5385da.tar.gz |
Added typemap signatures where the dimensions come before the data pointer
-rw-r--r-- | numpy/doc/swig/README | 14 | ||||
-rw-r--r-- | numpy/doc/swig/Series.i | 138 | ||||
-rw-r--r-- | numpy/doc/swig/numpy.i | 94 | ||||
-rw-r--r-- | numpy/doc/swig/numpy_swig.html | 51 | ||||
-rw-r--r-- | numpy/doc/swig/numpy_swig.txt | 49 | ||||
-rw-r--r-- | numpy/doc/swig/series.cxx | 55 | ||||
-rw-r--r-- | numpy/doc/swig/series.h | 15 | ||||
-rwxr-xr-x | numpy/doc/swig/testSeries.py | 452 |
8 files changed, 594 insertions, 274 deletions
diff --git a/numpy/doc/swig/README b/numpy/doc/swig/README index f552a01ec..c8cce65cb 100644 --- a/numpy/doc/swig/README +++ b/numpy/doc/swig/README @@ -1,4 +1,4 @@ -Notes for the swig_numpy/new directory +Notes for the numpy/doc/swig directory ====================================== This set of files is for developing and testing file numpy.i, which is @@ -21,8 +21,12 @@ consists of functions with argument signatures of the form:: (type* IN_ARRAY1, int DIM1) (type* IN_ARRAY2, int DIM1, int DIM2) + (int DIM1, type* IN_ARRAY1)`` + (int DIM1, int DIM2, type* IN_ARRAY2)`` (type* INPLACE_ARRAY1, int DIM1) (type* INPLACE_ARRAY2, int DIM1, int DIM2) + (int DIM1, type* INPLACE_ARRAY1) + (int DIM1, int DIM2, type* INPLACE_ARRAY2) which take a pointer to an array of type "type", whose length is specified by the integer(s) DIM1 (and DIM2). @@ -30,10 +34,10 @@ specified by the integer(s) DIM1 (and DIM2). The objective for the IN_ARRAY signatures is for SWIG to generate python wrappers that take a container that constitutes a valid argument to the numpy array constructor, and can be used to build an -array of type "type". Currently, types "char", "unsigned char", -"signed char", "short", "int", "long", "float", "double" and -"PyObject" are supported, although only the types "short", "int", -"long", "float" and "double" are tested. +array of type "type". Currently, types "signed char", "unsigned +char", "short", "unsigned short", "int", "unsigned int", "long", +"unsigned long", "long long", "unsigned long long", "float", "double", +"PyObject" and "char" are supported and tested. The objective for the INPLACE_ARRAY signatures is for SWIG to generate python wrappers that accept a numpy array of any of the above-listed diff --git a/numpy/doc/swig/Series.i b/numpy/doc/swig/Series.i index ec4799927..1ba9fd2ca 100644 --- a/numpy/doc/swig/Series.i +++ b/numpy/doc/swig/Series.i @@ -6,124 +6,38 @@ #include "series.h" %} -// Get the Numeric typemaps +// Get the NumPy typemaps %include "numpy.i" %init %{ import_array(); %} -// Apply the Numeric typemaps for 1D input arrays -%apply (signed char* IN_ARRAY1, int DIM1) - {(signed char* series, int size)}; -%apply (unsigned char* IN_ARRAY1, int DIM1) - {(unsigned char* series, int size)}; -%apply (short* IN_ARRAY1, int DIM1) - {(short* series, int size)}; -%apply (unsigned short* IN_ARRAY1, int DIM1) - {(unsigned short* series, int size)}; -%apply (int* IN_ARRAY1, int DIM1) - {(int* series, int size)}; -%apply (unsigned int* IN_ARRAY1, int DIM1) - {(unsigned int* series, int size)}; -%apply (long* IN_ARRAY1, int DIM1) - {(long* series, int size)}; -%apply (unsigned long* IN_ARRAY1, int DIM1) - {(unsigned long* series, int size)}; -%apply (long long* IN_ARRAY1, int DIM1) - {(long long* series, int size)}; -%apply (unsigned long long* IN_ARRAY1, int DIM1) - {(unsigned long long* series, int size)}; -%apply (float* IN_ARRAY1, int DIM1) - {(float* series, int size)}; -%apply (double* IN_ARRAY1, int DIM1) - {(double* series, int size)}; -%apply (long double* IN_ARRAY1, int DIM1) - {(long double* series, int size)}; - -// Apply the Numeric typemaps for 1D input/output arrays -%apply (signed char* INPLACE_ARRAY1, int DIM1) - {(signed char* array, int size)}; -%apply (unsigned char* INPLACE_ARRAY1, int DIM1) - {(unsigned char* array, int size)}; -%apply (short* INPLACE_ARRAY1, int DIM1) - {(short* array, int size)}; -%apply (unsigned short* INPLACE_ARRAY1, int DIM1) - {(unsigned short* array, int size)}; -%apply (int* INPLACE_ARRAY1, int DIM1) - {(int* array, int size)}; -%apply (unsigned int* INPLACE_ARRAY1, int DIM1) - {(unsigned int* array, int size)}; -%apply (long* INPLACE_ARRAY1, int DIM1) - {(long* array, int size)}; -%apply (unsigned long* INPLACE_ARRAY1, int DIM1) - {(unsigned long* array, int size)}; -%apply (long long* INPLACE_ARRAY1, int DIM1) - {(long long* array, int size)}; -%apply (unsigned long long* INPLACE_ARRAY1, int DIM1) - {(unsigned long long* array, int size)}; -%apply (float* INPLACE_ARRAY1, int DIM1) - {(float* array, int size)}; -%apply (double* INPLACE_ARRAY1, int DIM1) - {(double* array, int size)}; -%apply (long double* INPLACE_ARRAY1, int DIM1) - {(long double* array, int size)}; - -// Apply the Numeric typemaps for 2D input arrays -%apply (signed char* IN_ARRAY2, int DIM1, int DIM2) - {(signed char* matrix, int rows, int cols)}; -%apply (unsigned char* IN_ARRAY2, int DIM1, int DIM2) - {(unsigned char* matrix, int rows, int cols)}; -%apply (short* IN_ARRAY2, int DIM1, int DIM2) - {(short* matrix, int rows, int cols)}; -%apply (unsigned short* IN_ARRAY2, int DIM1, int DIM2) - {(unsigned short* matrix, int rows, int cols)}; -%apply (int* IN_ARRAY2, int DIM1, int DIM2) - {(int* matrix, int rows, int cols)}; -%apply (unsigned int* IN_ARRAY2, int DIM1, int DIM2) - {(unsigned int* matrix, int rows, int cols)}; -%apply (long* IN_ARRAY2, int DIM1, int DIM2) - {(long* matrix, int rows, int cols)}; -%apply (unsigned long* IN_ARRAY2, int DIM1, int DIM2) - {(unsigned long* matrix, int rows, int cols)}; -%apply (long long* IN_ARRAY2, int DIM1, int DIM2) - {(long long* matrix, int rows, int cols)}; -%apply (unsigned long long* IN_ARRAY2, int DIM1, int DIM2) - {(unsigned long long* matrix, int rows, int cols)}; -%apply (float* IN_ARRAY2, int DIM1, int DIM2) - {(float* matrix, int rows, int cols)}; -%apply (double* IN_ARRAY2, int DIM1, int DIM2) - {(double* matrix, int rows, int cols)}; -%apply (long double* IN_ARRAY2, int DIM1, int DIM2) - {(long double* matrix, int rows, int cols)}; - -// Apply the Numeric typemaps for 2D input/output arrays -%apply (signed char* INPLACE_ARRAY2, int DIM1, int DIM2) - {(signed char* array, int rows, int cols)}; -%apply (unsigned char* INPLACE_ARRAY2, int DIM1, int DIM2) - {(unsigned char* array, int rows, int cols)}; -%apply (short* INPLACE_ARRAY2, int DIM1, int DIM2) - {(short* array, int rows, int cols)}; -%apply (unsigned short* INPLACE_ARRAY2, int DIM1, int DIM2) - {(unsigned short* array, int rows, int cols)}; -%apply (int* INPLACE_ARRAY2, int DIM1, int DIM2) - {(int* array, int rows, int cols)}; -%apply (unsigned int* INPLACE_ARRAY2, int DIM1, int DIM2) - {(unsigned int* array, int rows, int cols)}; -%apply (long* INPLACE_ARRAY2, int DIM1, int DIM2) - {(long* array, int rows, int cols)}; -%apply (unsigned long* INPLACE_ARRAY2, int DIM1, int DIM2) - {(unsigned long* array, int rows, int cols)}; -%apply (long long* INPLACE_ARRAY2, int DIM1, int DIM2) - {(long long* array, int rows, int cols)}; -%apply (unsigned long long* INPLACE_ARRAY2, int DIM1, int DIM2) - {(unsigned long long* array, int rows, int cols)}; -%apply (float* INPLACE_ARRAY2, int DIM1, int DIM2) - {(float* array, int rows, int cols)}; -%apply (double* INPLACE_ARRAY2, int DIM1, int DIM2) - {(double* array, int rows, int cols)}; -%apply (long double* INPLACE_ARRAY2, int DIM1, int DIM2) - {(long double* array, int rows, int cols)}; +%define %apply_numpy_typemaps(TYPE) + +%apply (TYPE* IN_ARRAY1, int DIM1) {(TYPE* series, int size)}; +%apply (TYPE* IN_ARRAY2, int DIM1, int DIM2) {(TYPE* matrix, int rows, int cols)}; +%apply (TYPE* INPLACE_ARRAY1, int DIM1) {(TYPE* array, int size)}; +%apply (TYPE* INPLACE_ARRAY2, int DIM1, int DIM2) {(TYPE* array, int rows, int cols)}; +%apply (int DIM1, TYPE* IN_ARRAY1) {(int size, TYPE* series)}; +%apply (int DIM1, int DIM2, TYPE* IN_ARRAY2) {(int rows, int cols, TYPE* matrix)}; +%apply (int DIM1, TYPE* INPLACE_ARRAY1) {(int size, TYPE* array)}; +%apply (int DIM1, int DIM2, TYPE* INPLACE_ARRAY2) {(int rows, int cols, TYPE* array)}; + +%enddef /* %apply_numpy_typemaps() macro */ + +%apply_numpy_typemaps(signed char ) +%apply_numpy_typemaps(unsigned char ) +%apply_numpy_typemaps(short ) +%apply_numpy_typemaps(unsigned short ) +%apply_numpy_typemaps(int ) +%apply_numpy_typemaps(unsigned int ) +%apply_numpy_typemaps(long ) +%apply_numpy_typemaps(unsigned long ) +%apply_numpy_typemaps(long long ) +%apply_numpy_typemaps(unsigned long long) +%apply_numpy_typemaps(float ) +%apply_numpy_typemaps(double ) // Include the header file to be wrapped %include "series.h" diff --git a/numpy/doc/swig/numpy.i b/numpy/doc/swig/numpy.i index e843b6149..32346c9d6 100644 --- a/numpy/doc/swig/numpy.i +++ b/numpy/doc/swig/numpy.i @@ -304,6 +304,10 @@ int require_size(PyArrayObject* ary, npy_intp* size, int n) { * (TYPE* ARGOUT_ARRAY1[ANY]) * (TYPE* ARGOUT_ARRAY2[ANY][ANY]) * + * (int DIM1, TYPE* IN_ARRAY1) + * (int DIM1, int DIM2, TYPE* IN_ARRAY2) + * (int DIM1, TYPE* INPLACE_ARRAY1) + * * where "TYPE" is any type supported by the NumPy module. In python, * the dimensions will not need to be specified. The IN_ARRAYs can be * a numpy array or any sequence that can be converted to a numpy @@ -314,32 +318,45 @@ int require_size(PyArrayObject* ary, npy_intp* size, int n) { * These typemaps can be applied to existing functions using the * %apply directive: * - * %apply (double* IN_ARRAY1, int DIM1) {double* series, int length}; - * double sum(double* series, int length); + * %apply (double* IN_ARRAY1, int DIM1) {(double* series, int length)}; + * double prod(double* series, int length); * - * %apply (double* IN_ARRAY2, int DIM1, int DIM2) {double* mx, int rows, int cols}; + * %apply (double* IN_ARRAY2, int DIM1, int DIM2) {(double* mx, int rows, int cols)}; * double max(double* mx, int rows, int cols); * - * %apply (double* INPLACE_ARRAY1, int DIM1) {double* series, int length}; - * void negate(double* series, int length); + * %apply (double* INPLACE_ARRAY1, int DIM1) {(double* series, int length)}; + * void ones(double* series, int length); * - * %apply (double* INPLACE_ARRAY2, int DIM1, int DIM2) {double* mx, int rows, int cols}; - * void normalize(double* mx, int rows, int cols); + * %apply (double* INPLACE_ARRAY2, int DIM1, int DIM2) {(double* mx, int rows, int cols)}; + * void floor(double* mx, int rows, int cols); * - * %apply (double* ARGOUT_ARRAY1[ANY] {double series, int length}; + * %apply (double* ARGOUT_ARRAY1[ANY] {(double series, int length)}; * void negate(double* series, int length); * - * %apply (double* ARGOUT_ARRAY2[ANY][ANY]) {double* mx, int rows, int cols}; + * %apply (double* ARGOUT_ARRAY2[ANY][ANY]) {(double* mx, int rows, int cols)}; * void normalize(double* mx, int rows, int cols); * + * %apply (int DIM1, double* IN_ARRAY1) {(int length, double* series)} + * double sum(int length, double* series) + * + * %apply (int DIM1, int DIM2, double* IN_ARRAY2) {(int rows, int cols, double* matrix)} + * double min(int length, double* series) + * + * %apply (int DIM1, double* INPLACE_ARRAY1) {(int length, double* series)} + * double zeros(int length, double* series) + * * or directly with * - * double sum(double* IN_ARRAY1, int DIM1); + * double prod(double* IN_ARRAY1, int DIM1); * double max(double* IN_ARRAY2, int DIM1, int DIM2); - * void sum(double* INPLACE_ARRAY1, int DIM1); - * void sum(double* INPLACE_ARRAY2, int DIM1, int DIM2); - * void sum(double* ARGOUT_ARRAY1[ANY]); - * void sum(double* ARGOUT_ARRAY2[ANY][ANY]); + * void ones(double* INPLACE_ARRAY1, int DIM1); + * void floor(double* INPLACE_ARRAY2, int DIM1, int DIM2); + * void negate(double* ARGOUT_ARRAY1[ANY]); + * void normalize(double* ARGOUT_ARRAY2[ANY][ANY]); + * + * double sum(int DIM1, double* IN_ARRAY1) + * double min(int DIM1, int DIM2, double* IN_ARRAY2) + * void zeros(int DIM1, double* INPLACE_ARRAY1) */ %define %numpy_typemaps(TYPE, TYPECODE) @@ -441,6 +458,55 @@ int require_size(PyArrayObject* ary, npy_intp* size, int n) { } } +/* Typemap suite for (int DIM1, TYPE* IN_ARRAY1) + */ +%typemap(in) (int DIM1, TYPE* IN_ARRAY1) + (PyArrayObject* array=NULL, int is_new_object=0) { + array = obj_to_array_contiguous_allow_conversion($input, TYPECODE, &is_new_object); + npy_intp size[1] = {-1}; + if (!array || !require_dimensions(array, 1) || !require_size(array, size, 1)) SWIG_fail; + $1 = (int) array->dimensions[0]; + $2 = (TYPE*) array->data; +} +%typemap(freearg) (int DIM1, TYPE* IN_ARRAY1) { + if (is_new_object$argnum && array$argnum) Py_DECREF(array$argnum); +} + +/* Typemap suite for (int DIM1, int DIM2, TYPE* IN_ARRAY2) + */ +%typemap(in) (int DIM1, int DIM2, TYPE* IN_ARRAY2) + (PyArrayObject* array=NULL, int is_new_object=0) { + array = obj_to_array_contiguous_allow_conversion($input, TYPECODE, &is_new_object); + npy_intp size[2] = {-1,-1}; + if (!array || !require_dimensions(array, 2) || !require_size(array, size, 1)) SWIG_fail; + $1 = (int) array->dimensions[0]; + $2 = (int) array->dimensions[1]; + $3 = (TYPE*) array->data; +} +%typemap(freearg) (int DIM1, int DIM2, TYPE* IN_ARRAY2) { + if (is_new_object$argnum && array$argnum) Py_DECREF(array$argnum); +} + +/* Typemap suite for (int DIM1, TYPE* INPLACE_ARRAY1) + */ +%typemap(in) (int DIM1, TYPE* INPLACE_ARRAY1) (PyArrayObject* temp=NULL) { + temp = obj_to_array_no_conversion($input, TYPECODE); + if (!temp || !require_contiguous(temp)) SWIG_fail; + $1 = 1; + for (int i=0; i<temp->nd; ++i) $1 *= temp->dimensions[i]; + $2 = (TYPE*) temp->data; +} + +/* Typemap suite for (int DIM1, int DIM2, TYPE* INPLACE_ARRAY2) + */ +%typemap(in) (int DIM1, int DIM2, TYPE* INPLACE_ARRAY2) (PyArrayObject* temp=NULL) { + temp = obj_to_array_no_conversion($input, TYPECODE); + if (!temp || !require_contiguous(temp)) SWIG_fail; + $1 = (int) temp->dimensions[0]; + $2 = (int) temp->dimensions[1]; + $3 = (TYPE*) temp->data; +} + %enddef /* %numpy_typemaps() macro */ diff --git a/numpy/doc/swig/numpy_swig.html b/numpy/doc/swig/numpy_swig.html index 73fec1875..393bd6281 100644 --- a/numpy/doc/swig/numpy_swig.html +++ b/numpy/doc/swig/numpy_swig.html @@ -351,9 +351,16 @@ named <tt class="docutils literal"><span class="pre">rms.h</span></tt>. To obta <a class="reference" href="http://www.swig.org">SWIG</a> interface file would need the following:</p> <pre class="literal-block"> %{ +#define SWIG_FILE_WITH_INIT #include "rms.h" %} + %include "numpy.i" + +%init %{ +import_array(); +%} + %apply (double* IN_ARRAY1, int DIM1) {(double* seq, int n)}; %include "rms.h" </pre> @@ -365,19 +372,30 @@ argument names are intended to suggest that the <tt class="docutils literal"><sp is an input array of one dimension and that the <tt class="docutils literal"><span class="pre">int</span></tt> represents that dimension. This is precisely the pattern in the <tt class="docutils literal"><span class="pre">rms</span></tt> prototype.</p> -<p>Hopefully, no actual prototypes to be wrapped will have the names +<p>Most likely, no actual prototypes to be wrapped will have the names <tt class="docutils literal"><span class="pre">IN_ARRAY1</span></tt> and <tt class="docutils literal"><span class="pre">DIM1</span></tt>. We use the <tt class="docutils literal"><span class="pre">%apply</span></tt> directive to apply the typemap for one-dimensional input arrays of type <tt class="docutils literal"><span class="pre">double</span></tt> to the actual prototype used by <tt class="docutils literal"><span class="pre">rms</span></tt>. Using <tt class="docutils literal"><span class="pre">numpy.i</span></tt> effectively, therefore, requires knowing what typemaps are available and what they do.</p> +<p>Note that if the C function signature was in a different order:</p> +<pre class="literal-block"> +double rms(int n, double* seq); +</pre> +<p>that <a class="reference" href="http://www.swig.org">SWIG</a> would not match the typemap signature given above with +the argument list for <tt class="docutils literal"><span class="pre">rms</span></tt>. Fortunately, <tt class="docutils literal"><span class="pre">numpy.i</span></tt> has a set of +typemaps with the data pointer given last:</p> +<pre class="literal-block"> +%apply (int DIM1, double* IN_ARRAY1) {(int n, double* seq)}; +</pre> </div> <div class="section"> <h1><a id="using-numpy-i" name="using-numpy-i">Using numpy.i</a></h1> <p>The <tt class="docutils literal"><span class="pre">numpy.i</span></tt> file is currently located in the <tt class="docutils literal"><span class="pre">numpy/docs/swig</span></tt> sub-directory under the <tt class="docutils literal"><span class="pre">numpy</span></tt> installation directory. Typically, you will want to copy it to the directory where you are developing -your wrappers.</p> +your wrappers. If it is ever adopted by <a class="reference" href="http://www.swig.org">SWIG</a> developers, then it +will be installed in a standard place where <a class="reference" href="http://www.swig.org">SWIG</a> can find it.</p> <p>A simple module that only uses a single <a class="reference" href="http://www.swig.org">SWIG</a> interface file should include the following:</p> <pre class="literal-block"> @@ -389,17 +407,22 @@ include the following:</p> import_array(); %} </pre> -<p>Only one interface file should call <tt class="docutils literal"><span class="pre">import_array()</span></tt>. If you have -more than one <a class="reference" href="http://www.swig.org">SWIG</a> interface file, then subsequent files should not -<tt class="docutils literal"><span class="pre">#define</span> <span class="pre">SWIG_FILE_WITH_INIT</span></tt> and should not call -<tt class="docutils literal"><span class="pre">import_array()</span></tt>.</p> +<p>Within a compiled python module, <tt class="docutils literal"><span class="pre">import_array()</span></tt> should only get +called once. This could be in a C/C++ file that you have written and +is linked to the module. If this is the case, then none of your +interface files should <tt class="docutils literal"><span class="pre">#define</span> <span class="pre">SWIG_FILE_WITH_INIT</span></tt> or call +<tt class="docutils literal"><span class="pre">import_array()</span></tt>. Or, this initialization call could be in a +wrapper file generated by <a class="reference" href="http://www.swig.org">SWIG</a> from an interface file that has the +<tt class="docutils literal"><span class="pre">%init</span></tt> block as above. If this is the case, and you have more than +one <a class="reference" href="http://www.swig.org">SWIG</a> interface file, then only one interface file should +<tt class="docutils literal"><span class="pre">#define</span> <span class="pre">SWIG_FILE_WITH_INIT</span></tt> and call <tt class="docutils literal"><span class="pre">import_array()</span></tt>.</p> </div> <div class="section"> <h1><a id="available-typemaps" name="available-typemaps">Available Typemaps</a></h1> <p>The typemap directives provided by <tt class="docutils literal"><span class="pre">numpy.i</span></tt> for arrays of different data types, say <tt class="docutils literal"><span class="pre">double</span></tt> and <tt class="docutils literal"><span class="pre">int</span></tt>, are identical to one another except for the C and <a class="reference" href="http://numpy.scipy.org">NumPy</a> type specifications. The typemaps are -therefore implemented via a macro:</p> +therefore implemented (typically behind the scenes) via a macro:</p> <pre class="literal-block"> %numpy_typemaps(TYPE, TYPECODE) </pre> @@ -442,7 +465,7 @@ expansion:</p> %numpy_typemaps(bool, NPY_UINT) </pre> <p>to fix the data length problem, and <a class="reference" href="#input-arrays">Input Arrays</a> will work fine, -but <a class="reference" href="#in-place-arrays">In-place Arrays</a> will fail type-checking.</p> +but <a class="reference" href="#in-place-arrays">In-place Arrays</a> might fail type-checking.</p> <p>In the following descriptions, we reference a generic <tt class="docutils literal"><span class="pre">TYPE</span></tt>, which could be any of the C-types listed above.</p> <div class="section"> @@ -456,6 +479,8 @@ of array. The input array signatures are</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">(TYPE*</span> <span class="pre">IN_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1)</span></tt></li> <li><tt class="docutils literal"><span class="pre">(TYPE*</span> <span class="pre">IN_ARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2)</span></tt></li> +<li><tt class="docutils literal"><span class="pre">(int</span> <span class="pre">DIM1,</span> <span class="pre">TYPE*</span> <span class="pre">IN_ARRAY1)</span></tt></li> +<li><tt class="docutils literal"><span class="pre">(int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">TYPE*</span> <span class="pre">IN_ARRAY2)</span></tt></li> </ul> </blockquote> </div> @@ -470,6 +495,8 @@ signatures are</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">(TYPE*</span> <span class="pre">INPLACE_ARRAY1,</span> <span class="pre">int</span> <span class="pre">DIM1)</span></tt></li> <li><tt class="docutils literal"><span class="pre">(TYPE*</span> <span class="pre">INPLACE_ARRAY2,</span> <span class="pre">int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2)</span></tt></li> +<li><tt class="docutils literal"><span class="pre">(int</span> <span class="pre">DIM1,</span> <span class="pre">TYPE*</span> <span class="pre">INPLACE_ARRAY1)</span></tt></li> +<li><tt class="docutils literal"><span class="pre">(int</span> <span class="pre">DIM1,</span> <span class="pre">int</span> <span class="pre">DIM2,</span> <span class="pre">TYPE*</span> <span class="pre">INPLACE_ARRAY2)</span></tt></li> </ul> </blockquote> </div> @@ -481,9 +508,9 @@ more than one output variable and the single return argument is therefore not sufficient. In python, the convential way to return multiple arguments is to pack them into a tuple and return the tuple. This is what the argout typemaps do. If a wrapped function that uses -argout these argout typemaps has more than one return argument, they -are so packed. The python user does not pass these arrays in, they -simply get returned. The argout signatures are</p> +these argout typemaps has more than one return argument, they are so +packed. The python user does not pass these arrays in, they simply +get returned. The argout signatures are</p> <blockquote> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">(TYPE</span> <span class="pre">ARGOUT_ARRAY1[ANY])</span></tt></li> @@ -596,7 +623,7 @@ result possible.</p> </div> <div class="footer"> <hr class="footer" /> -Generated on: 2007-03-14 15:24 UTC. +Generated on: 2007-03-14 19:50 UTC. Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source. </div> diff --git a/numpy/doc/swig/numpy_swig.txt b/numpy/doc/swig/numpy_swig.txt index 148c24511..fb8772602 100644 --- a/numpy/doc/swig/numpy_swig.txt +++ b/numpy/doc/swig/numpy_swig.txt @@ -57,9 +57,16 @@ named ``rms.h``. To obtain the python interface discussed above, your `SWIG`_ interface file would need the following:: %{ + #define SWIG_FILE_WITH_INIT #include "rms.h" %} + %include "numpy.i" + + %init %{ + import_array(); + %} + %apply (double* IN_ARRAY1, int DIM1) {(double* seq, int n)}; %include "rms.h" @@ -72,20 +79,31 @@ is an input array of one dimension and that the ``int`` represents that dimension. This is precisely the pattern in the ``rms`` prototype. -Hopefully, no actual prototypes to be wrapped will have the names +Most likely, no actual prototypes to be wrapped will have the names ``IN_ARRAY1`` and ``DIM1``. We use the ``%apply`` directive to apply the typemap for one-dimensional input arrays of type ``double`` to the actual prototype used by ``rms``. Using ``numpy.i`` effectively, therefore, requires knowing what typemaps are available and what they do. +Note that if the C function signature was in a different order:: + + double rms(int n, double* seq); + +that `SWIG`_ would not match the typemap signature given above with +the argument list for ``rms``. Fortunately, ``numpy.i`` has a set of +typemaps with the data pointer given last:: + + %apply (int DIM1, double* IN_ARRAY1) {(int n, double* seq)}; + Using numpy.i ============= The ``numpy.i`` file is currently located in the ``numpy/docs/swig`` sub-directory under the ``numpy`` installation directory. Typically, you will want to copy it to the directory where you are developing -your wrappers. +your wrappers. If it is ever adopted by `SWIG`_ developers, then it +will be installed in a standard place where `SWIG`_ can find it. A simple module that only uses a single `SWIG`_ interface file should include the following:: @@ -98,10 +116,15 @@ include the following:: import_array(); %} -Only one interface file should call ``import_array()``. If you have -more than one `SWIG`_ interface file, then subsequent files should not -``#define SWIG_FILE_WITH_INIT`` and should not call -``import_array()``. +Within a compiled python module, ``import_array()`` should only get +called once. This could be in a C/C++ file that you have written and +is linked to the module. If this is the case, then none of your +interface files should ``#define SWIG_FILE_WITH_INIT`` or call +``import_array()``. Or, this initialization call could be in a +wrapper file generated by `SWIG`_ from an interface file that has the +``%init`` block as above. If this is the case, and you have more than +one `SWIG`_ interface file, then only one interface file should +``#define SWIG_FILE_WITH_INIT`` and call ``import_array()``. Available Typemaps ================== @@ -109,7 +132,7 @@ Available Typemaps The typemap directives provided by ``numpy.i`` for arrays of different data types, say ``double`` and ``int``, are identical to one another except for the C and `NumPy`_ type specifications. The typemaps are -therefore implemented via a macro:: +therefore implemented (typically behind the scenes) via a macro:: %numpy_typemaps(TYPE, TYPECODE) @@ -150,7 +173,7 @@ expansion:: %numpy_typemaps(bool, NPY_UINT) to fix the data length problem, and `Input Arrays`_ will work fine, -but `In-place Arrays`_ will fail type-checking. +but `In-place Arrays`_ might fail type-checking. In the following descriptions, we reference a generic ``TYPE``, which could be any of the C-types listed above. @@ -166,6 +189,8 @@ of array. The input array signatures are * ``(TYPE* IN_ARRAY1, int DIM1)`` * ``(TYPE* IN_ARRAY2, int DIM1, int DIM2)`` + * ``(int DIM1, TYPE* IN_ARRAY1)`` + * ``(int DIM1, int DIM2, TYPE* IN_ARRAY2)`` In-place Arrays --------------- @@ -178,6 +203,8 @@ signatures are * ``(TYPE* INPLACE_ARRAY1, int DIM1)`` * ``(TYPE* INPLACE_ARRAY2, int DIM1, int DIM2)`` + * ``(int DIM1, TYPE* INPLACE_ARRAY1)`` + * ``(int DIM1, int DIM2, TYPE* INPLACE_ARRAY2)`` Argout Arrays ------------- @@ -188,9 +215,9 @@ more than one output variable and the single return argument is therefore not sufficient. In python, the convential way to return multiple arguments is to pack them into a tuple and return the tuple. This is what the argout typemaps do. If a wrapped function that uses -argout these argout typemaps has more than one return argument, they -are so packed. The python user does not pass these arrays in, they -simply get returned. The argout signatures are +these argout typemaps has more than one return argument, they are so +packed. The python user does not pass these arrays in, they simply +get returned. The argout signatures are * ``(TYPE ARGOUT_ARRAY1[ANY])`` * ``(TYPE ARGOUT_ARRAY2[ANY][ANY])`` diff --git a/numpy/doc/swig/series.cxx b/numpy/doc/swig/series.cxx index 9c4285552..9e897619a 100644 --- a/numpy/doc/swig/series.cxx +++ b/numpy/doc/swig/series.cxx @@ -9,6 +9,10 @@ // void SNAMEOnes( TYPE * array, int size); // TYPE SNAMEMax( TYPE * matrix, int rows, int cols); // void SNAMEFloor(TYPE * array, int rows, int cols, TYPE floor); +// TYPE SNAMESum( int size, TYPE * series); +// void SNAMEZeros(int size, TYPE * array); +// TYPE SNAMEMin( int rows, int cols, TYPE * matrix); +// void SNAMECeil( int rows, int cols, TYPE * array, TYPE ceil); // // for any specified type TYPE (for example: short, unsigned int, long // long, etc.) with given short name SNAME (for example: short, uint, @@ -20,6 +24,10 @@ // * 1D in-place arrays // * 2D input arrays // * 2D in-place arrays +// * 1D input arrays, data last +// * 1D in-place arrays, data last +// * 2D input arrays, data last +// * 2D in-place arrays, data last // #define TEST_FUNCS(TYPE, SNAME) \ \ @@ -46,13 +54,45 @@ TYPE SNAME ## Max(TYPE * matrix, int rows, int cols) { \ } \ \ void SNAME ## Floor(TYPE * array, int rows, int cols, TYPE floor) { \ - int i, j, index; \ - for (j=0; j<cols; ++j) { \ - for (i=0; i<rows; ++i) { \ - index = j*rows + i; \ - if (array[index] < floor) array[index] = 0; \ - } \ - } \ + int i, j, index; \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = j*rows + i; \ + if (array[index] < floor) array[index] = floor; \ + } \ + } \ +} \ +\ +TYPE SNAME ## Sum(int size, TYPE * series) { \ + TYPE result = 0; \ + for (int i=0; i<size; ++i) result += series[i]; \ + return result; \ +} \ +\ +void SNAME ## Zeros(int size, TYPE * array) { \ + for (int i=0; i<size; ++i) array[i] = 0; \ +} \ +\ +TYPE SNAME ## Min(int rows, int cols, TYPE * matrix) { \ + int i, j, index; \ + TYPE result = matrix[0]; \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = j*rows + i; \ + if (matrix[index] < result) result = matrix[index]; \ + } \ + } \ + return result; \ +} \ +\ +void SNAME ## Ceil(int rows, int cols, TYPE * array, TYPE ceil) { \ + int i, j, index; \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = j*rows + i; \ + if (array[index] > ceil) array[index] = ceil; \ + } \ + } \ } TEST_FUNCS(signed char , schar ) @@ -67,4 +107,3 @@ TEST_FUNCS(long long , longLong ) TEST_FUNCS(unsigned long long, ulongLong ) TEST_FUNCS(float , float ) TEST_FUNCS(double , double ) -TEST_FUNCS(long double , longDouble) diff --git a/numpy/doc/swig/series.h b/numpy/doc/swig/series.h index 141f07bb4..c9a8b9cb5 100644 --- a/numpy/doc/swig/series.h +++ b/numpy/doc/swig/series.h @@ -8,6 +8,10 @@ // void SNAMEOnes( TYPE * array, int size); // TYPE SNAMEMax( TYPE * matrix, int rows, int cols); // void SNAMEFloor(TYPE * array, int rows, int cols, TYPE floor); +// TYPE SNAMESum( int size, TYPE * series); +// void SNAMEZeros(int size, TYPE * array); +// TYPE SNAMEMin( int rows, int cols, TYPE * matrix); +// void SNAMECeil( int rows, int cols, TYPE * array, TYPE ceil); // // for any specified type TYPE (for example: short, unsigned int, long // long, etc.) with given short name SNAME (for example: short, uint, @@ -19,13 +23,21 @@ // * 1D in-place arrays // * 2D input arrays // * 2D in-place arrays +// * 1D input arrays, data last +// * 1D in-place arrays, data last +// * 2D input arrays, data last +// * 2D in-place arrays, data last // #define TEST_FUNC_PROTOS(TYPE, SNAME) \ \ TYPE SNAME ## Prod( TYPE * series, int size); \ void SNAME ## Ones( TYPE * array, int size); \ TYPE SNAME ## Max( TYPE * matrix, int rows, int cols); \ -void SNAME ## Floor(TYPE * array, int rows, int cols, TYPE floor); +void SNAME ## Floor(TYPE * array, int rows, int cols, TYPE floor); \ +TYPE SNAME ## Sum( int size, TYPE * series); \ +void SNAME ## Zeros(int size, TYPE * array); \ +TYPE SNAME ## Min( int rows, int cols, TYPE * matrix); \ +void SNAME ## Ceil( int rows, int cols, TYPE * array, TYPE ceil); TEST_FUNC_PROTOS(signed char , schar ) TEST_FUNC_PROTOS(unsigned char , uchar ) @@ -39,6 +51,5 @@ TEST_FUNC_PROTOS(long long , longLong ) TEST_FUNC_PROTOS(unsigned long long, ulongLong ) TEST_FUNC_PROTOS(float , float ) TEST_FUNC_PROTOS(double , double ) -TEST_FUNC_PROTOS(long double , longDouble) #endif diff --git a/numpy/doc/swig/testSeries.py b/numpy/doc/swig/testSeries.py index 9189e9a87..f81bd0fa1 100755 --- a/numpy/doc/swig/testSeries.py +++ b/numpy/doc/swig/testSeries.py @@ -25,450 +25,682 @@ class SeriesTestCase(unittest.TestCase): #################################################### ### Test functions that take arrays of type BYTE ### def testScharProd(self): - "Test the scharProd function" + "Test scharProd function" self.assertEquals(Series.scharProd([1,2,3,4]), 24) def testScharProdNonContainer(self): - "Test the scharProd function with None" + "Test scharProd function with None" self.assertRaises(TypeError, Series.scharProd, None) def testScharOnes(self): - "Test the scharOnes function" + "Test scharOnes function" myArray = N.zeros(5,'b') Series.scharOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testScharMax(self): - "Test the scharMax function" + "Test scharMax function" matrix = [[-6,5,-4],[3,-2,1]] self.assertEquals(Series.scharMax(matrix), 5) def testScharMaxNonContainer(self): - "Test the scharMax function with None" + "Test scharMax function with None" self.assertRaises(TypeError, Series.scharMax, None) def testScharMaxWrongDim(self): - "Test the scharMax function with a 1D array" + "Test scharMax function with a 1D array" self.assertRaises(TypeError, Series.scharMax, [0, -1, 2, -3]) def testScharFloor(self): - "Test the scharFloor function" + "Test scharFloor function" matrix = N.array([[10,-2],[-6,7]],'b') Series.scharFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testScharSum(self): + "Test scharSum function" + self.assertEquals(Series.scharSum([-5,6,-7,8]), 2) + + def testScharZeros(self): + "Test scharZeros function" + myArray = N.ones(5,'b') + Series.scharZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testScharMin(self): + "Test scharMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.scharMin(matrix), 4) + + def testScharCeil(self): + "Test scharCeil function" + matrix = N.array([[10,-2],[-6,7]],'b') + Series.scharCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + ##################################################### ### Test functions that take arrays of type UBYTE ### def testUcharProd(self): - "Test the ucharProd function" + "Test ucharProd function" self.assertEquals(Series.ucharProd([1,2,3,4]), 24) def testUcharProdNonContainer(self): - "Test the ucharProd function with None" + "Test ucharProd function with None" self.assertRaises(TypeError, Series.ucharProd, None) def testUcharOnes(self): - "Test the ucharOnes function" + "Test ucharOnes function" myArray = N.zeros(5,'B') Series.ucharOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testUcharMax(self): - "Test the ucharMax function" + "Test ucharMax function" matrix = [[6,5,4],[3,2,1]] self.assertEquals(Series.ucharMax(matrix), 6) def testUcharMaxNonContainer(self): - "Test the ucharMax function with None" + "Test ucharMax function with None" self.assertRaises(TypeError, Series.ucharMax, None) def testUcharMaxWrongDim(self): - "Test the ucharMax function with a 1D array" + "Test ucharMax function with a 1D array" self.assertRaises(TypeError, Series.ucharMax, [0, 1, 2, 3]) def testUcharFloor(self): - "Test the ucharFloor function" + "Test ucharFloor function" matrix = N.array([[10,2],[6,7]],'B') Series.ucharFloor(matrix,7) - N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + N.testing.assert_array_equal(matrix, N.array([[10,7],[7,7]])) + + def testUcharSum(self): + "Test ucharSum function" + self.assertEquals(Series.ucharSum([5,6,7,8]), 26) + + def testUcharZeros(self): + "Test ucharZeros function" + myArray = N.ones(5,'B') + Series.ucharZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testUcharMin(self): + "Test ucharMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.ucharMin(matrix), 4) + + def testUcharCeil(self): + "Test ucharCeil function" + matrix = N.array([[10,2],[6,7]],'B') + Series.ucharCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,2],[5,5]])) ##################################################### ### Test functions that take arrays of type SHORT ### def testShortProd(self): - "Test the shortProd function" + "Test shortProd function" self.assertEquals(Series.shortProd([1,2,3,4]), 24) def testShortProdNonContainer(self): - "Test the shortProd function with None" + "Test shortProd function with None" self.assertRaises(TypeError, Series.shortProd, None) def testShortOnes(self): - "Test the shortOnes function" + "Test shortOnes function" myArray = N.zeros(5,'h') Series.shortOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testShortMax(self): - "Test the shortMax function" + "Test shortMax function" matrix = [[-6,5,-4],[3,-2,1]] self.assertEquals(Series.shortMax(matrix), 5) def testShortMaxNonContainer(self): - "Test the shortMax function with None" + "Test shortMax function with None" self.assertRaises(TypeError, Series.shortMax, None) def testShortMaxWrongDim(self): - "Test the shortMax function with a 1D array" + "Test shortMax function with a 1D array" self.assertRaises(TypeError, Series.shortMax, [0, -1, 2, -3]) def testShortFloor(self): - "Test the shortFloor function" + "Test shortFloor function" matrix = N.array([[10,-2],[-6,7]],'h') Series.shortFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testShortSum(self): + "Test shortSum function" + self.assertEquals(Series.shortSum([-5,6,-7,8]), 2) + + def testShortZeros(self): + "Test shortZeros function" + myArray = N.ones(5,'h') + Series.shortZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testShortMin(self): + "Test shortMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.shortMin(matrix), 4) + + def testShortCeil(self): + "Test shortCeil function" + matrix = N.array([[10,-2],[-6,7]],'h') + Series.shortCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + ###################################################### ### Test functions that take arrays of type USHORT ### def testUshortProd(self): - "Test the ushortProd function" + "Test ushortProd function" self.assertEquals(Series.ushortProd([1,2,3,4]), 24) def testUshortProdNonContainer(self): - "Test the ushortProd function with None" + "Test ushortProd function with None" self.assertRaises(TypeError, Series.ushortProd, None) def testUshortOnes(self): - "Test the ushortOnes function" + "Test ushortOnes function" myArray = N.zeros(5,'H') Series.ushortOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testUshortMax(self): - "Test the ushortMax function" + "Test ushortMax function" matrix = [[6,5,4],[3,2,1]] self.assertEquals(Series.ushortMax(matrix), 6) def testUshortMaxNonContainer(self): - "Test the ushortMax function with None" + "Test ushortMax function with None" self.assertRaises(TypeError, Series.ushortMax, None) def testUshortMaxWrongDim(self): - "Test the ushortMax function with a 1D array" + "Test ushortMax function with a 1D array" self.assertRaises(TypeError, Series.ushortMax, [0, 1, 2, 3]) def testUshortFloor(self): - "Test the ushortFloor function" + "Test ushortFloor function" matrix = N.array([[10,2],[6,7]],'H') Series.ushortFloor(matrix,7) - N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + N.testing.assert_array_equal(matrix, N.array([[10,7],[7,7]])) + + def testUshortSum(self): + "Test ushortSum function" + self.assertEquals(Series.ushortSum([5,6,7,8]), 26) + + def testUshortZeros(self): + "Test ushortZeros function" + myArray = N.ones(5,'H') + Series.ushortZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testUshortMin(self): + "Test ushortMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.ushortMin(matrix), 4) + + def testUshortCeil(self): + "Test ushortCeil function" + matrix = N.array([[10,2],[6,7]],'H') + Series.ushortCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,2],[5,5]])) ################################################### ### Test functions that take arrays of type INT ### def testIntProd(self): - "Test the intProd function" + "Test intProd function" self.assertEquals(Series.intProd([1,2,3,4]), 24) def testIntProdNonContainer(self): - "Test the intProd function with None" + "Test intProd function with None" self.assertRaises(TypeError, Series.intProd, None) def testIntOnes(self): - "Test the intOnes function" + "Test intOnes function" myArray = N.zeros(5,'i') Series.intOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testIntMax(self): - "Test the intMax function" + "Test intMax function" matrix = [[-6,5,-4],[3,-2,1]] self.assertEquals(Series.intMax(matrix), 5) def testIntMaxNonContainer(self): - "Test the intMax function with None" + "Test intMax function with None" self.assertRaises(TypeError, Series.intMax, None) def testIntMaxWrongDim(self): - "Test the intMax function with a 1D array" + "Test intMax function with a 1D array" self.assertRaises(TypeError, Series.intMax, [0, -1, 2, -3]) def testIntFloor(self): - "Test the intFloor function" + "Test intFloor function" matrix = N.array([[10,-2],[-6,7]],'i') Series.intFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testIntSum(self): + "Test intSum function" + self.assertEquals(Series.intSum([-5,6,-7,8]), 2) + + def testIntZeros(self): + "Test intZeros function" + myArray = N.ones(5,'i') + Series.intZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testIntMin(self): + "Test intMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.intMin(matrix), 4) + + def testIntCeil(self): + "Test intCeil function" + matrix = N.array([[10,-2],[-6,7]],'i') + Series.intCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + #################################################### ### Test functions that take arrays of type UINT ### def testUintProd(self): - "Test the uintProd function" + "Test uintProd function" self.assertEquals(Series.uintProd([1,2,3,4]), 24) def testUintProdNonContainer(self): - "Test the uintProd function with None" + "Test uintProd function with None" self.assertRaises(TypeError, Series.uintProd, None) def testUintOnes(self): - "Test the uintOnes function" + "Test uintOnes function" myArray = N.zeros(5,'I') Series.uintOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testUintMax(self): - "Test the uintMax function" + "Test uintMax function" matrix = [[6,5,4],[3,2,1]] self.assertEquals(Series.uintMax(matrix), 6) def testUintMaxNonContainer(self): - "Test the uintMax function with None" + "Test uintMax function with None" self.assertRaises(TypeError, Series.uintMax, None) def testUintMaxWrongDim(self): - "Test the uintMax function with a 1D array" + "Test uintMax function with a 1D array" self.assertRaises(TypeError, Series.uintMax, [0, 1, 2, 3]) def testUintFloor(self): - "Test the uintFloor function" + "Test uintFloor function" matrix = N.array([[10,2],[6,7]],'I') Series.uintFloor(matrix,7) - N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + N.testing.assert_array_equal(matrix, N.array([[10,7],[7,7]])) + + def testUintSum(self): + "Test uintSum function" + self.assertEquals(Series.uintSum([5,6,7,8]), 26) + + def testUintZeros(self): + "Test uintZeros function" + myArray = N.ones(5,'I') + Series.uintZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testUintMin(self): + "Test uintMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.uintMin(matrix), 4) + + def testUintCeil(self): + "Test uintCeil function" + matrix = N.array([[10,2],[6,7]],'I') + Series.uintCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,2],[5,5]])) #################################################### ### Test functions that take arrays of type LONG ### def testLongProd(self): - "Test the longProd function" + "Test longProd function" self.assertEquals(Series.longProd([1,2,3,4]), 24) def testLongProdNonContainer(self): - "Test the longProd function with None" + "Test longProd function with None" self.assertRaises(TypeError, Series.longProd, None) def testLongOnes(self): - "Test the longOnes function" + "Test longOnes function" myArray = N.zeros(5,'l') Series.longOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testLongMax(self): - "Test the longMax function" + "Test longMax function" matrix = [[-6,5,-4],[3,-2,1]] self.assertEquals(Series.longMax(matrix), 5) def testLongMaxNonContainer(self): - "Test the longMax function with None" + "Test longMax function with None" self.assertRaises(TypeError, Series.longMax, None) def testLongMaxWrongDim(self): - "Test the longMax function with a 1D array" + "Test longMax function with a 1D array" self.assertRaises(TypeError, Series.longMax, [0, -1, 2, -3]) def testLongFloor(self): - "Test the longFloor function" + "Test longFloor function" matrix = N.array([[10,-2],[-6,7]],'l') Series.longFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testLongSum(self): + "Test longSum function" + self.assertEquals(Series.longSum([-5,6,-7,8]), 2) + + def testLongZeros(self): + "Test longZeros function" + myArray = N.ones(5,'l') + Series.longZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testLongMin(self): + "Test longMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.longMin(matrix), 4) + + def testLongCeil(self): + "Test longCeil function" + matrix = N.array([[10,-2],[-6,7]],'l') + Series.longCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + ##################################################### ### Test functions that take arrays of type ULONG ### def testUlongProd(self): - "Test the ulongProd function" + "Test ulongProd function" self.assertEquals(Series.ulongProd([1,2,3,4]), 24) def testUlongProdNonContainer(self): - "Test the ulongProd function with None" + "Test ulongProd function with None" self.assertRaises(TypeError, Series.ulongProd, None) def testUlongOnes(self): - "Test the ulongOnes function" + "Test ulongOnes function" myArray = N.zeros(5,'L') Series.ulongOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testUlongMax(self): - "Test the ulongMax function" + "Test ulongMax function" matrix = [[6,5,4],[3,2,1]] self.assertEquals(Series.ulongMax(matrix), 6) def testUlongMaxNonContainer(self): - "Test the ulongMax function with None" + "Test ulongMax function with None" self.assertRaises(TypeError, Series.ulongMax, None) def testUlongMaxWrongDim(self): - "Test the ulongMax function with a 1D array" + "Test ulongMax function with a 1D array" self.assertRaises(TypeError, Series.ulongMax, [0, 1, 2, 3]) def testUlongFloor(self): - "Test the ulongFloor function" + "Test ulongFloor function" matrix = N.array([[10,2],[6,7]],'L') Series.ulongFloor(matrix,7) - N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + N.testing.assert_array_equal(matrix, N.array([[10,7],[7,7]])) + + def testUlongSum(self): + "Test ulongSum function" + self.assertEquals(Series.ulongSum([5,6,7,8]), 26) + + def testUlongZeros(self): + "Test ulongZeros function" + myArray = N.ones(5,'L') + Series.ulongZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testUlongMin(self): + "Test ulongMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.ulongMin(matrix), 4) + + def testUlongCeil(self): + "Test ulongCeil function" + matrix = N.array([[10,2],[6,7]],'L') + Series.ulongCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,2],[5,5]])) ######################################################## ### Test functions that take arrays of type LONGLONG ### def testLongLongProd(self): - "Test the longLongProd function" + "Test longLongProd function" self.assertEquals(Series.longLongProd([1,2,3,4]), 24) def testLongLongProdNonContainer(self): - "Test the longLongProd function with None" + "Test longLongProd function with None" self.assertRaises(TypeError, Series.longLongProd, None) def testLongLongOnes(self): - "Test the longLongOnes function" + "Test longLongOnes function" myArray = N.zeros(5,'q') Series.longLongOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testLongLongMax(self): - "Test the longLongMax function" + "Test longLongMax function" matrix = [[-6,5,-4],[3,-2,1]] self.assertEquals(Series.longLongMax(matrix), 5) def testLongLongMaxNonContainer(self): - "Test the longLongMax function with None" + "Test longLongMax function with None" self.assertRaises(TypeError, Series.longLongMax, None) def testLongLongMaxWrongDim(self): - "Test the longLongMax function with a 1D array" + "Test longLongMax function with a 1D array" self.assertRaises(TypeError, Series.longLongMax, [0, -1, 2, -3]) def testLongLongFloor(self): - "Test the longLongFloor function" + "Test longLongFloor function" matrix = N.array([[10,-2],[-6,7]],'q') Series.longLongFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testLongLongSum(self): + "Test longLongSum function" + self.assertEquals(Series.longLongSum([-5,6,-7,8]), 2) + + def testLongLongZeros(self): + "Test longLongZeros function" + myArray = N.ones(5,'q') + Series.longLongZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testLongLongMin(self): + "Test longLongMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.longLongMin(matrix), 4) + + def testLongLongCeil(self): + "Test longLongCeil function" + matrix = N.array([[10,-2],[-6,7]],'q') + Series.longLongCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + ######################################################### ### Test functions that take arrays of type ULONGLONG ### def testUlonglongProd(self): - "Test the ulongLongProd function" + "Test ulongLongProd function" self.assertEquals(Series.ulongLongProd([1,2,3,4]), 24) def testUlongLongProdNonContainer(self): - "Test the ulongLongProd function with None" + "Test ulongLongProd function with None" self.assertRaises(TypeError, Series.ulongLongProd, None) def testUlongLongOnes(self): - "Test the ulongLongOnes function" + "Test ulongLongOnes function" myArray = N.zeros(5,'Q') Series.ulongLongOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1])) def testUlongLongMax(self): - "Test the ulongLongMax function" + "Test ulongLongMax function" matrix = [[6,5,4],[3,2,1]] self.assertEquals(Series.ulongLongMax(matrix), 6) def testUlongLongMaxNonContainer(self): - "Test the ulongLongMax function with None" + "Test ulongLongMax function with None" self.assertRaises(TypeError, Series.ulongLongMax, None) def testUlongLongMaxWrongDim(self): - "Test the ulongLongMax function with a 1D array" + "Test ulongLongMax function with a 1D array" self.assertRaises(TypeError, Series.ulongLongMax, [0, 1, 2, 3]) def testUlongLongFloor(self): - "Test the ulongLongFloor function" + "Test ulongLongFloor function" matrix = N.array([[10,2],[6,7]],'Q') Series.ulongLongFloor(matrix,7) - N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + N.testing.assert_array_equal(matrix, N.array([[10,7],[7,7]])) + + def testUlongLongSum(self): + "Test ulongLongSum function" + self.assertEquals(Series.ulongLongSum([5,6,7,8]), 26) + + def testUlongLongZeros(self): + "Test ulongLongZeros function" + myArray = N.ones(5,'Q') + Series.ulongLongZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testUlongLongMin(self): + "Test ulongLongMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.ulongLongMin(matrix), 4) + + def testUlongLongCeil(self): + "Test ulongLongCeil function" + matrix = N.array([[10,2],[6,7]],'Q') + Series.ulongLongCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,2],[5,5]])) ##################################################### ### Test functions that take arrays of type FLOAT ### def testFloatProd(self): - "Test the floatProd function (to 5 decimal places)" + "Test floatProd function (to 5 decimal places)" self.assertAlmostEquals(Series.floatProd((1,2.718,3,4)), 32.616, 5) def testFloatProdBadContainer(self): - "Test the floatProd function with an invalid list" + "Test floatProd function with an invalid list" self.assertRaises(BadListError, Series.floatProd, [3.14, "pi"]) def testFloatOnes(self): - "Test the floatOnes function" + "Test floatOnes function" myArray = N.zeros(5,'f') Series.floatOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1.,1.,1.,1.,1.])) def testFloatOnesNonArray(self): - "Test the floatOnes function with a list" + "Test floatOnes function with a list" self.assertRaises(TypeError, Series.floatOnes, [True, 0, 2.718, "pi"]) def testFloatMax(self): - "Test the floatMax function" + "Test floatMax function" matrix = [[-6,5,-4],[3.14,-2.718,1]] self.assertEquals(Series.floatMax(matrix), 5.0) def testFloatMaxNonContainer(self): - "Test the floatMax function with None" + "Test floatMax function with None" self.assertRaises(TypeError, Series.floatMax, None) def testFloatMaxWrongDim(self): - "Test the floatMax function with a 1D array" + "Test floatMax function with a 1D array" self.assertRaises(TypeError, Series.floatMax, [0.0, -1, 2.718, -3.14]) def testFloatFloor(self): - "Test the floatFloor function" + "Test floatFloor function" matrix = N.array([[10,-2],[-6,7]],'f') Series.floatFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) + def testFloatSum(self): + "Test floatSum function" + self.assertEquals(Series.floatSum([-5,6,-7,8]), 2) + + def testFloatZeros(self): + "Test floatZeros function" + myArray = N.ones(5,'f') + Series.floatZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testFloatMin(self): + "Test floatMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.floatMin(matrix), 4) + + def testFloatCeil(self): + "Test floatCeil function" + matrix = N.array([[10,-2],[-6,7]],'f') + Series.floatCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) + ###################################################### ### Test functions that take arrays of type DOUBLE ### def testDoubleProd(self): - "Test the doubleProd function" + "Test doubleProd function" self.assertEquals(Series.doubleProd((1,2.718,3,4)), 32.616) def testDoubleProdBadContainer(self): - "Test the doubleProd function with an invalid list" + "Test doubleProd function with an invalid list" self.assertRaises(BadListError, Series.doubleProd, [3.14, "pi"]) def testDoubleOnes(self): - "Test the doubleOnes function" + "Test doubleOnes function" myArray = N.zeros(5,'d') Series.doubleOnes(myArray) N.testing.assert_array_equal(myArray, N.array([1.,1.,1.,1.,1.])) def testDoubleOnesNonArray(self): - "Test the doubleOnes function with a list" + "Test doubleOnes function with a list" self.assertRaises(TypeError, Series.doubleOnes, [True, 0, 2.718, "pi"]) def testDoubleMax(self): - "Test the doubleMax function" + "Test doubleMax function" matrix = [[-6,5,-4],[3.14,-2.718,1]] self.assertEquals(Series.doubleMax(matrix), 5.0) def testDoubleMaxNonContainer(self): - "Test the doubleMax function with None" + "Test doubleMax function with None" self.assertRaises(TypeError, Series.doubleMax, None) def testDoubleMaxWrongDim(self): - "Test the doubleMax function with a 1D array" + "Test doubleMax function with a 1D array" self.assertRaises(TypeError, Series.doubleMax, [0.0, -1, 2.718, -3.14]) def testDoubleFloor(self): - "Test the doubleFloor function" + "Test doubleFloor function" matrix = N.array([[10,-2],[-6,7]],'d') Series.doubleFloor(matrix,0) N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]])) - # ########################################################## - # ### Test functions that take arrays of type LONGDOUBLE ### - # def testLongDoubleProd(self): - # "Test the longDoubleProd function" - # self.assertEquals(Series.longDoubleProd((1,2.718,3,4)), 32.616) - - # def testLongDoubleProdBadContainer(self): - # "Test the longDoubleProd function with an invalid list" - # self.assertRaises(BadListError, Series.longDoubleProd, [3.14, "pi"]) - - # def testLongDoubleOnes(self): - # "Test the longDoubleOnes function" - # myArray = N.zeros(5,'g') - # Series.longDoubleOnes(myArray) - # N.testing.assert_array_equal(myArray, N.array([1.,1.,1.,1.,1.])) - - # def testLongDoubleOnesNonArray(self): - # "Test the longDoubleOnes function with a list" - # self.assertRaises(TypeError, Series.longDoubleOnes, [True, 0, 2.718, "pi"]) + def testDoubleSum(self): + "Test doubleSum function" + self.assertEquals(Series.doubleSum([-5,6,-7,8]), 2) + + def testDoubleZeros(self): + "Test doubleZeros function" + myArray = N.ones(5,'d') + Series.doubleZeros(myArray) + N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0])) + + def testDoubleMin(self): + "Test doubleMin function" + matrix = [[9,8],[7,6],[5,4]] + self.assertEquals(Series.doubleMin(matrix), 4) + + def testDoubleCeil(self): + "Test doubleCeil function" + matrix = N.array([[10,-2],[-6,7]],'d') + Series.doubleCeil(matrix,5) + N.testing.assert_array_equal(matrix, N.array([[5,-2],[-6,5]])) ###################################################################### |