-2

I am writing a code for circular convolution and now I am stuck at position where I need to create circular shift matrix can anyone help me to do this using python or numpy

I want to shift this matrix circularly [1, -1, 2, 0]

I want matrix like,

[ 1, -1,  2,  0]
[ 0,  1, -1,  2]
[-2,  0,  1, -1]
[-1, -2,  0,  1]

code :-

https://drive.google.com/file/d/16XNJ7Q5Iwdlg6Ouz8HU8PgW17xCd1gTp/view?usp=sharing

DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • 2
    Hi welcome to SO! Please add your code as text **not** as pictures. It helps if people can just cut & paste your code. People are very unlikely to type out chunks of code. – DrBwts Aug 04 '21 at 15:53
  • [Check out the answer to this question about rotating elements in a list](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) – DrBwts Aug 04 '21 at 15:57
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – martineau Aug 04 '21 at 16:42
  • Does this answer your question? [Efficient way to rotate a list in python](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) – ti7 Aug 04 '21 at 16:53

2 Answers2

0

when you shift by n you take last n elements and put then in the front side of the list and that is l[len(l)-n:]. And remaining 0 to len(l)-n-1 elements you put at the end and that is l[0:len(l)-n]

def shift(l,n):
    return l[len(l)-n:] + l[0:len(l)-n]

output = []
m = [1, -1, 2, 0]
for i in range(4):
    output.append(shift(m, i))
    
print(output)

# [[1, -1, 2, 0], 
#  [0, 1, -1, 2], 
#  [2, 0, 1, -1], 
#  [-1, 2, 0, 1]]
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0

As suggested in a duplicate, collections.deque.rotate (builtin library) or numpy.roll (more efficient 3rd-party library) is almost-certainly what you're looking for!

>>> from collections import deque as Deque
>>> d = Deque([1, -1, 2, 0])
>>> d
deque([1, -1, 2, 0])
>>> d.rotate(1)
>>> d
deque([0, 1, -1, 2])
>>> import numpy as np
>>> arr = np.array([1, -1, 2, 0])
>>> np.roll(arr, 1)
array([ 0,  1, -1,  2])
>>> np.roll(arr, 2)
array([ 2,  0,  1, -1])

NOTE that the deque mutates the original collection, while numpy.roll returns a rotated copy


You can create a single DataFrame by assembling each possible roll for the length of the array, though you may find it's more efficient to calculate the rolls when you need them

>>> arr = np.array([1, 2])
>>> pd.DataFrame([np.roll(arr, roll_index) for roll_index in range(len(arr))])
   0  1
0  1  2
1  2  1
>>> arr = np.array([1, -1, 2, 0, 9])
>>> pd.DataFrame([np.roll(arr, roll_index) for roll_index in range(len(arr))])
   0  1  2  3  4
0  1 -1  2  0  9
1  9  1 -1  2  0
2  0  9  1 -1  2
3  2  0  9  1 -1
4 -1  2  0  9  1
ti7
  • 16,375
  • 6
  • 40
  • 68
  • Thanks , I already tried this but I want all this in matrix form in one variable – suyog brid Aug 05 '21 at 07:45
  • @suyogbrid I added another block about how to create a single DataFrame from the roll, assuming that's what you're after! – ti7 Aug 20 '21 at 18:11