diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-03 20:17:32 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-03 20:17:32 +0000 |
commit | 7a90a212488d7a55a939b3865b5236108fa28325 (patch) | |
tree | 9cbe2695fbe6ae915487ec22bd952e3706a45130 /numpy/core/numeric.py | |
parent | 6b7f2361d969f0ceda30c5fb10a309531aa27c03 (diff) | |
download | numpy-7a90a212488d7a55a939b3865b5236108fa28325.tar.gz |
Add a require function, a issubdtype, and a load_ctypes_function to NumPy
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index ab5c5fcb3..71c74c123 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -10,7 +10,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', 'alterdot', 'restoredot', 'cross', 'array2string', 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', 'set_string_function', - 'little_endian', + 'little_endian', 'require', 'fromiter', 'indices', 'fromfunction', 'load', 'loads', 'isscalar', 'binary_repr', 'base_repr', @@ -133,6 +133,39 @@ def asfortranarray(a, dtype=None): """ return array(a, dtype, copy=False, order='F', ndmin=1) +def require(a, dtype=None, requirements=None): + if requirements is None: + requirements = [] + else: + requirements = [x.upper() for x in requirements] + + if not requirements: + return asanyarray(a, dtype=dtype) + + if 'ENSUREARRAY' in requirements or 'E' in requirements: + func = asarray + else: + func = asanyarray + + if 'FORCECAST' in requirements or 'FORCE' in requirements: + arr = func(a) + if dtype is not None and arr.dtype != dtype: + arr = arr.astype(dtype) + else: + arr = func(a, dtype=dtype) + + copychar = 'A' + if 'FORTRAN' in requirements or 'F' in requirements: + copychar = 'F' + elif 'CONTIGUOUS' in requirements or 'C' in requirements: + copychar = 'C' + + for prop in requirements: + if not arr.flags[prop]: + arr = arr.copy(copychar) + break + return arr + def isfortran(a): """Returns True if 'a' is laid out in Fortran-order in memory. """ |