Skip to content

Instantly share code, notes, and snippets.

@Shekharrajak
Created August 14, 2016 04:20
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/26f84de053ee3faa05dbe0ba3baf4422 to your computer and use it in GitHub Desktop.
Save Shekharrajak/26f84de053ee3faa05dbe0ba3baf4422 to your computer and use it in GitHub Desktop.
logic which is changed to issuperset
# logic for imageSet union
# img1 = ImageSet(Lambda(n, a*n + b), S.Integers)
# img2 = ImageSet(Lambda(n, c*n + d), S.Integers)
# When a != c and (c + (b - d)) mod a == 0, means (c + (b-d))*n may be multiple of a*n.
# After checking it for 3 values, returns img1.
# In [1]: Union(ImageSet(Lambda(n, 3*n), S.Integers), ImageSet(Lambda(n, 6*n), S.Integers))
# Out[1]: {3⋅n | n ∊ ℤ}
# In [2]: Union(ImageSet(Lambda(n, 3*n ), S.Integers), ImageSet(Lambda(n, 7*n ), S.Integers))
# Out[2]: {3⋅n | n ∊ ℤ} ∪ {7⋅n | n ∊ ℤ}
# In [3]: Union(ImageSet(Lambda(n, 3*n ), S.Integers), ImageSet(Lambda(n, n+2 ), S.Integers))
# Out[3]: {n + 2 | n ∊ ℤ}
# # -------------------------------------------------------------
# # 5*n + 5/4 ==> 5*n + 1 + 1/4 and (5 + 1 )%1 ==0(here a=1 , b=5, abs(b-d) = 1).
# # 5*n + 1 + 1/4 is in n + 1/4
# # cross check using img1.superset(img2) == true
# img1 = ImageSet(Lambda(n, n + S(1)/4 ), S.Integers)
# img2 = ImageSet(Lambda(n, 5*n + S(5)/4 ), S.Integers)
# In [4]: Union(img1, img2)
# Out[4]: {n + 1/4 | n ∊ ℤ}
# # -------------------------------------------------------------
# # 5*n + 5/4 ==> 5*n + 1 + 1/4 and (5 + 1) % 2 == 0 (here a=2 , b=5, abs(b-d) = 1)
# # but for n=2 it is 10 + 5/4 => 11 + 1/4 which can't be generated using 2*n + 1/4.
# # (This is the reason why I am checking for n = 1, 2, 3
# # means 5*1 + 5/4, 5*2 + 5/4, 5*3 + 5/4 is in 2*n + 1/4 or not)
# # cross check:
# # img1.issuperset(img2) is false
# img1 = ImageSet(Lambda(n, 2*n + S(1)/4 ), S.Integers)
# img2 = ImageSet(Lambda(n, 5*n +S(5)/4), S.Integers)
# In [5]: Union(img1, img2)
# Out[5]: {2⋅n + 1/4 | n ∊ ℤ} ∪ {5⋅n + 5/4 | n ∊ ℤ}
# # Here we should also check that if b==0 then d % a should be 0.
# # Union(ImageSet(Lambda(n, 3*n ), S.Integers), ImageSet(Lambda(n, 6*n+2 ), S.Integers))
# # should be returned as it is.
# this line is used in imageset/union/_attempt_1
if self_expr.coeff(var_self) != other_expr.coeff(var_self):
# e.g. expr1 = n + 1/4 , expr2 = 5*n + 5/4
# if (5*n + abs(5/4 - 1/4)) % n == 0 means union
# value = n + 1/4
other_const = other_expr.coeff(var_self, 0)
self_const = self_expr.coeff(var_self, 0)
gap = other_const - self_const
# checking if `other` + gap is multiple of `self` or not.
check_multiple = lambda i: val_at(other, i) - other_const + gap
self_term = val_at(self, 1) - self_const
check = lambda i: check_multiple(i) % self_term
# may be 1st values is multiple, so need to check for 2
# or 3 continuous value.
if all(check(i) == 0 for i in [1, 2, 3]):
return self
# otherwise can't club them, return None
return None
@Shekharrajak
Copy link
Author

img1 = ImageSet(Lambda(n, n*pi + pi/4), S.Integers)
    img2 = ImageSet(Lambda(n, 5*n*pi + 5*pi/4), S.Integers)
    # All 6*pi + pi + pi/4 is contained in n*pi + pi/4
    assert Union(img1, img2) == img1
    assert Union(img2, img1) == img1

@Shekharrajak
Copy link
Author

    n = Dummy('n', real=True)
    img_set1 = ImageSet(Lambda(n, n*pi + pi/4), S.Integers)
    img_set2 = ImageSet(Lambda(n, 4*n*pi + 5*pi/4), S.Integers)
    # coeff of `n` is not same and img_set1 is superset of img_set2
    assert Union(img_set1, img_set2) == img_set1

    img_set2 = ImageSet(Lambda(n, 5*n*pi + 5*pi/4), S.Integers)
    # All 6*pi*n + pi + pi/4 is contained in n*pi + pi/4
    # img1 is superset of img2
    assert Union(img_set1, img_set2) == img_set1
    assert Union(img_set2, img_set1) == img_set1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment