user10317393
user10317393

Reputation: 1

python sympy How do I simplify an expression where I know that a certain variable is an odd integer

Given, for example, the expression (3*cos(pi*n/2)+2*sin(pi*n/2))/n**2 and the knowledge that n is a positive odd integer (ie 1,3,5,...), the expression could simplify to 2*(-1)**((n-1)/2)/n**2 because the cos(pi*n/2) terms all go to zero, and the sin(pi*n/2) terms go to -1 or +1. Is there any way I can make sympy recognise this fact and perform the simplification?

Upvotes: 0

Views: 244

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14530

You can declare n to be odd:

In [72]: n = Symbol('n', odd=True)                                                                                                             

In [73]: (3*cos(pi*n/2)+2*sin(pi*n/2))/n**2                                                                                                    
Out[73]: 
      n   1
      ─ - ─
      2   2
2⋅(-1)     
───────────
      2    
     n 

Upvotes: 0

Related Questions