3 ms·
"There's very little to be gained by reading pure Fortran code, especially if you don't know the mathematical tricks/optimisations used beforehand. The Python c
by sampo 13y ago
"There's very little to be gained by reading pure Fortran code, especially if you don't know the mathematical tricks/optimisations used beforehand. The Python code, on the other hand, stays (relatively) readable."
Depends. His first Python code was:
x = np.asarray(x, dtype=float)
N = x.shape[0]
n = np.arange(N)
k = n.reshape((N, 1))
M = np.exp(-2j * np.pi * k * n / N)
return np.dot(M, x)
I can write more or less the same in Fortran:
n = reshape([(i, i=0,len-1)], shape=[1,len])
k = transpose(n)
M = exp(-2 * j * pi * matmul(k,n) / len)
res = matmul(M, x)
But of course Fortran is a typed language, so I need to explicitly write the types for everything, so my whole function gets longer
pure function dft_slow(x, len) result(res)
complex, intent(in) :: x(len)
integer, intent(in) :: len
complex :: n(1,len), k(len,1), M(len, len), res(len)
integer :: i
n = reshape([(i, i=0,len-1)], shape=[1,len])
k = transpose(n)
M = exp(-2 * j * pi * matmul(k,n) / len)
res = matmul(M, x)
end function dft_slow
and so yes, maybe less readable.
P.S. One must also define somewhere global constants
complex, parameter :: j = (0,1) ! imaginary unit
real, parameter :: pi = acos(-1.)
for the above function to work. Also everything is by default in single precision here.
- vonmoltke 13y agoI have a sudden urge to find an excuse to write Fortran again.