<commit_before>from lcapy import *
import unittest


class LcapyTester(unittest.TestCase):

    """Unit tests for lcapy

    """

    def test_DFT(self):

        self.assertEqual(nexpr('delta(n)').DFT(), 1, "delta(n)")
        self.assertEqual(nexpr('2 * delta(n)').DFT(), 2, "2 * delta(n)")

        self.assertEqual(nexpr('1').DFT(), kexpr('N * delta(k)'), "1")
        self.assertEqual(nexpr('2').DFT(), kexpr('2 * N * delta(k)'), "2")

        self.assertEqual(nexpr('x(n)').DFT(), kexpr('X(k)'), "x(n)")
        self.assertEqual(nexpr('2 * x(n)').DFT(), kexpr('2 * X(k)'), "2 * x(n)")
        self.assertEqual(nexpr('x(2 * n)').DFT(), kexpr('X(k / 2) / 2'), "x(2 * n)")        

        self.assertEqual(nexpr('delta(n - 1)').DFT(),
                         kexpr('exp(-j * 2 * pi * k / N)'), "delta(n - 1)")

        self.assertEqual(nexpr('2 * delta(n - 1)').DFT(),
                         kexpr('2 * exp(-j * 2 * pi * k / N)'), "2 * delta(n - 1)")

        self.assertEqual(nexpr('delta(n - 2)').DFT(),
                         kexpr('exp(-j * 2 * pi * 2 * k / N)'), "delta(n - 2)")

        # Temp. hack, ideally should get result 2 * N * delta(k + 1)
        self.assertEqual(nexpr('2 * exp(-j * 2 * pi * n / N)').DFT(), 
                         kexpr('2 * N * delta(k + 1 - N)'), "2 * exp(-j * 2 * pi * n / N)")


    def test_IDFT(self):

        self.assertEqual(kexpr('X(k)').IDFT(), nexpr('x(n)'), "X(k)")
        self.assertEqual(kexpr(1).IDFT(), delta(n), "1")
        self.assertEqual(delta(k).IDFT(), nexpr('1 / N'), "delta(k)")                

    def test_DFT_matrix(self):

        X1 = DFTmatrix(4)
        X2 = Matrix(((1, 1, 1, 1), (1, -j, -1, j), (1, -1, 1, -1), (1, j, -1, -j)))
        X3 = IDFTmatrix(4).conjugate * 4
        
        self.assertEqual(X1, X2, "DFTmatrix(4)")
        self.assertEqual(X3, X2, "IDFTmatrix(4)")        
        <commit_msg>Add DFT test for integer N<commit_after>from lcapy import *
import unittest


class LcapyTester(unittest.TestCase):

    """Unit tests for lcapy

    """

    def test_DFT(self):

        self.assertEqual(nexpr('delta(n)').DFT(), 1, "delta(n)")
        self.assertEqual(nexpr('2 * delta(n)').DFT(), 2, "2 * delta(n)")

        self.assertEqual(nexpr('1').DFT(), kexpr('N * delta(k)'), "1")
        self.assertEqual(nexpr('2').DFT(), kexpr('2 * N * delta(k)'), "2")

        self.assertEqual(nexpr('x(n)').DFT(), kexpr('X(k)'), "x(n)")
        self.assertEqual(nexpr('2 * x(n)').DFT(), kexpr('2 * X(k)'), "2 * x(n)")
        self.assertEqual(nexpr('x(2 * n)').DFT(), kexpr('X(k / 2) / 2'), "x(2 * n)")        

        self.assertEqual(nexpr('delta(n - 1)').DFT(),
                         kexpr('exp(-j * 2 * pi * k / N)'), "delta(n - 1)")

        self.assertEqual(nexpr('2 * delta(n - 1)').DFT(),
                         kexpr('2 * exp(-j * 2 * pi * k / N)'), "2 * delta(n - 1)")

        self.assertEqual(nexpr('delta(n - 2)').DFT(),
                         kexpr('exp(-j * 2 * pi * 2 * k / N)'), "delta(n - 2)")

        # Temp. hack, ideally should get result 2 * N * delta(k + 1)
        self.assertEqual(nexpr('2 * exp(-j * 2 * pi * n / N)').DFT(), 
                         kexpr('2 * N * delta(k + 1 - N)'), "2 * exp(-j * 2 * pi * n / N)")
        self.assertEqual(nexpr('x(n-1)').DFT(N=2), 
                         kexpr('X(k)*exp(-j * pi * k)'), "x(n-1)")
        

    def test_IDFT(self):

        self.assertEqual(kexpr('X(k)').IDFT(), nexpr('x(n)'), "X(k)")
        self.assertEqual(kexpr(1).IDFT(), delta(n), "1")
        self.assertEqual(delta(k).IDFT(), nexpr('1 / N'), "delta(k)")                

    def test_DFT_matrix(self):

        X1 = DFTmatrix(4)
        X2 = Matrix(((1, 1, 1, 1), (1, -j, -1, j), (1, -1, 1, -1), (1, j, -1, -j)))
        X3 = IDFTmatrix(4).conjugate * 4
        
        self.assertEqual(X1, X2, "DFTmatrix(4)")
        self.assertEqual(X3, X2, "IDFTmatrix(4)")        
        
{'fraction_non_alphanumeric': 0.23582013285641287, 'fraction_numerical': 0.03346959632089934, 'mean_word_length': 2.0538221528861156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6402146', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1511, 'n_words': 302}