diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/include/numpy/npy_math.h | 4 | ||||
-rw-r--r-- | numpy/core/setup.py | 3 | ||||
-rw-r--r-- | numpy/core/src/npymath/npy_math_complex.c.src | 72 | ||||
-rw-r--r-- | numpy/core/src/npymath/npy_math_private.h | 57 |
4 files changed, 134 insertions, 2 deletions
diff --git a/numpy/core/include/numpy/npy_math.h b/numpy/core/include/numpy/npy_math.h index 0e82c833a..9b389f708 100644 --- a/numpy/core/include/numpy/npy_math.h +++ b/numpy/core/include/numpy/npy_math.h @@ -279,4 +279,8 @@ npy_longdouble npy_logaddexp2l(npy_longdouble x, npy_longdouble y); #define npy_radiansf npy_deg2radf #define npy_radiansl npy_deg2radl +/* + * Complex declarations + */ + #endif diff --git a/numpy/core/setup.py b/numpy/core/setup.py index e113645c7..722450026 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -601,7 +601,8 @@ def configuration(parent_package='',top_path=None): config.add_installed_library('npymath', sources=[join('src', 'npymath', 'npy_math.c.src'), join('src', 'npymath', 'ieee754.c'), - get_mathlib_info], + join('src', 'npymath', 'npy_math_complex.c.src', + get_mathlib_info)], install_dir='lib') config.add_npy_pkg_config("npymath.ini.in", "lib/npy-pkg-config", subst_dict) diff --git a/numpy/core/src/npymath/npy_math_complex.c.src b/numpy/core/src/npymath/npy_math_complex.c.src new file mode 100644 index 000000000..51ed77539 --- /dev/null +++ b/numpy/core/src/npymath/npy_math_complex.c.src @@ -0,0 +1,72 @@ +/* + * Implement some C99-compatible complex math functions + * + * Most of the code is taken from the msun library in FreeBSD (HEAD @ 30th June + * 2009), under the following license: + * + * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#include <Python.h> +#include <math.h> + +#include "config.h" +#include "numpy/npy_math.h" + +#include "npy_math_private.h" + +#ifndef HAVE_CREAL +static inline double creal(complex z) +{ + return REALPART(z); +} +#endif + +#ifndef HAVE_CIMAG +static inline double cimag(complex z) +{ + return IMAGPART(z); +} +#endif + +#ifndef HAVE_CABS +static inline double cabs(complex z) +{ + return nypy_hypot(npy_creal(z), npy_cimag(z)); +} +#endif + +/* + * Decorate all the functions: those are the public ones + */ + +/**begin repeat + * #kind = creal,cimag,cabs# + */ +double npy_@kind@(npy_complex_double z) +{ + _double_complex t = {z}; + return @kind@(t.c99); +} +/**end repeat**/ diff --git a/numpy/core/src/npymath/npy_math_private.h b/numpy/core/src/npymath/npy_math_private.h index 04c3221cb..882dce3d2 100644 --- a/numpy/core/src/npymath/npy_math_private.h +++ b/numpy/core/src/npymath/npy_math_private.h @@ -18,7 +18,12 @@ #ifndef _NPY_MATH_PRIVATE_H_ #define _NPY_MATH_PRIVATE_H_ -#include <numpy/npy_endian.h> +#include <Python.h> +#include <math.h> + +//#include "config.h" +#include "numpy/npy_math.h" +#include "numpy/npy_endian.h" /* * The original fdlibm code used statements like: @@ -118,4 +123,54 @@ do { \ (d) = sl_u.value; \ } while (0) +/* + * C99 specifies that complex numbers have the same representation as + * an array of two elements, where the first element is the real part + * and the second element is the imaginary part. + */ +#ifdef HAVE_COMPLEX_H +#include "complex.h" +typedef complex npy_complex_double; +typedef union { + npy_complex_double npy; + complex c99; +} _double_complex; +#else +typedef struct { + double x, y; +} npy_complex_double; +typedef union { + npy_complex_double npy; + npy_complex_double c99; +} _double_complex; +#endif + +#if 0 +typedef union { + npy_complex_float f; + float a[2]; +} _float_complex; +#endif +#if 0 +typedef union { + npy_complex_longdouble f; + npy_longdouble a[2]; +} _long_double_complex; +#endif + +/* those can be used as lvalues */ +#define REALPART(z) ((z).a[0]) +#define IMAGPART(z) ((z).a[1]) + +static inline npy_complex_double cpack(double x, double y) +{ + _double_complex z; + +#if 0 + REALPART(z.npy_z) = x; + IMAGPART(z.npy_z) = y; +#endif + return z.npy; +} + #endif /* !_NPY_MATH_PRIVATE_H_ */ |