diff options
25 files changed, 1394 insertions, 589 deletions
diff --git a/.gitignore b/.gitignore index 8585f3096..a98742ffe 100644 --- a/.gitignore +++ b/.gitignore @@ -62,7 +62,7 @@ MANIFEST # Paver generated files # ######################### -release/ +/release # Logs and databases # ###################### diff --git a/.travis.yml b/.travis.yml index e3829dea0..38b1ec0c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,9 @@ before_install: - pip install nose # pip install coverage - python -V + - sudo apt-get install -qq libatlas-dev libatlas-base-dev - popd + install: # We used to use 'setup.py install' here, but that has the terrible # behaviour that if a copy of the package is already installed in diff --git a/doc/neps/ufunc-overrides.rst b/doc/neps/ufunc-overrides.rst new file mode 100644 index 000000000..1c0ab1c78 --- /dev/null +++ b/doc/neps/ufunc-overrides.rst @@ -0,0 +1,242 @@ +================================= +A Mechanism for Overriding Ufuncs +================================= + +:Author: Blake Griffith +:Contact: blake.g@utexa.edu +:Date: 2013-07-10 + +:Author: Pauli Virtanen + +:Author: Nathaniel Smith + + +Executive summary +================= + +NumPy's universal functions (ufuncs) currently have some limited +functionality for operating on user defined subclasses of ndarray using +``__array_prepare__`` and ``__array_wrap__`` [1]_, and there is little +to no support for arbitrary objects. e.g. SciPy's sparse matrices [2]_ +[3]_. + +Here we propose adding a mechanism to override ufuncs based on the ufunc +checking each of it's arguments for a ``__numpy_ufunc__`` method. +On discovery of ``__numpy_ufunc__`` the ufunc will hand off the +operation to the method. + +This covers some of the same ground as Travis Oliphant's proposal to +retro-fit NumPy with multi-methods [4]_, which would solve the same +problem. The mechanism here follows more closely the way Python enables +classes to override ``__mul__`` and other binary operations. + +.. [1] http://docs.scipy.org/doc/numpy/user/basics.subclassing.html +.. [2] https://github.com/scipy/scipy/issues/2123 +.. [3] https://github.com/scipy/scipy/issues/1569 +.. [4] http://technicaldiscovery.blogspot.com/2013/07/thoughts-after-scipy-2013-and-specific.html + + +Motivation +========== + +The current machinery for dispatching Ufuncs is generally agreed to be +insufficient. There have been lengthy discussions and other proposed +solutions [5]_. + +Using ufuncs with subclasses of ndarray is limited to ``__array_prepare__`` and +``__array_wrap__`` to prepare the arguments, but these don't allow you to for +example change the shape or the data of the arguments. Trying to ufunc things +that don't subclass ndarray is even more difficult, as the input arguments tend +to be cast to object arrays, which ends up producing surprising results. + +Take this example of ufuncs interoperability with sparse matrices.:: + + In [1]: import numpy as np + import scipy.sparse as sp + + a = np.random.randint(5, size=(3,3)) + b = np.random.randint(5, size=(3,3)) + + asp = sp.csr_matrix(a) + bsp = sp.csr_matrix(b) + + In [2]: a, b + Out[2]:(array([[0, 4, 4], + [1, 3, 2], + [1, 3, 1]]), + array([[0, 1, 0], + [0, 0, 1], + [4, 0, 1]])) + + In [3]: np.multiply(a, b) # The right answer + Out[3]: array([[0, 4, 0], + [0, 0, 2], + [4, 0, 1]]) + + In [4]: np.multiply(asp, bsp).todense() # calls __mul__ which does matrix multi + Out[4]: matrix([[16, 0, 8], + [ 8, 1, 5], + [ 4, 1, 4]], dtype=int64) + + In [5]: np.multiply(a, bsp) # Returns NotImplemented to user, bad! + Out[5]: NotImplemted + +Returning ``NotImplemented`` to user should not happen. Moreover:: + + In [6]: np.multiply(asp, b) + Out[6]: array([[ <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>], + [ <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>], + [ <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>, + <3x3 sparse matrix of type '<class 'numpy.int64'>' + with 8 stored elements in Compressed Sparse Row format>]], dtype=object) + +Here, it appears that the sparse matrix was converted to a object array +scalar, which was then multiplied with all elements of the ``b`` array. +However, this behavior is more confusing than useful, and having a +``TypeError`` would be preferable. + +Adding the ``__numpy_ufunc__`` functionality fixes this and would +deprecate the other ufunc modifying functions. + +.. [5] http://mail.scipy.org/pipermail/numpy-discussion/2011-June/056945.html + + +Proposed interface +================== + +Objects that want to override Ufuncs can define a ``__numpy_ufunc__`` method. +The method signature is:: + + def __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs) + +Here: + +- *ufunc* is the ufunc object that was called. +- *method* is a string indicating which Ufunc method was called + (one of ``"__call__"``, ``"reduce"``, ``"reduceat"``, + ``"accumulate"``, ``"outer"``, ``"inner"``). +- *i* is the index of *self* in *inputs*. +- *inputs* is a tuple of the input arguments to the ``ufunc`` +- *kwargs* are the keyword arguments passed to the function. The ``out`` + argument is always contained in *kwargs*, if given. + +The ufunc's arguments are first normalized into a tuple of input data +(``inputs``), and dict of keyword arguments. If the output argument is +passed as a positional argument it is moved to the keyword argmunets. + +The function dispatch proceeds as follows: + +- If one of the input arguments implements ``__numpy_ufunc__`` it is + executed instead of the Ufunc. + +- If more than one of the input arguments implements ``__numpy_ufunc__``, + they are tried in the following order: subclasses before superclasses, + otherwise left to right. The first ``__numpy_ufunc__`` method returning + something else than ``NotImplemented`` determines the return value of + the Ufunc. + +- If all ``__numpy_ufunc__`` methods of the input arguments return + ``NotImplemented``, a ``TypeError`` is raised. + +- If a ``__numpy_ufunc__`` method raises an error, the error is propagated + immediately. + +If none of the input arguments has a ``__numpy_ufunc__`` method, the +execution falls back on the default ufunc behaviour. + + +Demo +==== + +A pull request[6]_ has been made including the changes proposed in this NEP. +Here is a demo highlighting the functionality.:: + + In [1]: import numpy as np; + + In [2]: a = np.array([1]) + + In [3]: class B(): + ...: def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + ...: return "B" + ...: + + In [4]: b = B() + + In [5]: np.dot(a, b) + Out[5]: 'B' + + In [6]: np.multiply(a, b) + Out[6]: 'B' + +A simple ``__numpy_ufunc__`` has been added to SciPy's sparse matrices +Currently this only handles ``np.dot`` and ``np.multiply`` because it was the +two most common cases where users would attempt to use sparse matrices with ufuncs. +The method is defined below:: + + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + """Method for compatibility with NumPy's ufuncs and dot + functions. + """ + + without_self = list(inputs) + del without_self[pos] + without_self = tuple(without_self) + + if func == np.multiply: + return self.multiply(*without_self) + + elif func == np.dot: + if pos == 0: + return self.__mul__(inputs[1]) + if pos == 1: + return self.__rmul__(inputs[0]) + else: + return NotImplemented + +So we now get the expected behavior when using ufuncs with sparse matrices.:: + + In [1]: import numpy as np; import scipy.sparse as sp + + In [2]: a = np.random.randint(3, size=(3,3)) + + In [3]: b = np.random.randint(3, size=(3,3)) + + In [4]: asp = sp.csr_matrix(a); bsp = sp.csr_matrix(b) + + In [5]: np.dot(a,b) + Out[5]: + array([[2, 4, 8], + [2, 4, 8], + [2, 2, 3]]) + + In [6]: np.dot(asp,b) + Out[6]: + array([[2, 4, 8], + [2, 4, 8], + [2, 2, 3]], dtype=int64) + + In [7]: np.dot(asp, bsp).A + Out[7]: + array([[2, 4, 8], + [2, 4, 8], + [2, 2, 3]], dtype=int64) + +.. Local Variables: +.. mode: rst +.. coding: utf-8 +.. fill-column: 72 +.. End: + diff --git a/doc/release/1.8.0-notes.rst b/doc/release/1.8.0-notes.rst index b3e466165..a8840dd93 100644 --- a/doc/release/1.8.0-notes.rst +++ b/doc/release/1.8.0-notes.rst @@ -136,6 +136,10 @@ of the mirr function remains unchanged. New Features ============ +Building against OpenBLAS +~~~~~~~~~~~~~~~~~~~~~~~~~ +It is now possible to build numpy against OpenBLAS by editing site.cfg. + New constant ~~~~~~~~~~~~ Euler's constant is now exposed in numpy as euler_gamma. diff --git a/doc/release/1.9.0-notes.rst b/doc/release/1.9.0-notes.rst index 7d0a7b936..0b1ab77f1 100644 --- a/doc/release/1.9.0-notes.rst +++ b/doc/release/1.9.0-notes.rst @@ -23,6 +23,14 @@ Compatibility notes New Features ============ +Ufunc and Dot Overrides +~~~~~~~~~~~~~~~~~~~~~~~ + +For better compatibility with external objects you can now override universal +functions (ufuncs), ``numpy.core._dotblas.dot``, and +``numpy.core.multiarray.dot`` (the numpy.dot functions). By defining a +``__numpy_ufunc__`` method. + Improvements ============ diff --git a/doc/source/reference/arrays.classes.rst b/doc/source/reference/arrays.classes.rst index 5cdadd40e..82f95083e 100644 --- a/doc/source/reference/arrays.classes.rst +++ b/doc/source/reference/arrays.classes.rst @@ -38,6 +38,40 @@ Special attributes and methods Numpy provides several hooks that subclasses of :class:`ndarray` can customize: +.. function:: __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs) + + Any class (ndarray subclass or not) can define this method to + override behavior of Numpy's ufuncs. This works quite similarly to + Python's ``__mul__`` and other binary operation routines. + + - *ufunc* is the ufunc object that was called. + - *method* is a string indicating which Ufunc method was called + (one of ``"__call__"``, ``"reduce"``, ``"reduceat"``, + ``"accumulate"``, ``"outer"``, ``"inner"``). + - *i* is the index of *self* in *inputs*. + - *inputs* is a tuple of the input arguments to the ``ufunc`` + - *kwargs* is a dictionary containing the optional input arguments + of the ufunc. The ``out`` argument is always contained in + *kwargs*, if given. + + The method should return either the result of the operation, or + :obj:`NotImplemented` if the operation requested is not + implemented. + + If one of the arguments has a :func:`__numpy_ufunc__` method, it is + executed *instead* of the ufunc. If more than one of the input + arguments implements :func:`__numpy_ufunc__`, they are tried in the + order: subclasses before superclasses, otherwise left to right. The + first routine returning something else than :obj:`NotImplemented` + determines the result. If all of the :func:`__numpy_ufunc__` + operations returns :obj:`NotImplemented`, a :exc:`TypeError` is + raised. + + If an :class:`ndarray` subclass defines the :func:`__numpy_ufunc__` + method, this disables the :func:`__array_wrap__`, + :func:`__array_prepare__`, :data:`__array_priority__` mechanism + described below. + .. function:: __array_finalize__(self) This method is called whenever the system internally allocates a diff --git a/numpy/core/blasdot/_dotblas.c b/numpy/core/blasdot/_dotblas.c index ae6b1b1e5..7a9f858e3 100644 --- a/numpy/core/blasdot/_dotblas.c +++ b/numpy/core/blasdot/_dotblas.c @@ -2,11 +2,14 @@ * This module provides a BLAS optimized\nmatrix multiply, * inner product and dot for numpy arrays */ -#define NPY_NO_DEPRECATED_API NPY_API_VERSION +#define NPY_NO_DEPRECATED_API NPY_API_VERSION #include "Python.h" -#include "npy_config.h" + #include "numpy/arrayobject.h" +#include "npy_config.h" +#include "npy_pycompat.h" +#include "private/ufunc_override.h" #ifndef CBLAS_HEADER #define CBLAS_HEADER "cblas.h" #endif @@ -215,8 +218,12 @@ _bad_strides(PyArrayObject *ap) static PyObject * dotblas_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject* kwargs) { + static PyObject *cached_npy_dot = NULL; + PyObject *override = NULL; + PyObject *module; PyObject *op1, *op2; PyArrayObject *ap1 = NULL, *ap2 = NULL, *out = NULL, *ret = NULL; + int errval; int j, l, lda, ldb, ldc; int typenum, nd; npy_intp ap1stride = 0; @@ -232,6 +239,23 @@ dotblas_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject* kwa MatrixShape ap1shape, ap2shape; char* kwords[] = {"a", "b", "out", NULL }; + if (cached_npy_dot == NULL) { + module = PyImport_ImportModule("numpy.core._dotblas"); + cached_npy_dot = PyDict_GetItemString(PyModule_GetDict(module), "dot"); + + Py_INCREF(cached_npy_dot); + Py_DECREF(module); + } + + errval = PyUFunc_CheckOverride(cached_npy_dot, "__call__", args, kwargs, + &override, 2); + if (errval) { + return NULL; + } + else if (override) { + return override; + } + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O", kwords, &op1, &op2, &out)) { return NULL; diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 3937b2374..1c8cea4f7 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -881,7 +881,8 @@ def configuration(parent_package='',top_path=None): umath_deps = [ generate_umath_py, join('src', 'umath', 'simd.inc.src'), - join(codegen_dir, 'generate_ufunc_api.py')] + join(codegen_dir, 'generate_ufunc_api.py'), + join('src', 'private', 'ufunc_override.h')] if not ENABLE_SEPARATE_COMPILATION: umath_deps.extend(umath_src) diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 85dd8ab01..f4ceecde9 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -53,6 +53,7 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0; #include "ctors.h" #include "array_assign.h" #include "common.h" +#include "private/ufunc_override.h" /* Only here for API compatibility */ NPY_NO_EXPORT PyTypeObject PyBigArray_Type; @@ -2079,8 +2080,29 @@ array_innerproduct(PyObject *NPY_UNUSED(dummy), PyObject *args) static PyObject * array_matrixproduct(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject* kwds) { + int errval; + static PyObject *cached_npy_dot = NULL; + PyObject *override = NULL; PyObject *v, *a, *o = NULL; char* kwlist[] = {"a", "b", "out", NULL }; + PyObject *module; + + if (cached_npy_dot == NULL) { + module = PyImport_ImportModule("numpy.core.multiarray"); + cached_npy_dot = PyDict_GetItemString(PyModule_GetDict(module), "dot"); + + Py_INCREF(cached_npy_dot); + Py_DECREF(module); + } + + errval = PyUFunc_CheckOverride(cached_npy_dot, "__call__", args, kwds, + &override, 2); + if (errval) { + return NULL; + } + else if (override) { + return override; + } if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, &a, &v, &o)) { return NULL; diff --git a/numpy/core/src/private/ufunc_override.h b/numpy/core/src/private/ufunc_override.h new file mode 100644 index 000000000..5c3cbdb12 --- /dev/null +++ b/numpy/core/src/private/ufunc_override.h @@ -0,0 +1,195 @@ +#ifndef __UFUNC_OVERRIDE_H +#define __UFUNC_OVERRIDE_H +#include <npy_config.h> +#include "numpy/arrayobject.h" +#include "multiarray/common.h" + +/* + * Check a set of args for the `__numpy_ufunc__` method. If more than one of + * the input arguments implements `__numpy_ufunc__`, they are tried in the + * order: subclasses before superclasses, otherwise left to right. The first + * routine returning something other than `NotImplemented` determines the + * result. If all of the `__numpy_ufunc__` operations returns `NotImplemented`, + * a `TypeError` is raised. + */ +static int +PyUFunc_CheckOverride(PyObject *ufunc, char *method, + PyObject *args, PyObject *kwds, + PyObject **result, + int nin) +{ + int i; + int override_pos; /* Position of override in args.*/ + int j; + int pos_in_with_override; /* Position of override in with_override.*/ + + int nargs = PyTuple_GET_SIZE(args); + int noa = 0; /* Number of overriding args.*/ + int normalized = 0; /* Is normalized flag.*/ + + PyObject *obj; + PyObject *other_obj; + PyObject *override_args; + + PyObject *method_name = PyUString_FromString(method); + PyObject *normal_args = NULL; /* normal_* holds normalized arguments. */ + PyObject *normal_kwds = NULL; + PyObject *override_obj = NULL; /* overriding object */ + PyObject *numpy_ufunc = NULL; /* the __numpy_ufunc__ method */ + + PyObject *with_override[NPY_MAXARGS]; + /* Pos of each override in args */ + int with_override_pos[NPY_MAXARGS]; + + /* Checks */ + if (!PyTuple_Check(args)) { + goto fail; + } + if (PyTuple_GET_SIZE(args) > NPY_MAXARGS) { + goto fail; + } + + for (i = 0; i < nargs; ++i) { + obj = PyTuple_GET_ITEM(args, i); + if (PyArray_CheckExact(obj) || PyArray_IsAnyScalar(obj)) { + continue; + } + if (PyObject_HasAttrString(obj, "__numpy_ufunc__")) { + with_override[noa] = obj; + with_override_pos[noa] = i; + ++noa; + } + } + + /* No overrides, bail out.*/ + if (noa == 0) { + Py_DECREF(method_name); + return 0; + } + + while (1) { + obj = NULL; + override_obj = NULL; + *result = NULL; + + /* Choose an overriding argument */ + for (i = 0; i < noa; i++) { + obj = with_override[i]; + if (obj == NULL) { + continue; + } + /* Get the first instance of an overriding arg.*/ + override_pos = with_override_pos[i]; + override_obj = obj; + pos_in_with_override = i; + + /* Check for sub-types to the right of obj. */ + for (j = i + 1; j < noa; j++) { + other_obj = with_override[j]; + if (PyObject_Type(other_obj) != PyObject_Type(obj) && + PyObject_IsInstance(other_obj, + PyObject_Type(override_obj))) { + override_obj = NULL; + break; + } + } + /* override_obj had no subtypes to the right. */ + if (override_obj) { + break; + } + } + /* No good override_obj */ + if (!override_obj) { + break; + } + /* + * Normalize the ufuncs arguments. Returns a tuple of + * (args, kwds). + * + * Test with and without kwds. + */ + if (!normalized) { + PyObject *out_arg; + + /* If we have more args than nin, the last one must be `out`.*/ + if (nargs > nin) { + out_arg = PyTuple_GET_ITEM(args, nargs - 1); + + /* Build new args.*/ + normal_args = PyTuple_GetSlice(args, 0, nin); + + /* Build new kwds with out arg.*/ + if (kwds && PyDict_CheckExact(kwds)) { + normal_kwds = PyDict_Copy(kwds); + PyDict_SetItemString(normal_kwds, "out", out_arg); + } + else { + normal_kwds = PyDict_New(); + PyDict_SetItemString(normal_kwds, "out", out_arg); + } + + normalized = 1; + } + else { + /* Copy args */ + normal_args = PyTuple_GetSlice(args, 0, nin); + if (kwds && PyDict_CheckExact(kwds)) { + normal_kwds = PyDict_Copy(kwds); + } + else { + normal_kwds = PyDict_New(); + } + + normalized = 1; + } + } + + /* Calculate a result if we have a override. */ + if (override_obj) { + numpy_ufunc = PyObject_GetAttrString(override_obj, + "__numpy_ufunc__"); + override_args = Py_BuildValue("OOiO", ufunc, method_name, + override_pos, normal_args); + *result = PyObject_Call(numpy_ufunc, override_args, normal_kwds); + + Py_DECREF(numpy_ufunc); + Py_DECREF(override_args); + + /* Remove this arg if it gives not implemented */ + if (*result == Py_NotImplemented) { + with_override[pos_in_with_override] = NULL; + Py_DECREF(*result); + continue; + } + /* Good result. */ + else { + break; + } + } + + /* All overrides checked. */ + else { + break; + } + } + /* No acceptable override found. */ + if (!*result) { + PyErr_SetString(PyExc_TypeError, + "__numpy_ufunc__ not implemented for this type."); + Py_XDECREF(normal_args); + Py_XDECREF(normal_kwds); + goto fail; + } + /* Override found, return it. */ + Py_DECREF(method_name); + Py_XDECREF(normal_args); + Py_XDECREF(normal_kwds); + return 0; + +fail: + Py_DECREF(method_name); + return 1; + +} + +#endif diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index a71777ce3..062bf163b 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -44,6 +44,7 @@ #include "reduction.h" #include "ufunc_object.h" +#include "ufunc_override.h" /********** PRINTF DEBUG TRACING **************/ #define NPY_UF_DBG_TRACING 0 @@ -707,7 +708,6 @@ fail: return -1; } - /********* GENERIC UFUNC USING ITERATOR *********/ /* @@ -4043,6 +4043,7 @@ ufunc_generic_call(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) PyObject *retobj[NPY_MAXARGS]; PyObject *wraparr[NPY_MAXARGS]; PyObject *res; + PyObject *override = NULL; int errval; /* @@ -4053,6 +4054,18 @@ ufunc_generic_call(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) mps[i] = NULL; } + errval = PyUFunc_CheckOverride(ufunc, "__call__", args, kwds, &override, + ufunc->nin); + if (errval) { + return NULL; + } + else if (override) { + for (i = 0; i < ufunc->nargs; i++) { + PyArray_XDECREF_ERR(mps[i]); + } + return override; + } + errval = PyUFunc_GenericFunction(ufunc, args, kwds, mps); if (errval < 0) { for (i = 0; i < ufunc->nargs; i++) { @@ -4834,18 +4847,51 @@ ufunc_outer(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) static PyObject * ufunc_reduce(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) { + int errval; + PyObject *override = NULL; + + errval = PyUFunc_CheckOverride(ufunc, "reduce", args, kwds, &override, + ufunc->nin); + if (errval) { + return NULL; + } + else if (override) { + return override; + } return PyUFunc_GenericReduction(ufunc, args, kwds, UFUNC_REDUCE); } static PyObject * ufunc_accumulate(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) { + int errval; + PyObject *override = NULL; + + errval = PyUFunc_CheckOverride(ufunc, "accumulate", args, kwds, &override, + ufunc->nin); + if (errval) { + return NULL; + } + else if (override) { + return override; + } return PyUFunc_GenericReduction(ufunc, args, kwds, UFUNC_ACCUMULATE); } static PyObject * ufunc_reduceat(PyUFuncObject *ufunc, PyObject *args, PyObject *kwds) { + int errval; + PyObject *override = NULL; + + errval = PyUFunc_CheckOverride(ufunc, "reduceat", args, kwds, &override, + ufunc->nin); + if (errval) { + return NULL; + } + else if (override) { + return override; + } return PyUFunc_GenericReduction(ufunc, args, kwds, UFUNC_REDUCEAT); } diff --git a/numpy/core/tests/test_blasdot.py b/numpy/core/tests/test_blasdot.py index 624c617d3..caa576abc 100644 --- a/numpy/core/tests/test_blasdot.py +++ b/numpy/core/tests/test_blasdot.py @@ -151,3 +151,21 @@ def test_dot_array_order(): assert_almost_equal(c.T.dot(b.T).T, b.dot(c), decimal=prec) assert_almost_equal(b.dot(c), _dot(b, c), decimal=prec) assert_almost_equal(c.T.dot(b.T), _dot(c.T, b.T), decimal=prec) + +def test_dot_override(): + class A(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return "A" + + class B(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return NotImplemented + + a = A() + b = B() + c = np.array([[1]]) + + assert_equal(np.dot(a, b), "A") + assert_equal(c.dot(a), "A") + assert_raises(TypeError, np.dot, b, c) + assert_raises(TypeError, c.dot, b) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 9870d44f3..0e7f6ec61 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1324,6 +1324,24 @@ class TestMethods(TestCase): a.dot(b=b, out=c) assert_equal(c, np.dot(a, b)) + def test_dot_override(self): + class A(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return "A" + + class B(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return NotImplemented + + a = A() + b = B() + c = np.array([[1]]) + + assert_equal(np.dot(a, b), "A") + assert_equal(c.dot(a), "A") + assert_raises(TypeError, np.dot, b, c) + assert_raises(TypeError, c.dot, b) + def test_diagonal(self): a = np.arange(12).reshape((3, 4)) assert_equal(a.diagonal(), [0, 5, 10]) diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index eb18304ea..3706cfa81 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -868,6 +868,187 @@ class TestSpecialMethods(TestCase): assert_equal(ncu.maximum(a, B()), 0) assert_equal(ncu.maximum(a, C()), 0) + def test_ufunc_override(self): + class A(object): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return self, func, method, pos, inputs, kwargs + + a = A() + + b = np.matrix([1]) + c = np.array([1]) + res0 = np.multiply(a, b) + res1 = np.dot(a, b) + + # self + assert_equal(res0[0], a) + assert_equal(res1[0], a) + assert_equal(res0[1], np.multiply) + assert_equal(res1[1], np.dot) + assert_equal(res0[2], '__call__') + assert_equal(res1[2], '__call__') + assert_equal(res0[3], 0) + assert_equal(res1[3], 0) + assert_equal(res0[4], (a, b)) + assert_equal(res1[4], (a, b)) + assert_equal(res0[5], {}) + assert_equal(res1[5], {}) + + def test_ufunc_override_mro(self): + + # Some multi arg functions for testing. + def tres_mul(a, b, c): + return a * b * c + + def quatro_mul(a, b, c, d): + return a * b * c * d + + # Make these into ufuncs. + three_mul_ufunc = np.frompyfunc(tres_mul, 3, 1) + four_mul_ufunc = np.frompyfunc(quatro_mul, 4, 1) + + class A(object): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return "A" + + class ASub(A): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return "ASub" + + class B(object): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return "B" + + class C(object): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return NotImplemented + + class CSub(object): + def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs): + return NotImplemented + + + + a = A() + a_sub = ASub() + b = B() + c = C() + c_sub = CSub() + + # Standard + res = np.multiply(a, a_sub) + assert_equal(res, "ASub") + res = np.multiply(a_sub, b) + assert_equal(res, "ASub") + + # With 1 NotImplemented + res = np.multiply(c, a) + assert_equal(res, "A") + + # Both NotImplemented. + assert_raises(TypeError, np.multiply, c, c_sub) + assert_raises(TypeError, np.multiply, c_sub, c) + assert_raises(TypeError, np.multiply, 2, c) + + # Ternary testing. + assert_equal(three_mul_ufunc(a, 1, 2), "A") + assert_equal(three_mul_ufunc(1, a, 2), "A") + assert_equal(three_mul_ufunc(1, 2, a), "A") + + assert_equal(three_mul_ufunc(a, a, 6), "A") + assert_equal(three_mul_ufunc(a, 2, a), "A") + assert_equal(three_mul_ufunc(a, 2, b), "A") + assert_equal(three_mul_ufunc(a, 2, a_sub), "ASub") + assert_equal(three_mul_ufunc(a, a_sub, 3), "ASub") + assert_equal(three_mul_ufunc(c, a_sub, 3), "ASub") + assert_equal(three_mul_ufunc(1, a_sub, c), "ASub") + + assert_equal(three_mul_ufunc(a, b, c), "A") + assert_equal(three_mul_ufunc(a, b, c_sub), "A") + assert_equal(three_mul_ufunc(1, 2, b), "B") + + assert_raises(TypeError, three_mul_ufunc, 1, 2, c) + assert_raises(TypeError, three_mul_ufunc, c_sub, 2, c) + assert_raises(TypeError, three_mul_ufunc, c_sub, 2, 3) + + # Quaternary testing. + assert_equal(four_mul_ufunc(a, 1, 2, 3), "A") + assert_equal(four_mul_ufunc(1, a, 2, 3), "A") + assert_equal(four_mul_ufunc(1, 1, a, 3), "A") + assert_equal(four_mul_ufunc(1, 1, 2, a), "A") + + assert_equal(four_mul_ufunc(a, b, 2, 3), "A") + assert_equal(four_mul_ufunc(1, a, 2, b), "A") + assert_equal(four_mul_ufunc(b, 1, a, 3), "B") + assert_equal(four_mul_ufunc(a_sub, 1, 2, a), "ASub") + assert_equal(four_mul_ufunc(a, 1, 2, a_sub), "ASub") + + assert_raises(TypeError, four_mul_ufunc, 1, 2, 3, c) + assert_raises(TypeError, four_mul_ufunc, 1, 2, c_sub, c) + assert_raises(TypeError, four_mul_ufunc, 1, c, c_sub, c) + + def test_ufunc_override_methods(self): + class A(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + if method == "__call__": + return method + if method == "reduce": + return method + if method == "accumulate": + return method + if method == "reduceat": + return method + + a = A() + res = np.multiply(1, a) + assert_equal(res, "__call__") + + res = np.multiply.reduce(1, a) + assert_equal(res, "reduce") + + res = np.multiply.accumulate(1, a) + assert_equal(res, "accumulate") + + res = np.multiply.reduceat(1, a) + assert_equal(res, "reduceat") + + res = np.multiply(a, 1) + assert_equal(res, "__call__") + + res = np.multiply.reduce(a, 1) + assert_equal(res, "reduce") + + res = np.multiply.accumulate(a, 1) + assert_equal(res, "accumulate") + + res = np.multiply.reduceat(a, 1) + assert_equal(res, "reduceat") + + def test_ufunc_override_out(self): + class A(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return kwargs + + + class B(object): + def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs): + return kwargs + + a = A() + b = B() + res0 = np.multiply(a, b, 'out_arg') + res1 = np.multiply(a, b, out='out_arg') + res2 = np.multiply(2, b, 'out_arg') + res3 = np.multiply(3, b, out='out_arg') + res4 = np.multiply(a, 4, 'out_arg') + res5 = np.multiply(a, 5, out='out_arg') + + assert_equal(res0['out'], 'out_arg') + assert_equal(res1['out'], 'out_arg') + assert_equal(res2['out'], 'out_arg') + assert_equal(res3['out'], 'out_arg') + assert_equal(res4['out'], 'out_arg') + assert_equal(res5['out'], 'out_arg') class TestChoose(TestCase): def test_mixed(self): diff --git a/numpy/distutils/system_info.py b/numpy/distutils/system_info.py index 8ec1b8446..d9196ea5c 100644 --- a/numpy/distutils/system_info.py +++ b/numpy/distutils/system_info.py @@ -12,6 +12,7 @@ classes are available: lapack_atlas_info blas_info lapack_info + openblas_info blas_opt_info # usage recommended lapack_opt_info # usage recommended fftw_info,dfftw_info,sfftw_info @@ -119,7 +120,6 @@ import copy import warnings from glob import glob from functools import reduce - if sys.version_info[0] < 3: from ConfigParser import NoOptionError, ConfigParser else: @@ -298,6 +298,7 @@ def get_info(name, notfound_action=0): 'lapack_atlas': lapack_atlas_info, # use lapack_opt instead 'lapack_atlas_threads': lapack_atlas_threads_info, # ditto 'mkl': mkl_info, + 'openblas': openblas_info, # use blas_opt instead 'lapack_mkl': lapack_mkl_info, # use lapack_opt instead 'blas_mkl': blas_mkl_info, # use blas_opt instead 'x11': x11_info, @@ -1366,7 +1367,22 @@ class lapack_opt_info(system_info): def calc_info(self): - if sys.platform == 'darwin' and not os.environ.get('ATLAS', None): + openblas_info = get_info('openblas') + if openblas_info: + self.set_info(**openblas_info) + return + + lapack_mkl_info = get_info('lapack_mkl') + if lapack_mkl_info: + self.set_info(**lapack_mkl_info) + return + + atlas_info = get_info('atlas_threads') + if not atlas_info: + atlas_info = get_info('atlas') + + if sys.platform == 'darwin' and not atlas_info: + # Use the system lapack from Accelerate or vecLib under OSX args = [] link_args = [] if get_platform()[-4:] == 'i386' or 'intel' in get_platform() or \ @@ -1394,14 +1410,6 @@ class lapack_opt_info(system_info): define_macros=[('NO_ATLAS_INFO', 3)]) return - lapack_mkl_info = get_info('lapack_mkl') - if lapack_mkl_info: - self.set_info(**lapack_mkl_info) - return - - atlas_info = get_info('atlas_threads') - if not atlas_info: - atlas_info = get_info('atlas') #atlas_info = {} ## uncomment for testing need_lapack = 0 need_blas = 0 @@ -1455,7 +1463,22 @@ class blas_opt_info(system_info): def calc_info(self): - if sys.platform == 'darwin' and not os.environ.get('ATLAS', None): + blas_mkl_info = get_info('blas_mkl') + if blas_mkl_info: + self.set_info(**blas_mkl_info) + return + + openblas_info = get_info('openblas') + if openblas_info: + self.set_info(**openblas_info) + return + + atlas_info = get_info('atlas_blas_threads') + if not atlas_info: + atlas_info = get_info('atlas_blas') + + if sys.platform == 'darwin'and not atlas_info: + # Use the system BLAS from Accelerate or vecLib under OSX args = [] link_args = [] if get_platform()[-4:] == 'i386' or 'intel' in get_platform() or \ @@ -1487,14 +1510,6 @@ class blas_opt_info(system_info): define_macros=[('NO_ATLAS_INFO', 3)]) return - blas_mkl_info = get_info('blas_mkl') - if blas_mkl_info: - self.set_info(**blas_mkl_info) - return - - atlas_info = get_info('atlas_blas_threads') - if not atlas_info: - atlas_info = get_info('atlas_blas') need_blas = 0 info = {} if atlas_info: @@ -1537,6 +1552,23 @@ class blas_info(system_info): self.set_info(**info) +class openblas_info(blas_info): + section = 'openblas' + dir_env_var = 'OPENBLAS' + _lib_names = ['openblas'] + notfounderror = BlasNotFoundError + + def calc_info(self): + lib_dirs = self.get_lib_dirs() + + openblas_libs = self.get_libs('openblas_libs', self._lib_names) + info = self.check_libs(lib_dirs, openblas_libs, []) + if info is None: + return + info['language'] = 'f77' # XXX: is it generally true? + self.set_info(**info) + + class blas_src_info(system_info): section = 'blas_src' dir_env_var = 'BLAS_SRC' diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index 70eccdd0a..82c3ba9ea 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -7,63 +7,64 @@ import numpy as np import numpy.polynomial.chebyshev as cheb from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) -def trim(x) : + +def trim(x): return cheb.chebtrim(x, tol=1e-6) -T0 = [ 1] -T1 = [ 0, 1] -T2 = [-1, 0, 2] -T3 = [ 0, -3, 0, 4] -T4 = [ 1, 0, -8, 0, 8] -T5 = [ 0, 5, 0, -20, 0, 16] -T6 = [-1, 0, 18, 0, -48, 0, 32] -T7 = [ 0, -7, 0, 56, 0, -112, 0, 64] -T8 = [ 1, 0, -32, 0, 160, 0, -256, 0, 128] -T9 = [ 0, 9, 0, -120, 0, 432, 0, -576, 0, 256] +T0 = [1] +T1 = [0, 1] +T2 = [-1, 0, 2] +T3 = [0, -3, 0, 4] +T4 = [1, 0, -8, 0, 8] +T5 = [0, 5, 0, -20, 0, 16] +T6 = [-1, 0, 18, 0, -48, 0, 32] +T7 = [0, -7, 0, 56, 0, -112, 0, 64] +T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128] +T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256] Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] -class TestPrivate(TestCase) : +class TestPrivate(TestCase): - def test__cseries_to_zseries(self) : - for i in range(5) : + def test__cseries_to_zseries(self): + for i in range(5): inp = np.array([2] + [1]*i, np.double) tgt = np.array([.5]*i + [2] + [.5]*i, np.double) res = cheb._cseries_to_zseries(inp) assert_equal(res, tgt) - def test__zseries_to_cseries(self) : - for i in range(5) : + def test__zseries_to_cseries(self): + for i in range(5): inp = np.array([.5]*i + [2] + [.5]*i, np.double) tgt = np.array([2] + [1]*i, np.double) res = cheb._zseries_to_cseries(inp) assert_equal(res, tgt) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_chebdomain(self) : + def test_chebdomain(self): assert_equal(cheb.chebdomain, [-1, 1]) - def test_chebzero(self) : + def test_chebzero(self): assert_equal(cheb.chebzero, [0]) - def test_chebone(self) : + def test_chebone(self): assert_equal(cheb.chebone, [1]) - def test_chebx(self) : + def test_chebx(self): assert_equal(cheb.chebx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): - def test_chebadd(self) : - for i in range(5) : - for j in range(5) : + def test_chebadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -71,9 +72,9 @@ class TestArithmetic(TestCase) : res = cheb.chebadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_chebsub(self) : - for i in range(5) : - for j in range(5) : + def test_chebsub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -89,9 +90,9 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [.5, 0, .5] assert_equal(cheb.chebmulx(ser), tgt) - def test_chebmul(self) : - for i in range(5) : - for j in range(5) : + def test_chebmul(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(i + j + 1) tgt[i + j] += .5 @@ -99,9 +100,9 @@ class TestArithmetic(TestCase) : res = cheb.chebmul([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_chebdiv(self) : - for i in range(5) : - for j in range(5) : + def test_chebdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -121,22 +122,21 @@ class TestEvaluation(TestCase): x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_chebval(self) : + def test_chebval(self): #check empty input assert_equal(cheb.chebval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Tlist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i tgt = y[i] res = cheb.chebval(x, [0]*i + [1]) assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(cheb.chebval(x, [1]).shape, dims) @@ -206,9 +206,9 @@ class TestEvaluation(TestCase): assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_chebint(self) : + def test_chebint(self): # check exceptions assert_raises(ValueError, cheb.chebint, [0], .5) assert_raises(ValueError, cheb.chebint, [0], -1) @@ -221,7 +221,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [0, 1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -231,7 +231,7 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] chebpol = cheb.poly2cheb(pol) @@ -239,7 +239,7 @@ class TestIntegral(TestCase) : assert_almost_equal(cheb.chebval(-1, chebint), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -249,41 +249,41 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = cheb.chebint(tgt, m=1) res = cheb.chebint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k]) res = cheb.chebint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1) res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = cheb.chebint(tgt, m=1, k=[k], scl=2) res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -305,29 +305,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_chebder(self) : + def test_chebder(self): # check exceptions assert_raises(ValueError, cheb.chebder, [0], .5) assert_raises(ValueError, cheb.chebder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = cheb.chebder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = cheb.chebder(cheb.chebint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -349,13 +349,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_chebvander(self) : + def test_chebvander(self): # check for 1d x x = np.arange(3) v = cheb.chebvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], cheb.chebval(x, coef)) @@ -363,11 +362,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = cheb.chebvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], cheb.chebval(x, coef)) - def test_chebvander2d(self) : + def test_chebvander2d(self): # also tests chebval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -380,8 +379,7 @@ class TestVander(TestCase): van = cheb.chebvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_chebvander3d(self) : + def test_chebvander3d(self): # also tests chebval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -397,8 +395,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_chebfit(self) : - def f(x) : + def test_chebfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -475,26 +473,26 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_chebfromroots(self) : + def test_chebfromroots(self): res = cheb.chebfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) tgt = [0]*i + [1] res = cheb.chebfromroots(roots)*2**(i-1) assert_almost_equal(trim(res), trim(tgt)) - def test_chebroots(self) : + def test_chebroots(self): assert_almost_equal(cheb.chebroots([1]), []) assert_almost_equal(cheb.chebroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = cheb.chebroots(cheb.chebfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_chebtrim(self) : + def test_chebtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -505,15 +503,15 @@ class TestMisc(TestCase) : assert_equal(cheb.chebtrim(coef, 1), coef[:-3]) assert_equal(cheb.chebtrim(coef, 2), [0]) - def test_chebline(self) : + def test_chebline(self): assert_equal(cheb.chebline(3, 4), [3, 4]) - def test_cheb2poly(self) : - for i in range(10) : + def test_cheb2poly(self): + for i in range(10): assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i]) - def test_poly2cheb(self) : - for i in range(10) : + def test_poly2cheb(self): + for i in range(10): assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1]) def test_weight(self): @@ -537,7 +535,6 @@ class TestMisc(TestCase) : tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325] assert_almost_equal(cheb.chebpts1(4), tgt) - def test_chebpts2(self): #test exceptions assert_raises(ValueError, cheb.chebpts2, 1.5) diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py index 5708d936f..09b30a9e9 100644 --- a/numpy/polynomial/tests/test_classes.py +++ b/numpy/polynomial/tests/test_classes.py @@ -7,17 +7,17 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.polynomial import ( - Polynomial, Legendre, Chebyshev, Laguerre, - Hermite, HermiteE) + Polynomial, Legendre, Chebyshev, Laguerre, + Hermite, HermiteE) from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite, dec) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite, dec) from numpy.testing.noseclasses import KnownFailure classes = ( - Polynomial, Legendre, Chebyshev, Laguerre, - Hermite, HermiteE) + Polynomial, Legendre, Chebyshev, Laguerre, + Hermite, HermiteE) def test_class_methods(): @@ -52,13 +52,12 @@ def test_class_methods(): yield check_trim, Poly - # # helper functions # - random = np.random.random + def assert_poly_almost_equal(p1, p2, msg=""): try: assert_(np.all(p1.domain == p2.domain)) @@ -113,7 +112,7 @@ def check_cast(Poly1, Poly2): # -def check_identity(Poly) : +def check_identity(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 x = np.linspace(d[0], d[1], 11) @@ -151,9 +150,9 @@ def check_fromroots(Poly): assert_almost_equal(p2.coef[-1], 1) -def check_fit(Poly) : +def check_fit(Poly): - def f(x) : + def f(x): return x*(x - 1)*(x - 2) x = np.linspace(0, 3) y = f(x) @@ -186,7 +185,7 @@ def check_fit(Poly) : assert_almost_equal(p1(x), p2(x)) -def check_equal(Poly) : +def check_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -197,7 +196,7 @@ def check_equal(Poly) : assert_(not p1 == p4) -def check_not_equal(Poly) : +def check_not_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -208,7 +207,7 @@ def check_not_equal(Poly) : assert_(p1 != p4) -def check_add(Poly) : +def check_add(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -230,8 +229,7 @@ def check_add(Poly) : assert_raises(TypeError, p1.__add__, Polynomial([0])) - -def check_sub(Poly) : +def check_sub(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -253,7 +251,7 @@ def check_sub(Poly) : assert_raises(TypeError, p1.__sub__, Polynomial([0])) -def check_mul(Poly) : +def check_mul(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) p1 = Poly(c1) @@ -276,7 +274,7 @@ def check_mul(Poly) : assert_raises(TypeError, p1.__mul__, Polynomial([0])) -def check_floordiv(Poly) : +def check_floordiv(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) c3 = list(random((2,)) + .5) @@ -294,15 +292,17 @@ def check_floordiv(Poly) : assert_poly_almost_equal(np.array(c4) // p2, p1) assert_poly_almost_equal(2 // p2, Poly([0])) assert_poly_almost_equal(p2 // 2, 0.5*p2) - assert_raises(TypeError, p1.__floordiv__, Poly([0], domain=Poly.domain + 1)) - assert_raises(TypeError, p1.__floordiv__, Poly([0], window=Poly.window + 1)) + assert_raises( + TypeError, p1.__floordiv__, Poly([0], domain=Poly.domain + 1)) + assert_raises( + TypeError, p1.__floordiv__, Poly([0], window=Poly.window + 1)) if Poly is Polynomial: assert_raises(TypeError, p1.__floordiv__, Chebyshev([0])) else: assert_raises(TypeError, p1.__floordiv__, Polynomial([0])) -def check_mod(Poly) : +def check_mod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -329,7 +329,7 @@ def check_mod(Poly) : assert_raises(TypeError, p1.__mod__, Polynomial([0])) -def check_divmod(Poly) : +def check_divmod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -397,7 +397,7 @@ def check_copy(Poly): assert_(p1.window is not p2.window) -def check_integ(Poly) : +def check_integ(Poly): P = Polynomial # Check defaults p0 = Poly.cast(P([1*2, 2*3, 3*4])) @@ -456,19 +456,19 @@ def check_linspace(Poly): assert_almost_equal(yres, ytgt) -def check_pow(Poly) : +def check_pow(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 tgt = Poly([1], domain=d, window=d) tst = Poly([1, 2, 3], domain=d, window=d) - for i in range(5) : + for i in range(5): assert_poly_almost_equal(tst**i, tgt) tgt = tgt * tst assert_raises(ValueError, tgt.__pow__, 1.5) assert_raises(ValueError, tgt.__pow__, -1) -def check_call(Poly) : +def check_call(Poly): P = Polynomial d = Poly.domain x = np.linspace(d[0], d[1], 11) @@ -480,7 +480,7 @@ def check_call(Poly) : assert_almost_equal(res, tgt) -def check_cutdeg(Poly) : +def check_cutdeg(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.cutdeg, .5) assert_raises(ValueError, p.cutdeg, -1) @@ -490,7 +490,7 @@ def check_cutdeg(Poly) : assert_equal(len(p.cutdeg(0)), 1) -def check_truncate(Poly) : +def check_truncate(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.truncate, .5) assert_raises(ValueError, p.truncate, 0) @@ -500,7 +500,7 @@ def check_truncate(Poly) : assert_equal(len(p.truncate(1)), 1) -def check_trim(Poly) : +def check_trim(Poly): c = [1, 1e-6, 1e-12, 0] p = Poly(c) assert_equal(p.trim().coef, c[:3]) @@ -508,7 +508,7 @@ def check_trim(Poly) : assert_equal(p.trim(1e-5).coef, c[:1]) -def check_mapparms(Poly) : +def check_mapparms(Poly): # check with defaults. Should be identity. d = Poly.domain w = Poly.window diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py index 978c9c79b..ac60007d1 100644 --- a/numpy/polynomial/tests/test_hermite.py +++ b/numpy/polynomial/tests/test_hermite.py @@ -7,48 +7,48 @@ import numpy as np import numpy.polynomial.hermite as herm from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) - -H0 = np.array([ 1]) -H1 = np.array([0, 2]) -H2 = np.array([ -2, 0, 4]) -H3 = np.array([0, -12, 0, 8]) -H4 = np.array([ 12, 0, -48, 0, 16]) -H5 = np.array([0, 120, 0, -160, 0, 32]) -H6 = np.array([-120, 0, 720, 0, -480, 0, 64]) -H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128]) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) + +H0 = np.array([1]) +H1 = np.array([0, 2]) +H2 = np.array([-2, 0, 4]) +H3 = np.array([0, -12, 0, 8]) +H4 = np.array([12, 0, -48, 0, 16]) +H5 = np.array([0, 120, 0, -160, 0, 32]) +H6 = np.array([-120, 0, 720, 0, -480, 0, 64]) +H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128]) H8 = np.array([1680, 0, -13440, 0, 13440, 0, -3584, 0, 256]) H9 = np.array([0, 30240, 0, -80640, 0, 48384, 0, -9216, 0, 512]) Hlist = [H0, H1, H2, H3, H4, H5, H6, H7, H8, H9] -def trim(x) : +def trim(x): return herm.hermtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_hermdomain(self) : + def test_hermdomain(self): assert_equal(herm.hermdomain, [-1, 1]) - def test_hermzero(self) : + def test_hermzero(self): assert_equal(herm.hermzero, [0]) - def test_hermone(self) : + def test_hermone(self): assert_equal(herm.hermone, [1]) - def test_hermx(self) : + def test_hermx(self): assert_equal(herm.hermx, [0, .5]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_hermadd(self) : - for i in range(5) : - for j in range(5) : + def test_hermadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -56,9 +56,9 @@ class TestArithmetic(TestCase) : res = herm.hermadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_hermsub(self) : - for i in range(5) : - for j in range(5) : + def test_hermsub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -74,12 +74,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i, 0, .5] assert_equal(herm.hermmulx(ser), tgt) - def test_hermmul(self) : + def test_hermmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = herm.hermval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = herm.hermval(self.x, pol2) @@ -88,9 +88,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_hermdiv(self) : - for i in range(5) : - for j in range(5) : + def test_hermdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -100,7 +100,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2.5, 1., .75]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -110,15 +110,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_hermval(self) : + def test_hermval(self): #check empty input assert_equal(herm.hermval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Hlist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -126,7 +125,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(herm.hermval(x, [1]).shape, dims) @@ -196,9 +195,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_hermint(self) : + def test_hermint(self): # check exceptions assert_raises(ValueError, herm.hermint, [0], .5) assert_raises(ValueError, herm.hermint, [0], -1) @@ -211,7 +210,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [0, .5]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -221,7 +220,7 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] hermpol = herm.poly2herm(pol) @@ -229,7 +228,7 @@ class TestIntegral(TestCase) : assert_almost_equal(herm.hermval(-1, hermint), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -239,41 +238,41 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herm.hermint(tgt, m=1) res = herm.hermint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herm.hermint(tgt, m=1, k=[k]) res = herm.hermint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herm.hermint(tgt, m=1, k=[k], lbnd=-1) res = herm.hermint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herm.hermint(tgt, m=1, k=[k], scl=2) res = herm.hermint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -295,29 +294,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_hermder(self) : + def test_hermder(self): # check exceptions assert_raises(ValueError, herm.hermder, [0], .5) assert_raises(ValueError, herm.hermder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = herm.hermder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = herm.hermder(herm.hermint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -339,13 +338,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_hermvander(self) : + def test_hermvander(self): # check for 1d x x = np.arange(3) v = herm.hermvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], herm.hermval(x, coef)) @@ -353,11 +351,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = herm.hermvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], herm.hermval(x, coef)) - def test_hermvander2d(self) : + def test_hermvander2d(self): # also tests hermval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -370,8 +368,7 @@ class TestVander(TestCase): van = herm.hermvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_hermvander3d(self) : + def test_hermvander3d(self): # also tests hermval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -387,8 +384,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_hermfit(self) : - def f(x) : + def test_hermfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -465,12 +462,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_hermfromroots(self) : + def test_hermfromroots(self): res = herm.hermfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) pol = herm.hermfromroots(roots) res = herm.hermval(roots, pol) @@ -479,15 +476,15 @@ class TestMisc(TestCase) : assert_almost_equal(herm.herm2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_hermroots(self) : + def test_hermroots(self): assert_almost_equal(herm.hermroots([1]), []) assert_almost_equal(herm.hermroots([1, 1]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = herm.hermroots(herm.hermfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_hermtrim(self) : + def test_hermtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -498,15 +495,15 @@ class TestMisc(TestCase) : assert_equal(herm.hermtrim(coef, 1), coef[:-3]) assert_equal(herm.hermtrim(coef, 2), [0]) - def test_hermline(self) : + def test_hermline(self): assert_equal(herm.hermline(3, 4), [3, 2]) - def test_herm2poly(self) : - for i in range(10) : + def test_herm2poly(self): + for i in range(10): assert_almost_equal(herm.herm2poly([0]*i + [1]), Hlist[i]) - def test_poly2herm(self) : - for i in range(10) : + def test_poly2herm(self): + for i in range(10): assert_almost_equal(herm.poly2herm(Hlist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py index b27e8576b..5341dc7ff 100644 --- a/numpy/polynomial/tests/test_hermite_e.py +++ b/numpy/polynomial/tests/test_hermite_e.py @@ -8,44 +8,45 @@ import numpy.polynomial.hermite_e as herme from numpy.polynomial.polynomial import polyval from numpy.testing import * -He0 = np.array([ 1 ]) -He1 = np.array([ 0, 1 ]) -He2 = np.array([ -1, 0, 1 ]) -He3 = np.array([ 0, -3, 0, 1 ]) -He4 = np.array([ 3, 0, -6, 0, 1 ]) -He5 = np.array([ 0, 15, 0, -10, 0, 1 ]) -He6 = np.array([ -15, 0, 45, 0, -15, 0, 1 ]) -He7 = np.array([ 0, -105, 0, 105, 0, -21, 0, 1 ]) -He8 = np.array([ 105, 0, -420, 0, 210, 0, -28, 0, 1 ]) -He9 = np.array([ 0, 945, 0, -1260, 0, 378, 0, -36, 0, 1 ]) +He0 = np.array([1]) +He1 = np.array([0, 1]) +He2 = np.array([-1, 0, 1]) +He3 = np.array([0, -3, 0, 1]) +He4 = np.array([3, 0, -6, 0, 1]) +He5 = np.array([0, 15, 0, -10, 0, 1]) +He6 = np.array([-15, 0, 45, 0, -15, 0, 1]) +He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1]) +He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1]) +He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1]) Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9] -def trim(x) : + +def trim(x): return herme.hermetrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_hermedomain(self) : + def test_hermedomain(self): assert_equal(herme.hermedomain, [-1, 1]) - def test_hermezero(self) : + def test_hermezero(self): assert_equal(herme.hermezero, [0]) - def test_hermeone(self) : + def test_hermeone(self): assert_equal(herme.hermeone, [1]) - def test_hermex(self) : + def test_hermex(self): assert_equal(herme.hermex, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_hermeadd(self) : - for i in range(5) : - for j in range(5) : + def test_hermeadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -53,9 +54,9 @@ class TestArithmetic(TestCase) : res = herme.hermeadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_hermesub(self) : - for i in range(5) : - for j in range(5) : + def test_hermesub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -71,12 +72,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i, 0, 1] assert_equal(herme.hermemulx(ser), tgt) - def test_hermemul(self) : + def test_hermemul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = herme.hermeval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = herme.hermeval(self.x, pol2) @@ -85,9 +86,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_hermediv(self) : - for i in range(5) : - for j in range(5) : + def test_hermediv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -97,7 +98,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([4., 2., 3.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -107,15 +108,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_hermeval(self) : + def test_hermeval(self): #check empty input assert_equal(herme.hermeval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Helist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -123,7 +123,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(herme.hermeval(x, [1]).shape, dims) @@ -195,7 +195,7 @@ class TestEvaluation(TestCase) : class TestIntegral(TestCase): - def test_hermeint(self) : + def test_hermeint(self): # check exceptions assert_raises(ValueError, herme.hermeint, [0], .5) assert_raises(ValueError, herme.hermeint, [0], -1) @@ -208,7 +208,7 @@ class TestIntegral(TestCase): assert_almost_equal(res, [0, 1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -218,7 +218,7 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] hermepol = herme.poly2herme(pol) @@ -226,7 +226,7 @@ class TestIntegral(TestCase): assert_almost_equal(herme.hermeval(-1, hermeint), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -236,41 +236,41 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herme.hermeint(tgt, m=1) res = herme.hermeint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herme.hermeint(tgt, m=1, k=[k]) res = herme.hermeint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herme.hermeint(tgt, m=1, k=[k], lbnd=-1) res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = herme.hermeint(tgt, m=1, k=[k], scl=2) res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -292,31 +292,32 @@ class TestIntegral(TestCase): assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_hermeder(self) : + def test_hermeder(self): # check exceptions assert_raises(ValueError, herme.hermeder, [0], .5) assert_raises(ValueError, herme.hermeder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = herme.hermeder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = herme.hermeder(herme.hermeint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] - res = herme.hermeder(herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5) + res = herme.hermeder( + herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) def test_hermeder_axis(self): @@ -336,13 +337,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_hermevander(self) : + def test_hermevander(self): # check for 1d x x = np.arange(3) v = herme.hermevander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], herme.hermeval(x, coef)) @@ -350,11 +350,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = herme.hermevander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], herme.hermeval(x, coef)) - def test_hermevander2d(self) : + def test_hermevander2d(self): # also tests hermeval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -367,8 +367,7 @@ class TestVander(TestCase): van = herme.hermevander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_hermevander3d(self) : + def test_hermevander3d(self): # also tests hermeval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -384,8 +383,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_hermefit(self) : - def f(x) : + def test_hermefit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -462,12 +461,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_hermefromroots(self) : + def test_hermefromroots(self): res = herme.hermefromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) pol = herme.hermefromroots(roots) res = herme.hermeval(roots, pol) @@ -476,15 +475,15 @@ class TestMisc(TestCase) : assert_almost_equal(herme.herme2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_hermeroots(self) : + def test_hermeroots(self): assert_almost_equal(herme.hermeroots([1]), []) assert_almost_equal(herme.hermeroots([1, 1]), [-1]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = herme.hermeroots(herme.hermefromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_hermetrim(self) : + def test_hermetrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -495,15 +494,15 @@ class TestMisc(TestCase) : assert_equal(herme.hermetrim(coef, 1), coef[:-3]) assert_equal(herme.hermetrim(coef, 2), [0]) - def test_hermeline(self) : + def test_hermeline(self): assert_equal(herme.hermeline(3, 4), [3, 4]) - def test_herme2poly(self) : - for i in range(10) : + def test_herme2poly(self): + for i in range(10): assert_almost_equal(herme.herme2poly([0]*i + [1]), Helist[i]) - def test_poly2herme(self) : - for i in range(10) : + def test_poly2herme(self): + for i in range(10): assert_almost_equal(herme.poly2herme(Helist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py index d42bac67c..b3d8fe5ee 100644 --- a/numpy/polynomial/tests/test_laguerre.py +++ b/numpy/polynomial/tests/test_laguerre.py @@ -7,44 +7,45 @@ import numpy as np import numpy.polynomial.laguerre as lag from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) -L0 = np.array([1 ])/1 -L1 = np.array([1, -1 ])/1 -L2 = np.array([2, -4, 1 ])/2 -L3 = np.array([6, -18, 9, -1 ])/6 -L4 = np.array([24, -96, 72, -16, 1 ])/24 -L5 = np.array([120, -600, 600, -200, 25, -1 ])/120 -L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1 ])/720 +L0 = np.array([1])/1 +L1 = np.array([1, -1])/1 +L2 = np.array([2, -4, 1])/2 +L3 = np.array([6, -18, 9, -1])/6 +L4 = np.array([24, -96, 72, -16, 1])/24 +L5 = np.array([120, -600, 600, -200, 25, -1])/120 +L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1])/720 Llist = [L0, L1, L2, L3, L4, L5, L6] -def trim(x) : + +def trim(x): return lag.lagtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_lagdomain(self) : + def test_lagdomain(self): assert_equal(lag.lagdomain, [0, 1]) - def test_lagzero(self) : + def test_lagzero(self): assert_equal(lag.lagzero, [0]) - def test_lagone(self) : + def test_lagone(self): assert_equal(lag.lagone, [1]) - def test_lagx(self) : + def test_lagx(self): assert_equal(lag.lagx, [1, -1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_lagadd(self) : - for i in range(5) : - for j in range(5) : + def test_lagadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -52,9 +53,9 @@ class TestArithmetic(TestCase) : res = lag.lagadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_lagsub(self) : - for i in range(5) : - for j in range(5) : + def test_lagsub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -70,12 +71,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [-i, 2*i + 1, -(i + 1)] assert_almost_equal(lag.lagmulx(ser), tgt) - def test_lagmul(self) : + def test_lagmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = lag.lagval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = lag.lagval(self.x, pol2) @@ -84,9 +85,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_lagdiv(self) : - for i in range(5) : - for j in range(5) : + def test_lagdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -96,7 +97,7 @@ class TestArithmetic(TestCase) : assert_almost_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([9., -14., 6.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -106,14 +107,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - def test_lagval(self) : + def test_lagval(self): #check empty input assert_equal(lag.lagval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Llist] - for i in range(7) : + for i in range(7): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -121,7 +122,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(lag.lagval(x, [1]).shape, dims) @@ -191,9 +192,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_lagint(self) : + def test_lagint(self): # check exceptions assert_raises(ValueError, lag.lagint, [0], .5) assert_raises(ValueError, lag.lagint, [0], -1) @@ -206,7 +207,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [1, -1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -216,7 +217,7 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] lagpol = lag.poly2lag(pol) @@ -224,7 +225,7 @@ class TestIntegral(TestCase) : assert_almost_equal(lag.lagval(-1, lagint), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -234,41 +235,41 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = lag.lagint(tgt, m=1) res = lag.lagint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = lag.lagint(tgt, m=1, k=[k]) res = lag.lagint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1) res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = lag.lagint(tgt, m=1, k=[k], scl=2) res = lag.lagint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -290,29 +291,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_lagder(self) : + def test_lagder(self): # check exceptions assert_raises(ValueError, lag.lagder, [0], .5) assert_raises(ValueError, lag.lagder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = lag.lagder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = lag.lagder(lag.lagint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -334,13 +335,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_lagvander(self) : + def test_lagvander(self): # check for 1d x x = np.arange(3) v = lag.lagvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], lag.lagval(x, coef)) @@ -348,11 +348,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = lag.lagvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], lag.lagval(x, coef)) - def test_lagvander2d(self) : + def test_lagvander2d(self): # also tests lagval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -365,8 +365,7 @@ class TestVander(TestCase): van = lag.lagvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_lagvander3d(self) : + def test_lagvander3d(self): # also tests lagval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -382,8 +381,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_lagfit(self) : - def f(x) : + def test_lagfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -460,12 +459,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_lagfromroots(self) : + def test_lagfromroots(self): res = lag.lagfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) pol = lag.lagfromroots(roots) res = lag.lagval(roots, pol) @@ -474,15 +473,15 @@ class TestMisc(TestCase) : assert_almost_equal(lag.lag2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_lagroots(self) : + def test_lagroots(self): assert_almost_equal(lag.lagroots([1]), []) assert_almost_equal(lag.lagroots([0, 1]), [1]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(0, 3, i) res = lag.lagroots(lag.lagfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_lagtrim(self) : + def test_lagtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -493,15 +492,15 @@ class TestMisc(TestCase) : assert_equal(lag.lagtrim(coef, 1), coef[:-3]) assert_equal(lag.lagtrim(coef, 2), [0]) - def test_lagline(self) : + def test_lagline(self): assert_equal(lag.lagline(3, 4), [7, -4]) - def test_lag2poly(self) : - for i in range(7) : + def test_lag2poly(self): + for i in range(7): assert_almost_equal(lag.lag2poly([0]*i + [1]), Llist[i]) - def test_poly2lag(self) : - for i in range(7) : + def test_poly2lag(self): + for i in range(7): assert_almost_equal(lag.poly2lag(Llist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py index ae7a85851..e248f005d 100644 --- a/numpy/polynomial/tests/test_legendre.py +++ b/numpy/polynomial/tests/test_legendre.py @@ -7,47 +7,48 @@ import numpy as np import numpy.polynomial.legendre as leg from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) - -L0 = np.array([ 1]) -L1 = np.array([ 0, 1]) -L2 = np.array([-1, 0, 3])/2 -L3 = np.array([ 0, -3, 0, 5])/2 -L4 = np.array([ 3, 0, -30, 0, 35])/8 -L5 = np.array([ 0, 15, 0, -70, 0, 63])/8 -L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16 -L7 = np.array([ 0, -35, 0, 315, 0, -693, 0, 429])/16 -L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128 -L9 = np.array([ 0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128 + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) + +L0 = np.array([1]) +L1 = np.array([0, 1]) +L2 = np.array([-1, 0, 3])/2 +L3 = np.array([0, -3, 0, 5])/2 +L4 = np.array([3, 0, -30, 0, 35])/8 +L5 = np.array([0, 15, 0, -70, 0, 63])/8 +L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16 +L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429])/16 +L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128 +L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128 Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9] -def trim(x) : + +def trim(x): return leg.legtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_legdomain(self) : + def test_legdomain(self): assert_equal(leg.legdomain, [-1, 1]) - def test_legzero(self) : + def test_legzero(self): assert_equal(leg.legzero, [0]) - def test_legone(self) : + def test_legone(self): assert_equal(leg.legone, [1]) - def test_legx(self) : + def test_legx(self): assert_equal(leg.legx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-1, 1, 100) - def test_legadd(self) : - for i in range(5) : - for j in range(5) : + def test_legadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -55,9 +56,9 @@ class TestArithmetic(TestCase) : res = leg.legadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_legsub(self) : - for i in range(5) : - for j in range(5) : + def test_legsub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -74,12 +75,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i/tmp, 0, (i + 1)/tmp] assert_equal(leg.legmulx(ser), tgt) - def test_legmul(self) : + def test_legmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = leg.legval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = leg.legval(self.x, pol2) @@ -88,9 +89,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_legdiv(self) : - for i in range(5) : - for j in range(5) : + def test_legdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -100,7 +101,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2., 2., 2.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -110,14 +111,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - def test_legval(self) : + def test_legval(self): #check empty input assert_equal(leg.legval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Llist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -125,7 +126,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(leg.legval(x, [1]).shape, dims) @@ -195,9 +196,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_legint(self) : + def test_legint(self): # check exceptions assert_raises(ValueError, leg.legint, [0], .5) assert_raises(ValueError, leg.legint, [0], -1) @@ -210,7 +211,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [0, 1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -220,7 +221,7 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] legpol = leg.poly2leg(pol) @@ -228,7 +229,7 @@ class TestIntegral(TestCase) : assert_almost_equal(leg.legval(-1, legint), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -238,41 +239,41 @@ class TestIntegral(TestCase) : assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = leg.legint(tgt, m=1) res = leg.legint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = leg.legint(tgt, m=1, k=[k]) res = leg.legint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1) res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = leg.legint(tgt, m=1, k=[k], scl=2) res = leg.legint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -294,29 +295,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_legder(self) : + def test_legder(self): # check exceptions assert_raises(ValueError, leg.legder, [0], .5) assert_raises(ValueError, leg.legder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = leg.legder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -338,13 +339,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_legvander(self) : + def test_legvander(self): # check for 1d x x = np.arange(3) v = leg.legvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], leg.legval(x, coef)) @@ -352,11 +352,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = leg.legvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], leg.legval(x, coef)) - def test_legvander2d(self) : + def test_legvander2d(self): # also tests polyval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -369,7 +369,7 @@ class TestVander(TestCase): van = leg.legvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - def test_legvander3d(self) : + def test_legvander3d(self): # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -385,8 +385,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_legfit(self) : - def f(x) : + def test_legfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -463,12 +463,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_legfromroots(self) : + def test_legfromroots(self): res = leg.legfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) pol = leg.legfromroots(roots) res = leg.legval(roots, pol) @@ -477,15 +477,15 @@ class TestMisc(TestCase) : assert_almost_equal(leg.leg2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_legroots(self) : + def test_legroots(self): assert_almost_equal(leg.legroots([1]), []) assert_almost_equal(leg.legroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = leg.legroots(leg.legfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_legtrim(self) : + def test_legtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -496,15 +496,15 @@ class TestMisc(TestCase) : assert_equal(leg.legtrim(coef, 1), coef[:-3]) assert_equal(leg.legtrim(coef, 2), [0]) - def test_legline(self) : + def test_legline(self): assert_equal(leg.legline(3, 4), [3, 4]) - def test_leg2poly(self) : - for i in range(10) : + def test_leg2poly(self): + for i in range(10): assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i]) - def test_poly2leg(self) : - for i in range(10) : + def test_poly2leg(self): + for i in range(10): assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 373045aae..77092cd2f 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -6,46 +6,47 @@ from __future__ import division, absolute_import, print_function import numpy as np import numpy.polynomial.polynomial as poly from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) -def trim(x) : + +def trim(x): return poly.polytrim(x, tol=1e-6) -T0 = [ 1] -T1 = [ 0, 1] -T2 = [-1, 0, 2] -T3 = [ 0, -3, 0, 4] -T4 = [ 1, 0, -8, 0, 8] -T5 = [ 0, 5, 0, -20, 0, 16] -T6 = [-1, 0, 18, 0, -48, 0, 32] -T7 = [ 0, -7, 0, 56, 0, -112, 0, 64] -T8 = [ 1, 0, -32, 0, 160, 0, -256, 0, 128] -T9 = [ 0, 9, 0, -120, 0, 432, 0, -576, 0, 256] +T0 = [1] +T1 = [0, 1] +T2 = [-1, 0, 2] +T3 = [0, -3, 0, 4] +T4 = [1, 0, -8, 0, 8] +T5 = [0, 5, 0, -20, 0, 16] +T6 = [-1, 0, 18, 0, -48, 0, 32] +T7 = [0, -7, 0, 56, 0, -112, 0, 64] +T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128] +T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256] Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_polydomain(self) : + def test_polydomain(self): assert_equal(poly.polydomain, [-1, 1]) - def test_polyzero(self) : + def test_polyzero(self): assert_equal(poly.polyzero, [0]) - def test_polyone(self) : + def test_polyone(self): assert_equal(poly.polyone, [1]) - def test_polyx(self) : + def test_polyx(self): assert_equal(poly.polyx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): - def test_polyadd(self) : - for i in range(5) : - for j in range(5) : + def test_polyadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -53,9 +54,9 @@ class TestArithmetic(TestCase) : res = poly.polyadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_polysub(self) : - for i in range(5) : - for j in range(5) : + def test_polysub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -71,16 +72,16 @@ class TestArithmetic(TestCase) : tgt = [0]*(i + 1) + [1] assert_equal(poly.polymulx(ser), tgt) - def test_polymul(self) : - for i in range(5) : - for j in range(5) : + def test_polymul(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(i + j + 1) tgt[i + j] += 1 res = poly.polymul([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_polydiv(self) : + def test_polydiv(self): # check zero division assert_raises(ZeroDivisionError, poly.polydiv, [1], [0]) @@ -91,8 +92,8 @@ class TestArithmetic(TestCase) : assert_equal((quo, rem), ((1, 1), 0)) # check rest. - for i in range(5) : - for j in range(5) : + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1, 2] cj = [0]*j + [1, 2] @@ -112,15 +113,14 @@ class TestEvaluation(TestCase): x = np.random.random((3, 5))*2 - 1 y = poly.polyval(x, [1., 2., 3.]) - - def test_polyval(self) : + def test_polyval(self): #check empty input assert_equal(poly.polyval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [x**i for i in range(5)] - for i in range(5) : + for i in range(5): tgt = y[i] res = poly.polyval(x, [0]*i + [1]) assert_almost_equal(res, tgt) @@ -129,7 +129,7 @@ class TestEvaluation(TestCase): assert_almost_equal(res, tgt) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(poly.polyval(x, [1]).shape, dims) @@ -201,7 +201,7 @@ class TestEvaluation(TestCase): class TestIntegral(TestCase): - def test_polyint(self) : + def test_polyint(self): # check exceptions assert_raises(ValueError, poly.polyint, [0], .5) assert_raises(ValueError, poly.polyint, [0], -1) @@ -214,7 +214,7 @@ class TestIntegral(TestCase): assert_almost_equal(res, [0, 1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -222,14 +222,14 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] res = poly.polyint(pol, m=1, k=[i], lbnd=-1) assert_almost_equal(poly.polyval(-1, res), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -237,41 +237,41 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1) res = poly.polyint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k]) res = poly.polyint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1) res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k], scl=2) res = poly.polyint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -293,29 +293,29 @@ class TestIntegral(TestCase): assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_polyder(self) : + def test_polyder(self): # check exceptions assert_raises(ValueError, poly.polyder, [0], .5) assert_raises(ValueError, poly.polyder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = poly.polyder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = poly.polyder(poly.polyint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -337,13 +337,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_polyvander(self) : + def test_polyvander(self): # check for 1d x x = np.arange(3) v = poly.polyvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], poly.polyval(x, coef)) @@ -351,11 +350,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = poly.polyvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], poly.polyval(x, coef)) - def test_polyvander2d(self) : + def test_polyvander2d(self): # also tests polyval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -368,8 +367,7 @@ class TestVander(TestCase): van = poly.polyvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_polyvander3d(self) : + def test_polyvander3d(self): # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -398,27 +396,27 @@ class TestCompanion(TestCase): assert_(poly.polycompanion([1, 2])[0, 0] == -.5) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_polyfromroots(self) : + def test_polyfromroots(self): res = poly.polyfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) tgt = Tlist[i] res = poly.polyfromroots(roots)*2**(i-1) assert_almost_equal(trim(res), trim(tgt)) - def test_polyroots(self) : + def test_polyroots(self): assert_almost_equal(poly.polyroots([1]), []) assert_almost_equal(poly.polyroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = poly.polyroots(poly.polyfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_polyfit(self) : - def f(x) : + def test_polyfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -460,8 +458,7 @@ class TestMisc(TestCase) : x = [1, 1j, -1, -1j] assert_almost_equal(poly.polyfit(x, x, 1), [0, 1]) - - def test_polytrim(self) : + def test_polytrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -472,7 +469,7 @@ class TestMisc(TestCase) : assert_equal(poly.polytrim(coef, 1), coef[:-3]) assert_equal(poly.polytrim(coef, 2), [0]) - def test_polyline(self) : + def test_polyline(self): assert_equal(poly.polyline(3, 4), [3, 4]) diff --git a/numpy/polynomial/tests/test_polyutils.py b/numpy/polynomial/tests/test_polyutils.py index 11b37aa1a..c77ee2435 100644 --- a/numpy/polynomial/tests/test_polyutils.py +++ b/numpy/polynomial/tests/test_polyutils.py @@ -7,30 +7,31 @@ import numpy as np import numpy.polynomial.polyutils as pu from numpy.testing import * -class TestMisc(TestCase) : - def test_trimseq(self) : - for i in range(5) : +class TestMisc(TestCase): + + def test_trimseq(self): + for i in range(5): tgt = [1] res = pu.trimseq([1] + [0]*5) assert_equal(res, tgt) - def test_as_series(self) : + def test_as_series(self): # check exceptions assert_raises(ValueError, pu.as_series, [[]]) assert_raises(ValueError, pu.as_series, [[[1, 2]]]) assert_raises(ValueError, pu.as_series, [[1], ['a']]) # check common types types = ['i', 'd', 'O'] - for i in range(len(types)) : - for j in range(i) : + for i in range(len(types)): + for j in range(i): ci = np.ones(1, types[i]) cj = np.ones(1, types[j]) [resi, resj] = pu.as_series([ci, cj]) assert_(resi.dtype.char == resj.dtype.char) assert_(resj.dtype.char == types[i]) - def test_trimcoef(self) : + def test_trimcoef(self): coef = [2, -1, 1, 0] # Test exceptions assert_raises(ValueError, pu.trimcoef, coef, -1) @@ -40,9 +41,9 @@ class TestMisc(TestCase) : assert_equal(pu.trimcoef(coef, 2), [0]) -class TestDomain(TestCase) : +class TestDomain(TestCase): - def test_getdomain(self) : + def test_getdomain(self): # test for real values x = [1, 10, 3, -1] tgt = [-1, 10] @@ -55,7 +56,7 @@ class TestDomain(TestCase) : res = pu.getdomain(x) assert_almost_equal(res, tgt) - def test_mapdomain(self) : + def test_mapdomain(self): # test for real values dom1 = [0, 4] dom2 = [1, 3] @@ -86,7 +87,7 @@ class TestDomain(TestCase) : res = pu.mapdomain(x, dom1, dom2) assert_(isinstance(res, np.matrix)) - def test_mapparms(self) : + def test_mapparms(self): # test for real values dom1 = [0, 4] dom2 = [1, 3] diff --git a/numpy/polynomial/tests/test_printing.py b/numpy/polynomial/tests/test_printing.py index 883597cb9..86cd25732 100644 --- a/numpy/polynomial/tests/test_printing.py +++ b/numpy/polynomial/tests/test_printing.py @@ -3,37 +3,33 @@ from __future__ import division, absolute_import, print_function import numpy.polynomial as poly from numpy.testing import TestCase, run_module_suite, assert_ + class test_str(TestCase): def test_polynomial_str(self): res = str(poly.Polynomial([0, 1])) tgt = 'poly([0., 1.])' assert_(res, tgt) - def test_chebyshev_str(self): res = str(poly.Chebyshev([0, 1])) tgt = 'leg([0., 1.])' assert_(res, tgt) - def test_legendre_str(self): res = str(poly.Legendre([0, 1])) tgt = 'leg([0., 1.])' assert_(res, tgt) - def test_hermite_str(self): res = str(poly.Hermite([0, 1])) tgt = 'herm([0., 1.])' assert_(res, tgt) - def test_hermiteE_str(self): res = str(poly.HermiteE([0, 1])) tgt = 'herme([0., 1.])' assert_(res, tgt) - def test_laguerre_str(self): res = str(poly.Laguerre([0, 1])) tgt = 'lag([0., 1.])' @@ -46,31 +42,26 @@ class test_repr(TestCase): tgt = 'Polynomial([0., 1.])' assert_(res, tgt) - def test_chebyshev_str(self): res = repr(poly.Chebyshev([0, 1])) tgt = 'Chebyshev([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_legendre_repr(self): res = repr(poly.Legendre([0, 1])) tgt = 'Legendre([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_hermite_repr(self): res = repr(poly.Hermite([0, 1])) tgt = 'Hermite([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_hermiteE_repr(self): res = repr(poly.HermiteE([0, 1])) tgt = 'HermiteE([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_laguerre_repr(self): res = repr(poly.Laguerre([0, 1])) tgt = 'Laguerre([0., 1.], [0., 1.], [0., 1.])' diff --git a/site.cfg.example b/site.cfg.example index 89d108739..4cf4a33a4 100644 --- a/site.cfg.example +++ b/site.cfg.example @@ -63,33 +63,46 @@ #library_dirs = /usr/local/lib #include_dirs = /usr/local/include -# Optimized BLAS and LAPACK -# ------------------------- -# Use the blas_opt and lapack_opt sections to give any settings that are -# required to link against your chosen BLAS and LAPACK, including the regular -# FORTRAN reference BLAS and also ATLAS. Some other sections still exist for -# linking against certain optimized libraries (e.g. [atlas], [lapack_atlas]), -# however, they are now deprecated and should not be used. -# -# These are typical configurations for ATLAS (assuming that the library and -# include directories have already been set in [DEFAULT]; the include directory -# is important for the BLAS C interface): -# -#[blas_opt] -#libraries = f77blas, cblas, atlas -# -#[lapack_opt] -#libraries = lapack, f77blas, cblas, atlas -# -# If your ATLAS was compiled with pthreads, the names of the libraries might be -# different: -# -#[blas_opt] -#libraries = ptf77blas, ptcblas, atlas -# -#[lapack_opt] -#libraries = lapack, ptf77blas, ptcblas, atlas +# Atlas +# ----- +# Atlas is an open source optimized implementation of the BLAS and Lapack +# routines. Numpy will try to build against Atlas by default when available in +# the system library dirs. To build numpy against a custom installation of +# Atlas you can add an explicit section such as the following. Here we assume +# that Atlas was configured with ``prefix=/opt/atlas``. +# +# [atlas] +# library_dirs = /opt/atlas/lib +# include_dirs = /opt/atlas/include + +# OpenBLAS +# -------- +# OpenBLAS is another open source optimized implementation of BLAS and Lapack +# and can be seen as an alternative to Atlas. To build numpy against OpenBLAS +# instead of Atlas, use this section instead of the above, adjusting as needed +# for your configuration (in the following example we installed OpenBLAS with +# ``make install PREFIX=/opt/OpenBLAS``. +# +# [openblas] +# libraries = openblas +# library_dirs = /opt/OpenBLAS/lib +# include_dirs = /opt/OpenBLAS/include +# MKL +#---- +# MKL is Intel's very optimized yet proprietary implementation of BLAS and +# Lapack. +# For recent (9.0.21, for example) mkl, you need to change the names of the +# lapack library. Assuming you installed the mkl in /opt, for a 32 bits cpu: +# [mkl] +# library_dirs = /opt/intel/mkl/9.1.023/lib/32/ +# lapack_libs = mkl_lapack +# +# For 10.*, on 32 bits machines: +# [mkl] +# library_dirs = /opt/intel/mkl/10.0.1.014/lib/32/ +# lapack_libs = mkl_lapack +# mkl_libs = mkl, guide # UMFPACK # ------- @@ -110,7 +123,6 @@ #[umfpack] #umfpack_libs = umfpack - # FFT libraries # ------------- # There are two FFT libraries that we can configure here: FFTW (2 and 3) and djbfft. @@ -128,18 +140,3 @@ #[djbfft] #include_dirs = /usr/local/djbfft/include #library_dirs = /usr/local/djbfft/lib - - -# MKL -#---- -# For recent (9.0.21, for example) mkl, you need to change the names of the -# lapack library. Assuming you installed the mkl in /opt, for a 32 bits cpu: -# [mkl] -# library_dirs = /opt/intel/mkl/9.1.023/lib/32/ -# lapack_libs = mkl_lapack -# -# For 10.*, on 32 bits machines: -# [mkl] -# library_dirs = /opt/intel/mkl/10.0.1.014/lib/32/ -# lapack_libs = mkl_lapack -# mkl_libs = mkl, guide |
