Ask Your Question

Simplify trig expressions with the double angle formula

asked 2016-10-21 17:45:00 +0200

rtc gravatar image

I am trying to simplify the following expression in sage:


the resulting expression should be:


.simplify_full(), trig_reduce(), or simplify_trig() cannot produce this simplification.

Is sage currently capable of doing this?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-10-22 14:36:54 +0200

mforets gravatar image

updated 2016-10-22 16:01:59 +0200

With the algorithm by Fu et al, implemented as in Sympy:

import sympy as sy

f = sqrt(3)/3*cos(x)+1/3*sin(x)
f_reduced = SR(sy.fu(sy.sympify(f))); f_reduced

gives 2/3*sin(1/3*pi + x) (which is $\frac{\pi}{2}$-close to the answer you expected!).

From my own experience, this type of functionality is very useful (1). I'd define this reduction as: "try to get a formula with the smaller number of terms as possible". This is the type of simplification that often looks nicer, or simpler (to a physicist, say), and it is what I often want from a CAS -- though in many real cases a trained human does better and faster. A step forward would be that the program introduces new useful auxiliary variables, I ignore if this exists in some form already.

In the case at hand, there is a single specific reduction which works, sy.FU['TR10i'](expr), while using sy.fu(expr) runs through several reductions and picks the one which gives a "simpler" expression, as explained in the doc.

(1) The use case is solving physics/engineering problems that involve formulas with many trig functions/complex exponentials. As a concrete example, consider a determinant in Fourier space arising from the normal modes of a discrete system.

edit flag offensive delete link more


Thanks, I will have to look more into the algorithm. That gets me where I need to be.

rtc gravatar imagertc ( 2016-10-22 17:09:08 +0200 )edit

answered 2016-10-21 22:15:05 +0200

tmonteil gravatar image

Well, it is hard to decide which is a simplified version of the other. For many, the first expression is simpler than the second since the involved trigonometric functions are eveluated on x, not pi/6-x. But i agree that something might be missing to express what kind of modification the user expect from Sage.

However, Sage somehow knows that the two expressions are equal:

sage: bool((sqrt(3)/3*cos(x)+1/3*sin(x)) == 2/3*cos(pi/6-x))

See also:

sage: (sqrt(3)/3*cos(x)+1/3*sin(x)).full_simplify()
1/9*sqrt(3)*(sqrt(3)*sin(x) + 3*cos(x))
sage: ( 2/3*cos(pi/6-x)).full_simplify()
1/9*sqrt(3)*(sqrt(3)*sin(x) + 3*cos(x))
edit flag offensive delete link more


Thanks for your response. I understand that sage knows they are equal. However, getting the answer in the single term form helps me see a relationship more easily.

rtc gravatar imagertc ( 2016-10-22 17:08:25 +0200 )edit

answered 2018-07-17 17:34:15 +0200

Emmanuel Charpentier gravatar image

updated 2018-07-19 12:45:54 +0200

EDIT : A better (systematic) solution is available at this repost question.

Well... this is not as easy as it should, since Sage doesn't (yet) do partial substitutions (e. replace a two-factors product in a multiple-factors expression).

But it can be done, if one can follow a bit of intuition :

sage: f=sqrt(3)/3*cos(x)+1/3*sin(x)
sage: f*3/2
1/2*sqrt(3)*cos(x) + 1/2*sin(x)

Aha ! ISTR that $\cos{\pi \over 3}={1 \over 2}$ and that $\sin{\pi \over 3}={\sqrt{3} \over 2}$. Our f*3/2* would then be :

sage: bool((f*3/2)==sin(pi/3)*cos(x)+cos(pi/3)*sin(x))

Aha again. Now, this looks like $\sin({\pi \over 3} +x)$. Let's check :

sage: bool(f*3/2==sin(pi/3+x))

Again a distant memory stirs in my obsolescent neurons :

sage: bool(sin(x)==cos(pi/2-x))

Therefore, since ${\pi \over 2}-({\pi \over 3} + x) = {\pi \over 6}-x$, we have :

sage: bool(f*3/2==cos(pi/6-x))
sage: bool(f==cos(pi/6-x)*2/3)

Q. E. bloody D...

Here, apart from the already mentioned problem of Sage having difficulties with partial substituition, Sage doesn't have methods for going from/to sums to products of trig/hyperbolic expressions. It can be done with subs using a relevant dictionary (using SR wildcards) such as one of those :

sage: S2PW
[cos($1) + cos($0) == 2*cos(1/2*$1 + 1/2*$0)*cos(-1/2*$1 + 1/2*$0),
 -cos($1) + cos($0) == -2*sin(1/2*$1 + 1/2*$0)*sin(-1/2*$1 + 1/2*$0),
 sin($1) + sin($0) == 2*cos(-1/2*$1 + 1/2*$0)*sin(1/2*$1 + 1/2*$0),
 -sin($1) + sin($0) == 2*cos(1/2*$1 + 1/2*$0)*sin(-1/2*$1 + 1/2*$0)]
sage: P2SW
[cos($1)*sin($0) == 1/2*sin($1 + $0) + 1/2*sin(-$1 + $0),
 cos($0)*sin($1) == 1/2*sin($1 + $0) - 1/2*sin(-$1 + $0),
 cos($1)*cos($0) == 1/2*cos($1 + $0) + 1/2*cos(-$1 + $0),
 sin($1)*sin($0) == -1/2*cos($1 + $0) + 1/2*cos(-$1 + $0)]
sage: SQSW
[cos($0)^2 == 1/2*cos(2*$0) + 1/2,
 sin($0)^2 == -1/2*cos(2*$0) + 1/2,
 tan($0)^2 == -(cos(2*$0) - 1)/(cos(2*$0) + 1),
 cos($0) + 1 == 2*cos(1/2*$0)^2,
 -cos(2*$0) + 1 == 2*sin($0)^2]

Those are not very difficult to derive in Sage, but adding them to Sage (possibly as an optional package) would be useful...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-10-21 17:45:00 +0200

Seen: 1,254 times

Last updated: Jul 19 '18