Implement the Discrete Cosine Transform (DCT). This may implemented as either a function or a program and the sequence can be given as either an argument or using standard input. Your program must be able to handle sequences of any length (assuming a hypothetical version of your language which has no limits on things like memory and integer size).

There was a previous challenge for DFT, now lets compute DCT! ;-)

Use the DCT-II definition from Wikipedia:


Your program takes a sequence xn as input, and must produce the corresponding sequence Xk. In this formula, the cosine is in radians.


  • This is so the shortest solution wins.
  • Builtins that compute the DCT in forward or backward (also known as inverse) directions are not allowed.
  • Floating-point inaccuracies will not be counted against you.
  • You don't have to use the exact algorithm shown here, as long as you produce the same results.
  • 2
    \$\begingroup\$ Mind including an explanation on what DCT is? \$\endgroup\$ Commented May 13, 2017 at 0:32
  • \$\begingroup\$ The Wikipedia page shows several different versions. Can you please clarify? Voting to close for now. \$\endgroup\$ Commented May 13, 2017 at 1:04
  • \$\begingroup\$ @Nick Clifford lets assume DCT-II from that wikipedia page, as it clearly says: The DCT-II is probably the most commonly used form, and is often simply referred to as "the DCT" \$\endgroup\$
    – user69099
    Commented May 13, 2017 at 1:35
  • 10
    \$\begingroup\$ Please add some test cases. \$\endgroup\$
    – Shaggy
    Commented May 13, 2017 at 2:33
  • 3
    \$\begingroup\$ @xakepp35 how do we know if our program is correct, if we don't have at least two test cases to test it against? Specifically, as long as you produce the same results as what? \$\endgroup\$
    – Stephen
    Commented May 18, 2017 at 1:13

6 Answers 6


Jelly, 15 bytes


Try it online!

This uses the identity that cos(pi * x) = real(e^(i * pi * x)) = real((-1)^x)


LḶµ+.×þ÷L-*Ḟ³æ.  Input: array A
L                Length
 Ḷ               Lowered range - [0, 1, ..., len(A)-1]
  µ              Begin a new monadic chain
   +.            Add 0.5
     ×þ          Outer product using multiplication
       ÷         Divide by
        L        Length
         -*      Compute (-1)^x
           Ḟ     Real part
             æ.  Dot product with
            ³    The first argument (A)

Mathematica, 79 bytes


Since there are (currently) no test cases, as it turns out, there is a built-in for this: FourierDCT. However, the function "normalizes" the result by dividing it by a value before returning it. Thankfully, the documentation specifies that this division factor is just the square root of the input list's length (for DCT-II).

So, we can verify our results by multiplying the output of FourierDCT by the square root of the length of our input list. For anyone else who tries this problem, here are some test cases:


{10, -2.38896, 0, -2.07193}


{5, 0, 0, 0, 0}

Note that I've slightly edited the output of the last one; the last four values output as very small decimals, around 10-16, due to floating point precision errors around 0. My answer does give 0 for them, if you convert it to a decimal.

This solution gives exact answers when Mathematica can do so. If that's not okay and you want decimals, let me know.


Clojure, 100 bytes

#(for[c[(count %)]k(range c)](apply +(map(fn[n x](*(Math/cos(*(/ c)Math/PI(+ n 0.5)k))x))(range)%)))

Sometimes it is difficult to be creative.


C (gcc), 88 83 78 77 bytes


Try it online!

This assumes the output vector is cleared before use.

Thanks to @Peter Taylor for -5


Octave, 49 46 45 42 bytes

Here we consider the DCT as a matrix multiplication. The matrix is basically the entry-wise cosine of a rank 1 matrix which is very simple to construct.

Thanks @Cowsquack for -1 byte, thanks @ceilingcat for another -3 bytes!


Try it online!

  • \$\begingroup\$ Can you use .5 instead of 1/2? \$\endgroup\$
    – user41805
    Commented Aug 9, 2017 at 11:21
  • \$\begingroup\$ D'oh, of course! \$\endgroup\$
    – flawr
    Commented Aug 9, 2017 at 11:25
  • \$\begingroup\$ Oh of course, thanks a lot! \$\endgroup\$
    – flawr
    Commented Sep 20, 2017 at 16:15
  • \$\begingroup\$ Suggest rows() instead of numel() \$\endgroup\$
    – ceilingcat
    Commented Oct 19, 2019 at 18:55

Mathematica, 51 49 bytes


Try it online! (Using Mathics)

Based on my solution to DFT.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.