Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# master branch
In [1]: solveset(cos(x) + cos(3*x) + cos(5*x), x, S.Reals)
Out[1]:
⎧ π ⎫ ⎧ 3⋅π ⎫ ⎧ 4⋅π ⎫ ⎧ 2⋅π ⎫ ⎧ 5⋅π ⎫ ⎧ π ⎫ ⎧
2⋅n⋅π +| n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π +| n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π +
2 ⎭ ⎩ 2 ⎭ ⎩ 3 ⎭ ⎩ 3 ⎭ ⎩ 3 ⎭ ⎩ 3 ⎭ ⎩
7⋅π ⎫ ⎧ 5⋅π ⎫ ⎧ 11⋅π ⎫ ⎧ π ⎫
─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ─── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π + ──── | n ∊ ℤ⎬ ∪ ⎨2⋅n⋅π +| n ∊ ℤ⎬
6 ⎭ ⎩ 6 ⎭ ⎩ 6 ⎭ ⎩ 6
In [2]: print solveset(cos(x) + cos(3*x) + cos(5*x), x, S.Reals)
ImageSet(Lambda(_n, 2*_n*pi + pi/2), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 3*pi/2), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 4*pi/3), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 2*pi/3), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 5*pi/3), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + pi/3), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 7*pi/6), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 5*pi/6), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + 11*pi/6), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + pi/6), Integers())
# soln is
img1 = ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers)
img2 = ImageSet(Lambda(n, 2*n*pi + 3*pi/2), S.Integers)
img3 = ImageSet(Lambda(n, 2*n*pi + 4*pi/3), S.Integers)
img4 = ImageSet(Lambda(n, 2*n*pi + 2*pi/3), S.Integers)
img5 = ImageSet(Lambda(n, 2*n*pi + 5*pi/3), S.Integers)
img6 = ImageSet(Lambda(n, 2*n*pi + pi/3), S.Integers)
img7 = ImageSet(Lambda(n, 2*n*pi + 7*pi/6), S.Integers)
img8 = ImageSet(Lambda(n, 2*n*pi + 5*pi/6), S.Integers)
img9 = ImageSet(Lambda(n, 2*n*pi + 11*pi/6), S.Integers)
img10 = ImageSet(Lambda(n, 2*n*pi + pi/6), S.Integers)
#=====================================================================
# NEW IMPLEMENTATION (IMAGESET UNION IN SETS/FANCYSET.PY)
# PR : https://github.com/sympy/sympy/pull/11188
#----------------------------------------------------------
# img1 can be clubbed with img2
In [41]: Union(img1, img2)
Out[41]:
⎧ π ⎫
⎨π⋅n +| n ∊ ℤ⎬
2
newimg1 = ImageSet(Lambda(n, pi*n + pi/2), S.Integers)
#----------------------------------------------------------
# img8 can be clubbed with img9
In [45]: Union(img8, img9)
Out[45]:
5⋅π ⎫
⎨π⋅n + ─── | n ∊ ℤ⎬
6
In [44]: print Union(img8, img9)
ImageSet(Lambda(n, pi*n + 5*pi/6), Integers())
newimg2 = ImageSet(Lambda(n, pi*n + 5*pi/6), S.Integers)
#----------------------------------------------------------
# img7 can be clubbed with img10
In [46]: Union(img7, img10)
Out[46]:
⎧ π ⎫
⎨π⋅n +| n ∊ ℤ⎬
6
In [47]: print Union(img7, img10)
ImageSet(Lambda(n, pi*n + pi/6), Integers())
newimg3 = ImageSet(Lambda(n, pi*n + pi/6), S.Integers)
#----------------------------------------------------------
# img3 can be clubbed with img6
In [48]: Union(img6, img3)
Out[48]:
⎧ π ⎫
⎨π⋅n +| n ∊ ℤ⎬
3
In [49]: print Union(img6, img3)
ImageSet(Lambda(n, pi*n + pi/3), Integers())
newimg4 = ImageSet(Lambda(n, pi*n + pi/3), S.Integers)
#----------------------------------------------------------
# img4 can be clubbed with img5
In [50]: print Union(img4, img5)
ImageSet(Lambda(n, pi*n + 2*pi/3), Integers())
In [51]: Union(img4, img5)
Out[51]:
2⋅π ⎫
⎨π⋅n + ─── | n ∊ ℤ⎬
3
newimg5 = ImageSet(Lambda(n, pi*n + 2*pi/3), S.Integers)
#----------------------------------------------------------
# SO NOW REDUCED SOLN IS
newimg1 = ImageSet(Lambda(n, pi*n + pi/2), S.Integers)
newimg2 = ImageSet(Lambda(n, pi*n + 5*pi/6), S.Integers)
newimg3 = ImageSet(Lambda(n, pi*n + pi/6), S.Integers)
newimg4 = ImageSet(Lambda(n, pi*n + pi/3), S.Integers)
newimg5 = ImageSet(Lambda(n, pi*n + 2*pi/3), S.Integers)
# we still can reduce these imageset
In [58]: Union(newimg5, newimg3)
Out[58]:
⎧π⋅n π ⎫
⎨─── +| n ∊ ℤ⎬
2 6
In [59]: print Union(newimg5, newimg3)
ImageSet(Lambda(n, pi*n/2 + pi/6), Integers())
final1 = ImageSet(Lambda(n, pi*n/2 + pi/6), S.Integers)
#------------------------------------
# similarly
In [60]: Union(newimg2, newimg4)
Out[60]:
⎧π⋅n π ⎫
⎨─── +| n ∊ ℤ⎬
2 3
In [61]: print Union(newimg2, newimg4)
ImageSet(Lambda(n, pi*n/2 + pi/3), Integers())
final2 = ImageSet(Lambda(n, pi*n/2 + pi/3), S.Integers)
#=================================
# So final reduce soln is =
Union(final1, final2, newimg1)
⎧ π ⎫ ⎧n⋅π π ⎫ ⎧n⋅π π ⎫
⎨n⋅π +| n ∊ ℤ⎬ ∪ ⎨─── +| n ∊ ℤ⎬ ∪ ⎨─── +| n ∊ ℤ⎬
2 ⎭ ⎩ 2 6 ⎭ ⎩ 2 3
#==================================
# One can check the answer using following lines
for i in range(0, 10):
....: print newimg5.lamda(i) in final1
....: print newimg3.lamda(i) in final1
....: print newimg2.lamda(i) in final2
....: print newimg4.lamda(i) in final2
# all true will be printed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment