Skip to content

Instantly share code, notes, and snippets.

@Shekharrajak
Created June 10, 2016 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shekharrajak/ce4046df45252d4d5133cf84d28dcc7b to your computer and use it in GitHub Desktop.
Save Shekharrajak/ce4046df45252d4d5133cf84d28dcc7b to your computer and use it in GitHub Desktop.
Secod attempt for reduce_imageset methoid
def reduce_imageset(new_imgset, imgset = S.EmptySet):
"""
Try to reduce number of imageset in the args.
It is mostly helper to _solve_trig method defined in
solvers/solveset.py.
First extract the expression of imageset and put in list in new_list
(for new_imgset expr) and final_list(for imgset expr). If there is any
expr in new_list have difference of pi or -pi with any final_list expr.
then club them into one expr, add them and remove previous expr
from final_list.
At the end final_list contains the minimum number of expr that can generate
all the expressions of new_imgset and imgset.
Parameters
==========
imgset : S.EmptySet of imageset(s) we have.
new_imgset : S.EmptySet or imageset(s) to be added.
Returns
=======
simplified imageset if possible otherwise returns original
soln.
Examples
========
>>> from sympy import Lambda
>>> from sympy.sets.fancysets import ImageSet
>>> from sympy import pprint
>>> from sympy.sets.sets import reduce_imageset
>>> from sympy.core import Dummy, pi, S, Symbol
>>> n = Symbol('n', integer = True)
>>> pprint(reduce_imageset(ImageSet(Lambda(n, 2*n*pi + 3*pi/4), \
S.Integers)+ ImageSet(Lambda(n, 2*n*pi + 7*pi/4), \
S.Integers)), use_unicode = False)
3*pi
{pi*n + ---- | n in Integers()}
4
>>> soln = ImageSet(Lambda(n, 2*n*pi + pi/3), \
S.Integers) + ImageSet(Lambda(n, 2*n*pi + pi), S.Integers) \
+ ImageSet(Lambda(n, 2*n*pi), S.Integers)
>>> reduce_imageset(soln)
ImageSet(Lambda(n, pi*n), Integers()) U ImageSet(Lambda(n, 2*pi*n + pi/3), Integers())
"""
from sympy.sets.fancysets import ImageSet
from sympy.core.function import Lambda
from sympy.core import pi
if new_imgset is S.EmptySet:
return imgset
new_list = []
# number of imageset
if isinstance(new_imgset, ImageSet):
new_img_len = 1
new_list.append(new_imgset)
else:
new_img_len = len(new_imgset.args)
for i in range(0, new_img_len):
# list of imageset
new_list.append(new_imgset.args[i])
new_expr = []
final_expr = []
# soln list
final_list = []
final_len = 0
if isinstance(imgset, ImageSet):
final_len = 1
final_list.append(imgset)
else:
final_len = len(imgset.args)
for i in range(0, final_len):
# list of imageset
final_list.append(imgset.args[i])
n = Symbol('n', integer=True)
# Extracting expr
for s in new_list:
# lambda expression
lamb = s.lamda.expr
new_expr.append(lamb)
if final_len != 1:
for s in final_list:
# lambda expression
lamb = s.lamda.expr
final_expr.append(lamb)
else:
final_expr.append(imgset.lamda.expr)
final = S.EmptySet
i = 0
while i < new_img_len:
done = False
for j in range(0, len(final_expr)):
if final_expr[j] - new_expr[i] == pi:
# (2*x*pi + pi + expr1) -( 2*x*pi + exp1) = pi
# can be => x*pi + expr1
final_expr.extend([pi * n + new_expr[i].subs(n, 0)])
final_expr.remove(final_expr[j])
done = True
elif final_expr[j] - new_expr[i] == -pi:
# (2*x*pi + expr1) -( 2*x*pi + pi + exp1) = -pi
# can be => x*pi + expr1
final_expr += [pi * n + final_expr[j].subs(n, 0)]
final_expr.remove(final_expr[j])
done = True
if not done:
final_expr.append(new_expr[i])
i += 1
for s in final_expr:
final += ImageSet(Lambda(n, s), S.Integers)
return final
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment