1 | initial version |
Not directly in Sage or packages included in Sage :
sage: var("t")
t
sage: Sol=solve(cos(2*t)==sin(t), t, to_poly_solve=True)
sage: Sol
[t == -1/2*pi - 2*pi*z0, t == 1/6*pi + 2/3*pi*z1]
We have to declare the integer constants:
sage: var("z0, z1", domain="integer")
(z0, z1)
sage: assumptions()
[z0 is integer, z1 is integer]
However, neither Maxima nor Sympy can use the domain constraint to find the relevant solutions. For the first solution :
sage: solve([Sol[0].rhs()>=0, Sol[0].rhs()<=pi/2], z0)
[[(-1/2) < z0, z0 < (-1/4)], [z0 == (-1/2)], [z0 == (-1/4)]]
sage: solve([Sol[0].rhs()>=0, Sol[0].rhs()<=pi/2], z0, algorithm="sympy", domain=
....: "integer")
[[(-1/2) <= z0, z0 <= (-1/4)]]
and Sage has no direct way to find that the intersection of these intervals (both [-1/2 -1/4]
with $\mathbb{N}$ is the empty set.
For the second solution:
sage: solve([Sol[1].rhs()>=0, Sol[1].rhs()<=pi/2], z1)
[[(-1/4) < z1, z1 < (1/2)], [z1 == (-1/4)], [z1 == (1/2)]]
sage: solve([Sol[1].rhs()>=0, Sol[1].rhs()<=pi/2], z1, algorithm="sympy", domain=
....: "integer")
[[(-1/4) <= z1, z1 <= (1/2)]]
The admissible set of solutions for z1
is, in both cases, [-1/4 1/2]
, whose intersection with $\mathbb{N}$ (that Sage cannot directly compute) is {0}
, giving us the solution :
sage: Sol[1].subs(z1==0)
t == 1/6*pi
Note that this is one case where Mathematica can do the job more efficiently:
sage: mathematica.Solve([cos(2*t)==sin(t), t>=0, t<=pi/2], t)
{{t -> Pi/6}}
In Sage, objects representing infinite sets are available. One might try to play with them to obtain "better" representations of the solutins, but this is not (yet) implemented.
HTH,
2 | No.2 Revision |
EDIT : Sympy can do that. See below.
Not directly in Sage or packages included in Sage :
sage: var("t")
t
sage: Sol=solve(cos(2*t)==sin(t), t, to_poly_solve=True)
sage: Sol
[t == -1/2*pi - 2*pi*z0, t == 1/6*pi + 2/3*pi*z1]
We have to declare the integer constants:
sage: var("z0, z1", domain="integer")
(z0, z1)
sage: assumptions()
[z0 is integer, z1 is integer]
However, neither Maxima nor Sympy can use the domain constraint to find the relevant solutions. For the first solution :
sage: solve([Sol[0].rhs()>=0, Sol[0].rhs()<=pi/2], z0)
[[(-1/2) < z0, z0 < (-1/4)], [z0 == (-1/2)], [z0 == (-1/4)]]
sage: solve([Sol[0].rhs()>=0, Sol[0].rhs()<=pi/2], z0, algorithm="sympy", domain=
....: "integer")
[[(-1/2) <= z0, z0 <= (-1/4)]]
and Sage has no direct way to find that the intersection of these intervals (both [-1/2 -1/4]
with $\mathbb{N}$ is the empty set.
For the second solution:
sage: solve([Sol[1].rhs()>=0, Sol[1].rhs()<=pi/2], z1)
[[(-1/4) < z1, z1 < (1/2)], [z1 == (-1/4)], [z1 == (1/2)]]
sage: solve([Sol[1].rhs()>=0, Sol[1].rhs()<=pi/2], z1, algorithm="sympy", domain=
....: "integer")
[[(-1/4) <= z1, z1 <= (1/2)]]
The admissible set of solutions for z1
is, in both cases, [-1/4 1/2]
, whose intersection with $\mathbb{N}$ (that Sage cannot directly compute) is {0}
, giving us the solution :
sage: Sol[1].subs(z1==0)
t == 1/6*pi
Note that this is one case where Mathematica can do the job more efficiently:
sage: mathematica.Solve([cos(2*t)==sin(t), t>=0, t<=pi/2], t)
{{t -> Pi/6}}
In Sage, objects representing infinite sets are available. One might try to play with them to obtain "better" representations of the solutins, but this is not (yet) implemented.
EDIT : Sympy
can do that, but using sympy
's sets, whoch are not (yet) backtranslatable to Sage's Set
s :
t=var("t")
Eq=cos(2*t)==sin(t)
import sympy
SS=solve(Eq, t, algorithm="sympy"); SS
[ImageSet(Lambda(_n, 2*_n*pi + 3*pi/2), Integers),
ImageSet(Lambda(_n, 2*_n*pi + 5*pi/6), Integers),
ImageSet(Lambda(_n, 2*_n*pi + pi/6), Integers)]
Note that tis solution is a lis of sympy
objects, not directly backtranslatable to sage:
[type(u) for u in SS]
[<class 'sympy.sets.fancysets.ImageSet'>,
<class 'sympy.sets.fancysets.ImageSet'>,
<class 'sympy.sets.fancysets.ImageSet'>]
S1=reduce(lambda a,b:a.union(b), SS,
sympy.EmptySet).intersection(sympy.Interval(0,pi/2))
This solution is again a sympy
set, not directly backtranslatable to Sage :
S1
FiniteSet(pi/6)
S1._sage_()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-173-5ca5ebb7fb66> in <module>()
----> 1 S1._sage_()
AttributeError: 'FiniteSet' object has no attribute '_sage_'
A wokaround is to manually backtranslate :
Sol=Set([u._sage_() for u in S1.args]) ; Sol
{1/6*pi}
HTH,