test_piecewise.py 61 KB

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