This is faster for large N but may be considered cheating ;-)
It takes full advantage of the highly regular and repetitive pattern to save lots of evaluations of the square root.
def cheat(N):
values = np.sqrt(np.arange(3*N-2))
result = np.lib.stride_tricks.as_strided(values, (N, N, N), 3*values.strides)
return np.ascontiguousarray(result)
If you can live with a non-contiguous, read-only (by all practical means) view this can be substantially faster:
def cheat_nc_view(N):
values = np.sqrt(np.arange(3*N-2))
return np.lib.stride_tricks.as_strided(values, (N, N, N), 3*values.strides)
For reference:
def cheek(N):
arr = np.arange(N)
return np.sqrt(arr + arr[:,np.newaxis] + arr[:,np.newaxis,np.newaxis])
>>> np.all(cheek(20) == cheat(20))
True
>>> np.all(cheek(200) == cheat_nc_view(200))
True
Timings:
>>> timeit(lambda: cheek(20), number=1000)
0.05387042500660755
>>> timeit(lambda: cheat(20), number=1000)
0.020798540994292125
>>> timeit(lambda: cheat_nc_view(20), number=1000)
0.010791150998556986
>>> timeit(lambda: cheek(200), number=100)
6.823299437994137
>>> timeit(lambda: cheat(200), number=100)
2.0583883369981777
>>> timeit(lambda: cheat_nc_view(200), number=100)
0.0014881940151099116