diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/include/numpy/npy_common.h | 22 | ||||
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 9 | ||||
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 54 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 16 | ||||
-rw-r--r-- | numpy/polynomial/__init__.py | 11 | ||||
-rw-r--r-- | numpy/polynomial/polytemplate.py | 40 |
6 files changed, 115 insertions, 37 deletions
diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index 6ca83cc29..ad326da68 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -131,17 +131,23 @@ typedef unsigned PY_LONG_LONG npy_ulonglong; # ifdef _MSC_VER # define NPY_LONGLONG_FMT "I64d" # define NPY_ULONGLONG_FMT "I64u" -# define NPY_LONGLONG_SUFFIX(x) (x##i64) -# define NPY_ULONGLONG_SUFFIX(x) (x##Ui64) +# elif defined(__APPLE__) || defined(__FreeBSD__) +/* "%Ld" only parses 4 bytes -- "L" is floating modifier on MacOS X/BSD */ +# define NPY_LONGLONG_FMT "lld" +# define NPY_ULONGLONG_FMT "llu" +/* + another possible variant -- *quad_t works on *BSD, but is deprecated: + #define LONGLONG_FMT "qd" + #define ULONGLONG_FMT "qu" +*/ # else - /* #define LONGLONG_FMT "lld" Another possible variant - #define ULONGLONG_FMT "llu" - - #define LONGLONG_FMT "qd" -- BSD perhaps? - #define ULONGLONG_FMT "qu" - */ # define NPY_LONGLONG_FMT "Ld" # define NPY_ULONGLONG_FMT "Lu" +# endif +# ifdef _MSC_VER +# define NPY_LONGLONG_SUFFIX(x) (x##i64) +# define NPY_ULONGLONG_SUFFIX(x) (x##Ui64) +# else # define NPY_LONGLONG_SUFFIX(x) (x##LL) # define NPY_ULONGLONG_SUFFIX(x) (x##ULL) # endif diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index ba4b2fc59..4557bd94d 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -144,10 +144,11 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object) } } else { - /* If the dims match exactly, can assign directly */ - if (ndim == PyArray_NDIM(dest) && - PyArray_CompareLists(dims, PyArray_DIMS(dest), - ndim)) { + /* + * If there are more than enough dims, use AssignFromSequence + * because it can handle this style of broadcasting. + */ + if (ndim >= PyArray_NDIM(dest)) { int res; Py_DECREF(dtype); res = PyArray_AssignFromSequence(dest, src_object); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 8d4a7430b..275177559 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -542,26 +542,60 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, npy_intp offset) if (slen < 0) { goto fail; } - if (slen != a->dimensions[dim]) { + /* + * Either the dimensions match, or the sequence has length 1 and can + * be broadcast to the destination. + */ + if (slen != a->dimensions[dim] && slen != 1) { PyErr_Format(PyExc_ValueError, "cannot copy sequence with size %d to array axis " "with dimension %d", (int)slen, (int)a->dimensions[dim]); goto fail; } - for (i = 0; i < slen; i++) { - PyObject *o = PySequence_GetItem(s, i); - if ((a->nd - dim) > 1) { - res = setArrayFromSequence(a, o, dim+1, offset); + /* Broadcast the one element from the sequence to all the outputs */ + if (slen == 1) { + PyObject *o; + npy_intp alen = a->dimensions[dim]; + + o = PySequence_GetItem(s, 0); + if (o == NULL) { + goto fail; } - else { - res = a->descr->f->setitem(o, (a->data + offset), a); + for (i = 0; i < alen; i++) { + if ((a->nd - dim) > 1) { + res = setArrayFromSequence(a, o, dim+1, offset); + } + else { + res = a->descr->f->setitem(o, (a->data + offset), a); + } + if (res < 0) { + Py_DECREF(o); + goto fail; + } + offset += a->strides[dim]; } Py_DECREF(o); - if (res < 0) { - goto fail; + } + /* Copy element by element */ + else { + for (i = 0; i < slen; i++) { + PyObject *o = PySequence_GetItem(s, i); + if (o == NULL) { + goto fail; + } + if ((a->nd - dim) > 1) { + res = setArrayFromSequence(a, o, dim+1, offset); + } + else { + res = a->descr->f->setitem(o, (a->data + offset), a); + } + Py_DECREF(o); + if (res < 0) { + goto fail; + } + offset += a->strides[dim]; } - offset += a->strides[dim]; } Py_DECREF(s); diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 5db874ecd..a4cc2d725 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1613,5 +1613,21 @@ class TestRegression(TestCase): b[...] = a_obj assert_equal(b, a_rec) + def test_assign_obj_listoflists(self): + # Ticket # 1870 + # The inner list should get assigned to the object elements + a = np.zeros(4, dtype=object) + b = a.copy() + a[0] = [1] + a[1] = [2] + a[2] = [3] + a[3] = [4] + b[...] = [[1], [2], [3], [4]] + assert_equal(a, b) + # The first dimension should get broadcast + a = np.zeros((2,2), dtype=object) + a[...] = [[1,2]] + assert_equal(a, [[1,2], [1,2]]) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/polynomial/__init__.py b/numpy/polynomial/__init__.py index 851bde109..48c679ce1 100644 --- a/numpy/polynomial/__init__.py +++ b/numpy/polynomial/__init__.py @@ -298,10 +298,13 @@ def getdomain(x) : from numpy.polynomial.polyutils import getdomain return getdomain(x) -@deprecate(message='Please import mapparms from numpy.polynomial.polyutils') -def mapparms(old, new) : - from numpy.polynomial.polyutils import mapparms - return mapparms(old, new) +# Just remove this function as it screws up the documentation of the same +# named class method. +# +#@deprecate(message='Please import mapparms from numpy.polynomial.polyutils') +#def mapparms(old, new) : +# from numpy.polynomial.polyutils import mapparms +# return mapparms(old, new) @deprecate(message='Please import mapdomain from numpy.polynomial.polyutils') def mapdomain(x, old, new) : diff --git a/numpy/polynomial/polytemplate.py b/numpy/polynomial/polytemplate.py index 657b48508..3763123dc 100644 --- a/numpy/polynomial/polytemplate.py +++ b/numpy/polynomial/polytemplate.py @@ -25,6 +25,10 @@ import numpy as np class $name(pu.PolyBase) : """A $name series class. + $name instances provide the standard Python numerical methods '+', + '-', '*', '//', '%', 'divmod', '**', and '()' as well as the listed + methods. + Parameters ---------- coef : array_like @@ -60,10 +64,10 @@ class $name(pu.PolyBase) : Notes ----- - It is important to specify the domain for many uses of graded polynomial, - for instance in fitting data. This is because many of the important - properties of the polynomial basis only hold in a specified interval and - thus the data must be mapped into that domain in order to benefit. + It is important to specify the domain in many cases, for instance in + fitting data, because many of the important properties of the + polynomial basis only hold in a specified interval and consequently + the data must be mapped into that interval in order to benefit. Examples -------- @@ -408,13 +412,12 @@ class $name(pu.PolyBase) : def copy(self) : """Return a copy. - A new instance of $name is returned that has the same - coefficients and domain as the current instance. + Return a copy of the current $name instance. Returns ------- new_instance : $name - New instance of $name with the same coefficients and domain. + Copy of current instance. """ return self.__class__(self.coef, self.domain, self.window) @@ -514,12 +517,15 @@ class $name(pu.PolyBase) : Parameters ---------- domain : array_like, optional - The domain of the new series type instance. If the value is None, - then the default domain of `kind` is used. + The domain of the converted series. If the value is None, + the default domain of `kind` is used. kind : class, optional The polynomial series type class to which the current instance should be converted. If kind is None, then the class of the current instance is used. + window : array_like, optional + The window of the converted series. If the value is None, + the default window of `kind` is used. Returns ------- @@ -767,9 +773,21 @@ class $name(pu.PolyBase) : @staticmethod def fromroots(roots, domain=$domain, window=$domain) : - """Return $name object with specified roots. + """Return $name instance with specified roots. + + Returns an instance of $name representing the product + ``(x - r[0])*(x - r[1])*...*(x - r[n-1])``, where ``r`` is the + list of roots. - See ${nick}fromroots for full documentation. + Parameters + ---------- + roots : array_like + List of roots. + + Returns + ------- + object : $name + Series with the specified roots. See Also -------- |