summaryrefslogtreecommitdiff
path: root/numpy/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core/src')
-rw-r--r--numpy/core/src/arrayobject.c26
-rw-r--r--numpy/core/src/scalartypes.inc.src57
2 files changed, 75 insertions, 8 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index 999a43b79..1c2f4c624 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -504,6 +504,23 @@ 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 */
#include "scalartypes.inc"
#include "arraytypes.inc"
@@ -6198,15 +6215,8 @@ array_asarray_get(PyArrayObject *self)
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");
+ matrix_type = _load_matrix_type();
if (matrix_type == NULL) return NULL;
}
if (self->nd > 2) {
diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src
index ed53778aa..1945691f1 100644
--- a/numpy/core/src/scalartypes.inc.src
+++ b/numpy/core/src/scalartypes.inc.src
@@ -926,6 +926,47 @@ 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)
+{
+ Py_INCREF(self);
+ return self;
+}
+
+
static PyGetSetDef gentype_getsets[] = {
{"ndim",
(getter)gentype_ndim_get,
@@ -979,6 +1020,22 @@ 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,