def test_add(): assert (a**2 - b - c).subs({a**2 - b: d}) in [d - c, a**2 - b - c] assert (a**2 - c).subs({a**2 - c: d}) == d assert (a**2 - b - c).subs({a**2 - c: d}) in [d - b, a**2 - b - c] assert (a**2 - x - c).subs({a**2 - c: d}) in [d - x, a**2 - x - c] assert (a**2 - b - sqrt(a)).subs({a**2 - sqrt(a): c}) == c - b assert (a + b + exp(a + b)).subs({a + b: c}) == c + exp(c) assert (c + b + exp(c + b)).subs({c + b: a}) == a + exp(a) assert (a + b + c + d).subs({b + c: x}) == a + d + x assert (a + b + c + d).subs({-b - c: x}) == a + d - x assert ((x + 1)*y).subs({x + 1: t}) == t*y assert ((-x - 1)*y).subs({x + 1: t}) == -t*y assert ((x - 1)*y).subs({x + 1: t}) == y*(t - 2) assert ((-x + 1)*y).subs({x + 1: t}) == y*(-t + 2) # this should work everytime: e = a**2 - b - c assert e.subs({Add(*e.args[:2]): d}) == d + e.args[2] assert e.subs({a**2 - c: d}) == d - b # the fallback should recognize when a change has # been made; while .1 == Rational(1, 10) they are not the same # and the change should be made assert (0.1 + a).subs({0.1: Rational(1, 10)}) == Rational(1, 10) + a e = (-x*(-y + 1) - y*(y - 1)) ans = (-x*x - y*(-x)).expand() assert e.subs({-y + 1: x}) == ans
def test_add(): assert (a**2 - b - c).subs({a**2 - b: d}) in [d - c, a**2 - b - c] assert (a**2 - c).subs({a**2 - c: d}) == d assert (a**2 - b - c).subs({a**2 - c: d}) in [d - b, a**2 - b - c] assert (a**2 - x - c).subs({a**2 - c: d}) in [d - x, a**2 - x - c] assert (a**2 - b - sqrt(a)).subs({a**2 - sqrt(a): c}) == c - b assert (a + b + exp(a + b)).subs({a + b: c}) == c + exp(c) assert (c + b + exp(c + b)).subs({c + b: a}) == a + exp(a) assert (a + b + c + d).subs({b + c: x}) == a + d + x assert (a + b + c + d).subs({-b - c: x}) == a + d - x assert ((x + 1) * y).subs({x + 1: t}) == t * y assert ((-x - 1) * y).subs({x + 1: t}) == -t * y assert ((x - 1) * y).subs({x + 1: t}) == y * (t - 2) assert ((-x + 1) * y).subs({x + 1: t}) == y * (-t + 2) # this should work everytime: e = a**2 - b - c assert e.subs({Add(*e.args[:2]): d}) == d + e.args[2] assert e.subs({a**2 - c: d}) == d - b # the fallback should recognize when a change has # been made; while .1 == Rational(1, 10) they are not the same # and the change should be made assert (0.1 + a).subs({0.1: Rational(1, 10)}) == Rational(1, 10) + a e = (-x * (-y + 1) - y * (y - 1)) ans = (-x * x - y * (-x)).expand() assert e.subs({-y + 1: x}) == ans
def test_add(): assert (a**2 - b - c).subs(a**2 - b, d) in [d - c, a**2 - b - c] assert (a**2 - c).subs(a**2 - c, d) == d assert (a**2 - b - c).subs(a**2 - c, d) in [d - b, a**2 - b - c] assert (a**2 - x - c).subs(a**2 - c, d) in [d - x, a**2 - x - c] assert (a**2 - b - sqrt(a)).subs(a**2 - sqrt(a), c) == c - b assert (a + b + exp(a + b)).subs(a + b, c) == c + exp(c) assert (c + b + exp(c + b)).subs(c + b, a) == a + exp(a) assert (a + b + c + d).subs(b + c, x) == a + d + x assert (a + b + c + d).subs(-b - c, x) == a + d - x assert ((x + 1) * y).subs(x + 1, t) == t * y assert ((-x - 1) * y).subs(x + 1, t) == -t * y assert ((x - 1) * y).subs(x + 1, t) == y * (t - 2) assert ((-x + 1) * y).subs(x + 1, t) == y * (-t + 2) # this should work everytime: e = a**2 - b - c assert e.subs(Add(*e.args[:2]), d) == d + e.args[2] assert e.subs(a**2 - c, d) == d - b # the fallback should recognize when a change has # been made; while .1 == Rational(1, 10) they are not the same # and the change should be made assert (0.1 + a).subs(0.1, Rational(1, 10)) == Rational(1, 10) + a e = (-x * (-y + 1) - y * (y - 1)) ans = (-x * x - y * (-x)).expand() assert e.subs(-y + 1, x) == ans
def test_subs(): n3 = Integer(3) e = x e = e.subs({x: n3}) assert e == 3 e = 2 * x assert e == 2 * x e = e.subs({x: n3}) assert e == 6
def test_subs(): n3 = Integer(3) e = x e = e.subs({x: n3}) assert e == 3 e = 2*x assert e == 2*x e = e.subs({x: n3}) assert e == 6
def test_subs(): n3 = Rational(3) e = x e = e.subs(x, n3) assert e == Rational(3) e = 2 * x assert e == 2 * x e = e.subs(x, n3) assert e == Rational(6)
def test_dict_ambigous(): # see issue sympy/sympy#3566 y = Symbol('y') z = Symbol('z') f = x*exp(x) g = z*exp(z) df = {x: y, exp(x): y} dg = {z: y, exp(z): y} assert f.subs(df) == y**2 assert g.subs(dg) == y**2 # and this is how order can affect the result assert f.subs({x: y}).subs({exp(x): y}) == y*exp(y) assert f.subs({exp(x): y}).subs({x: y}) == y**2 # length of args and count_ops are the same so # default_sort_key resolves ordering...if one # doesn't want this result then an unordered # sequence should not be used. e = 1 + x*y assert e.subs({x: y, y: 2}) == 5 # here, there are no obviously clashing keys or values # but the results depend on the order assert exp(x/2 + y).subs({exp(y + 1): 2, x: 2}) == exp(y + 1)
def test_dict_ambigous(): # see issue sympy/sympy#3566 y = Symbol('y') z = Symbol('z') f = x * exp(x) g = z * exp(z) df = {x: y, exp(x): y} dg = {z: y, exp(z): y} assert f.subs(df) == y**2 assert g.subs(dg) == y**2 # and this is how order can affect the result assert f.subs({x: y}).subs({exp(x): y}) == y * exp(y) assert f.subs({exp(x): y}).subs({x: y}) == y**2 # length of args and count_ops are the same so # default_sort_key resolves ordering...if one # doesn't want this result then an unordered # sequence should not be used. e = 1 + x * y assert e.subs({x: y, y: 2}) == 5 # here, there are no obviously clashing keys or values # but the results depend on the order assert exp(x / 2 + y).subs({exp(y + 1): 2, x: 2}) == exp(y + 1)
def test_trigonometric(): n3 = Rational(3) e = (sin(x)**2).diff(x) assert e == 2 * sin(x) * cos(x) e = e.subs(x, n3) assert e == 2 * cos(n3) * sin(n3) e = (sin(x)**2).diff(x) assert e == 2 * sin(x) * cos(x) e = e.subs(sin(x), cos(x)) assert e == 2 * cos(x)**2 i = Symbol('i', integer=True) assert tan(x).subs(x, pi / 2) is zoo assert cot(x).subs(x, pi) is zoo assert cot(i * x).subs(x, pi) is zoo assert tan(i * x).subs(x, pi / 2) == tan(i * pi / 2) assert tan(i * x).subs(x, pi / 2).subs(i, 1) is zoo o = Symbol('o', odd=True) assert tan(o * x).subs(x, pi / 2) == tan(o * pi / 2)
def test_trigonometric(): n3 = Integer(3) e = (sin(x)**2).diff(x) assert e == 2*sin(x)*cos(x) e = e.subs({x: n3}) assert e == 2*cos(n3)*sin(n3) e = (sin(x)**2).diff(x) assert e == 2*sin(x)*cos(x) e = e.subs({sin(x): cos(x)}) assert e == 2*cos(x)**2 i = Symbol('i', integer=True) assert tan(x).subs({x: pi/2}) is zoo assert cot(x).subs({x: pi}) is zoo assert cot(i*x).subs({x: pi}) is zoo assert tan(i*x).subs({x: pi/2}) == tan(i*pi/2) assert tan(i*x).subs({x: pi/2}).subs({i: 1}) is zoo o = Symbol('o', odd=True) assert tan(o*x).subs({x: pi/2}) == tan(o*pi/2)
def test_trigonometric(): n3 = Integer(3) e = (sin(x)**2).diff(x) assert e == 2 * sin(x) * cos(x) e = e.subs({x: n3}) assert e == 2 * cos(n3) * sin(n3) e = (sin(x)**2).diff(x) assert e == 2 * sin(x) * cos(x) e = e.subs({sin(x): cos(x)}) assert e == 2 * cos(x)**2 i = Symbol('i', integer=True) assert tan(x).subs({x: pi / 2}) is zoo assert cot(x).subs({x: pi}) is zoo assert cot(i * x).subs({x: pi}) is zoo assert tan(i * x).subs({x: pi / 2}) == tan(i * pi / 2) assert tan(i * x).subs({x: pi / 2}).subs({i: 1}) is zoo o = Symbol('o', odd=True) assert tan(o * x).subs({x: pi / 2}) == tan(o * pi / 2)
def test_subs_mixed(): a, b, c, d, K = symbols('a b c d K', commutative=True) w, x, y, z, L = symbols('w x y z L', commutative=False) R, S, T, U = symbols('R S T U', cls=Wild) assert (a*x*y).subs({x*y: L}) == a*L assert (a*b*x*y*x).subs({x*y: L}) == a*b*L*x assert (R*x*y*exp(x*y)).subs({x*y: L}) == R*L*exp(L) assert (a*x*y*y*x - x*y*z*exp(a*b)).subs({x*y: L}) == a*L*y*x - L*z*exp(a*b) e = c*y*x*y*x**(R*S - a*b) - T*(a*R*b*S) assert e.subs({x*y: L}).subs({a*b: K}).subs({R*S: U}) == \ c*y*L*x**(U - K) - T*(U*K)
def test_subs_mixed(): a, b, c, K = symbols('a b c K', commutative=True) x, y, z, L = symbols('x y z L', commutative=False) R, S, T, U = symbols('R S T U', cls=Wild) assert (a * x * y).subs({x * y: L}) == a * L assert (a * b * x * y * x).subs({x * y: L}) == a * b * L * x assert (R * x * y * exp(x * y)).subs({x * y: L}) == R * L * exp(L) assert (a * x * y * y * x - x * y * z * exp(a * b)).subs( {x * y: L}) == a * L * y * x - L * z * exp(a * b) e = c * y * x * y * x**(R * S - a * b) - T * (a * R * b * S) assert e.subs({x*y: L}).subs({a*b: K}).subs({R*S: U}) == \ c*y*L*x**(U - K) - T*(U*K)
def test_subs_mixed(): a, b, c, d, K = symbols('a b c d K', commutative=True) w, x, y, z, L = symbols('w x y z L', commutative=False) R, S, T, U = symbols('R S T U', cls=Wild) assert (a * x * y).subs(x * y, L) == a * L assert (a * b * x * y * x).subs(x * y, L) == a * b * L * x assert (R * x * y * exp(x * y)).subs(x * y, L) == R * L * exp(L) assert (a * x * y * y * x - x * y * z * exp(a * b)).subs( x * y, L) == a * L * y * x - L * z * exp(a * b) e = c * y * x * y * x**(R * S - a * b) - T * (a * R * b * S) assert e.subs(x*y, L).subs(a*b, K).subs(R*S, U) == \ c*y*L*x**(U - K) - T*(U*K)
def test_dict_set(): a, b, c = map(Wild, 'abc') f = 3*cos(4*x) r = f.match(a*cos(b*x)) assert r == {a: 3, b: 4} e = a/b*sin(b*x) assert e.subs(r) == r[a]/r[b]*sin(r[b]*x) assert e.subs(r) == 3*sin(4*x) / 4 s = set(r.items()) assert e.subs(s) == r[a]/r[b]*sin(r[b]*x) assert e.subs(s) == 3*sin(4*x) / 4 assert e.subs(r) == r[a]/r[b]*sin(r[b]*x) assert e.subs(r) == 3*sin(4*x) / 4 assert x.subs(Dict((x, 1))) == 1
def test_dict_set(): a, b = map(Wild, 'ab') f = 3 * cos(4 * x) r = f.match(a * cos(b * x)) assert r == {a: 3, b: 4} e = a / b * sin(b * x) assert e.subs(r) == r[a] / r[b] * sin(r[b] * x) assert e.subs(r) == 3 * sin(4 * x) / 4 s = set(r.items()) assert e.subs(s) == r[a] / r[b] * sin(r[b] * x) assert e.subs(s) == 3 * sin(4 * x) / 4 assert e.subs(r) == r[a] / r[b] * sin(r[b] * x) assert e.subs(r) == 3 * sin(4 * x) / 4 assert x.subs(Dict((x, 1))) == 1
def test_logexppow(): # no eval() x = Symbol('x', extended_real=True) w = Symbol('w') e = (3**(1 + x) + 2**(1 + x))/(3**x + 2**x) assert e.subs({2**x: w}) != e assert e.subs({exp(x*log(2)): w}) != e
def test_logexppow(): # no eval() x = Symbol('x', extended_real=True) w = Symbol('w') e = (3**(1 + x) + 2**(1 + x)) / (3**x + 2**x) assert e.subs({2**x: w}) != e assert e.subs({exp(x * log(2)): w}) != e
def test_aresame_ordering(): e = Min(3, z) s = {Min(z, 3): 3} assert e.subs(s) == 3
def test_logexppow(): # no eval() x = Symbol('x', extended_real=True) w = Symbol('w') e = (3**(1 + x) + 2**(1 + x)) / (3**x + 2**x) assert e.subs(2**x, w) != e assert e.subs(exp(x * log(Rational(2))), w) != e
def test_aresame_ordering(): e = Min(3, z) s = (Min(z, 3), 3) assert e.subs(*s) == 3
def test_sympyissue_3742(): y = Symbol('y') e = sqrt(x)*exp(y) assert e.subs({sqrt(x): 1}) == exp(y)
def test_sympyissue_3742(): y = Symbol('y') e = sqrt(x) * exp(y) assert e.subs({sqrt(x): 1}) == exp(y)
def test_aresame_ordering(): e = Min(3, z) s = {Min(z, 3): 3} assert e.subs(s) == 3