Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Slicing example??

By: a guest on Jan 17th, 2011  |  syntax: Python  |  size: 0.70 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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!'