diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-06 09:17:02 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-06 09:17:02 +0000 |
commit | 2a37179758d8c8db59b0e35c5355c8fd6f9b21dc (patch) | |
tree | 0bf459bd9fbf5a5f39430b274d26305a7c440d1d /numpy | |
parent | 0f17838356834ecf848140cc931cba1a162fe6c6 (diff) | |
download | numpy-2a37179758d8c8db59b0e35c5355c8fd6f9b21dc.tar.gz |
Add .A, .H, .T, .M attributes to the ndarray.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/defmatrix.py | 25 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 72 |
2 files changed, 79 insertions, 18 deletions
diff --git a/numpy/core/defmatrix.py b/numpy/core/defmatrix.py index 839c4f058..455926356 100644 --- a/numpy/core/defmatrix.py +++ b/numpy/core/defmatrix.py @@ -276,25 +276,14 @@ class matrix(N.ndarray): def tolist(self): return self.__array__().tolist() - 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() - def getI(self): - from numpy.dual import inv - return matrix(inv(self)) - - A = property(getA, None, doc="base array") - T = property(getT, None, doc="transpose") - H = property(getH, None, doc="hermitian (conjugate) transpose") + M,N = self.shape + if M == N: + 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") diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 2c0cbebf1..999a43b79 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -6162,6 +6162,62 @@ array_flat_set(PyArrayObject *self, PyObject *val) return retval; } +static PyObject * +array_swaplast_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; +} + +static PyObject * +array_asarray_get(PyArrayObject *self) +{ + if (PyArray_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } + return PyArray_View(self, NULL, &PyArray_Type); +} + +static PyObject * +array_asmatrix_get(PyArrayObject *self) +{ + /* Keeps a reference */ + static PyTypeObject *matrix_type = NULL; + if (matrix_type == NULL) { + PyObject *mod; + /* Load it from numpy */ + mod = PyImport_ImportModule("numpy"); + if (mod == NULL) return NULL; + matrix_type = (PyTypeObject *) \ + PyObject_GetAttrString(mod, "matrix"); + 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 */ static PyObject * array_finalize_get(PyArrayObject *self) @@ -6231,6 +6287,22 @@ static PyGetSetDef array_getsetlist[] = { (getter)array_as_parameter_get, NULL, "allow array to be interpreted as a ctypes object"}, + {"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, + NULL, + "same as .view(ndarray)"}, {"__array_interface__", (getter)array_interface_get, NULL, |