test_piecewise.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. from sympy.concrete.summations import Sum
  2. from sympy.core.add import Add
  3. from sympy.core.basic import Basic
  4. from sympy.core.containers import Tuple
  5. from sympy.core.expr import unchanged
  6. from sympy.core.function import (Function, diff, expand)
  7. from sympy.core.mul import Mul
  8. from sympy.core.mod import Mod
  9. from sympy.core.numbers import (Float, I, Rational, oo, pi, zoo)
  10. from sympy.core.relational import (Eq, Ge, Gt, Ne)
  11. from sympy.core.singleton import S
  12. from sympy.core.symbol import (Symbol, symbols)
  13. from sympy.functions.combinatorial.factorials import factorial
  14. from sympy.functions.elementary.complexes import (Abs, adjoint, arg, conjugate, im, re, transpose)
  15. from sympy.functions.elementary.exponential import (exp, log)
  16. from sympy.functions.elementary.miscellaneous import (Max, Min, sqrt)
  17. from sympy.functions.elementary.piecewise import (Piecewise,
  18. piecewise_fold, piecewise_exclusive, Undefined, ExprCondPair)
  19. from sympy.functions.elementary.trigonometric import (cos, sin)
  20. from sympy.functions.special.delta_functions import (DiracDelta, Heaviside)
  21. from sympy.functions.special.tensor_functions import KroneckerDelta
  22. from sympy.integrals.integrals import (Integral, integrate)
  23. from sympy.logic.boolalg import (And, ITE, Not, Or)
  24. from sympy.matrices.expressions.matexpr import MatrixSymbol
  25. from sympy.printing import srepr
  26. from sympy.sets.contains import Contains
  27. from sympy.sets.sets import Interval
  28. from sympy.solvers.solvers import solve
  29. from sympy.testing.pytest import raises, slow
  30. from sympy.utilities.lambdify import lambdify
  31. a, b, c, d, x, y = symbols('a:d, x, y')
  32. z = symbols('z', nonzero=True)
  33. def test_piecewise1():
  34. # Test canonicalization
  35. assert unchanged(Piecewise, ExprCondPair(x, x < 1), ExprCondPair(0, True))
  36. assert Piecewise((x, x < 1), (0, True)) == Piecewise(ExprCondPair(x, x < 1),
  37. ExprCondPair(0, True))
  38. assert Piecewise((x, x < 1), (0, True), (1, True)) == \
  39. Piecewise((x, x < 1), (0, True))
  40. assert Piecewise((x, x < 1), (0, False), (-1, 1 > 2)) == \
  41. Piecewise((x, x < 1))
  42. assert Piecewise((x, x < 1), (0, x < 1), (0, True)) == \
  43. Piecewise((x, x < 1), (0, True))
  44. assert Piecewise((x, x < 1), (0, x < 2), (0, True)) == \
  45. Piecewise((x, x < 1), (0, True))
  46. assert Piecewise((x, x < 1), (x, x < 2), (0, True)) == \
  47. Piecewise((x, Or(x < 1, x < 2)), (0, True))
  48. assert Piecewise((x, x < 1), (x, x < 2), (x, True)) == x
  49. assert Piecewise((x, True)) == x
  50. # Explicitly constructed empty Piecewise not accepted
  51. raises(TypeError, lambda: Piecewise())
  52. # False condition is never retained
  53. assert Piecewise((2*x, x < 0), (x, False)) == \
  54. Piecewise((2*x, x < 0), (x, False), evaluate=False) == \
  55. Piecewise((2*x, x < 0))
  56. assert Piecewise((x, False)) == Undefined
  57. raises(TypeError, lambda: Piecewise(x))
  58. assert Piecewise((x, 1)) == x # 1 and 0 are accepted as True/False
  59. raises(TypeError, lambda: Piecewise((x, 2)))
  60. raises(TypeError, lambda: Piecewise((x, x**2)))
  61. raises(TypeError, lambda: Piecewise(([1], True)))
  62. assert Piecewise(((1, 2), True)) == Tuple(1, 2)
  63. cond = (Piecewise((1, x < 0), (2, True)) < y)
  64. assert Piecewise((1, cond)
  65. ) == Piecewise((1, ITE(x < 0, y > 1, y > 2)))
  66. assert Piecewise((1, x > 0), (2, And(x <= 0, x > -1))
  67. ) == Piecewise((1, x > 0), (2, x > -1))
  68. assert Piecewise((1, x <= 0), (2, (x < 0) & (x > -1))
  69. ) == Piecewise((1, x <= 0))
  70. # test for supporting Contains in Piecewise
  71. pwise = Piecewise(
  72. (1, And(x <= 6, x > 1, Contains(x, S.Integers))),
  73. (0, True))
  74. assert pwise.subs(x, pi) == 0
  75. assert pwise.subs(x, 2) == 1
  76. assert pwise.subs(x, 7) == 0
  77. # Test subs
  78. p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >= 0))
  79. p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >= 0))
  80. assert p.subs(x, x**2) == p_x2
  81. assert p.subs(x, -5) == -1
  82. assert p.subs(x, -1) == 1
  83. assert p.subs(x, 1) == log(1)
  84. # More subs tests
  85. p2 = Piecewise((1, x < pi), (-1, x < 2*pi), (0, x > 2*pi))
  86. p3 = Piecewise((1, Eq(x, 0)), (1/x, True))
  87. p4 = Piecewise((1, Eq(x, 0)), (2, 1/x>2))
  88. assert p2.subs(x, 2) == 1
  89. assert p2.subs(x, 4) == -1
  90. assert p2.subs(x, 10) == 0
  91. assert p3.subs(x, 0.0) == 1
  92. assert p4.subs(x, 0.0) == 1
  93. f, g, h = symbols('f,g,h', cls=Function)
  94. pf = Piecewise((f(x), x < -1), (f(x) + h(x) + 2, x <= 1))
  95. pg = Piecewise((g(x), x < -1), (g(x) + h(x) + 2, x <= 1))
  96. assert pg.subs(g, f) == pf
  97. assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 0) == 1
  98. assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 1) == 0
  99. assert Piecewise((1, Eq(x, y)), (0, True)).subs(x, y) == 1
  100. assert Piecewise((1, Eq(x, z)), (0, True)).subs(x, z) == 1
  101. assert Piecewise((1, Eq(exp(x), cos(z))), (0, True)).subs(x, z) == \
  102. Piecewise((1, Eq(exp(z), cos(z))), (0, True))
  103. p5 = Piecewise( (0, Eq(cos(x) + y, 0)), (1, True))
  104. assert p5.subs(y, 0) == Piecewise( (0, Eq(cos(x), 0)), (1, True))
  105. assert Piecewise((-1, y < 1), (0, x < 0), (1, Eq(x, 0)), (2, True)
  106. ).subs(x, 1) == Piecewise((-1, y < 1), (2, True))
  107. assert Piecewise((1, Eq(x**2, -1)), (2, x < 0)).subs(x, I) == 1
  108. p6 = Piecewise((x, x > 0))
  109. n = symbols('n', negative=True)
  110. assert p6.subs(x, n) == Undefined
  111. # Test evalf
  112. assert p.evalf() == Piecewise((-1.0, x < -1), (x**2, x < 0), (log(x), True))
  113. assert p.evalf(subs={x: -2}) == -1.0
  114. assert p.evalf(subs={x: -1}) == 1.0
  115. assert p.evalf(subs={x: 1}) == log(1)
  116. assert p6.evalf(subs={x: -5}) == Undefined
  117. # Test doit
  118. f_int = Piecewise((Integral(x, (x, 0, 1)), x < 1))
  119. assert f_int.doit() == Piecewise( (S.Half, x < 1) )
  120. # Test differentiation
  121. f = x
  122. fp = x*p
  123. dp = Piecewise((0, x < -1), (2*x, x < 0), (1/x, x >= 0))
  124. fp_dx = x*dp + p
  125. assert diff(p, x) == dp
  126. assert diff(f*p, x) == fp_dx
  127. # Test simple arithmetic
  128. assert x*p == fp
  129. assert x*p + p == p + x*p
  130. assert p + f == f + p
  131. assert p + dp == dp + p
  132. assert p - dp == -(dp - p)
  133. # Test power
  134. dp2 = Piecewise((0, x < -1), (4*x**2, x < 0), (1/x**2, x >= 0))
  135. assert dp**2 == dp2
  136. # Test _eval_interval
  137. f1 = x*y + 2
  138. f2 = x*y**2 + 3
  139. peval = Piecewise((f1, x < 0), (f2, x > 0))
  140. peval_interval = f1.subs(
  141. x, 0) - f1.subs(x, -1) + f2.subs(x, 1) - f2.subs(x, 0)
  142. assert peval._eval_interval(x, 0, 0) == 0
  143. assert peval._eval_interval(x, -1, 1) == peval_interval
  144. peval2 = Piecewise((f1, x < 0), (f2, True))
  145. assert peval2._eval_interval(x, 0, 0) == 0
  146. assert peval2._eval_interval(x, 1, -1) == -peval_interval
  147. assert peval2._eval_interval(x, -1, -2) == f1.subs(x, -2) - f1.subs(x, -1)
  148. assert peval2._eval_interval(x, -1, 1) == peval_interval
  149. assert peval2._eval_interval(x, None, 0) == peval2.subs(x, 0)
  150. assert peval2._eval_interval(x, -1, None) == -peval2.subs(x, -1)
  151. # Test integration
  152. assert p.integrate() == Piecewise(
  153. (-x, x < -1),
  154. (x**3/3 + Rational(4, 3), x < 0),
  155. (x*log(x) - x + Rational(4, 3), True))
  156. p = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x))
  157. assert integrate(p, (x, -2, 2)) == Rational(5, 6)
  158. assert integrate(p, (x, 2, -2)) == Rational(-5, 6)
  159. p = Piecewise((0, x < 0), (1, x < 1), (0, x < 2), (1, x < 3), (0, True))
  160. assert integrate(p, (x, -oo, oo)) == 2
  161. p = Piecewise((x, x < -10), (x**2, x <= -1), (x, 1 < x))
  162. assert integrate(p, (x, -2, 2)) == Undefined
  163. # Test commutativity
  164. assert isinstance(p, Piecewise) and p.is_commutative is True
  165. def test_piecewise_free_symbols():
  166. f = Piecewise((x, a < 0), (y, True))
  167. assert f.free_symbols == {x, y, a}
  168. def test_piecewise_integrate1():
  169. x, y = symbols('x y', real=True)
  170. f = Piecewise(((x - 2)**2, x >= 0), (1, True))
  171. assert integrate(f, (x, -2, 2)) == Rational(14, 3)
  172. g = Piecewise(((x - 5)**5, x >= 4), (f, True))
  173. assert integrate(g, (x, -2, 2)) == Rational(14, 3)
  174. assert integrate(g, (x, -2, 5)) == Rational(43, 6)
  175. assert g == Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
  176. g = Piecewise(((x - 5)**5, 2 <= x), (f, x < 2))
  177. assert integrate(g, (x, -2, 2)) == Rational(14, 3)
  178. assert integrate(g, (x, -2, 5)) == Rational(-701, 6)
  179. assert g == Piecewise(((x - 5)**5, 2 <= x), (f, True))
  180. g = Piecewise(((x - 5)**5, 2 <= x), (2*f, True))
  181. assert integrate(g, (x, -2, 2)) == Rational(28, 3)
  182. assert integrate(g, (x, -2, 5)) == Rational(-673, 6)
  183. def test_piecewise_integrate1b():
  184. g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
  185. assert integrate(g, (x, -1, 1)) == 0
  186. g = Piecewise((1, x - y < 0), (0, True))
  187. assert integrate(g, (y, -oo, 0)) == -Min(0, x)
  188. assert g.subs(x, -3).integrate((y, -oo, 0)) == 3
  189. assert integrate(g, (y, 0, -oo)) == Min(0, x)
  190. assert integrate(g, (y, 0, oo)) == -Max(0, x) + oo
  191. assert integrate(g, (y, -oo, 42)) == -Min(42, x) + 42
  192. assert integrate(g, (y, -oo, oo)) == -x + oo
  193. g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
  194. gy1 = g.integrate((x, y, 1))
  195. g1y = g.integrate((x, 1, y))
  196. for yy in (-1, S.Half, 2):
  197. assert g.integrate((x, yy, 1)) == gy1.subs(y, yy)
  198. assert g.integrate((x, 1, yy)) == g1y.subs(y, yy)
  199. assert gy1 == Piecewise(
  200. (-Min(1, Max(0, y))**2/2 + S.Half, y < 1),
  201. (-y + 1, True))
  202. assert g1y == Piecewise(
  203. (Min(1, Max(0, y))**2/2 - S.Half, y < 1),
  204. (y - 1, True))
  205. @slow
  206. def test_piecewise_integrate1ca():
  207. y = symbols('y', real=True)
  208. g = Piecewise(
  209. (1 - x, Interval(0, 1).contains(x)),
  210. (1 + x, Interval(-1, 0).contains(x)),
  211. (0, True)
  212. )
  213. gy1 = g.integrate((x, y, 1))
  214. g1y = g.integrate((x, 1, y))
  215. assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
  216. assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
  217. assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
  218. assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
  219. assert g.integrate((x, 2, 1)) == gy1.subs(y, 2)
  220. assert g.integrate((x, 1, 2)) == g1y.subs(y, 2)
  221. assert piecewise_fold(gy1.rewrite(Piecewise)
  222. ).simplify() == Piecewise(
  223. (1, y <= -1),
  224. (-y**2/2 - y + S.Half, y <= 0),
  225. (y**2/2 - y + S.Half, y < 1),
  226. (0, True))
  227. assert piecewise_fold(g1y.rewrite(Piecewise)
  228. ).simplify() == Piecewise(
  229. (-1, y <= -1),
  230. (y**2/2 + y - S.Half, y <= 0),
  231. (-y**2/2 + y - S.Half, y < 1),
  232. (0, True))
  233. assert gy1 == Piecewise(
  234. (
  235. -Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
  236. Min(1, Max(0, y))**2 + S.Half, y < 1),
  237. (0, True)
  238. )
  239. assert g1y == Piecewise(
  240. (
  241. Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
  242. Min(1, Max(0, y))**2 - S.Half, y < 1),
  243. (0, True))
  244. @slow
  245. def test_piecewise_integrate1cb():
  246. y = symbols('y', real=True)
  247. g = Piecewise(
  248. (0, Or(x <= -1, x >= 1)),
  249. (1 - x, x > 0),
  250. (1 + x, True)
  251. )
  252. gy1 = g.integrate((x, y, 1))
  253. g1y = g.integrate((x, 1, y))
  254. assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
  255. assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
  256. assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
  257. assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
  258. assert g.integrate((x, 2, 1)) == gy1.subs(y, 2)
  259. assert g.integrate((x, 1, 2)) == g1y.subs(y, 2)
  260. assert piecewise_fold(gy1.rewrite(Piecewise)
  261. ).simplify() == Piecewise(
  262. (1, y <= -1),
  263. (-y**2/2 - y + S.Half, y <= 0),
  264. (y**2/2 - y + S.Half, y < 1),
  265. (0, True))
  266. assert piecewise_fold(g1y.rewrite(Piecewise)
  267. ).simplify() == Piecewise(
  268. (-1, y <= -1),
  269. (y**2/2 + y - S.Half, y <= 0),
  270. (-y**2/2 + y - S.Half, y < 1),
  271. (0, True))
  272. # g1y and gy1 should simplify if the condition that y < 1
  273. # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
  274. assert gy1 == Piecewise(
  275. (
  276. -Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
  277. Min(1, Max(0, y))**2 + S.Half, y < 1),
  278. (0, True)
  279. )
  280. assert g1y == Piecewise(
  281. (
  282. Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
  283. Min(1, Max(0, y))**2 - S.Half, y < 1),
  284. (0, True))
  285. def test_piecewise_integrate2():
  286. from itertools import permutations
  287. lim = Tuple(x, c, d)
  288. p = Piecewise((1, x < a), (2, x > b), (3, True))
  289. q = p.integrate(lim)
  290. assert q == Piecewise(
  291. (-c + 2*d - 2*Min(d, Max(a, c)) + Min(d, Max(a, b, c)), c < d),
  292. (-2*c + d + 2*Min(c, Max(a, d)) - Min(c, Max(a, b, d)), True))
  293. for v in permutations((1, 2, 3, 4)):
  294. r = dict(zip((a, b, c, d), v))
  295. assert p.subs(r).integrate(lim.subs(r)) == q.subs(r)
  296. def test_meijer_bypass():
  297. # totally bypass meijerg machinery when dealing
  298. # with Piecewise in integrate
  299. assert Piecewise((1, x < 4), (0, True)).integrate((x, oo, 1)) == -3
  300. def test_piecewise_integrate3_inequality_conditions():
  301. from sympy.utilities.iterables import cartes
  302. lim = (x, 0, 5)
  303. # set below includes two pts below range, 2 pts in range,
  304. # 2 pts above range, and the boundaries
  305. N = (-2, -1, 0, 1, 2, 5, 6, 7)
  306. p = Piecewise((1, x > a), (2, x > b), (0, True))
  307. ans = p.integrate(lim)
  308. for i, j in cartes(N, repeat=2):
  309. reps = dict(zip((a, b), (i, j)))
  310. assert ans.subs(reps) == p.subs(reps).integrate(lim)
  311. assert ans.subs(a, 4).subs(b, 1) == 0 + 2*3 + 1
  312. p = Piecewise((1, x > a), (2, x < b), (0, True))
  313. ans = p.integrate(lim)
  314. for i, j in cartes(N, repeat=2):
  315. reps = dict(zip((a, b), (i, j)))
  316. assert ans.subs(reps) == p.subs(reps).integrate(lim)
  317. # delete old tests that involved c1 and c2 since those
  318. # reduce to the above except that a value of 0 was used
  319. # for two expressions whereas the above uses 3 different
  320. # values
  321. @slow
  322. def test_piecewise_integrate4_symbolic_conditions():
  323. a = Symbol('a', real=True)
  324. b = Symbol('b', real=True)
  325. x = Symbol('x', real=True)
  326. y = Symbol('y', real=True)
  327. p0 = Piecewise((0, Or(x < a, x > b)), (1, True))
  328. p1 = Piecewise((0, x < a), (0, x > b), (1, True))
  329. p2 = Piecewise((0, x > b), (0, x < a), (1, True))
  330. p3 = Piecewise((0, x < a), (1, x < b), (0, True))
  331. p4 = Piecewise((0, x > b), (1, x > a), (0, True))
  332. p5 = Piecewise((1, And(a < x, x < b)), (0, True))
  333. # check values of a=1, b=3 (and reversed) with values
  334. # of y of 0, 1, 2, 3, 4
  335. lim = Tuple(x, -oo, y)
  336. for p in (p0, p1, p2, p3, p4, p5):
  337. ans = p.integrate(lim)
  338. for i in range(5):
  339. reps = {a:1, b:3, y:i}
  340. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  341. reps = {a: 3, b:1, y:i}
  342. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  343. lim = Tuple(x, y, oo)
  344. for p in (p0, p1, p2, p3, p4, p5):
  345. ans = p.integrate(lim)
  346. for i in range(5):
  347. reps = {a:1, b:3, y:i}
  348. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  349. reps = {a:3, b:1, y:i}
  350. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  351. ans = Piecewise(
  352. (0, x <= Min(a, b)),
  353. (x - Min(a, b), x <= b),
  354. (b - Min(a, b), True))
  355. for i in (p0, p1, p2, p4):
  356. assert i.integrate(x) == ans
  357. assert p3.integrate(x) == Piecewise(
  358. (0, x < a),
  359. (-a + x, x <= Max(a, b)),
  360. (-a + Max(a, b), True))
  361. assert p5.integrate(x) == Piecewise(
  362. (0, x <= a),
  363. (-a + x, x <= Max(a, b)),
  364. (-a + Max(a, b), True))
  365. p1 = Piecewise((0, x < a), (S.Half, x > b), (1, True))
  366. p2 = Piecewise((S.Half, x > b), (0, x < a), (1, True))
  367. p3 = Piecewise((0, x < a), (1, x < b), (S.Half, True))
  368. p4 = Piecewise((S.Half, x > b), (1, x > a), (0, True))
  369. p5 = Piecewise((1, And(a < x, x < b)), (S.Half, x > b), (0, True))
  370. # check values of a=1, b=3 (and reversed) with values
  371. # of y of 0, 1, 2, 3, 4
  372. lim = Tuple(x, -oo, y)
  373. for p in (p1, p2, p3, p4, p5):
  374. ans = p.integrate(lim)
  375. for i in range(5):
  376. reps = {a:1, b:3, y:i}
  377. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  378. reps = {a: 3, b:1, y:i}
  379. assert ans.subs(reps) == p.subs(reps).integrate(lim.subs(reps))
  380. def test_piecewise_integrate5_independent_conditions():
  381. p = Piecewise((0, Eq(y, 0)), (x*y, True))
  382. assert integrate(p, (x, 1, 3)) == Piecewise((0, Eq(y, 0)), (4*y, True))
  383. def test_issue_22917():
  384. p = (Piecewise((0, ITE((x - y > 1) | (2 * x - 2 * y > 1), False,
  385. ITE(x - y > 1, 2 * y - 2 < -1, 2 * x - 2 * y > 1))),
  386. (Piecewise((0, ITE(x - y > 1, True, 2 * x - 2 * y > 1)),
  387. (2 * Piecewise((0, x - y > 1), (y, True)), True)), True))
  388. + 2 * Piecewise((1, ITE((x - y > 1) | (2 * x - 2 * y > 1), False,
  389. ITE(x - y > 1, 2 * y - 2 < -1, 2 * x - 2 * y > 1))),
  390. (Piecewise((1, ITE(x - y > 1, True, 2 * x - 2 * y > 1)),
  391. (2 * Piecewise((1, x - y > 1), (x, True)), True)), True)))
  392. assert piecewise_fold(p) == Piecewise((2, (x - y > S.Half) | (x - y > 1)),
  393. (2*y + 4, x - y > 1),
  394. (4*x + 2*y, True))
  395. assert piecewise_fold(p > 1).rewrite(ITE) == ITE((x - y > S.Half) | (x - y > 1), True,
  396. ITE(x - y > 1, 2*y + 4 > 1, 4*x + 2*y > 1))
  397. def test_piecewise_simplify():
  398. p = Piecewise(((x**2 + 1)/x**2, Eq(x*(1 + x) - x**2, 0)),
  399. ((-1)**x*(-1), True))
  400. assert p.simplify() == \
  401. Piecewise((zoo, Eq(x, 0)), ((-1)**(x + 1), True))
  402. # simplify when there are Eq in conditions
  403. assert Piecewise(
  404. (a, And(Eq(a, 0), Eq(a + b, 0))), (1, True)).simplify(
  405. ) == Piecewise(
  406. (0, And(Eq(a, 0), Eq(b, 0))), (1, True))
  407. assert Piecewise((2*x*factorial(a)/(factorial(y)*factorial(-y + a)),
  408. Eq(y, 0) & Eq(-y + a, 0)), (2*factorial(a)/(factorial(y)*factorial(-y
  409. + a)), Eq(y, 0) & Eq(-y + a, 1)), (0, True)).simplify(
  410. ) == Piecewise(
  411. (2*x, And(Eq(a, 0), Eq(y, 0))),
  412. (2, And(Eq(a, 1), Eq(y, 0))),
  413. (0, True))
  414. args = (2, And(Eq(x, 2), Ge(y, 0))), (x, True)
  415. assert Piecewise(*args).simplify() == Piecewise(*args)
  416. args = (1, Eq(x, 0)), (sin(x)/x, True)
  417. assert Piecewise(*args).simplify() == Piecewise(*args)
  418. assert Piecewise((2 + y, And(Eq(x, 2), Eq(y, 0))), (x, True)
  419. ).simplify() == x
  420. # check that x or f(x) are recognized as being Symbol-like for lhs
  421. args = Tuple((1, Eq(x, 0)), (sin(x) + 1 + x, True))
  422. ans = x + sin(x) + 1
  423. f = Function('f')
  424. assert Piecewise(*args).simplify() == ans
  425. assert Piecewise(*args.subs(x, f(x))).simplify() == ans.subs(x, f(x))
  426. # issue 18634
  427. d = Symbol("d", integer=True)
  428. n = Symbol("n", integer=True)
  429. t = Symbol("t", positive=True)
  430. expr = Piecewise((-d + 2*n, Eq(1/t, 1)), (t**(1 - 4*n)*t**(4*n - 1)*(-d + 2*n), True))
  431. assert expr.simplify() == -d + 2*n
  432. # issue 22747
  433. p = Piecewise((0, (t < -2) & (t < -1) & (t < 0)), ((t/2 + 1)*(t +
  434. 1)*(t + 2), (t < -1) & (t < 0)), ((S.Half - t/2)*(1 - t)*(t + 1),
  435. (t < -2) & (t < -1) & (t < 1)), ((t + 1)*(-t*(t/2 + 1) + (S.Half
  436. - t/2)*(1 - t)), (t < -2) & (t < -1) & (t < 0) & (t < 1)), ((t +
  437. 1)*((S.Half - t/2)*(1 - t) + (t/2 + 1)*(t + 2)), (t < -1) & (t <
  438. 1)), ((t + 1)*(-t*(t/2 + 1) + (S.Half - t/2)*(1 - t)), (t < -1) &
  439. (t < 0) & (t < 1)), (0, (t < -2) & (t < -1)), ((t/2 + 1)*(t +
  440. 1)*(t + 2), t < -1), ((t + 1)*(-t*(t/2 + 1) + (S.Half - t/2)*(t +
  441. 1)), (t < 0) & ((t < -2) | (t < 0))), ((S.Half - t/2)*(1 - t)*(t
  442. + 1), (t < 1) & ((t < -2) | (t < 1))), (0, True)) + Piecewise((0,
  443. (t < -1) & (t < 0) & (t < 1)), ((1 - t)*(t/2 + S.Half)*(t + 1),
  444. (t < 0) & (t < 1)), ((1 - t)*(1 - t/2)*(2 - t), (t < -1) & (t <
  445. 0) & (t < 2)), ((1 - t)*((1 - t)*(t/2 + S.Half) + (1 - t/2)*(2 -
  446. t)), (t < -1) & (t < 0) & (t < 1) & (t < 2)), ((1 - t)*((1 -
  447. t/2)*(2 - t) + (t/2 + S.Half)*(t + 1)), (t < 0) & (t < 2)), ((1 -
  448. t)*((1 - t)*(t/2 + S.Half) + (1 - t/2)*(2 - t)), (t < 0) & (t <
  449. 1) & (t < 2)), (0, (t < -1) & (t < 0)), ((1 - t)*(t/2 +
  450. S.Half)*(t + 1), t < 0), ((1 - t)*(t*(1 - t/2) + (1 - t)*(t/2 +
  451. S.Half)), (t < 1) & ((t < -1) | (t < 1))), ((1 - t)*(1 - t/2)*(2
  452. - t), (t < 2) & ((t < -1) | (t < 2))), (0, True))
  453. assert p.simplify() == Piecewise(
  454. (0, t < -2), ((t + 1)*(t + 2)**2/2, t < -1), (-3*t**3/2
  455. - 5*t**2/2 + 1, t < 0), (3*t**3/2 - 5*t**2/2 + 1, t < 1), ((1 -
  456. t)*(t - 2)**2/2, t < 2), (0, True))
  457. # coverage
  458. nan = Undefined
  459. covered = Piecewise((1, x > 3), (2, x < 2), (3, x > 1))
  460. assert covered.simplify().args == covered.args
  461. assert Piecewise((1, x < 2), (2, x < 1), (3, True)).simplify(
  462. ) == Piecewise((1, x < 2), (3, True))
  463. assert Piecewise((1, x > 2)).simplify() == Piecewise((1, x > 2),
  464. (nan, True))
  465. assert Piecewise((1, (x >= 2) & (x < oo))
  466. ).simplify() == Piecewise((1, (x >= 2) & (x < oo)), (nan, True))
  467. assert Piecewise((1, x < 2), (2, (x > 1) & (x < 3)), (3, True)
  468. ). simplify() == Piecewise((1, x < 2), (2, x < 3), (3, True))
  469. assert Piecewise((1, x < 2), (2, (x <= 3) & (x > 1)), (3, True)
  470. ).simplify() == Piecewise((1, x < 2), (2, x <= 3), (3, True))
  471. assert Piecewise((1, x < 2), (2, (x > 2) & (x < 3)), (3, True)
  472. ).simplify() == Piecewise((1, x < 2), (2, (x > 2) & (x < 3)),
  473. (3, True))
  474. assert Piecewise((1, x < 2), (2, (x >= 1) & (x <= 3)), (3, True)
  475. ).simplify() == Piecewise((1, x < 2), (2, x <= 3), (3, True))
  476. assert Piecewise((1, x < 1), (2, (x >= 2) & (x <= 3)), (3, True)
  477. ).simplify() == Piecewise((1, x < 1), (2, (x >= 2) & (x <= 3)),
  478. (3, True))
  479. def test_piecewise_solve():
  480. abs2 = Piecewise((-x, x <= 0), (x, x > 0))
  481. f = abs2.subs(x, x - 2)
  482. assert solve(f, x) == [2]
  483. assert solve(f - 1, x) == [1, 3]
  484. f = Piecewise(((x - 2)**2, x >= 0), (1, True))
  485. assert solve(f, x) == [2]
  486. g = Piecewise(((x - 5)**5, x >= 4), (f, True))
  487. assert solve(g, x) == [2, 5]
  488. g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
  489. assert solve(g, x) == [2, 5]
  490. g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2))
  491. assert solve(g, x) == [5]
  492. g = Piecewise(((x - 5)**5, x >= 2), (f, True))
  493. assert solve(g, x) == [5]
  494. g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False))
  495. assert solve(g, x) == [5]
  496. g = Piecewise(((x - 5)**5, x >= 2),
  497. (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0))
  498. assert solve(g, x) == [5]
  499. # if no symbol is given the piecewise detection must still work
  500. assert solve(Piecewise((x - 2, x > 2), (2 - x, True)) - 3) == [-1, 5]
  501. f = Piecewise(((x - 2)**2, x >= 0), (0, True))
  502. raises(NotImplementedError, lambda: solve(f, x))
  503. def nona(ans):
  504. return list(filter(lambda x: x is not S.NaN, ans))
  505. p = Piecewise((x**2 - 4, x < y), (x - 2, True))
  506. ans = solve(p, x)
  507. assert nona([i.subs(y, -2) for i in ans]) == [2]
  508. assert nona([i.subs(y, 2) for i in ans]) == [-2, 2]
  509. assert nona([i.subs(y, 3) for i in ans]) == [-2, 2]
  510. assert ans == [
  511. Piecewise((-2, y > -2), (S.NaN, True)),
  512. Piecewise((2, y <= 2), (S.NaN, True)),
  513. Piecewise((2, y > 2), (S.NaN, True))]
  514. # issue 6060
  515. absxm3 = Piecewise(
  516. (x - 3, 0 <= x - 3),
  517. (3 - x, 0 > x - 3)
  518. )
  519. assert solve(absxm3 - y, x) == [
  520. Piecewise((-y + 3, -y < 0), (S.NaN, True)),
  521. Piecewise((y + 3, y >= 0), (S.NaN, True))]
  522. p = Symbol('p', positive=True)
  523. assert solve(absxm3 - p, x) == [-p + 3, p + 3]
  524. # issue 6989
  525. f = Function('f')
  526. assert solve(Eq(-f(x), Piecewise((1, x > 0), (0, True))), f(x)) == \
  527. [Piecewise((-1, x > 0), (0, True))]
  528. # issue 8587
  529. f = Piecewise((2*x**2, And(0 < x, x < 1)), (2, True))
  530. assert solve(f - 1) == [1/sqrt(2)]
  531. def test_piecewise_fold():
  532. p = Piecewise((x, x < 1), (1, 1 <= x))
  533. assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
  534. assert piecewise_fold(p + p) == Piecewise((2*x, x < 1), (2, 1 <= x))
  535. assert piecewise_fold(Piecewise((1, x < 0), (2, True))
  536. + Piecewise((10, x < 0), (-10, True))) == \
  537. Piecewise((11, x < 0), (-8, True))
  538. p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
  539. p2 = Piecewise((0, x < 0), (1 - x, x <= 1), (0, True))
  540. p = 4*p1 + 2*p2
  541. assert integrate(
  542. piecewise_fold(p), (x, -oo, oo)) == integrate(2*x + 2, (x, 0, 1))
  543. assert piecewise_fold(
  544. Piecewise((1, y <= 0), (-Piecewise((2, y >= 0)), True)
  545. )) == Piecewise((1, y <= 0), (-2, y >= 0))
  546. assert piecewise_fold(Piecewise((x, ITE(x > 0, y < 1, y > 1)))
  547. ) == Piecewise((x, ((x <= 0) | (y < 1)) & ((x > 0) | (y > 1))))
  548. a, b = (Piecewise((2, Eq(x, 0)), (0, True)),
  549. Piecewise((x, Eq(-x + y, 0)), (1, Eq(-x + y, 1)), (0, True)))
  550. assert piecewise_fold(Mul(a, b, evaluate=False)
  551. ) == piecewise_fold(Mul(b, a, evaluate=False))
  552. def test_piecewise_fold_piecewise_in_cond():
  553. p1 = Piecewise((cos(x), x < 0), (0, True))
  554. p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
  555. assert p2.subs(x, -pi/2) == 0
  556. assert p2.subs(x, 1) == 0
  557. assert p2.subs(x, -pi/4) == 1
  558. p4 = Piecewise((0, Eq(p1, 0)), (1,True))
  559. ans = piecewise_fold(p4)
  560. for i in range(-1, 1):
  561. assert ans.subs(x, i) == p4.subs(x, i)
  562. r1 = 1 < Piecewise((1, x < 1), (3, True))
  563. ans = piecewise_fold(r1)
  564. for i in range(2):
  565. assert ans.subs(x, i) == r1.subs(x, i)
  566. p5 = Piecewise((1, x < 0), (3, True))
  567. p6 = Piecewise((1, x < 1), (3, True))
  568. p7 = Piecewise((1, p5 < p6), (0, True))
  569. ans = piecewise_fold(p7)
  570. for i in range(-1, 2):
  571. assert ans.subs(x, i) == p7.subs(x, i)
  572. def test_piecewise_fold_piecewise_in_cond_2():
  573. p1 = Piecewise((cos(x), x < 0), (0, True))
  574. p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
  575. p3 = Piecewise(
  576. (0, (x >= 0) | Eq(cos(x), 0)),
  577. (1/cos(x), x < 0),
  578. (zoo, True)) # redundant b/c all x are already covered
  579. assert(piecewise_fold(p2) == p3)
  580. def test_piecewise_fold_expand():
  581. p1 = Piecewise((1, Interval(0, 1, False, True).contains(x)), (0, True))
  582. p2 = piecewise_fold(expand((1 - x)*p1))
  583. cond = ((x >= 0) & (x < 1))
  584. assert piecewise_fold(expand((1 - x)*p1), evaluate=False
  585. ) == Piecewise((1 - x, cond), (-x, cond), (1, cond), (0, True), evaluate=False)
  586. assert piecewise_fold(expand((1 - x)*p1), evaluate=None
  587. ) == Piecewise((1 - x, cond), (0, True))
  588. assert p2 == Piecewise((1 - x, cond), (0, True))
  589. assert p2 == expand(piecewise_fold((1 - x)*p1))
  590. def test_piecewise_duplicate():
  591. p = Piecewise((x, x < -10), (x**2, x <= -1), (x, 1 < x))
  592. assert p == Piecewise(*p.args)
  593. def test_doit():
  594. p1 = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x))
  595. p2 = Piecewise((x, x < 1), (Integral(2 * x), -1 <= x), (x, 3 < x))
  596. assert p2.doit() == p1
  597. assert p2.doit(deep=False) == p2
  598. # issue 17165
  599. p1 = Sum(y**x, (x, -1, oo)).doit()
  600. assert p1.doit() == p1
  601. def test_piecewise_interval():
  602. p1 = Piecewise((x, Interval(0, 1).contains(x)), (0, True))
  603. assert p1.subs(x, -0.5) == 0
  604. assert p1.subs(x, 0.5) == 0.5
  605. assert p1.diff(x) == Piecewise((1, Interval(0, 1).contains(x)), (0, True))
  606. assert integrate(p1, x) == Piecewise(
  607. (0, x <= 0),
  608. (x**2/2, x <= 1),
  609. (S.Half, True))
  610. def test_piecewise_exclusive():
  611. p = Piecewise((0, x < 0), (S.Half, x <= 0), (1, True))
  612. assert piecewise_exclusive(p) == Piecewise((0, x < 0), (S.Half, Eq(x, 0)),
  613. (1, x > 0), evaluate=False)
  614. assert piecewise_exclusive(p + 2) == Piecewise((0, x < 0), (S.Half, Eq(x, 0)),
  615. (1, x > 0), evaluate=False) + 2
  616. assert piecewise_exclusive(Piecewise((1, y <= 0),
  617. (-Piecewise((2, y >= 0)), True))) == \
  618. Piecewise((1, y <= 0),
  619. (-Piecewise((2, y >= 0),
  620. (S.NaN, y < 0), evaluate=False), y > 0), evaluate=False)
  621. assert piecewise_exclusive(Piecewise((1, x > y))) == Piecewise((1, x > y),
  622. (S.NaN, x <= y),
  623. evaluate=False)
  624. assert piecewise_exclusive(Piecewise((1, x > y)),
  625. skip_nan=True) == Piecewise((1, x > y))
  626. xr, yr = symbols('xr, yr', real=True)
  627. p1 = Piecewise((1, xr < 0), (2, True), evaluate=False)
  628. p1x = Piecewise((1, xr < 0), (2, xr >= 0), evaluate=False)
  629. p2 = Piecewise((p1, yr < 0), (3, True), evaluate=False)
  630. p2x = Piecewise((p1, yr < 0), (3, yr >= 0), evaluate=False)
  631. p2xx = Piecewise((p1x, yr < 0), (3, yr >= 0), evaluate=False)
  632. assert piecewise_exclusive(p2) == p2xx
  633. assert piecewise_exclusive(p2, deep=False) == p2x
  634. def test_piecewise_collapse():
  635. assert Piecewise((x, True)) == x
  636. a = x < 1
  637. assert Piecewise((x, a), (x + 1, a)) == Piecewise((x, a))
  638. assert Piecewise((x, a), (x + 1, a.reversed)) == Piecewise((x, a))
  639. b = x < 5
  640. def canonical(i):
  641. if isinstance(i, Piecewise):
  642. return Piecewise(*i.args)
  643. return i
  644. for args in [
  645. ((1, a), (Piecewise((2, a), (3, b)), b)),
  646. ((1, a), (Piecewise((2, a), (3, b.reversed)), b)),
  647. ((1, a), (Piecewise((2, a), (3, b)), b), (4, True)),
  648. ((1, a), (Piecewise((2, a), (3, b), (4, True)), b)),
  649. ((1, a), (Piecewise((2, a), (3, b), (4, True)), b), (5, True))]:
  650. for i in (0, 2, 10):
  651. assert canonical(
  652. Piecewise(*args, evaluate=False).subs(x, i)
  653. ) == canonical(Piecewise(*args).subs(x, i))
  654. r1, r2, r3, r4 = symbols('r1:5')
  655. a = x < r1
  656. b = x < r2
  657. c = x < r3
  658. d = x < r4
  659. assert Piecewise((1, a), (Piecewise(
  660. (2, a), (3, b), (4, c)), b), (5, c)
  661. ) == Piecewise((1, a), (3, b), (5, c))
  662. assert Piecewise((1, a), (Piecewise(
  663. (2, a), (3, b), (4, c), (6, True)), c), (5, d)
  664. ) == Piecewise((1, a), (Piecewise(
  665. (3, b), (4, c)), c), (5, d))
  666. assert Piecewise((1, Or(a, d)), (Piecewise(
  667. (2, d), (3, b), (4, c)), b), (5, c)
  668. ) == Piecewise((1, Or(a, d)), (Piecewise(
  669. (2, d), (3, b)), b), (5, c))
  670. assert Piecewise((1, c), (2, ~c), (3, S.true)
  671. ) == Piecewise((1, c), (2, S.true))
  672. assert Piecewise((1, c), (2, And(~c, b)), (3,True)
  673. ) == Piecewise((1, c), (2, b), (3, True))
  674. assert Piecewise((1, c), (2, Or(~c, b)), (3,True)
  675. ).subs(dict(zip((r1, r2, r3, r4, x), (1, 2, 3, 4, 3.5)))) == 2
  676. assert Piecewise((1, c), (2, ~c)) == Piecewise((1, c), (2, True))
  677. def test_piecewise_lambdify():
  678. p = Piecewise(
  679. (x**2, x < 0),
  680. (x, Interval(0, 1, False, True).contains(x)),
  681. (2 - x, x >= 1),
  682. (0, True)
  683. )
  684. f = lambdify(x, p)
  685. assert f(-2.0) == 4.0
  686. assert f(0.0) == 0.0
  687. assert f(0.5) == 0.5
  688. assert f(2.0) == 0.0
  689. def test_piecewise_series():
  690. from sympy.series.order import O
  691. p1 = Piecewise((sin(x), x < 0), (cos(x), x > 0))
  692. p2 = Piecewise((x + O(x**2), x < 0), (1 + O(x**2), x > 0))
  693. assert p1.nseries(x, n=2) == p2
  694. def test_piecewise_as_leading_term():
  695. p1 = Piecewise((1/x, x > 1), (0, True))
  696. p2 = Piecewise((x, x > 1), (0, True))
  697. p3 = Piecewise((1/x, x > 1), (x, True))
  698. p4 = Piecewise((x, x > 1), (1/x, True))
  699. p5 = Piecewise((1/x, x > 1), (x, True))
  700. p6 = Piecewise((1/x, x < 1), (x, True))
  701. p7 = Piecewise((x, x < 1), (1/x, True))
  702. p8 = Piecewise((x, x > 1), (1/x, True))
  703. assert p1.as_leading_term(x) == 0
  704. assert p2.as_leading_term(x) == 0
  705. assert p3.as_leading_term(x) == x
  706. assert p4.as_leading_term(x) == 1/x
  707. assert p5.as_leading_term(x) == x
  708. assert p6.as_leading_term(x) == 1/x
  709. assert p7.as_leading_term(x) == x
  710. assert p8.as_leading_term(x) == 1/x
  711. def test_piecewise_complex():
  712. p1 = Piecewise((2, x < 0), (1, 0 <= x))
  713. p2 = Piecewise((2*I, x < 0), (I, 0 <= x))
  714. p3 = Piecewise((I*x, x > 1), (1 + I, True))
  715. p4 = Piecewise((-I*conjugate(x), x > 1), (1 - I, True))
  716. assert conjugate(p1) == p1
  717. assert conjugate(p2) == piecewise_fold(-p2)
  718. assert conjugate(p3) == p4
  719. assert p1.is_imaginary is False
  720. assert p1.is_real is True
  721. assert p2.is_imaginary is True
  722. assert p2.is_real is False
  723. assert p3.is_imaginary is None
  724. assert p3.is_real is None
  725. assert p1.as_real_imag() == (p1, 0)
  726. assert p2.as_real_imag() == (0, -I*p2)
  727. def test_conjugate_transpose():
  728. A, B = symbols("A B", commutative=False)
  729. p = Piecewise((A*B**2, x > 0), (A**2*B, True))
  730. assert p.adjoint() == \
  731. Piecewise((adjoint(A*B**2), x > 0), (adjoint(A**2*B), True))
  732. assert p.conjugate() == \
  733. Piecewise((conjugate(A*B**2), x > 0), (conjugate(A**2*B), True))
  734. assert p.transpose() == \
  735. Piecewise((transpose(A*B**2), x > 0), (transpose(A**2*B), True))
  736. def test_piecewise_evaluate():
  737. assert Piecewise((x, True)) == x
  738. assert Piecewise((x, True), evaluate=True) == x
  739. assert Piecewise((1, Eq(1, x))).args == ((1, Eq(x, 1)),)
  740. assert Piecewise((1, Eq(1, x)), evaluate=False).args == (
  741. (1, Eq(1, x)),)
  742. # like the additive and multiplicative identities that
  743. # cannot be kept in Add/Mul, we also do not keep a single True
  744. p = Piecewise((x, True), evaluate=False)
  745. assert p == x
  746. def test_as_expr_set_pairs():
  747. assert Piecewise((x, x > 0), (-x, x <= 0)).as_expr_set_pairs() == \
  748. [(x, Interval(0, oo, True, True)), (-x, Interval(-oo, 0))]
  749. assert Piecewise(((x - 2)**2, x >= 0), (0, True)).as_expr_set_pairs() == \
  750. [((x - 2)**2, Interval(0, oo)), (0, Interval(-oo, 0, True, True))]
  751. def test_S_srepr_is_identity():
  752. p = Piecewise((10, Eq(x, 0)), (12, True))
  753. q = S(srepr(p))
  754. assert p == q
  755. def test_issue_12587():
  756. # sort holes into intervals
  757. p = Piecewise((1, x > 4), (2, Not((x <= 3) & (x > -1))), (3, True))
  758. assert p.integrate((x, -5, 5)) == 23
  759. p = Piecewise((1, x > 1), (2, x < y), (3, True))
  760. lim = x, -3, 3
  761. ans = p.integrate(lim)
  762. for i in range(-1, 3):
  763. assert ans.subs(y, i) == p.subs(y, i).integrate(lim)
  764. def test_issue_11045():
  765. assert integrate(1/(x*sqrt(x**2 - 1)), (x, 1, 2)) == pi/3
  766. # handle And with Or arguments
  767. assert Piecewise((1, And(Or(x < 1, x > 3), x < 2)), (0, True)
  768. ).integrate((x, 0, 3)) == 1
  769. # hidden false
  770. assert Piecewise((1, x > 1), (2, x > x + 1), (3, True)
  771. ).integrate((x, 0, 3)) == 5
  772. # targetcond is Eq
  773. assert Piecewise((1, x > 1), (2, Eq(1, x)), (3, True)
  774. ).integrate((x, 0, 4)) == 6
  775. # And has Relational needing to be solved
  776. assert Piecewise((1, And(2*x > x + 1, x < 2)), (0, True)
  777. ).integrate((x, 0, 3)) == 1
  778. # Or has Relational needing to be solved
  779. assert Piecewise((1, Or(2*x > x + 2, x < 1)), (0, True)
  780. ).integrate((x, 0, 3)) == 2
  781. # ignore hidden false (handled in canonicalization)
  782. assert Piecewise((1, x > 1), (2, x > x + 1), (3, True)
  783. ).integrate((x, 0, 3)) == 5
  784. # watch for hidden True Piecewise
  785. assert Piecewise((2, Eq(1 - x, x*(1/x - 1))), (0, True)
  786. ).integrate((x, 0, 3)) == 6
  787. # overlapping conditions of targetcond are recognized and ignored;
  788. # the condition x > 3 will be pre-empted by the first condition
  789. assert Piecewise((1, Or(x < 1, x > 2)), (2, x > 3), (3, True)
  790. ).integrate((x, 0, 4)) == 6
  791. # convert Ne to Or
  792. assert Piecewise((1, Ne(x, 0)), (2, True)
  793. ).integrate((x, -1, 1)) == 2
  794. # no default but well defined
  795. assert Piecewise((x, (x > 1) & (x < 3)), (1, (x < 4))
  796. ).integrate((x, 1, 4)) == 5
  797. p = Piecewise((x, (x > 1) & (x < 3)), (1, (x < 4)))
  798. nan = Undefined
  799. i = p.integrate((x, 1, y))
  800. assert i == Piecewise(
  801. (y - 1, y < 1),
  802. (Min(3, y)**2/2 - Min(3, y) + Min(4, y) - S.Half,
  803. y <= Min(4, y)),
  804. (nan, True))
  805. assert p.integrate((x, 1, -1)) == i.subs(y, -1)
  806. assert p.integrate((x, 1, 4)) == 5
  807. assert p.integrate((x, 1, 5)) is nan
  808. # handle Not
  809. p = Piecewise((1, x > 1), (2, Not(And(x > 1, x< 3))), (3, True))
  810. assert p.integrate((x, 0, 3)) == 4
  811. # handle updating of int_expr when there is overlap
  812. p = Piecewise(
  813. (1, And(5 > x, x > 1)),
  814. (2, Or(x < 3, x > 7)),
  815. (4, x < 8))
  816. assert p.integrate((x, 0, 10)) == 20
  817. # And with Eq arg handling
  818. assert Piecewise((1, x < 1), (2, And(Eq(x, 3), x > 1))
  819. ).integrate((x, 0, 3)) is S.NaN
  820. assert Piecewise((1, x < 1), (2, And(Eq(x, 3), x > 1)), (3, True)
  821. ).integrate((x, 0, 3)) == 7
  822. assert Piecewise((1, x < 0), (2, And(Eq(x, 3), x < 1)), (3, True)
  823. ).integrate((x, -1, 1)) == 4
  824. # middle condition doesn't matter: it's a zero width interval
  825. assert Piecewise((1, x < 1), (2, Eq(x, 3) & (y < x)), (3, True)
  826. ).integrate((x, 0, 3)) == 7
  827. def test_holes():
  828. nan = Undefined
  829. assert Piecewise((1, x < 2)).integrate(x) == Piecewise(
  830. (x, x < 2), (nan, True))
  831. assert Piecewise((1, And(x > 1, x < 2))).integrate(x) == Piecewise(
  832. (nan, x < 1), (x, x < 2), (nan, True))
  833. assert Piecewise((1, And(x > 1, x < 2))).integrate((x, 0, 3)) is nan
  834. assert Piecewise((1, And(x > 0, x < 4))).integrate((x, 1, 3)) == 2
  835. # this also tests that the integrate method is used on non-Piecwise
  836. # arguments in _eval_integral
  837. A, B = symbols("A B")
  838. a, b = symbols('a b', real=True)
  839. assert Piecewise((A, And(x < 0, a < 1)), (B, Or(x < 1, a > 2))
  840. ).integrate(x) == Piecewise(
  841. (B*x, (a > 2)),
  842. (Piecewise((A*x, x < 0), (B*x, x < 1), (nan, True)), a < 1),
  843. (Piecewise((B*x, x < 1), (nan, True)), True))
  844. def test_issue_11922():
  845. def f(x):
  846. return Piecewise((0, x < -1), (1 - x**2, x < 1), (0, True))
  847. autocorr = lambda k: (
  848. f(x) * f(x + k)).integrate((x, -1, 1))
  849. assert autocorr(1.9) > 0
  850. k = symbols('k')
  851. good_autocorr = lambda k: (
  852. (1 - x**2) * f(x + k)).integrate((x, -1, 1))
  853. a = good_autocorr(k)
  854. assert a.subs(k, 3) == 0
  855. k = symbols('k', positive=True)
  856. a = good_autocorr(k)
  857. assert a.subs(k, 3) == 0
  858. assert Piecewise((0, x < 1), (10, (x >= 1))
  859. ).integrate() == Piecewise((0, x < 1), (10*x - 10, True))
  860. def test_issue_5227():
  861. f = 0.0032513612725229*Piecewise((0, x < -80.8461538461539),
  862. (-0.0160799238820171*x + 1.33215984776403, x < 2),
  863. (Piecewise((0.3, x > 123), (0.7, True)) +
  864. Piecewise((0.4, x > 2), (0.6, True)), x <=
  865. 123), (-0.00817409766454352*x + 2.10541401273885, x <
  866. 380.571428571429), (0, True))
  867. i = integrate(f, (x, -oo, oo))
  868. assert i == Integral(f, (x, -oo, oo)).doit()
  869. assert str(i) == '1.00195081676351'
  870. assert Piecewise((1, x - y < 0), (0, True)
  871. ).integrate(y) == Piecewise((0, y <= x), (-x + y, True))
  872. def test_issue_10137():
  873. a = Symbol('a', real=True)
  874. b = Symbol('b', real=True)
  875. x = Symbol('x', real=True)
  876. y = Symbol('y', real=True)
  877. p0 = Piecewise((0, Or(x < a, x > b)), (1, True))
  878. p1 = Piecewise((0, Or(a > x, b < x)), (1, True))
  879. assert integrate(p0, (x, y, oo)) == integrate(p1, (x, y, oo))
  880. p3 = Piecewise((1, And(0 < x, x < a)), (0, True))
  881. p4 = Piecewise((1, And(a > x, x > 0)), (0, True))
  882. ip3 = integrate(p3, x)
  883. assert ip3 == Piecewise(
  884. (0, x <= 0),
  885. (x, x <= Max(0, a)),
  886. (Max(0, a), True))
  887. ip4 = integrate(p4, x)
  888. assert ip4 == ip3
  889. assert p3.integrate((x, 2, 4)) == Min(4, Max(2, a)) - 2
  890. assert p4.integrate((x, 2, 4)) == Min(4, Max(2, a)) - 2
  891. def test_stackoverflow_43852159():
  892. f = lambda x: Piecewise((1, (x >= -1) & (x <= 1)), (0, True))
  893. Conv = lambda x: integrate(f(x - y)*f(y), (y, -oo, +oo))
  894. cx = Conv(x)
  895. assert cx.subs(x, -1.5) == cx.subs(x, 1.5)
  896. assert cx.subs(x, 3) == 0
  897. assert piecewise_fold(f(x - y)*f(y)) == Piecewise(
  898. (1, (y >= -1) & (y <= 1) & (x - y >= -1) & (x - y <= 1)),
  899. (0, True))
  900. def test_issue_12557():
  901. '''
  902. # 3200 seconds to compute the fourier part of issue
  903. import sympy as sym
  904. x,y,z,t = sym.symbols('x y z t')
  905. k = sym.symbols("k", integer=True)
  906. fourier = sym.fourier_series(sym.cos(k*x)*sym.sqrt(x**2),
  907. (x, -sym.pi, sym.pi))
  908. assert fourier == FourierSeries(
  909. sqrt(x**2)*cos(k*x), (x, -pi, pi), (Piecewise((pi**2,
  910. Eq(k, 0)), (2*(-1)**k/k**2 - 2/k**2, True))/(2*pi),
  911. SeqFormula(Piecewise((pi**2, (Eq(_n, 0) & Eq(k, 0)) | (Eq(_n, 0) &
  912. Eq(_n, k) & Eq(k, 0)) | (Eq(_n, 0) & Eq(k, 0) & Eq(_n, -k)) | (Eq(_n,
  913. 0) & Eq(_n, k) & Eq(k, 0) & Eq(_n, -k))), (pi**2/2, Eq(_n, k) | Eq(_n,
  914. -k) | (Eq(_n, 0) & Eq(_n, k)) | (Eq(_n, k) & Eq(k, 0)) | (Eq(_n, 0) &
  915. Eq(_n, -k)) | (Eq(_n, k) & Eq(_n, -k)) | (Eq(k, 0) & Eq(_n, -k)) |
  916. (Eq(_n, 0) & Eq(_n, k) & Eq(_n, -k)) | (Eq(_n, k) & Eq(k, 0) & Eq(_n,
  917. -k))), ((-1)**k*pi**2*_n**3*sin(pi*_n)/(pi*_n**4 - 2*pi*_n**2*k**2 +
  918. pi*k**4) - (-1)**k*pi**2*_n**3*sin(pi*_n)/(-pi*_n**4 + 2*pi*_n**2*k**2
  919. - pi*k**4) + (-1)**k*pi*_n**2*cos(pi*_n)/(pi*_n**4 - 2*pi*_n**2*k**2 +
  920. pi*k**4) - (-1)**k*pi*_n**2*cos(pi*_n)/(-pi*_n**4 + 2*pi*_n**2*k**2 -
  921. pi*k**4) - (-1)**k*pi**2*_n*k**2*sin(pi*_n)/(pi*_n**4 -
  922. 2*pi*_n**2*k**2 + pi*k**4) +
  923. (-1)**k*pi**2*_n*k**2*sin(pi*_n)/(-pi*_n**4 + 2*pi*_n**2*k**2 -
  924. pi*k**4) + (-1)**k*pi*k**2*cos(pi*_n)/(pi*_n**4 - 2*pi*_n**2*k**2 +
  925. pi*k**4) - (-1)**k*pi*k**2*cos(pi*_n)/(-pi*_n**4 + 2*pi*_n**2*k**2 -
  926. pi*k**4) - (2*_n**2 + 2*k**2)/(_n**4 - 2*_n**2*k**2 + k**4),
  927. True))*cos(_n*x)/pi, (_n, 1, oo)), SeqFormula(0, (_k, 1, oo))))
  928. '''
  929. x = symbols("x", real=True)
  930. k = symbols('k', integer=True, finite=True)
  931. abs2 = lambda x: Piecewise((-x, x <= 0), (x, x > 0))
  932. assert integrate(abs2(x), (x, -pi, pi)) == pi**2
  933. func = cos(k*x)*sqrt(x**2)
  934. assert integrate(func, (x, -pi, pi)) == Piecewise(
  935. (2*(-1)**k/k**2 - 2/k**2, Ne(k, 0)), (pi**2, True))
  936. def test_issue_6900():
  937. from itertools import permutations
  938. t0, t1, T, t = symbols('t0, t1 T t')
  939. f = Piecewise((0, t < t0), (x, And(t0 <= t, t < t1)), (0, t >= t1))
  940. g = f.integrate(t)
  941. assert g == Piecewise(
  942. (0, t <= t0),
  943. (t*x - t0*x, t <= Max(t0, t1)),
  944. (-t0*x + x*Max(t0, t1), True))
  945. for i in permutations(range(2)):
  946. reps = dict(zip((t0,t1), i))
  947. for tt in range(-1,3):
  948. assert (g.xreplace(reps).subs(t,tt) ==
  949. f.xreplace(reps).integrate(t).subs(t,tt))
  950. lim = Tuple(t, t0, T)
  951. g = f.integrate(lim)
  952. ans = Piecewise(
  953. (-t0*x + x*Min(T, Max(t0, t1)), T > t0),
  954. (0, True))
  955. for i in permutations(range(3)):
  956. reps = dict(zip((t0,t1,T), i))
  957. tru = f.xreplace(reps).integrate(lim.xreplace(reps))
  958. assert tru == ans.xreplace(reps)
  959. assert g == ans
  960. def test_issue_10122():
  961. assert solve(abs(x) + abs(x - 1) - 1 > 0, x
  962. ) == Or(And(-oo < x, x < S.Zero), And(S.One < x, x < oo))
  963. def test_issue_4313():
  964. u = Piecewise((0, x <= 0), (1, x >= a), (x/a, True))
  965. e = (u - u.subs(x, y))**2/(x - y)**2
  966. M = Max(0, a)
  967. assert integrate(e, x).expand() == Piecewise(
  968. (Piecewise(
  969. (0, x <= 0),
  970. (-y**2/(a**2*x - a**2*y) + x/a**2 - 2*y*log(-y)/a**2 +
  971. 2*y*log(x - y)/a**2 - y/a**2, x <= M),
  972. (-y**2/(-a**2*y + a**2*M) + 1/(-y + M) -
  973. 1/(x - y) - 2*y*log(-y)/a**2 + 2*y*log(-y +
  974. M)/a**2 - y/a**2 + M/a**2, True)),
  975. ((a <= y) & (y <= 0)) | ((y <= 0) & (y > -oo))),
  976. (Piecewise(
  977. (-1/(x - y), x <= 0),
  978. (-a**2/(a**2*x - a**2*y) + 2*a*y/(a**2*x - a**2*y) -
  979. y**2/(a**2*x - a**2*y) + 2*log(-y)/a - 2*log(x - y)/a +
  980. 2/a + x/a**2 - 2*y*log(-y)/a**2 + 2*y*log(x - y)/a**2 -
  981. y/a**2, x <= M),
  982. (-a**2/(-a**2*y + a**2*M) + 2*a*y/(-a**2*y +
  983. a**2*M) - y**2/(-a**2*y + a**2*M) +
  984. 2*log(-y)/a - 2*log(-y + M)/a + 2/a -
  985. 2*y*log(-y)/a**2 + 2*y*log(-y + M)/a**2 -
  986. y/a**2 + M/a**2, True)),
  987. a <= y),
  988. (Piecewise(
  989. (-y**2/(a**2*x - a**2*y), x <= 0),
  990. (x/a**2 + y/a**2, x <= M),
  991. (a**2/(-a**2*y + a**2*M) -
  992. a**2/(a**2*x - a**2*y) - 2*a*y/(-a**2*y + a**2*M) +
  993. 2*a*y/(a**2*x - a**2*y) + y**2/(-a**2*y + a**2*M) -
  994. y**2/(a**2*x - a**2*y) + y/a**2 + M/a**2, True)),
  995. True))
  996. def test__intervals():
  997. assert Piecewise((x + 2, Eq(x, 3)))._intervals(x) == (True, [])
  998. assert Piecewise(
  999. (1, x > x + 1),
  1000. (Piecewise((1, x < x + 1)), 2*x < 2*x + 1),
  1001. (1, True))._intervals(x) == (True, [(-oo, oo, 1, 1)])
  1002. assert Piecewise((1, Ne(x, I)), (0, True))._intervals(x) == (True,
  1003. [(-oo, oo, 1, 0)])
  1004. assert Piecewise((-cos(x), sin(x) >= 0), (cos(x), True)
  1005. )._intervals(x) == (True,
  1006. [(0, pi, -cos(x), 0), (-oo, oo, cos(x), 1)])
  1007. # the following tests that duplicates are removed and that non-Eq
  1008. # generated zero-width intervals are removed
  1009. assert Piecewise((1, Abs(x**(-2)) > 1), (0, True)
  1010. )._intervals(x) == (True,
  1011. [(-1, 0, 1, 0), (0, 1, 1, 0), (-oo, oo, 0, 1)])
  1012. def test_containment():
  1013. a, b, c, d, e = [1, 2, 3, 4, 5]
  1014. p = (Piecewise((d, x > 1), (e, True))*
  1015. Piecewise((a, Abs(x - 1) < 1), (b, Abs(x - 2) < 2), (c, True)))
  1016. assert p.integrate(x).diff(x) == Piecewise(
  1017. (c*e, x <= 0),
  1018. (a*e, x <= 1),
  1019. (a*d, x < 2), # this is what we want to get right
  1020. (b*d, x < 4),
  1021. (c*d, True))
  1022. def test_piecewise_with_DiracDelta():
  1023. d1 = DiracDelta(x - 1)
  1024. assert integrate(d1, (x, -oo, oo)) == 1
  1025. assert integrate(d1, (x, 0, 2)) == 1
  1026. assert Piecewise((d1, Eq(x, 2)), (0, True)).integrate(x) == 0
  1027. assert Piecewise((d1, x < 2), (0, True)).integrate(x) == Piecewise(
  1028. (Heaviside(x - 1), x < 2), (1, True))
  1029. # TODO raise error if function is discontinuous at limit of
  1030. # integration, e.g. integrate(d1, (x, -2, 1)) or Piecewise(
  1031. # (d1, Eq(x, 1)
  1032. def test_issue_10258():
  1033. assert Piecewise((0, x < 1), (1, True)).is_zero is None
  1034. assert Piecewise((-1, x < 1), (1, True)).is_zero is False
  1035. a = Symbol('a', zero=True)
  1036. assert Piecewise((0, x < 1), (a, True)).is_zero
  1037. assert Piecewise((1, x < 1), (a, x < 3)).is_zero is None
  1038. a = Symbol('a')
  1039. assert Piecewise((0, x < 1), (a, True)).is_zero is None
  1040. assert Piecewise((0, x < 1), (1, True)).is_nonzero is None
  1041. assert Piecewise((1, x < 1), (2, True)).is_nonzero
  1042. assert Piecewise((0, x < 1), (oo, True)).is_finite is None
  1043. assert Piecewise((0, x < 1), (1, True)).is_finite
  1044. b = Basic()
  1045. assert Piecewise((b, x < 1)).is_finite is None
  1046. # 10258
  1047. c = Piecewise((1, x < 0), (2, True)) < 3
  1048. assert c != True
  1049. assert piecewise_fold(c) == True
  1050. def test_issue_10087():
  1051. a, b = Piecewise((x, x > 1), (2, True)), Piecewise((x, x > 3), (3, True))
  1052. m = a*b
  1053. f = piecewise_fold(m)
  1054. for i in (0, 2, 4):
  1055. assert m.subs(x, i) == f.subs(x, i)
  1056. m = a + b
  1057. f = piecewise_fold(m)
  1058. for i in (0, 2, 4):
  1059. assert m.subs(x, i) == f.subs(x, i)
  1060. def test_issue_8919():
  1061. c = symbols('c:5')
  1062. x = symbols("x")
  1063. f1 = Piecewise((c[1], x < 1), (c[2], True))
  1064. f2 = Piecewise((c[3], x < Rational(1, 3)), (c[4], True))
  1065. assert integrate(f1*f2, (x, 0, 2)
  1066. ) == c[1]*c[3]/3 + 2*c[1]*c[4]/3 + c[2]*c[4]
  1067. f1 = Piecewise((0, x < 1), (2, True))
  1068. f2 = Piecewise((3, x < 2), (0, True))
  1069. assert integrate(f1*f2, (x, 0, 3)) == 6
  1070. y = symbols("y", positive=True)
  1071. a, b, c, x, z = symbols("a,b,c,x,z", real=True)
  1072. I = Integral(Piecewise(
  1073. (0, (x >= y) | (x < 0) | (b > c)),
  1074. (a, True)), (x, 0, z))
  1075. ans = I.doit()
  1076. assert ans == Piecewise((0, b > c), (a*Min(y, z) - a*Min(0, z), True))
  1077. for cond in (True, False):
  1078. for yy in range(1, 3):
  1079. for zz in range(-yy, 0, yy):
  1080. reps = [(b > c, cond), (y, yy), (z, zz)]
  1081. assert ans.subs(reps) == I.subs(reps).doit()
  1082. def test_unevaluated_integrals():
  1083. f = Function('f')
  1084. p = Piecewise((1, Eq(f(x) - 1, 0)), (2, x - 10 < 0), (0, True))
  1085. assert p.integrate(x) == Integral(p, x)
  1086. assert p.integrate((x, 0, 5)) == Integral(p, (x, 0, 5))
  1087. # test it by replacing f(x) with x%2 which will not
  1088. # affect the answer: the integrand is essentially 2 over
  1089. # the domain of integration
  1090. assert Integral(p, (x, 0, 5)).subs(f(x), x%2).n() == 10.0
  1091. # this is a test of using _solve_inequality when
  1092. # solve_univariate_inequality fails
  1093. assert p.integrate(y) == Piecewise(
  1094. (y, Eq(f(x), 1) | ((x < 10) & Eq(f(x), 1))),
  1095. (2*y, (x > -oo) & (x < 10)), (0, True))
  1096. def test_conditions_as_alternate_booleans():
  1097. a, b, c = symbols('a:c')
  1098. assert Piecewise((x, Piecewise((y < 1, x > 0), (y > 1, True)))
  1099. ) == Piecewise((x, ITE(x > 0, y < 1, y > 1)))
  1100. def test_Piecewise_rewrite_as_ITE():
  1101. a, b, c, d = symbols('a:d')
  1102. def _ITE(*args):
  1103. return Piecewise(*args).rewrite(ITE)
  1104. assert _ITE((a, x < 1), (b, x >= 1)) == ITE(x < 1, a, b)
  1105. assert _ITE((a, x < 1), (b, x < oo)) == ITE(x < 1, a, b)
  1106. assert _ITE((a, x < 1), (b, Or(y < 1, x < oo)), (c, y > 0)
  1107. ) == ITE(x < 1, a, b)
  1108. assert _ITE((a, x < 1), (b, True)) == ITE(x < 1, a, b)
  1109. assert _ITE((a, x < 1), (b, x < 2), (c, True)
  1110. ) == ITE(x < 1, a, ITE(x < 2, b, c))
  1111. assert _ITE((a, x < 1), (b, y < 2), (c, True)
  1112. ) == ITE(x < 1, a, ITE(y < 2, b, c))
  1113. assert _ITE((a, x < 1), (b, x < oo), (c, y < 1)
  1114. ) == ITE(x < 1, a, b)
  1115. assert _ITE((a, x < 1), (c, y < 1), (b, x < oo), (d, True)
  1116. ) == ITE(x < 1, a, ITE(y < 1, c, b))
  1117. assert _ITE((a, x < 0), (b, Or(x < oo, y < 1))
  1118. ) == ITE(x < 0, a, b)
  1119. raises(TypeError, lambda: _ITE((x + 1, x < 1), (x, True)))
  1120. # if `a` in the following were replaced with y then the coverage
  1121. # is complete but something other than as_set would need to be
  1122. # used to detect this
  1123. raises(NotImplementedError, lambda: _ITE((x, x < y), (y, x >= a)))
  1124. raises(ValueError, lambda: _ITE((a, x < 2), (b, x > 3)))
  1125. def test_issue_14052():
  1126. assert integrate(abs(sin(x)), (x, 0, 2*pi)) == 4
  1127. def test_issue_14240():
  1128. assert piecewise_fold(
  1129. Piecewise((1, a), (2, b), (4, True)) +
  1130. Piecewise((8, a), (16, True))
  1131. ) == Piecewise((9, a), (18, b), (20, True))
  1132. assert piecewise_fold(
  1133. Piecewise((2, a), (3, b), (5, True)) *
  1134. Piecewise((7, a), (11, True))
  1135. ) == Piecewise((14, a), (33, b), (55, True))
  1136. # these will hang if naive folding is used
  1137. assert piecewise_fold(Add(*[
  1138. Piecewise((i, a), (0, True)) for i in range(40)])
  1139. ) == Piecewise((780, a), (0, True))
  1140. assert piecewise_fold(Mul(*[
  1141. Piecewise((i, a), (0, True)) for i in range(1, 41)])
  1142. ) == Piecewise((factorial(40), a), (0, True))
  1143. def test_issue_14787():
  1144. x = Symbol('x')
  1145. f = Piecewise((x, x < 1), ((S(58) / 7), True))
  1146. assert str(f.evalf()) == "Piecewise((x, x < 1), (8.28571428571429, True))"
  1147. def test_issue_21481():
  1148. b, e = symbols('b e')
  1149. C = Piecewise(
  1150. (2,
  1151. ((b > 1) & (e > 0)) |
  1152. ((b > 0) & (b < 1) & (e < 0)) |
  1153. ((e >= 2) & (b < -1) & Eq(Mod(e, 2), 0)) |
  1154. ((e <= -2) & (b > -1) & (b < 0) & Eq(Mod(e, 2), 0))),
  1155. (S.Half,
  1156. ((b > 1) & (e < 0)) |
  1157. ((b > 0) & (e > 0) & (b < 1)) |
  1158. ((e <= -2) & (b < -1) & Eq(Mod(e, 2), 0)) |
  1159. ((e >= 2) & (b > -1) & (b < 0) & Eq(Mod(e, 2), 0))),
  1160. (-S.Half,
  1161. Eq(Mod(e, 2), 1) &
  1162. (((e <= -1) & (b < -1)) | ((e >= 1) & (b > -1) & (b < 0)))),
  1163. (-2,
  1164. ((e >= 1) & (b < -1) & Eq(Mod(e, 2), 1)) |
  1165. ((e <= -1) & (b > -1) & (b < 0) & Eq(Mod(e, 2), 1)))
  1166. )
  1167. A = Piecewise(
  1168. (1, Eq(b, 1) | Eq(e, 0) | (Eq(b, -1) & Eq(Mod(e, 2), 0))),
  1169. (0, Eq(b, 0) & (e > 0)),
  1170. (-1, Eq(b, -1) & Eq(Mod(e, 2), 1)),
  1171. (C, Eq(im(b), 0) & Eq(im(e), 0))
  1172. )
  1173. B = piecewise_fold(A)
  1174. sa = A.simplify()
  1175. sb = B.simplify()
  1176. v = (-2, -1, -S.Half, 0, S.Half, 1, 2)
  1177. for i in v:
  1178. for j in v:
  1179. r = {b:i, e:j}
  1180. ok = [k.xreplace(r) for k in (A, B, sa, sb)]
  1181. assert len(set(ok)) == 1
  1182. def test_issue_8458():
  1183. x, y = symbols('x y')
  1184. # Original issue
  1185. p1 = Piecewise((0, Eq(x, 0)), (sin(x), True))
  1186. assert p1.simplify() == sin(x)
  1187. # Slightly larger variant
  1188. p2 = Piecewise((x, Eq(x, 0)), (4*x + (y-2)**4, Eq(x, 0) & Eq(x+y, 2)), (sin(x), True))
  1189. assert p2.simplify() == sin(x)
  1190. # Test for problem highlighted during review
  1191. p3 = Piecewise((x+1, Eq(x, -1)), (4*x + (y-2)**4, Eq(x, 0) & Eq(x+y, 2)), (sin(x), True))
  1192. assert p3.simplify() == Piecewise((0, Eq(x, -1)), (sin(x), True))
  1193. def test_issue_16417():
  1194. z = Symbol('z')
  1195. assert unchanged(Piecewise, (1, Or(Eq(im(z), 0), Gt(re(z), 0))), (2, True))
  1196. x = Symbol('x')
  1197. assert unchanged(Piecewise, (S.Pi, re(x) < 0),
  1198. (0, Or(re(x) > 0, Ne(im(x), 0))),
  1199. (S.NaN, True))
  1200. r = Symbol('r', real=True)
  1201. p = Piecewise((S.Pi, re(r) < 0),
  1202. (0, Or(re(r) > 0, Ne(im(r), 0))),
  1203. (S.NaN, True))
  1204. assert p == Piecewise((S.Pi, r < 0),
  1205. (0, r > 0),
  1206. (S.NaN, True), evaluate=False)
  1207. # Does not work since imaginary != 0...
  1208. #i = Symbol('i', imaginary=True)
  1209. #p = Piecewise((S.Pi, re(i) < 0),
  1210. # (0, Or(re(i) > 0, Ne(im(i), 0))),
  1211. # (S.NaN, True))
  1212. #assert p == Piecewise((0, Ne(im(i), 0)),
  1213. # (S.NaN, True), evaluate=False)
  1214. i = I*r
  1215. p = Piecewise((S.Pi, re(i) < 0),
  1216. (0, Or(re(i) > 0, Ne(im(i), 0))),
  1217. (S.NaN, True))
  1218. assert p == Piecewise((0, Ne(im(i), 0)),
  1219. (S.NaN, True), evaluate=False)
  1220. assert p == Piecewise((0, Ne(r, 0)),
  1221. (S.NaN, True), evaluate=False)
  1222. def test_eval_rewrite_as_KroneckerDelta():
  1223. x, y, z, n, t, m = symbols('x y z n t m')
  1224. K = KroneckerDelta
  1225. f = lambda p: expand(p.rewrite(K))
  1226. p1 = Piecewise((0, Eq(x, y)), (1, True))
  1227. assert f(p1) == 1 - K(x, y)
  1228. p2 = Piecewise((x, Eq(y,0)), (z, Eq(t,0)), (n, True))
  1229. assert f(p2) == n*K(0, t)*K(0, y) - n*K(0, t) - n*K(0, y) + n + \
  1230. x*K(0, y) - z*K(0, t)*K(0, y) + z*K(0, t)
  1231. p3 = Piecewise((1, Ne(x, y)), (0, True))
  1232. assert f(p3) == 1 - K(x, y)
  1233. p4 = Piecewise((1, Eq(x, 3)), (4, True), (5, True))
  1234. assert f(p4) == 4 - 3*K(3, x)
  1235. p5 = Piecewise((3, Ne(x, 2)), (4, Eq(y, 2)), (5, True))
  1236. assert f(p5) == -K(2, x)*K(2, y) + 2*K(2, x) + 3
  1237. p6 = Piecewise((0, Ne(x, 1) & Ne(y, 4)), (1, True))
  1238. assert f(p6) == -K(1, x)*K(4, y) + K(1, x) + K(4, y)
  1239. p7 = Piecewise((2, Eq(y, 3) & Ne(x, 2)), (1, True))
  1240. assert f(p7) == -K(2, x)*K(3, y) + K(3, y) + 1
  1241. p8 = Piecewise((4, Eq(x, 3) & Ne(y, 2)), (1, True))
  1242. assert f(p8) == -3*K(2, y)*K(3, x) + 3*K(3, x) + 1
  1243. p9 = Piecewise((6, Eq(x, 4) & Eq(y, 1)), (1, True))
  1244. assert f(p9) == 5 * K(1, y) * K(4, x) + 1
  1245. p10 = Piecewise((4, Ne(x, -4) | Ne(y, 1)), (1, True))
  1246. assert f(p10) == -3 * K(-4, x) * K(1, y) + 4
  1247. p11 = Piecewise((1, Eq(y, 2) | Ne(x, -3)), (2, True))
  1248. assert f(p11) == -K(-3, x)*K(2, y) + K(-3, x) + 1
  1249. p12 = Piecewise((-1, Eq(x, 1) | Ne(y, 3)), (1, True))
  1250. assert f(p12) == -2*K(1, x)*K(3, y) + 2*K(3, y) - 1
  1251. p13 = Piecewise((3, Eq(x, 2) | Eq(y, 4)), (1, True))
  1252. assert f(p13) == -2*K(2, x)*K(4, y) + 2*K(2, x) + 2*K(4, y) + 1
  1253. p14 = Piecewise((1, Ne(x, 0) | Ne(y, 1)), (3, True))
  1254. assert f(p14) == 2 * K(0, x) * K(1, y) + 1
  1255. p15 = Piecewise((2, Eq(x, 3) | Ne(y, 2)), (3, Eq(x, 4) & Eq(y, 5)), (1, True))
  1256. assert f(p15) == -2*K(2, y)*K(3, x)*K(4, x)*K(5, y) + K(2, y)*K(3, x) + \
  1257. 2*K(2, y)*K(4, x)*K(5, y) - K(2, y) + 2
  1258. p16 = Piecewise((0, Ne(m, n)), (1, True))*Piecewise((0, Ne(n, t)), (1, True))\
  1259. *Piecewise((0, Ne(n, x)), (1, True)) - Piecewise((0, Ne(t, x)), (1, True))
  1260. assert f(p16) == K(m, n)*K(n, t)*K(n, x) - K(t, x)
  1261. p17 = Piecewise((0, Ne(t, x) & (Ne(m, n) | Ne(n, t) | Ne(n, x))),
  1262. (1, Ne(t, x)), (-1, Ne(m, n) | Ne(n, t) | Ne(n, x)), (0, True))
  1263. assert f(p17) == K(m, n)*K(n, t)*K(n, x) - K(t, x)
  1264. p18 = Piecewise((-4, Eq(y, 1) | (Eq(x, -5) & Eq(x, z))), (4, True))
  1265. assert f(p18) == 8*K(-5, x)*K(1, y)*K(x, z) - 8*K(-5, x)*K(x, z) - 8*K(1, y) + 4
  1266. p19 = Piecewise((0, x > 2), (1, True))
  1267. assert f(p19) == p19
  1268. p20 = Piecewise((0, And(x < 2, x > -5)), (1, True))
  1269. assert f(p20) == p20
  1270. p21 = Piecewise((0, Or(x > 1, x < 0)), (1, True))
  1271. assert f(p21) == p21
  1272. p22 = Piecewise((0, ~((Eq(y, -1) | Ne(x, 0)) & (Ne(x, 1) | Ne(y, -1)))), (1, True))
  1273. assert f(p22) == K(-1, y)*K(0, x) - K(-1, y)*K(1, x) - K(0, x) + 1
  1274. @slow
  1275. def test_identical_conds_issue():
  1276. from sympy.stats import Uniform, density
  1277. u1 = Uniform('u1', 0, 1)
  1278. u2 = Uniform('u2', 0, 1)
  1279. # Result is quite big, so not really important here (and should ideally be
  1280. # simpler). Should not give an exception though.
  1281. density(u1 + u2)
  1282. def test_issue_7370():
  1283. f = Piecewise((1, x <= 2400))
  1284. v = integrate(f, (x, 0, Float("252.4", 30)))
  1285. assert str(v) == '252.400000000000000000000000000'
  1286. def test_issue_14933():
  1287. x = Symbol('x')
  1288. y = Symbol('y')
  1289. inp = MatrixSymbol('inp', 1, 1)
  1290. rep_dict = {y: inp[0, 0], x: inp[0, 0]}
  1291. p = Piecewise((1, ITE(y > 0, x < 0, True)))
  1292. assert p.xreplace(rep_dict) == Piecewise((1, ITE(inp[0, 0] > 0, inp[0, 0] < 0, True)))
  1293. def test_issue_16715():
  1294. raises(NotImplementedError, lambda: Piecewise((x, x<0), (0, y>1)).as_expr_set_pairs())
  1295. def test_issue_20360():
  1296. t, tau = symbols("t tau", real=True)
  1297. n = symbols("n", integer=True)
  1298. lam = pi * (n - S.Half)
  1299. eq = integrate(exp(lam * tau), (tau, 0, t))
  1300. assert eq.simplify() == (2*exp(pi*t*(2*n - 1)/2) - 2)/(pi*(2*n - 1))
  1301. def test_piecewise_eval():
  1302. # XXX these tests might need modification if this
  1303. # simplification is moved out of eval and into
  1304. # boolalg or Piecewise simplification functions
  1305. f = lambda x: x.args[0].cond
  1306. # unsimplified
  1307. assert f(Piecewise((x, (x > -oo) & (x < 3)))
  1308. ) == ((x > -oo) & (x < 3))
  1309. assert f(Piecewise((x, (x > -oo) & (x < oo)))
  1310. ) == ((x > -oo) & (x < oo))
  1311. assert f(Piecewise((x, (x > -3) & (x < 3)))
  1312. ) == ((x > -3) & (x < 3))
  1313. assert f(Piecewise((x, (x > -3) & (x < oo)))
  1314. ) == ((x > -3) & (x < oo))
  1315. assert f(Piecewise((x, (x <= 3) & (x > -oo)))
  1316. ) == ((x <= 3) & (x > -oo))
  1317. assert f(Piecewise((x, (x <= 3) & (x > -3)))
  1318. ) == ((x <= 3) & (x > -3))
  1319. assert f(Piecewise((x, (x >= -3) & (x < 3)))
  1320. ) == ((x >= -3) & (x < 3))
  1321. assert f(Piecewise((x, (x >= -3) & (x < oo)))
  1322. ) == ((x >= -3) & (x < oo))
  1323. assert f(Piecewise((x, (x >= -3) & (x <= 3)))
  1324. ) == ((x >= -3) & (x <= 3))
  1325. # could simplify by keeping only the first
  1326. # arg of result
  1327. assert f(Piecewise((x, (x <= oo) & (x > -oo)))
  1328. ) == (x > -oo) & (x <= oo)
  1329. assert f(Piecewise((x, (x <= oo) & (x > -3)))
  1330. ) == (x > -3) & (x <= oo)
  1331. assert f(Piecewise((x, (x >= -oo) & (x < 3)))
  1332. ) == (x < 3) & (x >= -oo)
  1333. assert f(Piecewise((x, (x >= -oo) & (x < oo)))
  1334. ) == (x < oo) & (x >= -oo)
  1335. assert f(Piecewise((x, (x >= -oo) & (x <= 3)))
  1336. ) == (x <= 3) & (x >= -oo)
  1337. assert f(Piecewise((x, (x >= -oo) & (x <= oo)))
  1338. ) == (x <= oo) & (x >= -oo) # but cannot be True unless x is real
  1339. assert f(Piecewise((x, (x >= -3) & (x <= oo)))
  1340. ) == (x >= -3) & (x <= oo)
  1341. assert f(Piecewise((x, (Abs(arg(a)) <= 1) | (Abs(arg(a)) < 1)))
  1342. ) == (Abs(arg(a)) <= 1) | (Abs(arg(a)) < 1)
  1343. def test_issue_22533():
  1344. x = Symbol('x', real=True)
  1345. f = Piecewise((-1 / x, x <= 0), (1 / x, True))
  1346. assert integrate(f, x) == Piecewise((-log(x), x <= 0), (log(x), True))
  1347. def test_issue_24072():
  1348. assert Piecewise((1, x > 1), (2, x <= 1), (3, x <= 1)
  1349. ) == Piecewise((1, x > 1), (2, True))
  1350. def test_piecewise__eval_is_meromorphic():
  1351. """ Issue 24127: Tests eval_is_meromorphic auxiliary method """
  1352. x = symbols('x', real=True)
  1353. f = Piecewise((1, x < 0), (sqrt(1 - x), True))
  1354. assert f.is_meromorphic(x, I) is None
  1355. assert f.is_meromorphic(x, -1) == True
  1356. assert f.is_meromorphic(x, 0) == None
  1357. assert f.is_meromorphic(x, 1) == False
  1358. assert f.is_meromorphic(x, 2) == True
  1359. assert f.is_meromorphic(x, Symbol('a')) == None
  1360. assert f.is_meromorphic(x, Symbol('a', real=True)) == None