Added symetric array (can be improved)
This commit is contained in:
20
README.rst
20
README.rst
@@ -527,6 +527,26 @@ Expert
|
|||||||
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
|
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
|
Master
|
||||||
======
|
======
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user