Advertisement
Guest User

Slicing example??

a guest
Jan 17th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import numpy as np                                                    
  2. import itertools as it      
  3.  
  4. def use_list(x, length):
  5.     c = it.product(x, repeat=length)
  6.     return np.array(list(c), dtype=np.complex64)
  7.  
  8. def use_slicing(x, length):
  9.     data = np.empty((len(x)**length, length), dtype=np.complex64)
  10.     for i in range(length):                                      
  11.         for j in range(len(x)):
  12.             data[j::len(x), i] = x[j]
  13.     return data
  14.  
  15. length = 4
  16. x = [np.complex(1,0), np.complex(-1,0),
  17.      np.complex(0,1), np.complex(0,-1)]
  18.  
  19. a = use_list(x, length)
  20. b = use_slicing(x, length)
  21. print a
  22. print b
  23.  
  24. assert set(map(tuple, a))  == set(map(tuple, b)), 'Differing results!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement