summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2005-10-20 07:07:53 +0000
committerTravis Oliphant <oliphant@enthought.com>2005-10-20 07:07:53 +0000
commit8bc53bd09afa8be668fcd50f67542f2e21ce2d65 (patch)
treeb721f7234c5780bcc26d135f6e60143a77f75f13
parent143fa8c512e15c72c12301c05f263cb2e08b61b9 (diff)
downloadnumpy-8bc53bd09afa8be668fcd50f67542f2e21ce2d65.tar.gz
Added .fill method.
-rw-r--r--scipy/base/code_generators/generate_array_api.py4
-rw-r--r--scipy/base/include/scipy/arrayobject.h2
-rw-r--r--scipy/base/numeric.py5
-rw-r--r--scipy/base/src/arraymethods.c16
-rw-r--r--scipy/base/src/arrayobject.c55
-rw-r--r--scipy/base/src/scalartypes.inc.src9
-rw-r--r--scipy/doc/CAPI.txt5
7 files changed, 88 insertions, 8 deletions
diff --git a/scipy/base/code_generators/generate_array_api.py b/scipy/base/code_generators/generate_array_api.py
index b6d9b6633..a536a150b 100644
--- a/scipy/base/code_generators/generate_array_api.py
+++ b/scipy/base/code_generators/generate_array_api.py
@@ -262,6 +262,10 @@ objectapi_list = [
(r"""
""",
+ 'FillWithScalar','PyArrayObject *, PyObject *', 'int'),
+
+ (r"""
+ """,
'CheckStrides', 'int, int, intp, intp *, intp *', 'Bool')
]
diff --git a/scipy/base/include/scipy/arrayobject.h b/scipy/base/include/scipy/arrayobject.h
index a252acc52..e68f95786 100644
--- a/scipy/base/include/scipy/arrayobject.h
+++ b/scipy/base/include/scipy/arrayobject.h
@@ -1206,6 +1206,8 @@ typedef struct {
#define PyArray_FROM_O(m) PyArray_FromAny(m, NULL, 0, 0, 0)
#define PyArray_FROM_OF(m,flags) PyArray_FromAny(m, NULL, 0, 0, flags)
+#define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), (val), PyArray_NBYTES(obj))
+
#define REFCOUNT(obj) (((PyObject *)(obj))->ob_refcnt)
#define MAX_ELSIZE 2*SIZEOF_LONGDOUBLE
diff --git a/scipy/base/numeric.py b/scipy/base/numeric.py
index 2e0ac1d35..f2009b8bf 100644
--- a/scipy/base/numeric.py
+++ b/scipy/base/numeric.py
@@ -248,9 +248,8 @@ def ones(shape, dtype=int_, fortran=0):
"""ones(shape, dtype=int_) returns an array of the given
dimensions which is initialized to all ones.
"""
- a=zeros(shape, dtype, fortran)
- a+=1
- ### a[...]=1 -- slower?
+ a = empty(shape, dtype, fortran)
+ a.fill(1)
return a
def identity(n,dtype=int_):
diff --git a/scipy/base/src/arraymethods.c b/scipy/base/src/arraymethods.c
index 977622089..be6422d49 100644
--- a/scipy/base/src/arraymethods.c
+++ b/scipy/base/src/arraymethods.c
@@ -19,6 +19,20 @@ array_take(PyArrayObject *self, PyObject *args, PyObject *kwds)
return _ARET(PyArray_Take(self, indices, dimension));
}
+static char doc_fill[] = "a.fill(value) places the scalar value at every"\
+ "position in the array.";
+
+static PyObject *
+array_fill(PyArrayObject *self, PyObject *args)
+{
+ PyObject *obj;
+ if (!PyArg_ParseTuple(args, "O", &obj))
+ return NULL;
+ if (PyArray_FillWithScalar(self, obj) < 0) return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static char doc_put[] = "a.put(indices, values) sets a.flat[n] = v[n] "\
"for each n in indices. v can be scalar or shorter than indices, "\
"will repeat.";
@@ -1365,6 +1379,8 @@ static PyMethodDef array_methods[] = {
{"dump", (PyCFunction) array_dump, 1, doc_dump},
/* Extended methods added 2005 */
+ {"fill", (PyCFunction)array_fill,
+ METH_VARARGS, doc_fill},
{"transpose", (PyCFunction)array_transpose,
METH_VARARGS, doc_transpose},
{"take", (PyCFunction)array_take,
diff --git a/scipy/base/src/arrayobject.c b/scipy/base/src/arrayobject.c
index 8d9b207eb..1a07b2853 100644
--- a/scipy/base/src/arrayobject.c
+++ b/scipy/base/src/arrayobject.c
@@ -3399,6 +3399,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape)
}
+/* Assumes contiguous */
static void
PyArray_FillObjectArray(PyArrayObject *arr, PyObject *obj)
{
@@ -3419,6 +3420,60 @@ PyArray_FillObjectArray(PyArrayObject *arr, PyObject *obj)
}
}
+static int
+PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
+{
+ PyObject *newarr;
+ int itemsize;
+ void *fromptr;
+ PyArray_Typecode type = {0,0,0};
+ intp size;
+
+ type.type_num = PyArray_TYPE(arr);
+ itemsize = PyArray_ITEMSIZE(arr);
+ type.itemsize = itemsize;
+ newarr = PyArray_FromAny(obj, &type, 0,0, BEHAVED_FLAGS_RO);
+ if (newarr == NULL) return -1;
+ fromptr = PyArray_DATA(newarr);
+ size=PyArray_SIZE(arr);
+ if (PyArray_ISONESEGMENT(arr)) {
+ char *toptr=PyArray_DATA(arr);
+ while(size--) {
+ memcpy(toptr, fromptr, itemsize);
+ toptr += itemsize;
+ }
+ }
+ else {
+ PyArrayIterObject *iter;
+ PyObject *behaved;
+
+ behaved = PyArray_FromAny((PyObject *)arr, NULL, 0,0,
+ BEHAVED_FLAGS | UPDATEIFCOPY);
+ if (behaved == NULL) {
+ Py_DECREF(newarr);
+ return -1;
+ }
+
+ iter = (PyArrayIterObject *) \
+ PyArray_IterNew(behaved);
+
+ if (iter == NULL) {
+ Py_DECREF(behaved);
+ Py_DECREF(newarr);
+ return -1;
+ }
+
+ while(size--) {
+ memcpy(iter->dataptr, fromptr, itemsize);
+ PyArray_ITER_NEXT(iter);
+ }
+
+ Py_DECREF(iter);
+ Py_DECREF(behaved);
+ }
+ Py_DECREF(newarr);
+ return 0;
+}
static PyObject *
array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
diff --git a/scipy/base/src/scalartypes.inc.src b/scipy/base/src/scalartypes.inc.src
index f8742fed1..2119f42fc 100644
--- a/scipy/base/src/scalartypes.inc.src
+++ b/scipy/base/src/scalartypes.inc.src
@@ -861,7 +861,7 @@ gentype_wraparray(PyObject *scalar, PyObject *args)
/**begin repeat
-#name=tolist, toscalar, tostring, astype, copy, resize, __deepcopy__, choose, sort, argsort, searchsorted, argmax, argmin, reshape, view, swapaxes, max, min, ptp, conj, conjugate, nonzero, all, any, flatten, ravel#
+#name=tolist, toscalar, tostring, astype, copy, resize, __deepcopy__, choose, sort, argsort, searchsorted, argmax, argmin, reshape, view, swapaxes, max, min, ptp, conj, conjugate, nonzero, all, any, flatten, ravel, fill, transpose#
*/
static PyObject *
@@ -920,7 +920,7 @@ gentype_byteswap(PyObject *self, PyObject *args)
/**begin repeat
-#name=transpose, getfield, take, put, putmask, repeat, tofile, mean, trace, diagonal, clip, std, sum, cumsum, prod, cumprod, compress#
+#name=getfield, take, put, putmask, repeat, tofile, mean, trace, diagonal, clip, std, sum, cumsum, prod, cumprod, compress#
*/
static PyObject *
@@ -1038,8 +1038,10 @@ static PyMethodDef gentype_methods[] = {
{"dump", (PyCFunction) gentype_dump, 1, NULL},
/* Methods for array */
+ {"fill", (PyCFunction)gentype_fill,
+ METH_VARARGS, NULL},
{"transpose", (PyCFunction)gentype_transpose,
- METH_VARARGS|METH_KEYWORDS, NULL},
+ METH_VARARGS, NULL},
{"take", (PyCFunction)gentype_take,
METH_VARARGS|METH_KEYWORDS, NULL},
{"put", (PyCFunction)gentype_put,
@@ -1634,6 +1636,7 @@ PyArray_TypecodeFromTypeObject(PyObject *type, PyArray_Typecode *typecode) {
itemsize = descr->elsize;
}
typecode->itemsize = itemsize;
+ typecode->fortran = 0;
return;
}
diff --git a/scipy/doc/CAPI.txt b/scipy/doc/CAPI.txt
index 78b4c2c6c..45cd05507 100644
--- a/scipy/doc/CAPI.txt
+++ b/scipy/doc/CAPI.txt
@@ -111,10 +111,11 @@ PyArray_OBJECT.
PyArray_SimpleNew(nd, dims, typenum) is a drop-in replacement for
PyArray_FromDims (except it takes intp* dims instead of int* dims which
- matters on 64-bit systems).
+ matters on 64-bit systems) and it does not initialize
+ the memory to zero.
PyArray_SimpleNew is just a macro for PyArray_New with default arguments.
-
+Use PyArray_FILLWBYTE(arr, 0) to fill with zeros.
The PyArray_FromDims and family of functions are still available and
are loose wrappers around this function. These functions still take