2D array Of All Cyclic Shifts Of A 1D array

Viewed 689

Suppose a is some 1d numppy.array with n elements:

a = np.array([a_0, a_1, ..., a_n_minus_1])

I'd like to generate the 2d (n X n) numpy.array containing, at row i, the ith cyclic shift of a:

np.array([[a_0, a_1, ..., a_n_minus_1], [a_n_minus_1, a_0, a_1, ...], ...]])

preferably without loops. How can this be done efficiently?

(The function np.roll seems related, but apparently takes only a scalar shift.)

2 Answers
Related