Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Fast & simple numpy.linspace replacement (sans the `endpoint` and `retstep` params)
- # Based on recipe by Peter Williams
- # http://code.activestate.com/recipes/66472/#c12
- def linspace(start, stop, n=50):
- L = [0.0] * n
- nm1 = n - 1
- nm1inv = 1.0 / nm1
- for i in range(n):
- L[i] = nm1inv * (start*(nm1 - i) + stop*i)
- return L
- if __name__ == '__main__':
- # Test needs Numpy
- from numpy import linspace as numpy_linspace
- help(numpy_linspace)
- for args in (
- (1, 100),
- (0, 1, 50),
- (-3, 5, 22),
- (5, -4, 451),
- (0, 0, 42),
- ):
- numpy_result = numpy_linspace(*args)
- my_result = linspace(*args)
- assert len(numpy_result) == len(my_result)
- for a, b in zip(numpy_result, my_result):
- assert abs(a - b) < 0.00000001
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement