Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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))
True

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

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

Again a distant memory stirs in my obsolescent neurons :

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

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

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

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 : 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))
True

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

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

Again a distant memory stirs in my obsolescent neurons :

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

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

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

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...