First, to compute the difference, instead of
C[:, :, 1:] - C[:, :, 0:C.shape[2]-1]
you could use numpy.diff:
np.diff(C, axis = -1)
In [27]: C = np.random.rand(2,3,3)
In [28]: D = C[:, :, 1:] - C[:, :, 0:C.shape[2]-1]
In [29]: E = np.diff(C, axis = -1)
In [30]: np.allclose(D, E)
Out[30]: True
Next, if you know you want to retrieve the original C
, perhaps it is better not to overwrite the values in the first place. Just save the differences in a separate array:
E = np.diff(C, axis = -1)
After all, there is no quicker way to perform a calculation than to not compute at all :).
But if you really do want to overwrite the values, then, to retrieve the original values, use np.cumsum:
In [20]: C = np.random.rand(2,3,3)
In [21]: D = C.copy()
In [22]: C[:, :, 1:] = np.diff(C, axis = -1)
In [23]: C = np.cumsum(C, axis = -1)
In [24]: np.allclose(C,D)
Out[24]: True