Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sympy can't solve this double angular summation #23576

Open
ctr26 opened this issue Jun 4, 2022 · 2 comments
Open

Sympy can't solve this double angular summation #23576

ctr26 opened this issue Jun 4, 2022 · 2 comments
Labels

Comments

@ctr26
Copy link

ctr26 commented Jun 4, 2022

Hi all,

    v     v                         
   ____  ____                       
   ╲     ╲                          
    ╲     ╲                         
     ╲     ╲  ⎛       ⎛2⋅π⋅(i - j)⎞⎞
2⋅   ╱     ╱  ⎜1 - cos⎜───────────⎟⎟
    ╱     ╱   ⎝       ⎝     v     ⎠⎠
   ╱     ╱                          
   ‾‾‾‾  ‾‾‾‾                       
  j = 1 i = 1    

In wolfram alpha it comes out correctly as =v

sqrt(sum_i=1^v (sum_j=1^v 2*((((1)*(1))-((1)*(1))cos(2*pi/v*((j-1)-(i-1)))))))

In sympy it outright gives up.

sp.sympify('2*Sum(1 - cos(2*pi*(i - j)/v), (i, 1, v))').doit()

Any thoughts?

Thanks

Craig

@oscarbenjamin
Copy link
Contributor

The code you've shown doesn't match the depicted formula so I'm not sure exactly what sum you want to compute.

I'm not sure what approach could be used for sums like this in general. The only thing I can think is rewriting to exp. If you declare v as a positive integer then rewrite as exp then you get a complicated result:

In [16]: v = symbols('v', integer=True, positive=True)

In [17]: i, j = symbols('i, j')

In [18]: s = 2*Sum(1 - cos(2*pi*(i - j)/v), (i, 1, v), (j, 1, v))

In [19]: s
Out[19]: 
    v     v                         
   ____  ____                       
   ╲     ╲                          
    ╲     ╲                         
     ╲     ╲  ⎛       ⎛2π⋅(i - j)⎞⎞
2⋅   ╱     ╱  ⎜1 - cos⎜───────────⎟⎟
    ╱     ╱   ⎝       ⎝     v     ⎠⎠
   ╱     ╱                          
   ‾‾‾‾  ‾‾‾‾                       
  j = 1 i = 1                       

In [20]: s.doit()
Out[20]: 
    v     v                         
   ____  ____                       
   ╲     ╲                          
    ╲     ╲                         
     ╲     ╲  ⎛       ⎛2π⋅(i - j)⎞⎞
2⋅   ╱     ╱  ⎜1 - cos⎜───────────⎟⎟
    ╱     ╱   ⎝       ⎝     v     ⎠⎠
   ╱     ╱                          
   ‾‾‾‾  ‾‾‾‾                       
  j = 1 i = 1                       

In [21]: s.rewrite(exp)
Out[21]: 
    v     v                                            
  _____ _____                                          
  ╲     ╲                                              
   ╲     ╲    ⎛   2π⋅(i - j)        -2π⋅(i - j) ⎞
    ╲     ╲   ⎜   ─────────────        ───────────────⎟
     ╲     ╲  ⎜         v                     v2⋅   ╱     ╱  ⎜                                     ⎟
    ╱     ╱   ⎜- ────────────── + 1 - ────────────────⎟
   ╱     ╱    ⎝        2                     2        ⎠
  ╱     ╱                                              
  ‾‾‾‾‾ ‾‾‾‾‾                                          
  j = 1 i = 1                                          

In [22]: s.rewrite(exp).doit()
Out[22]: 
                              ⎛⎧                              2π    ⎞   ⎛⎧                      
                              ⎜⎪                              ─────    ⎟   ⎜⎪                      
                              ⎜⎪                                v      ⎟   ⎜⎪                      
                              ⎜⎪           v             for       = 1⎟   ⎜⎪              v       
       ⎛⎧        -2π     ⎞ ⎜⎪                                       ⎟   ⎜⎪                      
       ⎜⎪        ───────    ⎟ ⎜⎪ 2π    2π⋅(v + 1)                ⎟   ⎜⎪   -2π⋅(v + 1)     
   2   ⎜⎪           v       ⎟ ⎜⎪ ─────    ─────────────                ⎟   ⎜⎪   ───────────────    
2v  - ⎜⎨v  for         = 1⎟⋅⎜⎨   v            v- ⎜⎨          v           
       ⎜⎪                   ⎟ ⎜⎪      -                              ⎟   ⎜⎪-                 + 
       ⎜⎪0     otherwise    ⎟ ⎜⎪───────────────────────    otherwise   ⎟   ⎜⎪──────────────────────
       ⎝⎩                   ⎠ ⎜⎪            2π                      ⎟   ⎜⎪              -2π  
                              ⎜⎪            ─────                      ⎟   ⎜⎪              ─────── 
                              ⎜⎪              v                        ⎟   ⎜⎪                 v    
                              ⎜⎪       1 -                            ⎟   ⎜⎪         1 -         
                              ⎝⎩                                       ⎠   ⎝⎩                      

              -2π     ⎞                     
              ───────    ⎟                     
                 vfor         = 1⎟                     
                         ⎟ ⎛⎧        2π-2π                   ⎟ ⎜⎪        ─────    ⎟
───────                  ⎟ ⎜⎪          vv                     ⎟⋅⎜⎨v  for       = 1⎟
                         ⎟ ⎜⎪                 ⎟
───────     otherwise    ⎟ ⎜⎪0    otherwise   ⎟
                         ⎟ ⎝⎩                 ⎠
                         ⎟                     
                         ⎟                     
                         ⎟                     
                         ⎠                     

In [23]: simplify(_)
Out[23]: 
⎧           -2π         2π    
⎪           ───────        ─────    
⎪              v             v0    for         = 1      = 1
⎪                                   
⎪   22v             otherwise          

I think that the problem is that we need v to be greater than 1 (the formula doesn't hold when v=1) but it isn't easy to express that requirement in SymPy. The Piecewise above does represent that though (if you substitute v=1 you'll get 0).

@ctr26
Copy link
Author

ctr26 commented Jun 4, 2022

I should've specified that the assumptions are that v is a positive integer, but in practise it's larger than 2 and probably 3; and that v is the the number of vertices of a 2D polygon. Rewrite exp looks like the way though I don't understand how mathematica and wolfram know to try that, unless they just have more summation identities?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants