I\'d like to \'shear\' a numpy array. I\'m not sure I\'m using the term \'shear\' correctly; by shear, I mean something like:
Shift the first column by 0 places
Here is a cleaned-up version of your own approach:
def shear2(a, strength=1, shift_axis=0, increase_axis=1, edges='clip'):
indices = numpy.indices(a.shape)
indices[shift_axis] -= strength * indices[increase_axis]
indices[shift_axis] %= a.shape[shift_axis]
res = a[tuple(indices)]
if edges == 'clip':
res[indices[shift_axis] < 0] = 0
res[indices[shift_axis] >= a.shape[shift_axis]] = 0
return res
The main difference is that it uses numpy.indices() instead of rolling your own version of this.