m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1284 lines
49 KiB

6 months ago
  1. from sympy.core.expr import unchanged
  2. from sympy.sets.contains import Contains
  3. from sympy.sets.fancysets import (ImageSet, Range, normalize_theta_set,
  4. ComplexRegion)
  5. from sympy.sets.sets import (FiniteSet, Interval, Union, imageset,
  6. Intersection, ProductSet)
  7. from sympy.sets.conditionset import ConditionSet
  8. from sympy.simplify.simplify import simplify
  9. from sympy.core.basic import Basic
  10. from sympy.core.containers import Tuple
  11. from sympy.core.function import Lambda
  12. from sympy.core.numbers import (I, Rational, oo, pi)
  13. from sympy.core.relational import Eq
  14. from sympy.core.singleton import S
  15. from sympy.core.symbol import (Dummy, Symbol, symbols)
  16. from sympy.functions.elementary.complexes import Abs
  17. from sympy.functions.elementary.exponential import (exp, log)
  18. from sympy.functions.elementary.integers import floor
  19. from sympy.functions.elementary.miscellaneous import sqrt
  20. from sympy.functions.elementary.trigonometric import (cos, sin, tan)
  21. from sympy.logic.boolalg import And
  22. from sympy.matrices.dense import eye
  23. from sympy.testing.pytest import XFAIL, raises
  24. from sympy.abc import x, y, t, z
  25. from sympy.core.mod import Mod
  26. import itertools
  27. def test_naturals():
  28. N = S.Naturals
  29. assert 5 in N
  30. assert -5 not in N
  31. assert 5.5 not in N
  32. ni = iter(N)
  33. a, b, c, d = next(ni), next(ni), next(ni), next(ni)
  34. assert (a, b, c, d) == (1, 2, 3, 4)
  35. assert isinstance(a, Basic)
  36. assert N.intersect(Interval(-5, 5)) == Range(1, 6)
  37. assert N.intersect(Interval(-5, 5, True, True)) == Range(1, 5)
  38. assert N.boundary == N
  39. assert N.is_open == False
  40. assert N.is_closed == True
  41. assert N.inf == 1
  42. assert N.sup is oo
  43. assert not N.contains(oo)
  44. for s in (S.Naturals0, S.Naturals):
  45. assert s.intersection(S.Reals) is s
  46. assert s.is_subset(S.Reals)
  47. assert N.as_relational(x) == And(Eq(floor(x), x), x >= 1, x < oo)
  48. def test_naturals0():
  49. N = S.Naturals0
  50. assert 0 in N
  51. assert -1 not in N
  52. assert next(iter(N)) == 0
  53. assert not N.contains(oo)
  54. assert N.contains(sin(x)) == Contains(sin(x), N)
  55. def test_integers():
  56. Z = S.Integers
  57. assert 5 in Z
  58. assert -5 in Z
  59. assert 5.5 not in Z
  60. assert not Z.contains(oo)
  61. assert not Z.contains(-oo)
  62. zi = iter(Z)
  63. a, b, c, d = next(zi), next(zi), next(zi), next(zi)
  64. assert (a, b, c, d) == (0, 1, -1, 2)
  65. assert isinstance(a, Basic)
  66. assert Z.intersect(Interval(-5, 5)) == Range(-5, 6)
  67. assert Z.intersect(Interval(-5, 5, True, True)) == Range(-4, 5)
  68. assert Z.intersect(Interval(5, S.Infinity)) == Range(5, S.Infinity)
  69. assert Z.intersect(Interval.Lopen(5, S.Infinity)) == Range(6, S.Infinity)
  70. assert Z.inf is -oo
  71. assert Z.sup is oo
  72. assert Z.boundary == Z
  73. assert Z.is_open == False
  74. assert Z.is_closed == True
  75. assert Z.as_relational(x) == And(Eq(floor(x), x), -oo < x, x < oo)
  76. def test_ImageSet():
  77. raises(ValueError, lambda: ImageSet(x, S.Integers))
  78. assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1)
  79. assert ImageSet(Lambda(x, y), S.Integers) == {y}
  80. assert ImageSet(Lambda(x, 1), S.EmptySet) == S.EmptySet
  81. empty = Intersection(FiniteSet(log(2)/pi), S.Integers)
  82. assert unchanged(ImageSet, Lambda(x, 1), empty) # issue #17471
  83. squares = ImageSet(Lambda(x, x**2), S.Naturals)
  84. assert 4 in squares
  85. assert 5 not in squares
  86. assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9)
  87. assert 16 not in squares.intersect(Interval(0, 10))
  88. si = iter(squares)
  89. a, b, c, d = next(si), next(si), next(si), next(si)
  90. assert (a, b, c, d) == (1, 4, 9, 16)
  91. harmonics = ImageSet(Lambda(x, 1/x), S.Naturals)
  92. assert Rational(1, 5) in harmonics
  93. assert Rational(.25) in harmonics
  94. assert 0.25 not in harmonics
  95. assert Rational(.3) not in harmonics
  96. assert (1, 2) not in harmonics
  97. assert harmonics.is_iterable
  98. assert imageset(x, -x, Interval(0, 1)) == Interval(-1, 0)
  99. assert ImageSet(Lambda(x, x**2), Interval(0, 2)).doit() == Interval(0, 4)
  100. assert ImageSet(Lambda((x, y), 2*x), {4}, {3}).doit() == FiniteSet(8)
  101. assert (ImageSet(Lambda((x, y), x+y), {1, 2, 3}, {10, 20, 30}).doit() ==
  102. FiniteSet(11, 12, 13, 21, 22, 23, 31, 32, 33))
  103. c = Interval(1, 3) * Interval(1, 3)
  104. assert Tuple(2, 6) in ImageSet(Lambda(((x, y),), (x, 2*y)), c)
  105. assert Tuple(2, S.Half) in ImageSet(Lambda(((x, y),), (x, 1/y)), c)
  106. assert Tuple(2, -2) not in ImageSet(Lambda(((x, y),), (x, y**2)), c)
  107. assert Tuple(2, -2) in ImageSet(Lambda(((x, y),), (x, -2)), c)
  108. c3 = ProductSet(Interval(3, 7), Interval(8, 11), Interval(5, 9))
  109. assert Tuple(8, 3, 9) in ImageSet(Lambda(((t, y, x),), (y, t, x)), c3)
  110. assert Tuple(Rational(1, 8), 3, 9) in ImageSet(Lambda(((t, y, x),), (1/y, t, x)), c3)
  111. assert 2/pi not in ImageSet(Lambda(((x, y),), 2/x), c)
  112. assert 2/S(100) not in ImageSet(Lambda(((x, y),), 2/x), c)
  113. assert Rational(2, 3) in ImageSet(Lambda(((x, y),), 2/x), c)
  114. S1 = imageset(lambda x, y: x + y, S.Integers, S.Naturals)
  115. assert S1.base_pset == ProductSet(S.Integers, S.Naturals)
  116. assert S1.base_sets == (S.Integers, S.Naturals)
  117. # Passing a set instead of a FiniteSet shouldn't raise
  118. assert unchanged(ImageSet, Lambda(x, x**2), {1, 2, 3})
  119. S2 = ImageSet(Lambda(((x, y),), x+y), {(1, 2), (3, 4)})
  120. assert 3 in S2.doit()
  121. # FIXME: This doesn't yet work:
  122. #assert 3 in S2
  123. assert S2._contains(3) is None
  124. raises(TypeError, lambda: ImageSet(Lambda(x, x**2), 1))
  125. def test_image_is_ImageSet():
  126. assert isinstance(imageset(x, sqrt(sin(x)), Range(5)), ImageSet)
  127. def test_halfcircle():
  128. r, th = symbols('r, theta', real=True)
  129. L = Lambda(((r, th),), (r*cos(th), r*sin(th)))
  130. halfcircle = ImageSet(L, Interval(0, 1)*Interval(0, pi))
  131. assert (1, 0) in halfcircle
  132. assert (0, -1) not in halfcircle
  133. assert (0, 0) in halfcircle
  134. assert halfcircle._contains((r, 0)) is None
  135. # This one doesn't work:
  136. #assert (r, 2*pi) not in halfcircle
  137. assert not halfcircle.is_iterable
  138. def test_ImageSet_iterator_not_injective():
  139. L = Lambda(x, x - x % 2) # produces 0, 2, 2, 4, 4, 6, 6, ...
  140. evens = ImageSet(L, S.Naturals)
  141. i = iter(evens)
  142. # No repeats here
  143. assert (next(i), next(i), next(i), next(i)) == (0, 2, 4, 6)
  144. def test_inf_Range_len():
  145. raises(ValueError, lambda: len(Range(0, oo, 2)))
  146. assert Range(0, oo, 2).size is S.Infinity
  147. assert Range(0, -oo, -2).size is S.Infinity
  148. assert Range(oo, 0, -2).size is S.Infinity
  149. assert Range(-oo, 0, 2).size is S.Infinity
  150. def test_Range_set():
  151. empty = Range(0)
  152. assert Range(5) == Range(0, 5) == Range(0, 5, 1)
  153. r = Range(10, 20, 2)
  154. assert 12 in r
  155. assert 8 not in r
  156. assert 11 not in r
  157. assert 30 not in r
  158. assert list(Range(0, 5)) == list(range(5))
  159. assert list(Range(5, 0, -1)) == list(range(5, 0, -1))
  160. assert Range(5, 15).sup == 14
  161. assert Range(5, 15).inf == 5
  162. assert Range(15, 5, -1).sup == 15
  163. assert Range(15, 5, -1).inf == 6
  164. assert Range(10, 67, 10).sup == 60
  165. assert Range(60, 7, -10).inf == 10
  166. assert len(Range(10, 38, 10)) == 3
  167. assert Range(0, 0, 5) == empty
  168. assert Range(oo, oo, 1) == empty
  169. assert Range(oo, 1, 1) == empty
  170. assert Range(-oo, 1, -1) == empty
  171. assert Range(1, oo, -1) == empty
  172. assert Range(1, -oo, 1) == empty
  173. assert Range(1, -4, oo) == empty
  174. ip = symbols('ip', positive=True)
  175. assert Range(0, ip, -1) == empty
  176. assert Range(0, -ip, 1) == empty
  177. assert Range(1, -4, -oo) == Range(1, 2)
  178. assert Range(1, 4, oo) == Range(1, 2)
  179. assert Range(-oo, oo).size == oo
  180. assert Range(oo, -oo, -1).size == oo
  181. raises(ValueError, lambda: Range(-oo, oo, 2))
  182. raises(ValueError, lambda: Range(x, pi, y))
  183. raises(ValueError, lambda: Range(x, y, 0))
  184. assert 5 in Range(0, oo, 5)
  185. assert -5 in Range(-oo, 0, 5)
  186. assert oo not in Range(0, oo)
  187. ni = symbols('ni', integer=False)
  188. assert ni not in Range(oo)
  189. u = symbols('u', integer=None)
  190. assert Range(oo).contains(u) is not False
  191. inf = symbols('inf', infinite=True)
  192. assert inf not in Range(-oo, oo)
  193. raises(ValueError, lambda: Range(0, oo, 2)[-1])
  194. raises(ValueError, lambda: Range(0, -oo, -2)[-1])
  195. assert Range(-oo, 1, 1)[-1] is S.Zero
  196. assert Range(oo, 1, -1)[-1] == 2
  197. assert inf not in Range(oo)
  198. assert Range(1, 10, 1)[-1] == 9
  199. assert all(i.is_Integer for i in Range(0, -1, 1))
  200. it = iter(Range(-oo, 0, 2))
  201. raises(TypeError, lambda: next(it))
  202. assert empty.intersect(S.Integers) == empty
  203. assert Range(-1, 10, 1).intersect(S.Integers) == Range(-1, 10, 1)
  204. assert Range(-1, 10, 1).intersect(S.Naturals) == Range(1, 10, 1)
  205. assert Range(-1, 10, 1).intersect(S.Naturals0) == Range(0, 10, 1)
  206. # test slicing
  207. assert Range(1, 10, 1)[5] == 6
  208. assert Range(1, 12, 2)[5] == 11
  209. assert Range(1, 10, 1)[-1] == 9
  210. assert Range(1, 10, 3)[-1] == 7
  211. raises(ValueError, lambda: Range(oo,0,-1)[1:3:0])
  212. raises(ValueError, lambda: Range(oo,0,-1)[:1])
  213. raises(ValueError, lambda: Range(1, oo)[-2])
  214. raises(ValueError, lambda: Range(-oo, 1)[2])
  215. raises(IndexError, lambda: Range(10)[-20])
  216. raises(IndexError, lambda: Range(10)[20])
  217. raises(ValueError, lambda: Range(2, -oo, -2)[2:2:0])
  218. assert Range(2, -oo, -2)[2:2:2] == empty
  219. assert Range(2, -oo, -2)[:2:2] == Range(2, -2, -4)
  220. raises(ValueError, lambda: Range(-oo, 4, 2)[:2:2])
  221. assert Range(-oo, 4, 2)[::-2] == Range(2, -oo, -4)
  222. raises(ValueError, lambda: Range(-oo, 4, 2)[::2])
  223. assert Range(oo, 2, -2)[::] == Range(oo, 2, -2)
  224. assert Range(-oo, 4, 2)[:-2:-2] == Range(2, 0, -4)
  225. assert Range(-oo, 4, 2)[:-2:2] == Range(-oo, 0, 4)
  226. raises(ValueError, lambda: Range(-oo, 4, 2)[:0:-2])
  227. raises(ValueError, lambda: Range(-oo, 4, 2)[:2:-2])
  228. assert Range(-oo, 4, 2)[-2::-2] == Range(0, -oo, -4)
  229. raises(ValueError, lambda: Range(-oo, 4, 2)[-2:0:-2])
  230. raises(ValueError, lambda: Range(-oo, 4, 2)[0::2])
  231. assert Range(oo, 2, -2)[0::] == Range(oo, 2, -2)
  232. raises(ValueError, lambda: Range(-oo, 4, 2)[0:-2:2])
  233. assert Range(oo, 2, -2)[0:-2:] == Range(oo, 6, -2)
  234. raises(ValueError, lambda: Range(oo, 2, -2)[0:2:])
  235. raises(ValueError, lambda: Range(-oo, 4, 2)[2::-1])
  236. assert Range(-oo, 4, 2)[-2::2] == Range(0, 4, 4)
  237. assert Range(oo, 0, -2)[-10:0:2] == empty
  238. raises(ValueError, lambda: Range(oo, 0, -2)[0])
  239. raises(ValueError, lambda: Range(oo, 0, -2)[-10:10:2])
  240. raises(ValueError, lambda: Range(oo, 0, -2)[0::-2])
  241. assert Range(oo, 0, -2)[0:-4:-2] == empty
  242. assert Range(oo, 0, -2)[:0:2] == empty
  243. raises(ValueError, lambda: Range(oo, 0, -2)[:1:-1])
  244. # test empty Range
  245. assert Range(x, x, y) == empty
  246. assert empty.reversed == empty
  247. assert 0 not in empty
  248. assert list(empty) == []
  249. assert len(empty) == 0
  250. assert empty.size is S.Zero
  251. assert empty.intersect(FiniteSet(0)) is S.EmptySet
  252. assert bool(empty) is False
  253. raises(IndexError, lambda: empty[0])
  254. assert empty[:0] == empty
  255. raises(NotImplementedError, lambda: empty.inf)
  256. raises(NotImplementedError, lambda: empty.sup)
  257. assert empty.as_relational(x) is S.false
  258. AB = [None] + list(range(12))
  259. for R in [
  260. Range(1, 10),
  261. Range(1, 10, 2),
  262. ]:
  263. r = list(R)
  264. for a, b, c in itertools.product(AB, AB, [-3, -1, None, 1, 3]):
  265. for reverse in range(2):
  266. r = list(reversed(r))
  267. R = R.reversed
  268. result = list(R[a:b:c])
  269. ans = r[a:b:c]
  270. txt = ('\n%s[%s:%s:%s] = %s -> %s' % (
  271. R, a, b, c, result, ans))
  272. check = ans == result
  273. assert check, txt
  274. assert Range(1, 10, 1).boundary == Range(1, 10, 1)
  275. for r in (Range(1, 10, 2), Range(1, oo, 2)):
  276. rev = r.reversed
  277. assert r.inf == rev.inf and r.sup == rev.sup
  278. assert r.step == -rev.step
  279. builtin_range = range
  280. raises(TypeError, lambda: Range(builtin_range(1)))
  281. assert S(builtin_range(10)) == Range(10)
  282. assert S(builtin_range(1000000000000)) == Range(1000000000000)
  283. # test Range.as_relational
  284. assert Range(1, 4).as_relational(x) == (x >= 1) & (x <= 3) & Eq(Mod(x, 1), 0)
  285. assert Range(oo, 1, -2).as_relational(x) == (x >= 3) & (x < oo) & Eq(Mod(x + 1, -2), 0)
  286. def test_Range_symbolic():
  287. # symbolic Range
  288. xr = Range(x, x + 4, 5)
  289. sr = Range(x, y, t)
  290. i = Symbol('i', integer=True)
  291. ip = Symbol('i', integer=True, positive=True)
  292. ipr = Range(ip)
  293. inr = Range(0, -ip, -1)
  294. ir = Range(i, i + 19, 2)
  295. ir2 = Range(i, i*8, 3*i)
  296. i = Symbol('i', integer=True)
  297. inf = symbols('inf', infinite=True)
  298. raises(ValueError, lambda: Range(inf))
  299. raises(ValueError, lambda: Range(inf, 0, -1))
  300. raises(ValueError, lambda: Range(inf, inf, 1))
  301. raises(ValueError, lambda: Range(1, 1, inf))
  302. # args
  303. assert xr.args == (x, x + 5, 5)
  304. assert sr.args == (x, y, t)
  305. assert ir.args == (i, i + 20, 2)
  306. assert ir2.args == (i, 10*i, 3*i)
  307. # reversed
  308. raises(ValueError, lambda: xr.reversed)
  309. raises(ValueError, lambda: sr.reversed)
  310. assert ipr.reversed.args == (ip - 1, -1, -1)
  311. assert inr.reversed.args == (-ip + 1, 1, 1)
  312. assert ir.reversed.args == (i + 18, i - 2, -2)
  313. assert ir2.reversed.args == (7*i, -2*i, -3*i)
  314. # contains
  315. assert inf not in sr
  316. assert inf not in ir
  317. assert 0 in ipr
  318. assert 0 in inr
  319. raises(TypeError, lambda: 1 in ipr)
  320. raises(TypeError, lambda: -1 in inr)
  321. assert .1 not in sr
  322. assert .1 not in ir
  323. assert i + 1 not in ir
  324. assert i + 2 in ir
  325. raises(TypeError, lambda: x in xr) # XXX is this what contains is supposed to do?
  326. raises(TypeError, lambda: 1 in sr) # XXX is this what contains is supposed to do?
  327. # iter
  328. raises(ValueError, lambda: next(iter(xr)))
  329. raises(ValueError, lambda: next(iter(sr)))
  330. assert next(iter(ir)) == i
  331. assert next(iter(ir2)) == i
  332. assert sr.intersect(S.Integers) == sr
  333. assert sr.intersect(FiniteSet(x)) == Intersection({x}, sr)
  334. raises(ValueError, lambda: sr[:2])
  335. raises(ValueError, lambda: xr[0])
  336. raises(ValueError, lambda: sr[0])
  337. # len
  338. assert len(ir) == ir.size == 10
  339. assert len(ir2) == ir2.size == 3
  340. raises(ValueError, lambda: len(xr))
  341. raises(ValueError, lambda: xr.size)
  342. raises(ValueError, lambda: len(sr))
  343. raises(ValueError, lambda: sr.size)
  344. # bool
  345. assert bool(Range(0)) == False
  346. assert bool(xr)
  347. assert bool(ir)
  348. assert bool(ipr)
  349. assert bool(inr)
  350. raises(ValueError, lambda: bool(sr))
  351. raises(ValueError, lambda: bool(ir2))
  352. # inf
  353. raises(ValueError, lambda: xr.inf)
  354. raises(ValueError, lambda: sr.inf)
  355. assert ipr.inf == 0
  356. assert inr.inf == -ip + 1
  357. assert ir.inf == i
  358. raises(ValueError, lambda: ir2.inf)
  359. # sup
  360. raises(ValueError, lambda: xr.sup)
  361. raises(ValueError, lambda: sr.sup)
  362. assert ipr.sup == ip - 1
  363. assert inr.sup == 0
  364. assert ir.inf == i
  365. raises(ValueError, lambda: ir2.sup)
  366. # getitem
  367. raises(ValueError, lambda: xr[0])
  368. raises(ValueError, lambda: sr[0])
  369. raises(ValueError, lambda: sr[-1])
  370. raises(ValueError, lambda: sr[:2])
  371. assert ir[:2] == Range(i, i + 4, 2)
  372. assert ir[0] == i
  373. assert ir[-2] == i + 16
  374. assert ir[-1] == i + 18
  375. assert ir2[:2] == Range(i, 7*i, 3*i)
  376. assert ir2[0] == i
  377. assert ir2[-2] == 4*i
  378. assert ir2[-1] == 7*i
  379. raises(ValueError, lambda: Range(i)[-1])
  380. assert ipr[0] == ipr.inf == 0
  381. assert ipr[-1] == ipr.sup == ip - 1
  382. assert inr[0] == inr.sup == 0
  383. assert inr[-1] == inr.inf == -ip + 1
  384. raises(ValueError, lambda: ipr[-2])
  385. assert ir.inf == i
  386. assert ir.sup == i + 18
  387. raises(ValueError, lambda: Range(i).inf)
  388. # as_relational
  389. assert ir.as_relational(x) == ((x >= i) & (x <= i + 18) &
  390. Eq(Mod(-i + x, 2), 0))
  391. assert ir2.as_relational(x) == Eq(
  392. Mod(-i + x, 3*i), 0) & (((x >= i) & (x <= 7*i) & (3*i >= 1)) |
  393. ((x <= i) & (x >= 7*i) & (3*i <= -1)))
  394. assert Range(i, i + 1).as_relational(x) == Eq(x, i)
  395. assert sr.as_relational(z) == Eq(
  396. Mod(t, 1), 0) & Eq(Mod(x, 1), 0) & Eq(Mod(-x + z, t), 0
  397. ) & (((z >= x) & (z <= -t + y) & (t >= 1)) |
  398. ((z <= x) & (z >= -t + y) & (t <= -1)))
  399. assert xr.as_relational(z) == Eq(z, x) & Eq(Mod(x, 1), 0)
  400. # symbols can clash if user wants (but it must be integer)
  401. assert xr.as_relational(x) == Eq(Mod(x, 1), 0)
  402. # contains() for symbolic values (issue #18146)
  403. e = Symbol('e', integer=True, even=True)
  404. o = Symbol('o', integer=True, odd=True)
  405. assert Range(5).contains(i) == And(i >= 0, i <= 4)
  406. assert Range(1).contains(i) == Eq(i, 0)
  407. assert Range(-oo, 5, 1).contains(i) == (i <= 4)
  408. assert Range(-oo, oo).contains(i) == True
  409. assert Range(0, 8, 2).contains(i) == Contains(i, Range(0, 8, 2))
  410. assert Range(0, 8, 2).contains(e) == And(e >= 0, e <= 6)
  411. assert Range(0, 8, 2).contains(2*i) == And(2*i >= 0, 2*i <= 6)
  412. assert Range(0, 8, 2).contains(o) == False
  413. assert Range(1, 9, 2).contains(e) == False
  414. assert Range(1, 9, 2).contains(o) == And(o >= 1, o <= 7)
  415. assert Range(8, 0, -2).contains(o) == False
  416. assert Range(9, 1, -2).contains(o) == And(o >= 3, o <= 9)
  417. assert Range(-oo, 8, 2).contains(i) == Contains(i, Range(-oo, 8, 2))
  418. def test_range_range_intersection():
  419. for a, b, r in [
  420. (Range(0), Range(1), S.EmptySet),
  421. (Range(3), Range(4, oo), S.EmptySet),
  422. (Range(3), Range(-3, -1), S.EmptySet),
  423. (Range(1, 3), Range(0, 3), Range(1, 3)),
  424. (Range(1, 3), Range(1, 4), Range(1, 3)),
  425. (Range(1, oo, 2), Range(2, oo, 2), S.EmptySet),
  426. (Range(0, oo, 2), Range(oo), Range(0, oo, 2)),
  427. (Range(0, oo, 2), Range(100), Range(0, 100, 2)),
  428. (Range(2, oo, 2), Range(oo), Range(2, oo, 2)),
  429. (Range(0, oo, 2), Range(5, 6), S.EmptySet),
  430. (Range(2, 80, 1), Range(55, 71, 4), Range(55, 71, 4)),
  431. (Range(0, 6, 3), Range(-oo, 5, 3), S.EmptySet),
  432. (Range(0, oo, 2), Range(5, oo, 3), Range(8, oo, 6)),
  433. (Range(4, 6, 2), Range(2, 16, 7), S.EmptySet),]:
  434. assert a.intersect(b) == r
  435. assert a.intersect(b.reversed) == r
  436. assert a.reversed.intersect(b) == r
  437. assert a.reversed.intersect(b.reversed) == r
  438. a, b = b, a
  439. assert a.intersect(b) == r
  440. assert a.intersect(b.reversed) == r
  441. assert a.reversed.intersect(b) == r
  442. assert a.reversed.intersect(b.reversed) == r
  443. def test_range_interval_intersection():
  444. p = symbols('p', positive=True)
  445. assert isinstance(Range(3).intersect(Interval(p, p + 2)), Intersection)
  446. assert Range(4).intersect(Interval(0, 3)) == Range(4)
  447. assert Range(4).intersect(Interval(-oo, oo)) == Range(4)
  448. assert Range(4).intersect(Interval(1, oo)) == Range(1, 4)
  449. assert Range(4).intersect(Interval(1.1, oo)) == Range(2, 4)
  450. assert Range(4).intersect(Interval(0.1, 3)) == Range(1, 4)
  451. assert Range(4).intersect(Interval(0.1, 3.1)) == Range(1, 4)
  452. assert Range(4).intersect(Interval.open(0, 3)) == Range(1, 3)
  453. assert Range(4).intersect(Interval.open(0.1, 0.5)) is S.EmptySet
  454. # Null Range intersections
  455. assert Range(0).intersect(Interval(0.2, 0.8)) is S.EmptySet
  456. assert Range(0).intersect(Interval(-oo, oo)) is S.EmptySet
  457. def test_range_is_finite_set():
  458. assert Range(-100, 100).is_finite_set is True
  459. assert Range(2, oo).is_finite_set is False
  460. assert Range(-oo, 50).is_finite_set is False
  461. assert Range(-oo, oo).is_finite_set is False
  462. assert Range(oo, -oo).is_finite_set is True
  463. assert Range(0, 0).is_finite_set is True
  464. assert Range(oo, oo).is_finite_set is True
  465. assert Range(-oo, -oo).is_finite_set is True
  466. n = Symbol('n', integer=True)
  467. m = Symbol('m', integer=True)
  468. assert Range(n, n + 49).is_finite_set is True
  469. assert Range(n, 0).is_finite_set is True
  470. assert Range(-3, n + 7).is_finite_set is True
  471. assert Range(n, m).is_finite_set is True
  472. assert Range(n + m, m - n).is_finite_set is True
  473. assert Range(n, n + m + n).is_finite_set is True
  474. assert Range(n, oo).is_finite_set is False
  475. assert Range(-oo, n).is_finite_set is False
  476. assert Range(n, -oo).is_finite_set is True
  477. assert Range(oo, n).is_finite_set is True
  478. def test_Range_is_iterable():
  479. assert Range(-100, 100).is_iterable is True
  480. assert Range(2, oo).is_iterable is False
  481. assert Range(-oo, 50).is_iterable is False
  482. assert Range(-oo, oo).is_iterable is False
  483. assert Range(oo, -oo).is_iterable is True
  484. assert Range(0, 0).is_iterable is True
  485. assert Range(oo, oo).is_iterable is True
  486. assert Range(-oo, -oo).is_iterable is True
  487. n = Symbol('n', integer=True)
  488. m = Symbol('m', integer=True)
  489. p = Symbol('p', integer=True, positive=True)
  490. assert Range(n, n + 49).is_iterable is True
  491. assert Range(n, 0).is_iterable is False
  492. assert Range(-3, n + 7).is_iterable is False
  493. assert Range(-3, p + 7).is_iterable is False # Should work with better __iter__
  494. assert Range(n, m).is_iterable is False
  495. assert Range(n + m, m - n).is_iterable is False
  496. assert Range(n, n + m + n).is_iterable is False
  497. assert Range(n, oo).is_iterable is False
  498. assert Range(-oo, n).is_iterable is False
  499. x = Symbol('x')
  500. assert Range(x, x + 49).is_iterable is False
  501. assert Range(x, 0).is_iterable is False
  502. assert Range(-3, x + 7).is_iterable is False
  503. assert Range(x, m).is_iterable is False
  504. assert Range(x + m, m - x).is_iterable is False
  505. assert Range(x, x + m + x).is_iterable is False
  506. assert Range(x, oo).is_iterable is False
  507. assert Range(-oo, x).is_iterable is False
  508. def test_Integers_eval_imageset():
  509. ans = ImageSet(Lambda(x, 2*x + Rational(3, 7)), S.Integers)
  510. im = imageset(Lambda(x, -2*x + Rational(3, 7)), S.Integers)
  511. assert im == ans
  512. im = imageset(Lambda(x, -2*x - Rational(11, 7)), S.Integers)
  513. assert im == ans
  514. y = Symbol('y')
  515. L = imageset(x, 2*x + y, S.Integers)
  516. assert y + 4 in L
  517. a, b, c = 0.092, 0.433, 0.341
  518. assert a in imageset(x, a + c*x, S.Integers)
  519. assert b in imageset(x, b + c*x, S.Integers)
  520. _x = symbols('x', negative=True)
  521. eq = _x**2 - _x + 1
  522. assert imageset(_x, eq, S.Integers).lamda.expr == _x**2 + _x + 1
  523. eq = 3*_x - 1
  524. assert imageset(_x, eq, S.Integers).lamda.expr == 3*_x + 2
  525. assert imageset(x, (x, 1/x), S.Integers) == \
  526. ImageSet(Lambda(x, (x, 1/x)), S.Integers)
  527. def test_Range_eval_imageset():
  528. a, b, c = symbols('a b c')
  529. assert imageset(x, a*(x + b) + c, Range(3)) == \
  530. imageset(x, a*x + a*b + c, Range(3))
  531. eq = (x + 1)**2
  532. assert imageset(x, eq, Range(3)).lamda.expr == eq
  533. eq = a*(x + b) + c
  534. r = Range(3, -3, -2)
  535. imset = imageset(x, eq, r)
  536. assert imset.lamda.expr != eq
  537. assert list(imset) == [eq.subs(x, i).expand() for i in list(r)]
  538. def test_fun():
  539. assert (FiniteSet(*ImageSet(Lambda(x, sin(pi*x/4)),
  540. Range(-10, 11))) == FiniteSet(-1, -sqrt(2)/2, 0, sqrt(2)/2, 1))
  541. def test_Range_is_empty():
  542. i = Symbol('i', integer=True)
  543. n = Symbol('n', negative=True, integer=True)
  544. p = Symbol('p', positive=True, integer=True)
  545. assert Range(0).is_empty
  546. assert not Range(1).is_empty
  547. assert Range(1, 0).is_empty
  548. assert not Range(-1, 0).is_empty
  549. assert Range(i).is_empty is None
  550. assert Range(n).is_empty
  551. assert Range(p).is_empty is False
  552. assert Range(n, 0).is_empty is False
  553. assert Range(n, p).is_empty is False
  554. assert Range(p, n).is_empty
  555. assert Range(n, -1).is_empty is None
  556. assert Range(p, n, -1).is_empty is False
  557. def test_Reals():
  558. assert 5 in S.Reals
  559. assert S.Pi in S.Reals
  560. assert -sqrt(2) in S.Reals
  561. assert (2, 5) not in S.Reals
  562. assert sqrt(-1) not in S.Reals
  563. assert S.Reals == Interval(-oo, oo)
  564. assert S.Reals != Interval(0, oo)
  565. assert S.Reals.is_subset(Interval(-oo, oo))
  566. assert S.Reals.intersect(Range(-oo, oo)) == Range(-oo, oo)
  567. assert S.ComplexInfinity not in S.Reals
  568. assert S.NaN not in S.Reals
  569. assert x + S.ComplexInfinity not in S.Reals
  570. def test_Complex():
  571. assert 5 in S.Complexes
  572. assert 5 + 4*I in S.Complexes
  573. assert S.Pi in S.Complexes
  574. assert -sqrt(2) in S.Complexes
  575. assert -I in S.Complexes
  576. assert sqrt(-1) in S.Complexes
  577. assert S.Complexes.intersect(S.Reals) == S.Reals
  578. assert S.Complexes.union(S.Reals) == S.Complexes
  579. assert S.Complexes == ComplexRegion(S.Reals*S.Reals)
  580. assert (S.Complexes == ComplexRegion(Interval(1, 2)*Interval(3, 4))) == False
  581. assert str(S.Complexes) == "Complexes"
  582. assert repr(S.Complexes) == "Complexes"
  583. def take(n, iterable):
  584. "Return first n items of the iterable as a list"
  585. return list(itertools.islice(iterable, n))
  586. def test_intersections():
  587. assert S.Integers.intersect(S.Reals) == S.Integers
  588. assert 5 in S.Integers.intersect(S.Reals)
  589. assert 5 in S.Integers.intersect(S.Reals)
  590. assert -5 not in S.Naturals.intersect(S.Reals)
  591. assert 5.5 not in S.Integers.intersect(S.Reals)
  592. assert 5 in S.Integers.intersect(Interval(3, oo))
  593. assert -5 in S.Integers.intersect(Interval(-oo, 3))
  594. assert all(x.is_Integer
  595. for x in take(10, S.Integers.intersect(Interval(3, oo)) ))
  596. def test_infinitely_indexed_set_1():
  597. from sympy.abc import n, m
  598. assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(m, m), S.Integers)
  599. assert imageset(Lambda(n, 2*n), S.Integers).intersect(
  600. imageset(Lambda(m, 2*m + 1), S.Integers)) is S.EmptySet
  601. assert imageset(Lambda(n, 2*n), S.Integers).intersect(
  602. imageset(Lambda(n, 2*n + 1), S.Integers)) is S.EmptySet
  603. assert imageset(Lambda(m, 2*m), S.Integers).intersect(
  604. imageset(Lambda(n, 3*n), S.Integers)).dummy_eq(
  605. ImageSet(Lambda(t, 6*t), S.Integers))
  606. assert imageset(x, x/2 + Rational(1, 3), S.Integers).intersect(S.Integers) is S.EmptySet
  607. assert imageset(x, x/2 + S.Half, S.Integers).intersect(S.Integers) is S.Integers
  608. # https://github.com/sympy/sympy/issues/17355
  609. S53 = ImageSet(Lambda(n, 5*n + 3), S.Integers)
  610. assert S53.intersect(S.Integers) == S53
  611. def test_infinitely_indexed_set_2():
  612. from sympy.abc import n
  613. a = Symbol('a', integer=True)
  614. assert imageset(Lambda(n, n), S.Integers) == \
  615. imageset(Lambda(n, n + a), S.Integers)
  616. assert imageset(Lambda(n, n + pi), S.Integers) == \
  617. imageset(Lambda(n, n + a + pi), S.Integers)
  618. assert imageset(Lambda(n, n), S.Integers) == \
  619. imageset(Lambda(n, -n + a), S.Integers)
  620. assert imageset(Lambda(n, -6*n), S.Integers) == \
  621. ImageSet(Lambda(n, 6*n), S.Integers)
  622. assert imageset(Lambda(n, 2*n + pi), S.Integers) == \
  623. ImageSet(Lambda(n, 2*n + pi - 2), S.Integers)
  624. def test_imageset_intersect_real():
  625. from sympy.abc import n
  626. assert imageset(Lambda(n, n + (n - 1)*(n + 1)*I), S.Integers).intersect(S.Reals) == FiniteSet(-1, 1)
  627. im = (n - 1)*(n + S.Half)
  628. assert imageset(Lambda(n, n + im*I), S.Integers
  629. ).intersect(S.Reals) == FiniteSet(1)
  630. assert imageset(Lambda(n, n + im*(n + 1)*I), S.Naturals0
  631. ).intersect(S.Reals) == FiniteSet(1)
  632. assert imageset(Lambda(n, n/2 + im.expand()*I), S.Integers
  633. ).intersect(S.Reals) == ImageSet(Lambda(x, x/2), ConditionSet(
  634. n, Eq(n**2 - n/2 - S(1)/2, 0), S.Integers))
  635. assert imageset(Lambda(n, n/(1/n - 1) + im*(n + 1)*I), S.Integers
  636. ).intersect(S.Reals) == FiniteSet(S.Half)
  637. assert imageset(Lambda(n, n/(n - 6) +
  638. (n - 3)*(n + 1)*I/(2*n + 2)), S.Integers).intersect(
  639. S.Reals) == FiniteSet(-1)
  640. assert imageset(Lambda(n, n/(n**2 - 9) +
  641. (n - 3)*(n + 1)*I/(2*n + 2)), S.Integers).intersect(
  642. S.Reals) is S.EmptySet
  643. s = ImageSet(
  644. Lambda(n, -I*(I*(2*pi*n - pi/4) + log(Abs(sqrt(-I))))),
  645. S.Integers)
  646. # s is unevaluated, but after intersection the result
  647. # should be canonical
  648. assert s.intersect(S.Reals) == imageset(
  649. Lambda(n, 2*n*pi - pi/4), S.Integers) == ImageSet(
  650. Lambda(n, 2*pi*n + pi*Rational(7, 4)), S.Integers)
  651. def test_imageset_intersect_interval():
  652. from sympy.abc import n
  653. f1 = ImageSet(Lambda(n, n*pi), S.Integers)
  654. f2 = ImageSet(Lambda(n, 2*n), Interval(0, pi))
  655. f3 = ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers)
  656. # complex expressions
  657. f4 = ImageSet(Lambda(n, n*I*pi), S.Integers)
  658. f5 = ImageSet(Lambda(n, 2*I*n*pi + pi/2), S.Integers)
  659. # non-linear expressions
  660. f6 = ImageSet(Lambda(n, log(n)), S.Integers)
  661. f7 = ImageSet(Lambda(n, n**2), S.Integers)
  662. f8 = ImageSet(Lambda(n, Abs(n)), S.Integers)
  663. f9 = ImageSet(Lambda(n, exp(n)), S.Naturals0)
  664. assert f1.intersect(Interval(-1, 1)) == FiniteSet(0)
  665. assert f1.intersect(Interval(0, 2*pi, False, True)) == FiniteSet(0, pi)
  666. assert f2.intersect(Interval(1, 2)) == Interval(1, 2)
  667. assert f3.intersect(Interval(-1, 1)) == S.EmptySet
  668. assert f3.intersect(Interval(-5, 5)) == FiniteSet(pi*Rational(-3, 2), pi/2)
  669. assert f4.intersect(Interval(-1, 1)) == FiniteSet(0)
  670. assert f4.intersect(Interval(1, 2)) == S.EmptySet
  671. assert f5.intersect(Interval(0, 1)) == S.EmptySet
  672. assert f6.intersect(Interval(0, 1)) == FiniteSet(S.Zero, log(2))
  673. assert f7.intersect(Interval(0, 10)) == Intersection(f7, Interval(0, 10))
  674. assert f8.intersect(Interval(0, 2)) == Intersection(f8, Interval(0, 2))
  675. assert f9.intersect(Interval(1, 2)) == Intersection(f9, Interval(1, 2))
  676. def test_imageset_intersect_diophantine():
  677. from sympy.abc import m, n
  678. # Check that same lambda variable for both ImageSets is handled correctly
  679. img1 = ImageSet(Lambda(n, 2*n + 1), S.Integers)
  680. img2 = ImageSet(Lambda(n, 4*n + 1), S.Integers)
  681. assert img1.intersect(img2) == img2
  682. # Empty solution set returned by diophantine:
  683. assert ImageSet(Lambda(n, 2*n), S.Integers).intersect(
  684. ImageSet(Lambda(n, 2*n + 1), S.Integers)) == S.EmptySet
  685. # Check intersection with S.Integers:
  686. assert ImageSet(Lambda(n, 9/n + 20*n/3), S.Integers).intersect(
  687. S.Integers) == FiniteSet(-61, -23, 23, 61)
  688. # Single solution (2, 3) for diophantine solution:
  689. assert ImageSet(Lambda(n, (n - 2)**2), S.Integers).intersect(
  690. ImageSet(Lambda(n, -(n - 3)**2), S.Integers)) == FiniteSet(0)
  691. # Single parametric solution for diophantine solution:
  692. assert ImageSet(Lambda(n, n**2 + 5), S.Integers).intersect(
  693. ImageSet(Lambda(m, 2*m), S.Integers)).dummy_eq(ImageSet(
  694. Lambda(n, 4*n**2 + 4*n + 6), S.Integers))
  695. # 4 non-parametric solution couples for dioph. equation:
  696. assert ImageSet(Lambda(n, n**2 - 9), S.Integers).intersect(
  697. ImageSet(Lambda(m, -m**2), S.Integers)) == FiniteSet(-9, 0)
  698. # Double parametric solution for diophantine solution:
  699. assert ImageSet(Lambda(m, m**2 + 40), S.Integers).intersect(
  700. ImageSet(Lambda(n, 41*n), S.Integers)).dummy_eq(Intersection(
  701. ImageSet(Lambda(m, m**2 + 40), S.Integers),
  702. ImageSet(Lambda(n, 41*n), S.Integers)))
  703. # Check that diophantine returns *all* (8) solutions (permute=True)
  704. assert ImageSet(Lambda(n, n**4 - 2**4), S.Integers).intersect(
  705. ImageSet(Lambda(m, -m**4 + 3**4), S.Integers)) == FiniteSet(0, 65)
  706. assert ImageSet(Lambda(n, pi/12 + n*5*pi/12), S.Integers).intersect(
  707. ImageSet(Lambda(n, 7*pi/12 + n*11*pi/12), S.Integers)).dummy_eq(ImageSet(
  708. Lambda(n, 55*pi*n/12 + 17*pi/4), S.Integers))
  709. # TypeError raised by diophantine (#18081)
  710. assert ImageSet(Lambda(n, n*log(2)), S.Integers).intersection(
  711. S.Integers).dummy_eq(Intersection(ImageSet(
  712. Lambda(n, n*log(2)), S.Integers), S.Integers))
  713. # NotImplementedError raised by diophantine (no solver for cubic_thue)
  714. assert ImageSet(Lambda(n, n**3 + 1), S.Integers).intersect(
  715. ImageSet(Lambda(n, n**3), S.Integers)).dummy_eq(Intersection(
  716. ImageSet(Lambda(n, n**3 + 1), S.Integers),
  717. ImageSet(Lambda(n, n**3), S.Integers)))
  718. def test_infinitely_indexed_set_3():
  719. from sympy.abc import n, m
  720. assert imageset(Lambda(m, 2*pi*m), S.Integers).intersect(
  721. imageset(Lambda(n, 3*pi*n), S.Integers)).dummy_eq(
  722. ImageSet(Lambda(t, 6*pi*t), S.Integers))
  723. assert imageset(Lambda(n, 2*n + 1), S.Integers) == \
  724. imageset(Lambda(n, 2*n - 1), S.Integers)
  725. assert imageset(Lambda(n, 3*n + 2), S.Integers) == \
  726. imageset(Lambda(n, 3*n - 1), S.Integers)
  727. def test_ImageSet_simplification():
  728. from sympy.abc import n, m
  729. assert imageset(Lambda(n, n), S.Integers) == S.Integers
  730. assert imageset(Lambda(n, sin(n)),
  731. imageset(Lambda(m, tan(m)), S.Integers)) == \
  732. imageset(Lambda(m, sin(tan(m))), S.Integers)
  733. assert imageset(n, 1 + 2*n, S.Naturals) == Range(3, oo, 2)
  734. assert imageset(n, 1 + 2*n, S.Naturals0) == Range(1, oo, 2)
  735. assert imageset(n, 1 - 2*n, S.Naturals) == Range(-1, -oo, -2)
  736. def test_ImageSet_contains():
  737. assert (2, S.Half) in imageset(x, (x, 1/x), S.Integers)
  738. assert imageset(x, x + I*3, S.Integers).intersection(S.Reals) is S.EmptySet
  739. i = Dummy(integer=True)
  740. q = imageset(x, x + I*y, S.Integers).intersection(S.Reals)
  741. assert q.subs(y, I*i).intersection(S.Integers) is S.Integers
  742. q = imageset(x, x + I*y/x, S.Integers).intersection(S.Reals)
  743. assert q.subs(y, 0) is S.Integers
  744. assert q.subs(y, I*i*x).intersection(S.Integers) is S.Integers
  745. z = cos(1)**2 + sin(1)**2 - 1
  746. q = imageset(x, x + I*z, S.Integers).intersection(S.Reals)
  747. assert q is not S.EmptySet
  748. def test_ComplexRegion_contains():
  749. r = Symbol('r', real=True)
  750. # contains in ComplexRegion
  751. a = Interval(2, 3)
  752. b = Interval(4, 6)
  753. c = Interval(7, 9)
  754. c1 = ComplexRegion(a*b)
  755. c2 = ComplexRegion(Union(a*b, c*a))
  756. assert 2.5 + 4.5*I in c1
  757. assert 2 + 4*I in c1
  758. assert 3 + 4*I in c1
  759. assert 8 + 2.5*I in c2
  760. assert 2.5 + 6.1*I not in c1
  761. assert 4.5 + 3.2*I not in c1
  762. assert c1.contains(x) == Contains(x, c1, evaluate=False)
  763. assert c1.contains(r) == False
  764. assert c2.contains(x) == Contains(x, c2, evaluate=False)
  765. assert c2.contains(r) == False
  766. r1 = Interval(0, 1)
  767. theta1 = Interval(0, 2*S.Pi)
  768. c3 = ComplexRegion(r1*theta1, polar=True)
  769. assert (0.5 + I*6/10) in c3
  770. assert (S.Half + I*6/10) in c3
  771. assert (S.Half + .6*I) in c3
  772. assert (0.5 + .6*I) in c3
  773. assert I in c3
  774. assert 1 in c3
  775. assert 0 in c3
  776. assert 1 + I not in c3
  777. assert 1 - I not in c3
  778. assert c3.contains(x) == Contains(x, c3, evaluate=False)
  779. assert c3.contains(r + 2*I) == Contains(
  780. r + 2*I, c3, evaluate=False) # is in fact False
  781. assert c3.contains(1/(1 + r**2)) == Contains(
  782. 1/(1 + r**2), c3, evaluate=False) # is in fact True
  783. r2 = Interval(0, 3)
  784. theta2 = Interval(pi, 2*pi, left_open=True)
  785. c4 = ComplexRegion(r2*theta2, polar=True)
  786. assert c4.contains(0) == True
  787. assert c4.contains(2 + I) == False
  788. assert c4.contains(-2 + I) == False
  789. assert c4.contains(-2 - I) == True
  790. assert c4.contains(2 - I) == True
  791. assert c4.contains(-2) == False
  792. assert c4.contains(2) == True
  793. assert c4.contains(x) == Contains(x, c4, evaluate=False)
  794. assert c4.contains(3/(1 + r**2)) == Contains(
  795. 3/(1 + r**2), c4, evaluate=False) # is in fact True
  796. raises(ValueError, lambda: ComplexRegion(r1*theta1, polar=2))
  797. def test_symbolic_Range():
  798. n = Symbol('n')
  799. raises(ValueError, lambda: Range(n)[0])
  800. raises(IndexError, lambda: Range(n, n)[0])
  801. raises(ValueError, lambda: Range(n, n+1)[0])
  802. raises(ValueError, lambda: Range(n).size)
  803. n = Symbol('n', integer=True)
  804. raises(ValueError, lambda: Range(n)[0])
  805. raises(IndexError, lambda: Range(n, n)[0])
  806. assert Range(n, n+1)[0] == n
  807. raises(ValueError, lambda: Range(n).size)
  808. assert Range(n, n+1).size == 1
  809. n = Symbol('n', integer=True, nonnegative=True)
  810. raises(ValueError, lambda: Range(n)[0])
  811. raises(IndexError, lambda: Range(n, n)[0])
  812. assert Range(n+1)[0] == 0
  813. assert Range(n, n+1)[0] == n
  814. assert Range(n).size == n
  815. assert Range(n+1).size == n+1
  816. assert Range(n, n+1).size == 1
  817. n = Symbol('n', integer=True, positive=True)
  818. assert Range(n)[0] == 0
  819. assert Range(n, n+1)[0] == n
  820. assert Range(n).size == n
  821. assert Range(n, n+1).size == 1
  822. m = Symbol('m', integer=True, positive=True)
  823. assert Range(n, n+m)[0] == n
  824. assert Range(n, n+m).size == m
  825. assert Range(n, n+1).size == 1
  826. assert Range(n, n+m, 2).size == floor(m/2)
  827. m = Symbol('m', integer=True, positive=True, even=True)
  828. assert Range(n, n+m, 2).size == m/2
  829. def test_issue_18400():
  830. n = Symbol('n', integer=True)
  831. raises(ValueError, lambda: imageset(lambda x: x*2, Range(n)))
  832. n = Symbol('n', integer=True, positive=True)
  833. # No exception
  834. assert imageset(lambda x: x*2, Range(n)) == imageset(lambda x: x*2, Range(n))
  835. def test_ComplexRegion_intersect():
  836. # Polar form
  837. X_axis = ComplexRegion(Interval(0, oo)*FiniteSet(0, S.Pi), polar=True)
  838. unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, 2*S.Pi), polar=True)
  839. upper_half_unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, S.Pi), polar=True)
  840. upper_half_disk = ComplexRegion(Interval(0, oo)*Interval(0, S.Pi), polar=True)
  841. lower_half_disk = ComplexRegion(Interval(0, oo)*Interval(S.Pi, 2*S.Pi), polar=True)
  842. right_half_disk = ComplexRegion(Interval(0, oo)*Interval(-S.Pi/2, S.Pi/2), polar=True)
  843. first_quad_disk = ComplexRegion(Interval(0, oo)*Interval(0, S.Pi/2), polar=True)
  844. assert upper_half_disk.intersect(unit_disk) == upper_half_unit_disk
  845. assert right_half_disk.intersect(first_quad_disk) == first_quad_disk
  846. assert upper_half_disk.intersect(right_half_disk) == first_quad_disk
  847. assert upper_half_disk.intersect(lower_half_disk) == X_axis
  848. c1 = ComplexRegion(Interval(0, 4)*Interval(0, 2*S.Pi), polar=True)
  849. assert c1.intersect(Interval(1, 5)) == Interval(1, 4)
  850. assert c1.intersect(Interval(4, 9)) == FiniteSet(4)
  851. assert c1.intersect(Interval(5, 12)) is S.EmptySet
  852. # Rectangular form
  853. X_axis = ComplexRegion(Interval(-oo, oo)*FiniteSet(0))
  854. unit_square = ComplexRegion(Interval(-1, 1)*Interval(-1, 1))
  855. upper_half_unit_square = ComplexRegion(Interval(-1, 1)*Interval(0, 1))
  856. upper_half_plane = ComplexRegion(Interval(-oo, oo)*Interval(0, oo))
  857. lower_half_plane = ComplexRegion(Interval(-oo, oo)*Interval(-oo, 0))
  858. right_half_plane = ComplexRegion(Interval(0, oo)*Interval(-oo, oo))
  859. first_quad_plane = ComplexRegion(Interval(0, oo)*Interval(0, oo))
  860. assert upper_half_plane.intersect(unit_square) == upper_half_unit_square
  861. assert right_half_plane.intersect(first_quad_plane) == first_quad_plane
  862. assert upper_half_plane.intersect(right_half_plane) == first_quad_plane
  863. assert upper_half_plane.intersect(lower_half_plane) == X_axis
  864. c1 = ComplexRegion(Interval(-5, 5)*Interval(-10, 10))
  865. assert c1.intersect(Interval(2, 7)) == Interval(2, 5)
  866. assert c1.intersect(Interval(5, 7)) == FiniteSet(5)
  867. assert c1.intersect(Interval(6, 9)) is S.EmptySet
  868. # unevaluated object
  869. C1 = ComplexRegion(Interval(0, 1)*Interval(0, 2*S.Pi), polar=True)
  870. C2 = ComplexRegion(Interval(-1, 1)*Interval(-1, 1))
  871. assert C1.intersect(C2) == Intersection(C1, C2, evaluate=False)
  872. def test_ComplexRegion_union():
  873. # Polar form
  874. c1 = ComplexRegion(Interval(0, 1)*Interval(0, 2*S.Pi), polar=True)
  875. c2 = ComplexRegion(Interval(0, 1)*Interval(0, S.Pi), polar=True)
  876. c3 = ComplexRegion(Interval(0, oo)*Interval(0, S.Pi), polar=True)
  877. c4 = ComplexRegion(Interval(0, oo)*Interval(S.Pi, 2*S.Pi), polar=True)
  878. p1 = Union(Interval(0, 1)*Interval(0, 2*S.Pi), Interval(0, 1)*Interval(0, S.Pi))
  879. p2 = Union(Interval(0, oo)*Interval(0, S.Pi), Interval(0, oo)*Interval(S.Pi, 2*S.Pi))
  880. assert c1.union(c2) == ComplexRegion(p1, polar=True)
  881. assert c3.union(c4) == ComplexRegion(p2, polar=True)
  882. # Rectangular form
  883. c5 = ComplexRegion(Interval(2, 5)*Interval(6, 9))
  884. c6 = ComplexRegion(Interval(4, 6)*Interval(10, 12))
  885. c7 = ComplexRegion(Interval(0, 10)*Interval(-10, 0))
  886. c8 = ComplexRegion(Interval(12, 16)*Interval(14, 20))
  887. p3 = Union(Interval(2, 5)*Interval(6, 9), Interval(4, 6)*Interval(10, 12))
  888. p4 = Union(Interval(0, 10)*Interval(-10, 0), Interval(12, 16)*Interval(14, 20))
  889. assert c5.union(c6) == ComplexRegion(p3)
  890. assert c7.union(c8) == ComplexRegion(p4)
  891. assert c1.union(Interval(2, 4)) == Union(c1, Interval(2, 4), evaluate=False)
  892. assert c5.union(Interval(2, 4)) == Union(c5, ComplexRegion.from_real(Interval(2, 4)))
  893. def test_ComplexRegion_from_real():
  894. c1 = ComplexRegion(Interval(0, 1) * Interval(0, 2 * S.Pi), polar=True)
  895. raises(ValueError, lambda: c1.from_real(c1))
  896. assert c1.from_real(Interval(-1, 1)) == ComplexRegion(Interval(-1, 1) * FiniteSet(0), False)
  897. def test_ComplexRegion_measure():
  898. a, b = Interval(2, 5), Interval(4, 8)
  899. theta1, theta2 = Interval(0, 2*S.Pi), Interval(0, S.Pi)
  900. c1 = ComplexRegion(a*b)
  901. c2 = ComplexRegion(Union(a*theta1, b*theta2), polar=True)
  902. assert c1.measure == 12
  903. assert c2.measure == 9*pi
  904. def test_normalize_theta_set():
  905. # Interval
  906. assert normalize_theta_set(Interval(pi, 2*pi)) == \
  907. Union(FiniteSet(0), Interval.Ropen(pi, 2*pi))
  908. assert normalize_theta_set(Interval(pi*Rational(9, 2), 5*pi)) == Interval(pi/2, pi)
  909. assert normalize_theta_set(Interval(pi*Rational(-3, 2), pi/2)) == Interval.Ropen(0, 2*pi)
  910. assert normalize_theta_set(Interval.open(pi*Rational(-3, 2), pi/2)) == \
  911. Union(Interval.Ropen(0, pi/2), Interval.open(pi/2, 2*pi))
  912. assert normalize_theta_set(Interval.open(pi*Rational(-7, 2), pi*Rational(-3, 2))) == \
  913. Union(Interval.Ropen(0, pi/2), Interval.open(pi/2, 2*pi))
  914. assert normalize_theta_set(Interval(-pi/2, pi/2)) == \
  915. Union(Interval(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi))
  916. assert normalize_theta_set(Interval.open(-pi/2, pi/2)) == \
  917. Union(Interval.Ropen(0, pi/2), Interval.open(pi*Rational(3, 2), 2*pi))
  918. assert normalize_theta_set(Interval(-4*pi, 3*pi)) == Interval.Ropen(0, 2*pi)
  919. assert normalize_theta_set(Interval(pi*Rational(-3, 2), -pi/2)) == Interval(pi/2, pi*Rational(3, 2))
  920. assert normalize_theta_set(Interval.open(0, 2*pi)) == Interval.open(0, 2*pi)
  921. assert normalize_theta_set(Interval.Ropen(-pi/2, pi/2)) == \
  922. Union(Interval.Ropen(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi))
  923. assert normalize_theta_set(Interval.Lopen(-pi/2, pi/2)) == \
  924. Union(Interval(0, pi/2), Interval.open(pi*Rational(3, 2), 2*pi))
  925. assert normalize_theta_set(Interval(-pi/2, pi/2)) == \
  926. Union(Interval(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi))
  927. assert normalize_theta_set(Interval.open(4*pi, pi*Rational(9, 2))) == Interval.open(0, pi/2)
  928. assert normalize_theta_set(Interval.Lopen(4*pi, pi*Rational(9, 2))) == Interval.Lopen(0, pi/2)
  929. assert normalize_theta_set(Interval.Ropen(4*pi, pi*Rational(9, 2))) == Interval.Ropen(0, pi/2)
  930. assert normalize_theta_set(Interval.open(3*pi, 5*pi)) == \
  931. Union(Interval.Ropen(0, pi), Interval.open(pi, 2*pi))
  932. # FiniteSet
  933. assert normalize_theta_set(FiniteSet(0, pi, 3*pi)) == FiniteSet(0, pi)
  934. assert normalize_theta_set(FiniteSet(0, pi/2, pi, 2*pi)) == FiniteSet(0, pi/2, pi)
  935. assert normalize_theta_set(FiniteSet(0, -pi/2, -pi, -2*pi)) == FiniteSet(0, pi, pi*Rational(3, 2))
  936. assert normalize_theta_set(FiniteSet(pi*Rational(-3, 2), pi/2)) == \
  937. FiniteSet(pi/2)
  938. assert normalize_theta_set(FiniteSet(2*pi)) == FiniteSet(0)
  939. # Unions
  940. assert normalize_theta_set(Union(Interval(0, pi/3), Interval(pi/2, pi))) == \
  941. Union(Interval(0, pi/3), Interval(pi/2, pi))
  942. assert normalize_theta_set(Union(Interval(0, pi), Interval(2*pi, pi*Rational(7, 3)))) == \
  943. Interval(0, pi)
  944. # ValueError for non-real sets
  945. raises(ValueError, lambda: normalize_theta_set(S.Complexes))
  946. # NotImplementedError for subset of reals
  947. raises(NotImplementedError, lambda: normalize_theta_set(Interval(0, 1)))
  948. # NotImplementedError without pi as coefficient
  949. raises(NotImplementedError, lambda: normalize_theta_set(Interval(1, 2*pi)))
  950. raises(NotImplementedError, lambda: normalize_theta_set(Interval(2*pi, 10)))
  951. raises(NotImplementedError, lambda: normalize_theta_set(FiniteSet(0, 3, 3*pi)))
  952. def test_ComplexRegion_FiniteSet():
  953. x, y, z, a, b, c = symbols('x y z a b c')
  954. # Issue #9669
  955. assert ComplexRegion(FiniteSet(a, b, c)*FiniteSet(x, y, z)) == \
  956. FiniteSet(a + I*x, a + I*y, a + I*z, b + I*x, b + I*y,
  957. b + I*z, c + I*x, c + I*y, c + I*z)
  958. assert ComplexRegion(FiniteSet(2)*FiniteSet(3)) == FiniteSet(2 + 3*I)
  959. def test_union_RealSubSet():
  960. assert (S.Complexes).union(Interval(1, 2)) == S.Complexes
  961. assert (S.Complexes).union(S.Integers) == S.Complexes
  962. def test_issue_9980():
  963. c1 = ComplexRegion(Interval(1, 2)*Interval(2, 3))
  964. c2 = ComplexRegion(Interval(1, 5)*Interval(1, 3))
  965. R = Union(c1, c2)
  966. assert simplify(R) == ComplexRegion(Union(Interval(1, 2)*Interval(2, 3), \
  967. Interval(1, 5)*Interval(1, 3)), False)
  968. assert c1.func(*c1.args) == c1
  969. assert R.func(*R.args) == R
  970. def test_issue_11732():
  971. interval12 = Interval(1, 2)
  972. finiteset1234 = FiniteSet(1, 2, 3, 4)
  973. pointComplex = Tuple(1, 5)
  974. assert (interval12 in S.Naturals) == False
  975. assert (interval12 in S.Naturals0) == False
  976. assert (interval12 in S.Integers) == False
  977. assert (interval12 in S.Complexes) == False
  978. assert (finiteset1234 in S.Naturals) == False
  979. assert (finiteset1234 in S.Naturals0) == False
  980. assert (finiteset1234 in S.Integers) == False
  981. assert (finiteset1234 in S.Complexes) == False
  982. assert (pointComplex in S.Naturals) == False
  983. assert (pointComplex in S.Naturals0) == False
  984. assert (pointComplex in S.Integers) == False
  985. assert (pointComplex in S.Complexes) == True
  986. def test_issue_11730():
  987. unit = Interval(0, 1)
  988. square = ComplexRegion(unit ** 2)
  989. assert Union(S.Complexes, FiniteSet(oo)) != S.Complexes
  990. assert Union(S.Complexes, FiniteSet(eye(4))) != S.Complexes
  991. assert Union(unit, square) == square
  992. assert Intersection(S.Reals, square) == unit
  993. def test_issue_11938():
  994. unit = Interval(0, 1)
  995. ival = Interval(1, 2)
  996. cr1 = ComplexRegion(ival * unit)
  997. assert Intersection(cr1, S.Reals) == ival
  998. assert Intersection(cr1, unit) == FiniteSet(1)
  999. arg1 = Interval(0, S.Pi)
  1000. arg2 = FiniteSet(S.Pi)
  1001. arg3 = Interval(S.Pi / 4, 3 * S.Pi / 4)
  1002. cp1 = ComplexRegion(unit * arg1, polar=True)
  1003. cp2 = ComplexRegion(unit * arg2, polar=True)
  1004. cp3 = ComplexRegion(unit * arg3, polar=True)
  1005. assert Intersection(cp1, S.Reals) == Interval(-1, 1)
  1006. assert Intersection(cp2, S.Reals) == Interval(-1, 0)
  1007. assert Intersection(cp3, S.Reals) == FiniteSet(0)
  1008. def test_issue_11914():
  1009. a, b = Interval(0, 1), Interval(0, pi)
  1010. c, d = Interval(2, 3), Interval(pi, 3 * pi / 2)
  1011. cp1 = ComplexRegion(a * b, polar=True)
  1012. cp2 = ComplexRegion(c * d, polar=True)
  1013. assert -3 in cp1.union(cp2)
  1014. assert -3 in cp2.union(cp1)
  1015. assert -5 not in cp1.union(cp2)
  1016. def test_issue_9543():
  1017. assert ImageSet(Lambda(x, x**2), S.Naturals).is_subset(S.Reals)
  1018. def test_issue_16871():
  1019. assert ImageSet(Lambda(x, x), FiniteSet(1)) == {1}
  1020. assert ImageSet(Lambda(x, x - 3), S.Integers
  1021. ).intersection(S.Integers) is S.Integers
  1022. @XFAIL
  1023. def test_issue_16871b():
  1024. assert ImageSet(Lambda(x, x - 3), S.Integers).is_subset(S.Integers)
  1025. def test_issue_18050():
  1026. assert imageset(Lambda(x, I*x + 1), S.Integers
  1027. ) == ImageSet(Lambda(x, I*x + 1), S.Integers)
  1028. assert imageset(Lambda(x, 3*I*x + 4 + 8*I), S.Integers
  1029. ) == ImageSet(Lambda(x, 3*I*x + 4 + 2*I), S.Integers)
  1030. # no 'Mod' for next 2 tests:
  1031. assert imageset(Lambda(x, 2*x + 3*I), S.Integers
  1032. ) == ImageSet(Lambda(x, 2*x + 3*I), S.Integers)
  1033. r = Symbol('r', positive=True)
  1034. assert imageset(Lambda(x, r*x + 10), S.Integers
  1035. ) == ImageSet(Lambda(x, r*x + 10), S.Integers)
  1036. # reduce real part:
  1037. assert imageset(Lambda(x, 3*x + 8 + 5*I), S.Integers
  1038. ) == ImageSet(Lambda(x, 3*x + 2 + 5*I), S.Integers)
  1039. def test_Rationals():
  1040. assert S.Integers.is_subset(S.Rationals)
  1041. assert S.Naturals.is_subset(S.Rationals)
  1042. assert S.Naturals0.is_subset(S.Rationals)
  1043. assert S.Rationals.is_subset(S.Reals)
  1044. assert S.Rationals.inf is -oo
  1045. assert S.Rationals.sup is oo
  1046. it = iter(S.Rationals)
  1047. assert [next(it) for i in range(12)] == [
  1048. 0, 1, -1, S.Half, 2, Rational(-1, 2), -2,
  1049. Rational(1, 3), 3, Rational(-1, 3), -3, Rational(2, 3)]
  1050. assert Basic() not in S.Rationals
  1051. assert S.Half in S.Rationals
  1052. assert S.Rationals.contains(0.5) == Contains(0.5, S.Rationals, evaluate=False)
  1053. assert 2 in S.Rationals
  1054. r = symbols('r', rational=True)
  1055. assert r in S.Rationals
  1056. raises(TypeError, lambda: x in S.Rationals)
  1057. # issue #18134:
  1058. assert S.Rationals.boundary == S.Reals
  1059. assert S.Rationals.closure == S.Reals
  1060. assert S.Rationals.is_open == False
  1061. assert S.Rationals.is_closed == False
  1062. def test_NZQRC_unions():
  1063. # check that all trivial number set unions are simplified:
  1064. nbrsets = (S.Naturals, S.Naturals0, S.Integers, S.Rationals,
  1065. S.Reals, S.Complexes)
  1066. unions = (Union(a, b) for a in nbrsets for b in nbrsets)
  1067. assert all(u.is_Union is False for u in unions)
  1068. def test_imageset_intersection():
  1069. n = Dummy()
  1070. s = ImageSet(Lambda(n, -I*(I*(2*pi*n - pi/4) +
  1071. log(Abs(sqrt(-I))))), S.Integers)
  1072. assert s.intersect(S.Reals) == ImageSet(
  1073. Lambda(n, 2*pi*n + pi*Rational(7, 4)), S.Integers)
  1074. def test_issue_17858():
  1075. assert 1 in Range(-oo, oo)
  1076. assert 0 in Range(oo, -oo, -1)
  1077. assert oo not in Range(-oo, oo)
  1078. assert -oo not in Range(-oo, oo)
  1079. def test_issue_17859():
  1080. r = Range(-oo,oo)
  1081. raises(ValueError,lambda: r[::2])
  1082. raises(ValueError, lambda: r[::-2])
  1083. r = Range(oo,-oo,-1)
  1084. raises(ValueError,lambda: r[::2])
  1085. raises(ValueError, lambda: r[::-2])