Evaluation of symbolic integrals

32 views
Skip to first unread message

brombo

unread,
Sep 7, 2022, 10:38:44 AM9/7/22
to sympy
I calculated an integral g when printed grives:

Piecewise((sin(w/2)/w - 2*cos(w/2)/w**2 + 4*sin(w/2)/w**3, Ne(w, 0)), (2/3, True)) + 2*Piecewise((-sin(w/2)/(2*w) + cos(w/2)/w**2 + sin(w/2)/w**3 - sin(3*w/2)/w**3, Ne(w, 0)), (1/6, True))

First, how can I extract from g (without cutting and pasting the output) what I really want which is:

(sin(w/2)/w - 2*cos(w/2)/w**2 + 4*sin(w/2)/w**3)+2*(-sin(w/2)/(2*w) + cos(w/2)/w**2 + sin(w/2)/w**3 - sin(3*w/2)/w**3)

Secondly,  I can substitute y = w/2.  In the resulting expression force expansion of sin(3*y). trigsimp doesn't do that.  I know the answer is supposed to be of the form (sin(y)/y)**3.

Oscar

unread,
Sep 7, 2022, 11:36:24 AM9/7/22
to sympy
On Wednesday, 7 September 2022 at 15:38:44 UTC+1 brombo wrote:
I calculated an integral g when printed grives:

Piecewise((sin(w/2)/w - 2*cos(w/2)/w**2 + 4*sin(w/2)/w**3, Ne(w, 0)), (2/3, True)) + 2*Piecewise((-sin(w/2)/(2*w) + cos(w/2)/w**2 + sin(w/2)/w**3 - sin(3*w/2)/w**3, Ne(w, 0)), (1/6, True))

So this is what we have:

In [13]: w = symbols('w')

In [14]: result = Piecewise((sin(w/2)/w - 2*cos(w/2)/w**2 + 4*sin(w/2)/w**3, Ne(w, 0)), (2/3, True))
    ...: + 2*Piecewise((-sin(w/2)/(2*w) + cos(w/2)/w**2 + sin(w/2)/w**3 - sin(3*w/2)/w**3, Ne(w, 0)),
    ...:  (1/6, True))

In [15]: result = nsimplify(result)

In [16]: result
Out[16]:
⎛⎧   ⎛w⎞        ⎛w⎞        ⎛w⎞           ⎞     ⎛⎧     ⎛w⎞      ⎛w⎞      ⎛w⎞      ⎛3⋅w⎞           ⎞
⎜⎪sin⎜─⎟   2⋅cos⎜─⎟   4⋅sin⎜─⎟           ⎟     ⎜⎪  sin⎜─⎟   cos⎜─⎟   sin⎜─⎟   sin⎜───⎟           ⎟
⎜⎪   ⎝2⎠        ⎝2⎠        ⎝2⎠           ⎟     ⎜⎪     ⎝2⎠      ⎝2⎠      ⎝2⎠      ⎝ 2 ⎠           ⎟
⎜⎪────── - ──────── + ────────  for w ≠ 0⎟     ⎜⎪- ────── + ────── + ────── - ────────  for w ≠ 0⎟
⎜⎨  w          2          3              ⎟ + 2⋅⎜⎨   2⋅w        2        3         3              ⎟
⎜⎪            w          w               ⎟     ⎜⎪             w        w         w               ⎟
⎜⎪                                       ⎟     ⎜⎪                                                ⎟
⎜⎪            2/3               otherwise⎟     ⎜⎪                 1/6                   otherwise⎟
⎝⎩                                       ⎠     ⎝⎩                                                ⎠

 

First, how can I extract from g (without cutting and pasting the output) what I really want which is:

(sin(w/2)/w - 2*cos(w/2)/w**2 + 4*sin(w/2)/w**3)+2*(-sin(w/2)/(2*w) + cos(w/2)/w**2 + sin(w/2)/w**3 - sin(3*w/2)/w**3)

In [23]: piecewise_fold(result)
Out[23]:
⎧     ⎛w⎞        ⎛3⋅w⎞          
⎪6⋅sin⎜─⎟   2⋅sin⎜───⎟          
⎪     ⎝2⎠        ⎝ 2 ⎠          
⎪──────── - ──────────  for w ≠ 0
⎨    3           3              
⎪   w           w                
⎪                                
⎪          1            otherwise
⎩                                

In [24]: piecewise_fold(result).args[0][0]
Out[24]:
     ⎛w⎞        ⎛3⋅w⎞
6⋅sin⎜─⎟   2⋅sin⎜───⎟
     ⎝2⎠        ⎝ 2 ⎠
──────── - ──────────
    3           3    
   w           w 

 

Secondly,  I can substitute y = w/2.  In the resulting expression force expansion of sin(3*y). trigsimp doesn't do that.  I know the answer is supposed to be of the form (sin(y)/y)**3.


 In [26]: _.subs(w, 2*y)
Out[26]:
3⋅sin(y)   sin(3⋅y)
──────── - ────────
     3          3  
  4⋅y        4⋅y  

In [27]: _.expand(trig=True)
Out[27]:
   3  
sin (y)
───────
    3  
   y 

--
Oscar

Aaron Meurer

unread,
Sep 7, 2022, 3:26:43 PM9/7/22
to sy...@googlegroups.com
You can also use refine() to select specific parts from a Piecewise.

>>> refine(expr, Ne(w, 0))
6*sin(w/2)/w**3 - 2*sin(3*w/2)/w**3

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/32972cc3-85ca-4bca-abc4-f871295610e8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages