diff options
-rw-r--r-- | scipy/base/_internal.py | 5 | ||||
-rw-r--r-- | scipy/base/code_generators/generate_umath.py | 21 | ||||
-rw-r--r-- | scipy/base/numeric.py | 8 | ||||
-rw-r--r-- | scipy/base/src/arrayobject.c | 6 | ||||
-rw-r--r-- | scipy/base/src/multiarraymodule.c | 1 | ||||
-rw-r--r-- | scipy/base/src/scalartypes.inc.src | 25 | ||||
-rw-r--r-- | scipy/base/src/umathmodule.c.src | 48 | ||||
-rw-r--r-- | scipy/base/ufunclike.py | 15 |
8 files changed, 80 insertions, 49 deletions
diff --git a/scipy/base/_internal.py b/scipy/base/_internal.py index f5d37f57f..94ec1da57 100644 --- a/scipy/base/_internal.py +++ b/scipy/base/_internal.py @@ -21,12 +21,13 @@ _cnum = _flagdict['CONTIGUOUS'] _fnum = _flagdict['FORTRAN'] class flagsobj(dict): - def __init__(self, arr, flags): + def __init__(self, arr, flags, scalar): self._arr = arr self._flagnum = flags for k in _defflags: num = _flagdict[k] dict.__setitem__(self, k, flags & num == num) + self.scalar = scalar def __getitem__(self, key): if not isinstance(key, str): @@ -68,6 +69,8 @@ class flagsobj(dict): raise KeyError, "Unknown flag: %s" % key def __setitem__(self, item, val): + if self.scalar: + raise ValueError, "Cannot set flags on array scalars." val = not not val # convert to boolean if item not in _setable: raise KeyError, "Cannot set flag", item diff --git a/scipy/base/code_generators/generate_umath.py b/scipy/base/code_generators/generate_umath.py index 3d4546be2..a68d3854f 100644 --- a/scipy/base/code_generators/generate_umath.py +++ b/scipy/base/code_generators/generate_umath.py @@ -73,11 +73,12 @@ defdict = { (1,1), None, "takes the conjugate of x elementwise." ], -'remainder' : [intflt,fltsO, - ("fmod,"*3, "PyNumber_Remainder"), - (2,1), Zero, - "computes x1 % x2 elementwise." - ], + +'fmod' : [intflt,fltsM, + ("fmod,"*3, "fmod"), + (2,1), Zero, + "computes (C-like) x1 % x2 elementwise." + ], 'power' : [nobool,noint, ("pow,"*6, "PyNumber_Power"), @@ -266,10 +267,12 @@ defdict = { (2,1), None, "a safe and correct arctan(x1/x2)" ], -'fmod' : [fltsM, fltsM, - ("fmod,"*3,'"fmod"'), - (2,1), None, - "computes x1-n*x2 where n is the quotient of x1 / x2"], + +'remainder' : [intflt, 'O', + ("PyObject_Remainder"), + (2,1), None, + "computes x1-n*x2 where n is floor(x1 / x2)"], + 'hypot' : [fltsM, fltsM, ("hypot,"*3, '"hypot"'), (2,1), None, diff --git a/scipy/base/numeric.py b/scipy/base/numeric.py index d2c98c573..931a6cfff 100644 --- a/scipy/base/numeric.py +++ b/scipy/base/numeric.py @@ -68,8 +68,7 @@ def zeros_like(a): use empty_like(), which is faster as it only allocates memory.""" a = asarray(a) - return a.__array_wrap__(zeros(a.shape,a.dtype, - a.flags['FORTRAN'] and a.ndim > 1)) + return zeros(a.shape,a.dtype, a.flags['FNC']) def empty_like(a): """Return an empty (uninitialized) array of the shape and typecode of a. @@ -78,9 +77,8 @@ def empty_like(a): your array to be initialized, you should use zeros_like(). """ - asarray(a) - return a.__array_wrap__(empty(a.shape,a.dtype, - a.flags['FORTRAN'] and a.ndim > 1)) + a = asarray(a) + return empty(a.shape,a.dtype, a.flags['FNC']) # end Fernando's utilities diff --git a/scipy/base/src/arrayobject.c b/scipy/base/src/arrayobject.c index cf8b002b0..305bf46bf 100644 --- a/scipy/base/src/arrayobject.c +++ b/scipy/base/src/arrayobject.c @@ -2341,7 +2341,7 @@ array_divmod(PyArrayObject *op1, PyObject *op2) { PyObject *divp, *modp, *result; - divp = array_divide(op1, op2); + divp = array_floor_divide(op1, op2); if (divp == NULL) return NULL; modp = array_remainder(op1, op2); if (modp == NULL) { @@ -3556,8 +3556,8 @@ array_flags_get(PyArrayObject *self) module = PyImport_ImportModule("scipy.base._internal"); if (module == NULL) return NULL; } - return PyObject_CallMethod(module, "flagsobj", "Oi", - self, self->flags); + return PyObject_CallMethod(module, "flagsobj", "Oii", + self, self->flags, 0); } /* diff --git a/scipy/base/src/multiarraymodule.c b/scipy/base/src/multiarraymodule.c index 5c060a1fb..eb85c10fe 100644 --- a/scipy/base/src/multiarraymodule.c +++ b/scipy/base/src/multiarraymodule.c @@ -3727,7 +3727,6 @@ array_can_cast_safely(PyObject *dummy, PyObject *args, PyObject *kwds) return retobj; } - static struct PyMethodDef array_module_methods[] = { {"set_string_function", (PyCFunction)array_set_string_function, METH_VARARGS|METH_KEYWORDS, doc_set_string_function}, diff --git a/scipy/base/src/scalartypes.inc.src b/scipy/base/src/scalartypes.inc.src index 7825fab21..34041ea99 100644 --- a/scipy/base/src/scalartypes.inc.src +++ b/scipy/base/src/scalartypes.inc.src @@ -513,20 +513,17 @@ gentype_ndim_get(PyObject *self) static PyObject * gentype_flags_get(PyObject *self) { - PyObject *dict; - dict = PyDict_New(); - -#define ADDFLAG(flag, val) PyDict_SetItemString(dict, #flag, Py_##val); - - ADDFLAG(CONTIGUOUS, True); - ADDFLAG(OWN_DATA, True); - ADDFLAG(FORTRAN, True); - ADDFLAG(ALIGNED, True); - ADDFLAG(NOTSWAPPED, True); - ADDFLAG(WRITEABLE, False); - ADDFLAG(UPDATEIFCOPY, False); - return dict; -#undef ADDFLAG + static int flags=CONTIGUOUS | OWNDATA | FORTRAN | ALIGNED | \ + NOTSWAPPED; + static PyObject *module=NULL; + + if (module==NULL) { + module = PyImport_ImportModule("scipy.base._internal"); + if (module == NULL) return NULL; + } + return PyObject_CallMethod(module, "flagsobj", "Oii", + self, flags, 1); + } static PyObject * diff --git a/scipy/base/src/umathmodule.c.src b/scipy/base/src/umathmodule.c.src index 67742fa7e..758b45be8 100644 --- a/scipy/base/src/umathmodule.c.src +++ b/scipy/base/src/umathmodule.c.src @@ -1266,11 +1266,53 @@ static void /**begin repeat +#TYPE=BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG# +#typ=byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong# +*/ +static void +@TYPE@_remainder(char **args, intp *dimensions, intp *steps, void *func) +{ + register intp i; + intp is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0]; + char *i1=args[0], *i2=args[1], *op=args[2]; + double x, y, res; + for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) { + x = *((@typ@ *)i1); + y = *((@typ@ *)i2); + res = x - floor(x/y)*y; + *((@typ@ *)op)= (@typ@)(res); + } +} +/**end repeat**/ + +/**begin repeat +#TYPE=FLOAT,DOUBLE,LONGDOUBLE# +#typ=float,double,longdouble# +#c=f,,l# +*/ +static void +@TYPE@_remainder(char **args, intp *dimensions, intp *steps, void *func) +{ + register intp i; + intp is1=steps[0],is2=steps[1],os=steps[2], n=dimensions[0]; + char *i1=args[0], *i2=args[1], *op=args[2]; + @typ@ x, y, res; + for(i=0; i<n; i++, i1+=is1, i2+=is2, op+=os) { + x = *((@typ@ *)i1); + y = *((@typ@ *)i2); + res = x - floor@c@(x/y)*y; + *((@typ@ *)op)= res; + } +} +/**end repeat**/ + + +/**begin repeat #TYPE=(BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG)*6# #typ=(byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong)*6# #OP= %*10, &*10, |*10, ^*10, <<*10, >>*10# -#kind=remainder*10, bitwise_and*10, bitwise_or*10, bitwise_xor*10, left_shift*10, right_shift*10# +#kind=fmod*10, bitwise_and*10, bitwise_or*10, bitwise_xor*10, left_shift*10, right_shift*10# */ static void @@ -1581,7 +1623,7 @@ static struct PyMethodDef methods[] = { }; DL_EXPORT(void) initumath(void) { - PyObject *m, *d, *s, *c_api; + PyObject *m, *d, *s, *s2, *c_api; double pinf, pzero, mynan; /* Create the module and add the functions */ @@ -1662,11 +1704,13 @@ DL_EXPORT(void) initumath(void) { PyModule_AddObject(m, "NAN", PyFloat_FromDouble(mynan)); s = PyDict_GetItemString(d, "conjugate"); + s2 = PyDict_GetItemString(d, "remainder"); /* Setup the array object's numerical structures with appropriate ufuncs in d*/ PyArray_SetNumericOps(d); PyDict_SetItemString(d, "conj", s); + PyDict_SetItemString(d, "mod", s2); err: /* Check for errors */ diff --git a/scipy/base/ufunclike.py b/scipy/base/ufunclike.py index a64d01a3f..8ef479057 100644 --- a/scipy/base/ufunclike.py +++ b/scipy/base/ufunclike.py @@ -3,7 +3,7 @@ import numeric as _nx from numeric import asarray, empty, empty_like, isinf, signbit import umath -__all__ = ['fix','mod','isneginf','isposinf','sign'] +__all__ = ['fix','isneginf','isposinf','sign'] def fix(x, y=None): """ Round x to nearest integer towards zero. @@ -16,19 +16,6 @@ def fix(x, y=None): y[x<0] = y[x<0]+1 return y -def mod(x,y,z=None): - """ x - y*floor(x/y) - - For numeric arrays, x % y has the same sign as x while - mod(x,y) has the same sign as y. - """ - x = asarray(x) - y = asarray(y) - if z is None: - z = empty_like(x) - tmp = _nx.floor(x*1.0/y) - return _nx.subtract(x, y*tmp, z) - def isposinf(x, y=None): if y is None: y = empty(x.shape, dtype='?') |