+ 1
Is there a more efficient way to calculate the time dilation for a range of velocities instead of using map and lambda?
https://sololearn.com/compiler-playground/cfO7Y3S2vI3M/?ref=app
2 odpowiedzi
+ 4
In your line 38 the time_dilation function takes two arguments, but one of them is essentially a constant. You can use functools.partial() to "fix" this argument, then it's shorter to express the map expression without lambda.
https://docs.python.org/3/library/functools.html#functools.partial
Another tip is looking at numpy.vectorize, this can create another numpy array using any custom function.
https://numpy.org/doc/stable/reference/generated/numpy.vectorize.html
+ 1
You can replace the 'map' and 'lambda' usage with a list comprehension to make the code more concise while maintaining readability. Here is how you can modify the code to calculate time dilation for a range of velocities more efficiently using list comprehension:
velocities = np.linspace(0, 0.995, 100) * speed_of_light
times = [time_dilation(time, v) for v in velocities]