expanding powers of cos()

46 views
Skip to first unread message

Jeremy Holleman

unread,
Mar 27, 2015, 2:36:43 PM3/27/15
to sy...@googlegroups.com
Hi,

I'm relatively new to SymPy, and enjoying it so far.  I'm trying to analyze the harmonics that result when a sinusoid goes through a non-linear system.  I have an expression that includes powers of a cos() and I want to expand it using the sum/difference angle identities.  So I want 

In [5]: some_function(cos(x)**2)
Out[5]: 1/2 + cos(2*x)

But I'm getting 
In [5]: expand_trig(cos(x)**2)
Out[5]: cos(x)**2

I've tried expand_trig(), trigsimp(), expand() (with several different sets of hints), simplify(), and http://www.sympygamma.com/ and they all just return the same expression that I've given them.  Any ideas would be much appreciated.

Thanks,
Jeremy

Paul Royik

unread,
Mar 27, 2015, 2:46:06 PM3/27/15
to sy...@googlegroups.com
Hello, Jeremy.
Try this:

from sympy.simplify.fu import TR7

TR7
(cos(x)**2)

Jeremy Holleman

unread,
Mar 27, 2015, 4:51:53 PM3/27/15
to sy...@googlegroups.com
Thanks!  That did exactly what I want for cos**2.  I would also like to reduce the power on higher order terms.  I tried TR7() on cos(x)**3 and cos(x)**4 and it did not make any changes.  From quick glance here (https://github.com/sympy/sympy/blob/master/sympy/simplify/fu.py), TR7 seems like the right function for power-reducing.  Do you know of a method to do the same thing with larger powers?

A clean way to iteratively break off 2nd-order chunks, apply TR7, and distribute would be a viable, but kludgy, alternative.

Thanks,
Jeremy

Jeremy Holleman

unread,
Mar 27, 2015, 5:33:27 PM3/27/15
to sy...@googlegroups.com
Update - Some combination of TR7, TR8, and expand() seems to work for powers that I've tried.  For example: 

In [5]: TR8(expand(TR8(cos(x)**7)))
Out[5]: 35*cos(x)/64 + 21*cos(3*x)/64 + 7*cos(5*x)/64 + cos(7*x)/64

But it's not clear to me how I would do that in a programmatic way.  

Jeremy

Paul Royik

unread,
Mar 27, 2015, 6:05:30 PM3/27/15
to sy...@googlegroups.com
What do you mean "how I would do that in a programmatic way"?

Jeremy Holleman

unread,
Mar 27, 2015, 8:01:38 PM3/27/15
to sy...@googlegroups.com
I just mean that if I'm trying to write a script and I want to reduce all of the powers of cos or sin, I'm not sure how I would code that, because I can't tell if there's a pattern of which calls need to be made in which order to reduce an arbitrary polynomial of trig functions.  For my present purpose, I'm doing the analysis interactively, so a little trial-and-error is fine.  For future projects I'm trying to get a clearer understanding of how these functions work.  So for example, why does TR7(cos(x)**7) just return cos(x)**7?

Thanks,
Jeremy

Aaron Meurer

unread,
Mar 27, 2015, 8:30:03 PM3/27/15
to sy...@googlegroups.com
I would take a look at the fu paper for more references on what these
rules are supposed to be, but I suspect that this is a bug.

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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/1a9d685b-8fa9-4664-9b97-8756c9797ac8%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Jeremy Holleman

unread,
Mar 27, 2015, 9:16:53 PM3/27/15
to sy...@googlegroups.com
I'll do that.  Thanks to both of you for the help!  

Paul Royik

unread,
Mar 28, 2015, 3:14:02 AM3/28/15
to sy...@googlegroups.com
It seems like TR7 reduces only cos(x)**2

Kalevi Suominen

unread,
Mar 28, 2015, 3:59:13 AM3/28/15
to sy...@googlegroups.com


On Saturday, March 28, 2015 at 2:01:38 AM UTC+2, Jeremy Holleman wrote:
I just mean that if I'm trying to write a script and I want to reduce all of the powers of cos or sin, I'm not sure how I would code that, because I can't tell if there's a pattern of which calls need to be made in which order to reduce an arbitrary polynomial of trig functions.  For my present purpose, I'm doing the analysis interactively, so a little trial-and-error is fine.  For future projects I'm trying to get a clearer understanding of how these functions work.  So for example, why does TR7(cos(x)**7) just return cos(x)**7?

It seems that fu does not deal with higher powers of cos and sin, but if you are writing a script, you could easily
derive a general formula to reduce the powers.

For example, if you start with Euler's formula

     cos(x) = (exp(I*x) + exp(-I*x))/2

and use the binomial formula to obtain

     cos(x)**n = (exp(I*n*x) + C(n, 1)*exp(I*(n-2)*x) + C(n, 2)*exp(I*(n-4)*x) + ,,, + exp(-I*n*x)) / 2**n ,

then using  C(n, n-k) = C(n, k) you get

     cos(x)**n = (cos(n*x) + C(n, 1)*cos((n-2)*x) + ...) / 2**(n - 1) .

(If n is even, the last term in the parentheses is  C(n, n/2)/2.)

 Kalevi Suominen

mario

unread,
Mar 28, 2015, 5:56:23 AM3/28/15
to sy...@googlegroups.com
For example

>>> expand((cos(x)**5).rewrite(exp)).rewrite(cos)
5*cos(x)/8 + 5*cos(3*x)/16 + cos(5*x)/16
Reply all
Reply to author
Forward
0 new messages