The Python Oracle

In scipy why doesn't idct(dct(a)) equal to a?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Magical Minnie Puzzles

--

Chapters
00:00 Question
01:14 Accepted answer (Score 13)
01:39 Thank you

--

Full question
https://stackoverflow.com/questions/3489...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #scipy #dct

#avk47



ACCEPTED ANSWER

Score 13


You need to set scaling to ortho for both dct2 and idct2:

def dct2 (block):
  return dct(dct(block.T, norm = 'ortho').T, norm = 'ortho')

also, you cannot expect the values to be exactly the same, but almost the same within some margin of error:

np.allclose (a, idct2(dct2(a)))