Advertisement
Guest User

Untitled

a guest
Apr 10th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1.  
  2. # Fast & simple numpy.linspace replacement (sans the `endpoint` and `retstep` params)
  3.  
  4. # Based on recipe by Peter Williams
  5. # http://code.activestate.com/recipes/66472/#c12
  6.  
  7. def linspace(start, stop, n=50):
  8. L = [0.0] * n
  9. nm1 = n - 1
  10. nm1inv = 1.0 / nm1
  11. for i in range(n):
  12. L[i] = nm1inv * (start*(nm1 - i) + stop*i)
  13. return L
  14.  
  15. if __name__ == '__main__':
  16. # Test needs Numpy
  17. from numpy import linspace as numpy_linspace
  18.  
  19. help(numpy_linspace)
  20.  
  21. for args in (
  22. (1, 100),
  23. (0, 1, 50),
  24. (-3, 5, 22),
  25. (5, -4, 451),
  26. (0, 0, 42),
  27. ):
  28. numpy_result = numpy_linspace(*args)
  29. my_result = linspace(*args)
  30. assert len(numpy_result) == len(my_result)
  31. for a, b in zip(numpy_result, my_result):
  32. assert abs(a - b) < 0.00000001
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement