0

I have defined the following symbolic matrix:

def DFT(d):    
    a = symbols('pi')    
    DFT = Matrix(d, d, lambda i,j: exp((2*I/d)*i*j*a))    
    return(DFT)

Now, I would like to simplify the exponential to the corresponding 1,-1,I,-I when its argument is an intiger or half integer value, but for the rest of the cases I would like to keep the symbolic expression. Is there any method that I could use? How should I do it?

Regards!

L3viathan
  • 26,748
  • 2
  • 58
  • 81
Federico Vega
  • 355
  • 2
  • 8

1 Answers1

1

If I use the predefined symbol sympy.pi instead of a in the definition of DFT, it simplifies the values automatically:

In [27]: from sympy import *

In [28]: def DFT(d):    
    ...:     DFT = Matrix(d, d, lambda i,j: exp((2*I/d)*i*j*pi))
    ...:     return(DFT)
    ...: 

In [29]: DFT(4)
Out[29]: 
Matrix([
[1,  1,  1,  1],
[1,  I, -1, -I],
[1, -1,  1, -1],
[1, -I, -1,  I]])

In [30]: DFT(6)
Out[30]: 
Matrix([
[1,             1,              1,  1,              1,              1],
[1,   exp(I*pi/3),  exp(2*I*pi/3), -1,  exp(4*I*pi/3),  exp(5*I*pi/3)],
[1, exp(2*I*pi/3),  exp(4*I*pi/3),  1,  exp(8*I*pi/3), exp(10*I*pi/3)],
[1,            -1,              1, -1,              1,             -1],
[1, exp(4*I*pi/3),  exp(8*I*pi/3),  1, exp(16*I*pi/3), exp(20*I*pi/3)],
[1, exp(5*I*pi/3), exp(10*I*pi/3), -1, exp(20*I*pi/3), exp(25*I*pi/3)]])

In [31]: DFT(8)
Out[31]: 
Matrix([
[1,             1,  1,              1,  1,              1,  1,              1],
[1,   exp(I*pi/4),  I,  exp(3*I*pi/4), -1,  exp(5*I*pi/4), -I,  exp(7*I*pi/4)],
[1,             I, -1,             -I,  1,              I, -1,             -I],
[1, exp(3*I*pi/4), -I,  exp(9*I*pi/4), -1, exp(15*I*pi/4),  I, exp(21*I*pi/4)],
[1,            -1,  1,             -1,  1,             -1,  1,             -1],
[1, exp(5*I*pi/4),  I, exp(15*I*pi/4), -1, exp(25*I*pi/4), -I, exp(35*I*pi/4)],
[1,            -I, -1,              I,  1,             -I, -1,              I],
[1, exp(7*I*pi/4), -I, exp(21*I*pi/4), -1, exp(35*I*pi/4),  I, exp(49*I*pi/4)]])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214