diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-07 08:18:02 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-07 08:18:02 +0000 |
commit | 80f1d7825004f8be40fa265eaaf165471467112d (patch) | |
tree | 77cac0aee76daab0016121de404830e9fd553dc2 /numpy/core | |
parent | ddfe20b98506f0e0eb5f27286dc515ccab4b44a8 (diff) | |
download | numpy-80f1d7825004f8be40fa265eaaf165471467112d.tar.gz |
Remove .M .A .H attribute. Keep .T attribute as .transpose for >=2d. Creates 2-d from 1-d and returns self for 0-d. Fix-up flag-checking when stride 0 is coupled with dim == 1
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/defmatrix.py | 23 | ||||
-rw-r--r-- | numpy/core/ma.py | 31 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 123 | ||||
-rw-r--r-- | numpy/core/src/scalartypes.inc.src | 44 |
4 files changed, 74 insertions, 147 deletions
diff --git a/numpy/core/defmatrix.py b/numpy/core/defmatrix.py index 455926356..7942e6a93 100644 --- a/numpy/core/defmatrix.py +++ b/numpy/core/defmatrix.py @@ -1,5 +1,5 @@ -__all__ = ['matrix', 'bmat', 'mat', 'asmatrix'] +__all__ = ['matrix', 'bmat', 'mat', 'asmatrix', 'asmat'] import numeric as N from numeric import concatenate, isscalar, binary_repr @@ -282,10 +282,24 @@ class matrix(N.ndarray): from numpy.dual import inv as func else: from numpy.dual import pinv as func - return func(self).M - - I = property(getI, None, doc="inverse") + return asmatrix(func(self)) + + def getA(self): + return self.__array__() + + def getT(self): + return self.transpose() + def getH(self): + if issubclass(self.dtype.type, N.complexfloating): + return self.transpose().conjugate() + else: + return self.transpose() + + T = property(getT, None, doc="transpose") + A = property(getA, None, doc="base array") + H = property(getH, None, doc="hermitian (conjugate) transpose") + I = property(getI, None, doc="inverse") def _from_string(str,gdict,ldict): rows = str.split(';') @@ -349,3 +363,4 @@ def bmat(obj, ldict=None, gdict=None): return matrix(obj) mat = matrix +asmat = asmatrix diff --git a/numpy/core/ma.py b/numpy/core/ma.py index 4a2c51e5d..6231ed5c7 100644 --- a/numpy/core/ma.py +++ b/numpy/core/ma.py @@ -1391,21 +1391,14 @@ array(data = %(data)s, def _get_ctypes(self): return self._data.ctypes - def _get_M(self): - if self._mask is nomask: - return self._data.M - return self.filled().M - - def _get_A(self): - if self._mask is nomask: - return self._data.A - return self.filled().A - def _get_T(self): - return self.swapaxes(-2,-1) - - def _get_H(self): - return self.conjugate().swapaxes(-2,-1) + if (self.ndim == 0): + return self + if (self.ndim == 1): + ret = self.view() + ret.shape = (self.shape[0], 1) + return ret + return self.transpose() shape = property(_get_shape, _set_shape, doc = 'tuple giving the shape of the array') @@ -1422,10 +1415,8 @@ array(data = %(data)s, imag = imaginary ctypes = property(_get_ctypes, None, doc="ctypes") - M = property(_get_M, None, doc="get matrix") - A = property(_get_A, None, doc="get array") + T = property(_get_T, None, doc="get transpose") - H = property(_get_H, None, doc="get conj. transpose") #end class MaskedArray @@ -1871,7 +1862,7 @@ def swapaxes (a, axis1, axis2): def take (a, indices, axis=0): - "take(a, indices, axis=0) returns selection of items from a." + "returns selection of items from a." m = getmask(a) # d = masked_array(a).raw_data() d = masked_array(a).data @@ -1882,7 +1873,7 @@ def take (a, indices, axis=0): mask = numeric.take(m, indices, axis)) def transpose(a, axes=None): - "transpose(a, axes=None) reorder dimensions per tuple axes" + "reorder dimensions per tuple axes" m = getmask(a) d = filled(a) if m is nomask: @@ -1893,7 +1884,7 @@ def transpose(a, axes=None): def put(a, indices, values): - """put(a, indices, values) sets storage-indexed locations to corresponding values. + """sets storage-indexed locations to corresponding values. Values and indices are filled if necessary. diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 5a8fd79f9..f305a9b89 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -503,22 +503,6 @@ copy_and_swap(void *dst, void *src, int itemsize, intp numitems, static PyArray_Descr **userdescrs=NULL; #define error_converting(x) (((x) == -1) && PyErr_Occurred()) - -static PyTypeObject *matrix_type=NULL; - -static PyTypeObject * -_load_matrix_type(void) -{ - PyObject *mod; - PyTypeObject *ret; - /* Load it from numpy */ - mod = PyImport_ImportModule("numpy"); - if (mod == NULL) return NULL; - ret = (PyTypeObject *) \ - PyObject_GetAttrString(mod, "matrix"); - Py_DECREF(mod); - return ret; -} /* Computer-generated arraytype and scalartype code */ @@ -2037,7 +2021,6 @@ parse_index(PyArrayObject *self, PyObject *op, PyObject *op1=NULL; int is_slice; - if (PySlice_Check(op) || op == Py_Ellipsis || op == Py_None) { n = 1; op1 = op; @@ -2075,7 +2058,8 @@ parse_index(PyArrayObject *self, PyObject *op, if (start == -1) break; if (n_steps == PseudoIndex) { - dimensions[nd_new] = 1; strides[nd_new] = 0; nd_new++; + dimensions[nd_new] = 1; strides[nd_new] = 0; + nd_new++; } else { if (n_steps == RubberIndex) { for(j=i+1, n_pseudo=0; j<n; j++) { @@ -4509,6 +4493,7 @@ _IsContiguous(PyArrayObject *ap) register intp sd; register intp dim; register int i; + register intp stride; if (ap->nd == 0) return 1; @@ -4519,7 +4504,9 @@ _IsContiguous(PyArrayObject *ap) dim = ap->dimensions[i]; /* contiguous by definition */ if (dim == 0) return 1; - if (ap->strides[i] != sd) return 0; + stride = ap->strides[i]; + if (stride == 0 && dim == 1) continue; + if (stride != sd) return 0; sd *= dim; } return 1; @@ -4532,6 +4519,7 @@ _IsFortranContiguous(PyArrayObject *ap) register intp sd; register intp dim; register int i; + register intp stride; if (ap->nd == 0) return 1; sd = ap->descr->elsize; @@ -4539,9 +4527,11 @@ _IsFortranContiguous(PyArrayObject *ap) sd == ap->strides[0]); for (i=0; i< ap->nd; ++i) { dim = ap->dimensions[i]; - /* contiguous by definition */ + /* fortran contiguous by definition */ if (dim == 0) return 1; - if (ap->strides[i] != sd) return 0; + stride = ap->strides[i]; + if (stride == 0 && dim == 1) continue; + if (stride != sd) return 0; sd *= dim; } return 1; @@ -4645,7 +4635,8 @@ PyArray_UpdateFlags(PyArrayObject *ret, int flagmask) if (_IsAligned(ret)) ret->flags |= ALIGNED; else ret->flags &= ~ALIGNED; } - /* This is not checked by default WRITEABLE is not part of UPDATE_ALL_FLAGS */ + /* This is not checked by default WRITEABLE is not + part of UPDATE_ALL_FLAGS */ if (flagmask & WRITEABLE) { if (_IsWriteable(ret)) ret->flags |= WRITEABLE; else ret->flags &= ~WRITEABLE; @@ -6191,55 +6182,41 @@ array_flat_set(PyArrayObject *self, PyObject *val) } static PyObject * -array_swaplast_get(PyArrayObject *self) +array_transpose_get(PyArrayObject *self) { - if (self->nd < 2) { - Py_INCREF(self); - return (PyObject *)self; - } - return PyArray_SwapAxes(self, -2, -1); -} - -static PyObject * -array_conj_swaplast_get(PyArrayObject *self) -{ - PyObject *a, *b; - a = PyArray_Conjugate(self); - if (self->nd < 2) { - return a; - } - b = PyArray_SwapAxes((PyArrayObject *)a, -2, -1); - Py_DECREF(a); - return b; -} + intp dims[2]; + intp strides[2]; + PyObject *new; -static PyObject * -array_asarray_get(PyArrayObject *self) -{ - if (PyArray_CheckExact(self)) { + switch(self->nd) { + case 0: Py_INCREF(self); return (PyObject *)self; + case 1: + dims[0] = self->dimensions[0]; + dims[1] = 1; + strides[0] = self->strides[0]; + strides[1] = 0; + Py_INCREF(self->descr); + new = PyArray_NewFromDescr(self->ob_type, + self->descr, + 2, dims, + strides, + self->data, + self->flags, + (PyObject *)self); + if (new==NULL) return NULL; + Py_INCREF(self); + PyArray_BASE(new) = (PyObject *)self; + return new; + default: + return PyArray_Transpose(self, NULL); } - return PyArray_View(self, NULL, &PyArray_Type); -} - -static PyObject * -array_asmatrix_get(PyArrayObject *self) -{ - if (matrix_type == NULL) { - matrix_type = _load_matrix_type(); - if (matrix_type == NULL) return NULL; - } - if (self->nd > 2) { - PyErr_SetString(PyExc_ValueError, - "too many dimensions."); - return NULL; - } - return PyArray_View(self, NULL, matrix_type); } - -/* If this is None, no function call is made */ +/* If this is None, no function call is made + --- default sub-class behavior +*/ static PyObject * array_finalize_get(PyArrayObject *self) { @@ -6307,23 +6284,11 @@ static PyGetSetDef array_getsetlist[] = { {"_as_parameter_", (getter)array_as_parameter_get, NULL, - "allow array to be interpreted as a ctypes object"}, + "allow array to be interpreted as a ctypes object by returning the data memory location as an integer"}, {"T", - (getter)array_swaplast_get, - NULL, - "swap last two axes."}, - {"H", - (getter)array_conj_swaplast_get, - NULL, - "conjugate and swap last two axes."}, - {"M", - (getter)array_asmatrix_get, - NULL, - "same as .view(asmatrix)"}, - {"A", - (getter)array_asarray_get, + (getter)array_transpose_get, NULL, - "same as .view(ndarray)"}, + "return transpose for self.ndim <= 2 "}, {"__array_interface__", (getter)array_interface_get, NULL, diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src index 1945691f1..96accc4b2 100644 --- a/numpy/core/src/scalartypes.inc.src +++ b/numpy/core/src/scalartypes.inc.src @@ -926,38 +926,6 @@ gentype_flat_get(PyObject *self) return ret; } -static PyObject * -gentype_asmatrix_get(PyObject *self) -{ - PyObject *ret, *arr; - arr = PyArray_FromScalar(self, NULL); - if (arr == NULL) return NULL; - if (matrix_type == NULL) { - matrix_type = _load_matrix_type(); - if (matrix_type == NULL) return NULL; - } - ret = PyArray_View((PyArrayObject *)arr, NULL, matrix_type); - Py_DECREF(arr); - return ret; -} - -static PyObject * -gentype_asarray_get(PyObject *self) -{ - return PyArray_FromScalar(self, NULL); -} - -static PyObject * -gentype_ctranspose_get(PyObject *self) -{ - if (PyArray_IsScalar(self, ComplexFloating)) { - return PyObject_CallMethod(self, "conjugate", "()"); - } - else { - Py_INCREF(self); - return self; - } -} static PyObject * gentype_transpose_get(PyObject *self) @@ -1020,22 +988,10 @@ static PyGetSetDef gentype_getsets[] = { (getter)gentype_flat_get, (setter)0, "a 1-d view of scalar"}, - {"M", - (getter)gentype_asmatrix_get, - (setter)0, - "scalars as a matrix"}, - {"A", - (getter)gentype_asarray_get, - (setter)0, - "scalars as array"}, {"T", (getter)gentype_transpose_get, (setter)0, "transpose"}, - {"H", - (getter)gentype_ctranspose_get, - (setter)0, - "conjugate transpose"}, {"__array_interface__", (getter)gentype_interface_get, NULL, |