summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-03-23 22:14:45 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-03-23 22:14:45 +0000
commit2db96a7f02233493849343ff9fed470cd7dc30bd (patch)
tree000a2b0c1afc209c713e2281cf77af85512c0ebe
parent3fa921bb348ca4fabfbf00973bc61be1b59c6097 (diff)
downloadnumpy-2db96a7f02233493849343ff9fed470cd7dc30bd.tar.gz
Added ascontiguous and asfortran functions.
-rw-r--r--numpy/core/numeric.py9
-rw-r--r--numpy/core/src/multiarraymodule.c39
2 files changed, 35 insertions, 13 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py
index 1769894a5..689e5c733 100644
--- a/numpy/core/numeric.py
+++ b/numpy/core/numeric.py
@@ -4,7 +4,8 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc',
'getbuffer',
'where', 'concatenate', 'fastCopyAndTranspose', 'lexsort',
'register_dtype', 'set_numeric_ops', 'can_cast',
- 'asarray', 'asanyarray', 'isfortran', 'empty_like', 'zeros_like',
+ 'asarray', 'asanyarray', 'ascontiguous', 'asfortran',
+ 'isfortran', 'empty_like', 'zeros_like',
'correlate', 'convolve', 'inner', 'dot', 'outer', 'vdot',
'alterdot', 'restoredot', 'cross',
'array2string', 'get_printoptions', 'set_printoptions',
@@ -119,6 +120,12 @@ def asanyarray(a, dtype=None, copy=False, fortran=None, ndmin=0):
"""
return array(a, dtype, copy=copy, fortran=fortran, subok=1, ndmin=ndmin)
+def ascontiguous(a, dtype=None, copy=False):
+ return array(a, dtype, copy=copy, fortran=False, ndmin=1)
+
+def asfortran(a, dtype=None, copy=False):
+ return array(a, dtype, copy=copy, fortran=True, ndmin=1)
+
def isfortran(a):
return a.flags.fnc
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c
index 0ec933aeb..101af121d 100644
--- a/numpy/core/src/multiarraymodule.c
+++ b/numpy/core/src/multiarraymodule.c
@@ -4414,8 +4414,8 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin)
#define _ARET(x) PyArray_Return((PyArrayObject *)(x))
-static char doc_fromobject[] = "array(object, dtype=None, copy=1, fortran=0, "\
- "subok=0,ndmin=0)\n"\
+static char doc_fromobject[] = "array(object, dtype=None, copy=1, "\
+ "fortran=None, subok=0,ndmin=0)\n"\
"will return a new array formed from the given object type given.\n"\
"Object can be anything with an __array__ method, or any object\n"\
"exposing the array interface, or any (nested) sequence.\n"\
@@ -4428,7 +4428,16 @@ static char doc_fromobject[] = "array(object, dtype=None, copy=1, fortran=0, "\
"array may be returned. Otherwise, a base-class ndarray is returned\n"\
"The ndmin argument specifies how many dimensions the returned\n"\
"array should have as a minimum. 1's will be pre-pended to the\n"\
- "shape as needed to meet this requirement.";
+ "shape as needed to meet this requirement. If fortran is None\n"\
+ "then single-segment array is not guaranteed. If fortran is False\n" \
+ "then a C-style contiguous array will be returned. If fotran is True\n"\
+ "then a Fortran-style contiguous array will be returned.";
+
+#define STRIDING_OK(op, fortran) ((fortran) == PyArray_DONTCARE || \
+ ((fortran) == PyArray_FALSE && \
+ PyArray_ISCONTIGUOUS(op)) || \
+ ((fortran) == PyArray_TRUE && \
+ PyArray_ISFORTRAN(op)))
static PyObject *
_array_fromobject(PyObject *ignored, PyObject *args, PyObject *kws)
@@ -4444,6 +4453,12 @@ _array_fromobject(PyObject *ignored, PyObject *args, PyObject *kws)
PyArray_CONDITION fortran=PyArray_DONTCARE;
int flags=0;
+ if (PyTuple_GET_SIZE(args) > 2) {
+ PyErr_SetString(PyExc_ValueError,
+ "only 2 non-keyword arguments accepted");
+ return NULL;
+ }
+
if(!PyArg_ParseTupleAndKeywords(args, kws, "O|O&O&O&O&i", kwd, &op,
PyArray_DescrConverter2,
&type,
@@ -4456,8 +4471,7 @@ _array_fromobject(PyObject *ignored, PyObject *args, PyObject *kws)
/* fast exit if simple call */
if (PyArray_CheckExact(op)) {
if (type==NULL) {
- if (!copy && (fortran == PyArray_DONTCARE \
- || fortran==PyArray_ISFORTRAN(op))) {
+ if (!copy && STRIDING_OK(op, fortran)) {
Py_INCREF(op);
ret = op;
goto finish;
@@ -4471,8 +4485,7 @@ _array_fromobject(PyObject *ignored, PyObject *args, PyObject *kws)
/* One more chance */
oldtype = PyArray_DESCR(op);
if (PyArray_EquivTypes(oldtype, type)) {
- if (!copy && (fortran == PyArray_DONTCARE || \
- fortran==PyArray_ISFORTRAN(op))) {
+ if (!copy && STRIDING_OK(op, fortran)) {
Py_INCREF(op);
ret = op;
goto finish;
@@ -4492,11 +4505,13 @@ _array_fromobject(PyObject *ignored, PyObject *args, PyObject *kws)
if (copy) {
flags = ENSURECOPY;
}
- if (fortran!=PyArray_FALSE && \
- ((fortran == PyArray_TRUE) ||
- (PyArray_Check(op) && PyArray_ISFORTRAN(op)))) {
- flags |= FORTRAN;
- }
+ if (fortran == PyArray_FALSE) {
+ flags |= CONTIGUOUS;
+ }
+ else if ((fortran == PyArray_TRUE) ||
+ (PyArray_Check(op) && PyArray_ISFORTRAN(op))) {
+ flags |= FORTRAN;
+ }
if (!subok) {
flags |= ENSUREARRAY;
}