本文整理汇总了Python中sympy.Piecewise.subs方法的典型用法代码示例。如果您正苦于以下问题:Python Piecewise.subs方法的具体用法?Python Piecewise.subs怎么用?Python Piecewise.subs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Piecewise
的用法示例。
在下文中一共展示了Piecewise.subs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_piecewise_interval
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_interval():
p1 = Piecewise((x, Interval(0, 1).contains(x)), (0, True))
assert p1.subs(x, -0.5) == 0
assert p1.subs(x, 0.5) == 0.5
assert p1.diff(x) == Piecewise((1, Interval(0, 1).contains(x)), (0, True))
assert integrate(
p1, x) == Piecewise((x**2/2, Interval(0, 1).contains(x)), (0, True))
示例2: test_piecewise
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise():
# Test canonization
assert Piecewise((x, x < 1), (0, True)) == Piecewise((x, x < 1), (0, True))
assert Piecewise((x, x < 1), (0, False), (-1, 1>2)) == Piecewise((x, x < 1))
assert Piecewise((x, True)) == x
raises(TypeError,"Piecewise(x)")
raises(TypeError,"Piecewise((x,x**2))")
# Test subs
p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >=0))
p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >=0))
assert p.subs(x,x**2) == p_x2
assert p.subs(x,-5) == -1
assert p.subs(x,-1) == 1
assert p.subs(x,1) == log(1)
# Test evalf
assert p.evalf() == p
assert p.evalf(subs={x:-2}) == -1
assert p.evalf(subs={x:-1}) == 1
assert p.evalf(subs={x:1}) == log(1)
# Test doit
f_int = Piecewise((Integral(x,(x,0,1)), x < 1))
assert f_int.doit() == Piecewise( (1.0/2.0, x < 1) )
# Test differentiation
f = x
fp = x*p
dp = Piecewise((0, x < -1), (2*x, x < 0), (1/x, x >= 0))
fp_dx = x*dp + p
assert diff(p,x) == dp
assert diff(f*p,x) == fp_dx
# Test simple arithmetic
assert x*p == fp
assert x*p + p == p + x*p
assert p + f == f + p
assert p + dp == dp + p
assert p - dp == -(dp - p)
# Test _eval_interval
f1 = x*y + 2
f2 = x*y**2 + 3
peval = Piecewise( (f1, x<0), (f2, x>0))
peval_interval = f1.subs(x,0) - f1.subs(x,-1) + f2.subs(x,1) - f2.subs(x,0)
assert peval._eval_interval(x, -1, 1) == peval_interval
# Test integration
p_int = Piecewise((-x,x < -1), (x**3/3.0, x < 0), (-x + x*log(x), x >= 0))
assert integrate(p,x) == p_int
p = Piecewise((x, x < 1),(x**2, -1 <= x),(x,3<x))
assert integrate(p,(x,-2,2)) == 5.0/6.0
assert integrate(p,(x,2,-2)) == -5.0/6.0
p = Piecewise((0, x < 0), (1,x < 1), (0, x < 2), (1, x < 3), (0, True))
assert integrate(p, (x,-oo,oo)) == 2
p = Piecewise((x, x < -10),(x**2, x <= -1),(x, 1 < x))
raises(ValueError, "integrate(p,(x,-2,2))")
示例3: test_issue_4313
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_issue_4313():
u = Piecewise((0, x <= 0), (1, x >= a), (x/a, True))
e = (u - u.subs(x, y))**2/(x - y)**2
M = Max(0, a)
assert integrate(e, x).expand() == Piecewise(
(Piecewise(
(0, x <= 0),
(-y**2/(a**2*x - a**2*y) + x/a**2 - 2*y*log(-y)/a**2 +
2*y*log(x - y)/a**2 - y/a**2, x <= M),
(-y**2/(-a**2*y + a**2*M) + 1/(-y + M) -
1/(x - y) - 2*y*log(-y)/a**2 + 2*y*log(-y +
M)/a**2 - y/a**2 + M/a**2, True)),
((a <= y) & (y <= 0)) | ((y <= 0) & (y > -oo))),
(Piecewise(
(-1/(x - y), x <= 0),
(-a**2/(a**2*x - a**2*y) + 2*a*y/(a**2*x - a**2*y) -
y**2/(a**2*x - a**2*y) + 2*log(-y)/a - 2*log(x - y)/a +
2/a + x/a**2 - 2*y*log(-y)/a**2 + 2*y*log(x - y)/a**2 -
y/a**2, x <= M),
(-a**2/(-a**2*y + a**2*M) + 2*a*y/(-a**2*y +
a**2*M) - y**2/(-a**2*y + a**2*M) +
2*log(-y)/a - 2*log(-y + M)/a + 2/a -
2*y*log(-y)/a**2 + 2*y*log(-y + M)/a**2 -
y/a**2 + M/a**2, True)),
a <= y),
(Piecewise(
(-y**2/(a**2*x - a**2*y), x <= 0),
(x/a**2 + y/a**2, x <= M),
(a**2/(-a**2*y + a**2*M) -
a**2/(a**2*x - a**2*y) - 2*a*y/(-a**2*y + a**2*M) +
2*a*y/(a**2*x - a**2*y) + y**2/(-a**2*y + a**2*M) -
y**2/(a**2*x - a**2*y) + y/a**2 + M/a**2, True)),
True))
示例4: test_Function_subs
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_Function_subs():
from sympy.abc import x, y
f, g, h, i = symbols("f g h i", cls=Function)
p = Piecewise((g(f(x, y)), x < -1), (g(x), x <= 1))
assert p.subs(g, h) == Piecewise((h(f(x, y)), x < -1), (h(x), x <= 1))
assert (f(y) + g(x)).subs({f: h, g: i}) == i(x) + h(y)
示例5: test_piecewise_solve
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_solve():
abs2 = Piecewise((-x, x <= 0), (x, x > 0))
f = abs2.subs(x, x - 2)
assert solve(f, x) == [2]
assert solve(f - 1,x) == [1, 3]
f = Piecewise(((x - 2)**2, x >= 0), (1, True))
assert solve(f, x) == [2]
g = Piecewise(((x - 5)**5, x >= 4), (f, True))
assert solve(g, x) == [2, 5]
g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
assert solve(g, x) == [2, 5]
g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2))
assert solve(g, x) == [5]
g = Piecewise(((x - 5)**5, x >= 2), (f, True))
assert solve(g, x) == [5]
g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False))
assert solve(g, x) == [5]
assert solve(Piecewise((x, x < 0), (x, -1))) == [0]
示例6: test_piecewise_fold_piecewise_in_cond
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_fold_piecewise_in_cond():
p1 = Piecewise((cos(x), x < 0), (0, True))
p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
p3 = piecewise_fold(p2)
assert p2.subs(x, -pi / 2) == 0.0
assert p2.subs(x, 1) == 0.0
assert p2.subs(x, -pi / 4) == 1.0
p4 = Piecewise((0, Eq(p1, 0)), (1, True))
assert piecewise_fold(p4) == Piecewise((0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True))
r1 = 1 < Piecewise((1, x < 1), (3, True))
assert piecewise_fold(r1) == Not(x < 1)
p5 = Piecewise((1, x < 0), (3, True))
p6 = Piecewise((1, x < 1), (3, True))
p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
assert Piecewise((1, And(Not(x < 1), x < 0)), (0, True))
示例7: test_issue_12587
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_issue_12587():
# sort holes into intervals
p = Piecewise((1, x > 4), (2, Not((x <= 3) & (x > -1))), (3, True))
assert p.integrate((x, -5, 5)) == 23
p = Piecewise((1, x > 1), (2, x < y), (3, True))
lim = x, -3, 3
ans = p.integrate(lim)
for i in range(-1, 3):
assert ans.subs(y, i) == p.subs(y, i).integrate(lim)
示例8: test_piecewise_integrate3_inequality_conditions
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_integrate3_inequality_conditions():
from sympy.utilities.iterables import cartes
lim = (x, 0, 5)
# set below includes two pts below range, 2 pts in range,
# 2 pts above range, and the boundaries
N = (-2, -1, 0, 1, 2, 5, 6, 7)
p = Piecewise((1, x > a), (2, x > b), (0, True))
ans = p.integrate(lim)
for i, j in cartes(N, repeat=2):
reps = dict(zip((a, b), (i, j)))
assert ans.subs(reps) == p.subs(reps).integrate(lim)
assert ans.subs(a, 4).subs(b, 1) == 0 + 2*3 + 1
p = Piecewise((1, x > a), (2, x < b), (0, True))
ans = p.integrate(lim)
for i, j in cartes(N, repeat=2):
reps = dict(zip((a, b), (i, j)))
assert ans.subs(reps) == p.subs(reps).integrate(lim)
示例9: test_piecewise_integrate2
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_integrate2():
from itertools import permutations
lim = Tuple(x, c, d)
p = Piecewise((1, x < a), (2, x > b), (3, True))
q = p.integrate(lim)
assert q == Piecewise(
(-c + 2*d - 2*Min(d, Max(a, c)) + Min(d, Max(a, b, c)), c < d),
(-2*c + d + 2*Min(c, Max(a, d)) - Min(c, Max(a, b, d)), True))
for v in permutations((1, 2, 3, 4)):
r = dict(zip((a, b, c, d), v))
assert p.subs(r).integrate(lim.subs(r)) == q.subs(r)
示例10: test_piecewise_fold_piecewise_in_cond
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_fold_piecewise_in_cond():
p1 = Piecewise((cos(x), x < 0), (0, True))
p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
p3 = piecewise_fold(p2)
assert(p2.subs(x, -pi/2) == 0.0)
assert(p2.subs(x, 1) == 0.0)
assert(p2.subs(x, -pi/4) == 1.0)
p4 = Piecewise((0, Eq(p1, 0)), (1,True))
ans = piecewise_fold(p4)
for i in range(-1, 1):
assert ans.subs(x, i) == p4.subs(x, i)
r1 = 1 < Piecewise((1, x < 1), (3, True))
ans = piecewise_fold(r1)
for i in range(2):
assert ans.subs(x, i) == r1.subs(x, i)
p5 = Piecewise((1, x < 0), (3, True))
p6 = Piecewise((1, x < 1), (3, True))
p7 = Piecewise((1, p5 < p6), (0, True))
ans = piecewise_fold(p7)
for i in range(-1, 2):
assert ans.subs(x, i) == p7.subs(x, i)
示例11: test_piecewise_integrate1b
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_integrate1b():
g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
assert integrate(g, (x, -1, 1)) == 0
g = Piecewise((1, x - y < 0), (0, True))
assert integrate(g, (y, -oo, 0)) == -Min(0, x)
assert g.subs(x, -3).integrate((y, -oo, 0)) == 3
assert integrate(g, (y, 0, -oo)) == Min(0, x)
assert integrate(g, (y, 0, oo)) == -Max(0, x) + oo
assert integrate(g, (y, -oo, 42)) == -Min(42, x) + 42
assert integrate(g, (y, -oo, oo)) == -x + oo
g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
gy1 = g.integrate((x, y, 1))
g1y = g.integrate((x, 1, y))
for yy in (-1, S.Half, 2):
assert g.integrate((x, yy, 1)) == gy1.subs(y, yy)
assert g.integrate((x, 1, yy)) == g1y.subs(y, yy)
assert gy1 == Piecewise(
(-Min(1, Max(0, y))**2/2 + 1/2, y < 1),
(-y + 1, True))
assert g1y == Piecewise(
(Min(1, Max(0, y))**2/2 - 1/2, y < 1),
(y - 1, True))
示例12: test_piecewise_integrate1
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_integrate1():
x, y = symbols('x y', real=True, finite=True)
f = Piecewise(((x - 2)**2, x >= 0), (1, True))
assert integrate(f, (x, -2, 2)) == Rational(14, 3)
g = Piecewise(((x - 5)**5, x >= 4), (f, True))
assert integrate(g, (x, -2, 2)) == Rational(14, 3)
assert integrate(g, (x, -2, 5)) == Rational(43, 6)
assert g == Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
g = Piecewise(((x - 5)**5, 2 <= x), (f, x < 2))
assert integrate(g, (x, -2, 2)) == Rational(14, 3)
assert integrate(g, (x, -2, 5)) == -Rational(701, 6)
assert g == Piecewise(((x - 5)**5, 2 <= x), (f, True))
g = Piecewise(((x - 5)**5, 2 <= x), (2*f, True))
assert integrate(g, (x, -2, 2)) == 2 * Rational(14, 3)
assert integrate(g, (x, -2, 5)) == -Rational(673, 6)
g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
assert integrate(g, (x, -1, 1)) == 0
g = Piecewise((1, x - y < 0), (0, True))
assert integrate(g, (y, -oo, 0)) == -Min(0, x)
assert g.subs(x, -3).integrate((y, -oo, 0)) == 3
assert integrate(g, (y, 0, -oo)) == Min(0, x)
assert integrate(g, (y, 0, oo)) == -Max(0, x) + oo
assert integrate(g, (y, -oo, 42)) == -Min(42, x) + 42
assert integrate(g, (y, -oo, oo)) == -x + oo
g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
assert integrate(g, (x, -5, 1)) == Rational(1, 2)
assert integrate(g, (x, -5, y)).subs(y, 1) == Rational(1, 2)
assert integrate(g, (x, y, 1)).subs(y, -5) == Rational(1, 2)
assert integrate(g, (x, 1, -5)) == -Rational(1, 2)
assert integrate(g, (x, 1, y)).subs(y, -5) == -Rational(1, 2)
assert integrate(g, (x, y, -5)).subs(y, 1) == -Rational(1, 2)
assert integrate(g, (x, -5, y)) == Piecewise(
(y - Min(0, y)**2/2 + Min(1, y)**2/2 - Min(1, y), y > -5),
(0, True))
assert integrate(g, (x, y, 1)) == Piecewise(
(-Min(1, Max(0, y))**2/2 + 1/2, y < 1),
(-y + 1, True))
for j, g in enumerate([
Piecewise((1 - x, Interval(0, 1).contains(x)),
(1 + x, Interval(-1, 0).contains(x)), (0, True)),
Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0),
(1 + x, True))]):
assert integrate(g, (x, -5, 1)) == 1
assert integrate(g, (x, 1, -5)) == -1
assert integrate(g, (x, 1, y)).subs(y, -5) == -1
assert integrate(g, (x, y, -5)).subs(y, 1) == -1
i = integrate(g, (x, -5, y))
assert i.subs(y, 1) == 1
assert piecewise_fold(i.rewrite(Piecewise)
) == Piecewise(
(1, y >= 1),
(-y**2/2 + y + 1/2, y >= 0),
(y**2/2 + y + 1/2, y >= -1),
(0, True))
assert i == Piecewise(
(-Min(-1, y)**2/2 - Min(-1, y) +
Min(0, y)**2 - Min(1, y)**2/2 + Min(1, y), y > -5),
(0, True))
i = integrate(g, (x, y, 1))
assert i.subs(y, -5) == 1
assert piecewise_fold(i.rewrite(Piecewise)
)== Piecewise(
(1, y <= -1),
(-y**2/2 - y + 1/2, y <= 0),
(y**2/2 - y + 1/2, y < 1),
(0, True))
# the solutions are different because
# Min(1, Max(-1, y), Max(0, y)) is not
# simplifying to Min(1, Max(-1, y))
assert i == [
Piecewise(
(
-Min(1, Max(-1, y), Max(0, y))**2/2
-Min(1, Max(-1, y), Max(0, y))
+Min(1, Max(0, y))**2
+1/2,
y < 1),
(0, True)),
Piecewise(
(
-Min(1, Max(-1, y))**2/2
-Min(1, Max(-1, y))
+Min(1, Max(0, y))**2
+1/2,
y < 1),
(0, True))][j]
示例13: test_Function_subs
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_Function_subs():
f, g, h, i = symbols('f g h i', cls=Function)
p = Piecewise((g(f(x, y)), x < -1), (g(x), x <= 1))
assert p.subs(g, h) == Piecewise((h(f(x, y)), x < -1), (h(x), x <= 1))
assert (f(y) + g(x)).subs({f: h, g: i}) == i(x) + h(y)
示例14: test_piecewise
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise():
# Test canonization
assert Piecewise((x, x < 1), (0, True)) == Piecewise((x, x < 1), (0, True))
assert Piecewise((x, x < 1), (0, True), (1, True)) == \
Piecewise((x, x < 1), (0, True))
assert Piecewise((x, x < 1), (0, False), (-1, 1 > 2)) == \
Piecewise((x, x < 1))
assert Piecewise((x, x < 1), (0, x < 1), (0, True)) == \
Piecewise((x, x < 1), (0, True))
assert Piecewise((x, x < 1), (0, x < 2), (0, True)) == \
Piecewise((x, x < 1), (0, True))
assert Piecewise((x, x < 1), (x, x < 2), (0, True)) == \
Piecewise((x, Or(x < 1, x < 2)), (0, True))
assert Piecewise((x, x < 1), (x, x < 2), (x, True)) == x
assert Piecewise((x, True)) == x
raises(TypeError, lambda: Piecewise(x))
raises(TypeError, lambda: Piecewise((x, x**2)))
# Test subs
p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >= 0))
p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >= 0))
assert p.subs(x, x**2) == p_x2
assert p.subs(x, -5) == -1
assert p.subs(x, -1) == 1
assert p.subs(x, 1) == log(1)
# More subs tests
p2 = Piecewise((1, x < pi), (-1, x < 2*pi), (0, x > 2*pi))
p3 = Piecewise((1, Eq(x, 0)), (1/x, True))
p4 = Piecewise((1, Eq(x, 0)), (2, 1/x>2))
assert p2.subs(x, 2) == 1
assert p2.subs(x, 4) == -1
assert p2.subs(x, 10) == 0
assert p3.subs(x, 0.0) == 1
assert p4.subs(x, 0.0) == 1
f, g, h = symbols('f,g,h', cls=Function)
pf = Piecewise((f(x), x < -1), (f(x) + h(x) + 2, x <= 1))
pg = Piecewise((g(x), x < -1), (g(x) + h(x) + 2, x <= 1))
assert pg.subs(g, f) == pf
assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 0) == 1
assert Piecewise((1, Eq(x, 0)), (0, True)).subs(x, 1) == 0
assert Piecewise((1, Eq(x, y)), (0, True)).subs(x, y) == 1
assert Piecewise((1, Eq(x, z)), (0, True)).subs(x, z) == 1
assert Piecewise((1, Eq(exp(x), cos(z))), (0, True)).subs(x, z) == \
Piecewise((1, Eq(exp(z), cos(z))), (0, True))
assert Piecewise((1, Eq(x, y*(y + 1))), (0, True)).subs(x, y**2 + y) == 1
p5 = Piecewise( (0, Eq(cos(x) + y, 0)), (1, True))
assert p5.subs(y, 0) == Piecewise( (0, Eq(cos(x), 0)), (1, True))
# Test evalf
assert p.evalf() == p
assert p.evalf(subs={x: -2}) == -1
assert p.evalf(subs={x: -1}) == 1
assert p.evalf(subs={x: 1}) == log(1)
# Test doit
f_int = Piecewise((Integral(x, (x, 0, 1)), x < 1))
assert f_int.doit() == Piecewise( (1.0/2.0, x < 1) )
# Test differentiation
f = x
fp = x*p
dp = Piecewise((0, x < -1), (2*x, x < 0), (1/x, x >= 0))
fp_dx = x*dp + p
assert diff(p, x) == dp
assert diff(f*p, x) == fp_dx
# Test simple arithmetic
assert x*p == fp
assert x*p + p == p + x*p
assert p + f == f + p
assert p + dp == dp + p
assert p - dp == -(dp - p)
# Test power
dp2 = Piecewise((0, x < -1), (4*x**2, x < 0), (1/x**2, x >= 0))
assert dp**2 == dp2
# Test _eval_interval
f1 = x*y + 2
f2 = x*y**2 + 3
peval = Piecewise( (f1, x < 0), (f2, x > 0))
peval_interval = f1.subs(
x, 0) - f1.subs(x, -1) + f2.subs(x, 1) - f2.subs(x, 0)
assert peval._eval_interval(x, 0, 0) == 0
assert peval._eval_interval(x, -1, 1) == peval_interval
peval2 = Piecewise((f1, x < 0), (f2, True))
assert peval2._eval_interval(x, 0, 0) == 0
assert peval2._eval_interval(x, 1, -1) == -peval_interval
assert peval2._eval_interval(x, -1, -2) == f1.subs(x, -2) - f1.subs(x, -1)
assert peval2._eval_interval(x, -1, 1) == peval_interval
assert peval2._eval_interval(x, None, 0) == peval2.subs(x, 0)
assert peval2._eval_interval(x, -1, None) == -peval2.subs(x, -1)
# Test integration
#.........这里部分代码省略.........
示例15: test_piecewise_solve
# 需要导入模块: from sympy import Piecewise [as 别名]
# 或者: from sympy.Piecewise import subs [as 别名]
def test_piecewise_solve():
abs2 = Piecewise((-x, x <= 0), (x, x > 0))
f = abs2.subs(x, x - 2)
assert solve(f, x) == [2]
assert solve(f - 1, x) == [1, 3]
f = Piecewise(((x - 2)**2, x >= 0), (1, True))
assert solve(f, x) == [2]
g = Piecewise(((x - 5)**5, x >= 4), (f, True))
assert solve(g, x) == [2, 5]
g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4))
assert solve(g, x) == [2, 5]
g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2))
assert solve(g, x) == [5]
g = Piecewise(((x - 5)**5, x >= 2), (f, True))
assert solve(g, x) == [5]
g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False))
assert solve(g, x) == [5]
g = Piecewise(((x - 5)**5, x >= 2),
(-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0))
assert solve(g, x) == [5]
# if no symbol is given the piecewise detection must still work
assert solve(Piecewise((x - 2, x > 2), (2 - x, True)) - 3) == [-1, 5]
f = Piecewise(((x - 2)**2, x >= 0), (0, True))
assert solve(f, x) == [2, Piecewise((x, x < 0), (S.NaN, True))]
f = Piecewise(((x - 2)**2, x >= 0), (0, x > -1), (x + 3, True))
assert solve(f, x) == [
-3, 2, Piecewise((x, (x > -1) & (x < 0)), (S.NaN, True))]
f = Piecewise(((x - 2)**2, x >= 0), (0, x < 3), (x + 3, True))
assert solve(f, x) == [
2, Piecewise((x, (x < 0) & (x < 3)), (S.NaN, True))]
def nona(ans):
return list(filter(lambda x: x is not S.NaN, ans))
p = Piecewise((x**2 - 4, x < y), (x - 2, True))
ans = solve(p, x)
assert nona([i.subs(y, -2) for i in ans]) == [2]
assert nona([i.subs(y, 2) for i in ans]) == [-2, 2]
assert nona([i.subs(y, 3) for i in ans]) == [-2, 2]
assert ans == [
Piecewise((-2, y > -2), (S.NaN, True)),
Piecewise((2, y <= 2), (S.NaN, True)),
Piecewise((2, y > 2), (S.NaN, True))]
# issue 6060
absxm3 = Piecewise(
(x - 3, S(0) <= x - 3),
(3 - x, S(0) > x - 3)
)
assert solve(absxm3 - y, x) == [
Piecewise((-y + 3, -y < 0), (S.NaN, True)),
Piecewise((y + 3, y >= 0), (S.NaN, True))]
p = Symbol('p', positive=True)
assert solve(absxm3 - p, x) == [-p + 3, p + 3]
# issue 6989
f = Function('f')
assert solve(Eq(-f(x), Piecewise((1, x > 0), (0, True))), f(x)) == \
[Piecewise((-1, x > 0), (0, True))]
# issue 8587
f = Piecewise((2*x**2, And(S(0) < x, x < 1)), (2, True))
assert solve(f - 1) == [1/sqrt(2)]