On computing the discrete Fourier and cosine transforms