summaryrefslogtreecommitdiff
path: root/numpy/core/src/arrayobject.c
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-07-06 09:17:02 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-07-06 09:17:02 +0000
commit2a37179758d8c8db59b0e35c5355c8fd6f9b21dc (patch)
tree0bf459bd9fbf5a5f39430b274d26305a7c440d1d /numpy/core/src/arrayobject.c
parent0f17838356834ecf848140cc931cba1a162fe6c6 (diff)
downloadnumpy-2a37179758d8c8db59b0e35c5355c8fd6f9b21dc.tar.gz
Add .A, .H, .T, .M attributes to the ndarray.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r--numpy/core/src/arrayobject.c72
1 files changed, 72 insertions, 0 deletions
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,