Added symetric array (can be improved)
+20 · 1 file changed
-
README.rst+20 −0
@@ -527,6 +527,26 @@ Expert C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) +3. Create a 2D array subclass such that Z[i,j] == Z[j,i] + + .. code:: python + + # Eric O. Lebigot + # Note: only works for 2d array and value setting using indices + + class Symetric(np.ndarray): + def __setitem__(self, (i,j), value): + super(Symetric, self).__setitem__((i,j), value) + super(Symetric, self).__setitem__((j,i), value) + + def symetric(Z): + return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric) + + S = symetric(np.random.randint(0,10,(5,5)) + S[2,3] = 42 + print S + + Master ======