From a5924cf5484ff25f806e4d0a288fa6bb08a382e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Fri, 1 Mar 2019 17:00:14 +0200 Subject: [PATCH] Transparent indexing for TCMatrix Former-commit-id: 8fa13d88fa4fba4a8114fb3ffe7be1189d5d4453 --- qpms/qpms_c.pyx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/qpms/qpms_c.pyx b/qpms/qpms_c.pyx index ddca209..1b0d7c8 100644 --- a/qpms/qpms_c.pyx +++ b/qpms/qpms_c.pyx @@ -1076,11 +1076,12 @@ cdef class CTMatrix: # N.B. there is another type called TMatrix in tmatrices.py cdef qpms_tmatrix_t t def __cinit__(CTMatrix self, BaseSpec spec, matrix): - self.t.spec = spec.rawpointer(); + self.spec = spec + self.t.spec = self.spec.rawpointer(); # The following will raise an exception if shape is wrong self.m = np.array(matrix, dtype=complex, copy=True, order='C').reshape((len(spec), len(spec))) - self.m.setflags(write=False) # checkme - cdef const cdouble[:,:] m_memview = self.m + #self.m.setflags(write=False) # checkme + cdef cdouble[:,:] m_memview = self.m self.t.m = &(m_memview[0,0]) self.t.owns_m = False # Memory in self.t.m is "owned" by self.m, not by self.t... @@ -1090,7 +1091,15 @@ cdef class CTMatrix: # N.B. there is another type called TMatrix in tmatrices.py ''' return &(self.t) - def as_array(CTMatrix self): + # Transparent access to the T-matrix elements. + def __getitem__(self, key): + return self.m[key] + def __setitem__(self, key, value): + self.m[key] = value + + def as_ndarray(CTMatrix self): + ''' Returns a copy of the T-matrix as a numpy array.''' + # Maybe not totally needed after all, as np.array(T[...]) should be equivalent and not longer return np.array(self.m, copy=True) cdef class FinitePointGroup: