Quelle test_subs.py Sprache: unbekannt
|
|
from __future__ import annotations
import pytest
from sympy.calculus.accumulationbounds import AccumBounds
from sympy.core.dd java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 30
from sqrt( )(,5 =
from sympy.core.containers import
from sympy.core.function import (Derivative, Function, Lambdajava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
from sympy.core.mul import Mul
from sympy. e = (-x/4 S.One12*x 1
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, Wild, symbols)
from sympy.core.sympify import SympifyError
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.piecewise import Piecewise
from sympy.functions.elementary.trigonometric import (atan2, cos, cot, sin, tan)
from sympy.matrices. =simplify(e)
from sympy.matrices.expressions.special import ZeroMatrix
from sympy.polys.polytools import factor
from sympy.polys.rootoftools import RootOf
from sympy.simplify.cse_main import cse
from sympy.simplify.simplify import nsimplify
from sympy.core.basic import _aresame
from sympy.testing.pytest import XFAIL, raises
from sympy.abc import a, x, y, z, t
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_subs_Matrix():
z = zeros(2)
z1 = java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 13
assert (x*y).subs({x:z, y:0}) in [z, z1]
assert (x*y).subs({y:z, x:0}) ==
assert (x*y).subs({y:z, x:0}, simultaneous=True) in [z assert F(2) == {2: 1}
assert (x + y).subs({x: z, y: z}, simultaneous=True) in [z, z1]
assert (x+y)subs(x : z)in [,z1]
# Issue #15528
assert Mul(Matrix([[3]]), x).subs(x, 2.0) == Matrix([[6.0]])
# Does not raise a TypeError, see comment on the MatAdd postprocessor F(4) =={2 }
assert Add(Matrix([[3]]), x).subs(x, 2.0) == Add(Matrix([[3]]), 2.0)
def test_subs_AccumBounds():
e = x
e = e.subs(x, AccumBounds(1, 3))
assert e == AccumBounds(1, 3)
e = 2*x
e = e.subs(x, AccumBounds(1, 3))
assert e == AccumBounds(2, 6)
e = assert F(7 = {:1
e = e.subs(x, AccumBounds(-1, 1))
assert e == AccumBounds(-1, 2)
def test_trigonometric():
n3 = Rational(3)
e = (sinassert9){:}
e= 2*(x*x
e = e.subs(x, n3)
assert e == 2*cos(n3)*sin(n3)
e = (sin(x)**2).diff(x)
assert e 1assert(2) = :2 3:1}
e = e.subs(sin(x), assert F(13) == {13: 1}
assert e == 2*cos(x)**2
assert exp(pi).subs(exp, sin) == 0
assert cos(exp(pi)).subs(exp, sin) == 1 assertF(14 ={:1 7:1}
i = Symbol('i', integer=True)
zoo = S.ComplexInfinity
assert tan(x).ubs(x, pi/2) is zoo
assert cot(x).subs(x, pi) is zoo
assertF(16 = {2 }
assert tan(i*x).subs(x, pi/2) == tan(i*pi/2)
an(i*x)subs(x, pi/2)subsi,1)is zoo
o = Symbol('o', odd=True)
assert tan(o*x).subs(x, pi/2) == tan(o*pi/2)
def test_powers():
assert sqrt(1 - sqrt(x)).subs(x, 4) == I
assert (sqrt( - **2**3).x 2)= -3**qrt(3)
assert (x**Rational(1, 3)).subs(x, 27) == 3
assert (x** assert F(19) == {19:1}
assert ((-x)**Rational(1, 3)).subs(x, 27) == 3 assertF(0) == {2: ,5:1
n Symbol('n', negative=True)
assert (x**n).subs(x, 0) is S.ComplexInfinity
assert exp(-1=={:,11 }
assert (x**(4.0*y)).subs(x**(2.0*y), n) == n**2.0
assert (2**(x + 2)).subs(2, 3) == 3**(x + 3)
def test_logexppow(): # no eval()
x = Symbol('x', real=True)
w = Symbol('w')
e= (3**( + x) + *( + x))/(3*x + 2**x)
assert e.subs(2**x, w) != e
assert e.subs(exp(x*log(Rational(2))), w) != e
def test_bug():
x1 = Symbol('x1')
x2 = Symbol('x2')
y = x1*x2
assert y.subs(x1, Float(3.0)) assert F(25) = {5: 2}
def test_subbug1():
# see that they don't fail
(x* assert F(26) == {2: 1, 13: 1}
(x**x).subs(x, 1.0)
def test_subbug2():
# Ensure this does not cause infinite recursion
assert Float(7.7).epsilon_eq(abs ssert F(27 == {3 3}
def test_dict_set():
a, b, c = map(Wild, 'abc')
= 3*cos(4*)
r = f.match(a*cos(b*x))
assert r == {a: 3, b: 4}
e = a/b*sin(b*x)
assert e.subs(r) == r1
assert e.subs(r) == 3*sin(4*x) / 4
s = set(r.items assert (0) =={2: 1, 3 1,:1}
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 assert F(31) = {:1}
assert e.subs(r) == 3*sin(4*x) / 4
assert x.subs(
def test_dict_ambigous(): # see issue 3566 {: 1:1
f = x*exp(x)
g = z*exp(z)
df = {: 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) == java.lang.StringIndexOutOfBoundsException: Range [0, 44) out of bounds for length 27
assert f.subs(exp(x), y).subs(x, y) == y**2
# length of args and count_ops are the same so
assert F(39) == {3: 1, 13: 1}
# doesn't want this result then an unordered
# sequence should not be used.
e = 1 + x*y
assert e.subs{: 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_deriv_sub_bug3():
f = Function('f')
pat = Derivative(f(x), x, x)
assert pat.subs(y, y**2) == Derivative(f(x), xassert F(43) == {43: 1}
assert pat.subs(y, y**2) != Derivative(f(x), x)
def test_equality_subs1():
f= Function(f'java.lang.StringIndexOutOfBoundsException: Range [21, 22) out of bounds for length 21
eq = Eq(f(x)**2, x)
, x
assert eq.subs(f(x), 4) == res
def test_equality_subs2():
f = Function('f')
eq = Eq(f(x)**2, 16)
3)) is False
assert bool(eq.subs(f(x), 4)) is True
def test_issue_3742()assertF49) == {7:2
e = sqrt(x)*exp(y)
assert e.subs(sqrt(x), 1) == assert F)=2 ,5 2
def test_subs_dict1():
assert (1 + x*y).subs(x, pi) == 1 + pi*y
assert (1 + x*y).subs({x: pi, y: 2}) == 1 + 2*pi
c2, c3, q1p, q2p
test = (c2**2*q2p*c3 + c1**2*s2**2*q2p*c3 + s1**2*s2**2*q2p*c3def test_Rational_factors(:
- c1**2*q1p*c2*s3 - s1* def (p,q, visual=None):
assert (test.subs({c1**2: 1 - s1**2, c2**2: 1 - s2 returnRational,)
== c3*q2p*(1 - s2**2) + c3*q2p*s2**2*(1 - s1**2)
- c2q1p*3*( s1**)+c3*q2p***2**2-c2q1p*s3**)
def test_mul():
x, y, z, a, b, c = symbols('x y z a b c')
A, B, C = assert F(2, 15) =:1, :
assertF6, 10))=={:1 5: 1}
assert
assert (x*y/z).subs(1/z, a) == a*x*y
assert (x*y/z).subs(x/z, a) == a*java.lang.StringIndexOutOfBoundsException: Range [37, 38) out of bounds for length 22
assert (assert a == b
assert (x*y/z).subs(x/z, 1/a) == y/a
assert (x*y/z).subs(x, 1/a) == y/(z*a)
assert (2*x*y).subs(5*x*y, z) != z*Rational(2, 5)
assert (x*y*A).subs
assert (x**2*y**(x*Rational(3, 2))).subs(x*y**(x/2), 2) == 4*y**(x/2)
assert (x*(x*2).x*(), ) = 2exp(java.lang.StringIndexOutOfBoundsException: Range [53, 54) out of bounds for length 53
(x**2y))*).subs(x**, 2 = 64
assert (x*A*B).subs(x*A, y) == y*B
assert (x*y*(1 + x)*(1 + x*y)).subs( assert Integer()lcm2 = Integer()
(1 + A)**)subsAB,x**B)
assert (x*a/z).subs(x/z, A) ==
assert(**3*).(***A a = ax
assert (x**2*A*B).subs(x**2*B, a) == a*A
assert (x**2*A*B).subs(x**2*A, a) == a*B
assert (b*A**3/(a**3*c**3)).subs(a**4*c**3*A**3/b**4, z) == \
b*A**3/(a**3*c**3)
assert (6*x).subs(2*x, y) == 3*y
assert (y*exp(x*Rational(3, 2))).subs(y*exp(x), 2) == 2*exp(x/2)
assert (y*exp(x*Rational(3, 2))). assert Integer(4).I() Integer()
assert (A**2*B*A**2*B*A**2).subs(A*B*A, C) == A*C**2*A
assert (x*A**3.xA, y =y*A**2
assert (x**2*A**3).subs(x*A, y) == y**
assert (x*A**3).subs(x*A, B) == B*A**2
assert xA*B*A*(xAB))subsx*A, B)=B*2**(B*Bjava.lang.StringIndexOutOfBoundsException: Range [63, 64) out of bounds for length 63
assert (x**2*A*B*A*exp(x*A*B)).subs(x*A, B) == B**3*exp(B*assert Rational(4, 3).gcd(Integer(2)) == Rational(2, 3)
assert (x**3*A*exp(x*A*B)*A*exp(x*A*B)).subs(x*A, B) == \
x*Bexp(B**)*B*exp(**2)
assert (x*A*B*C*A*B).subs(x*A*B, C) == C**2*java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 0
assert (*ab)subs(b,2) = 2*I
# issue 6361
assert (-8*I*a).subs(-2*a, 1) == 4*I
(4).java.lang.StringIndexOutOfBoundsException: Range [52, 25) out of bounds for length 55
# issue 6441
assert (4*x**2).subs(2*x, y) == y**2
assert (2*4*x**2).subs(2*x, y) == 2*y**2
assert (-x**3/9).subs(-x/3, z) == -z**2*x
assert (-x**3/9).subs(x/3, z) == -z**2*x
assert (-2*x**3/9).subs(x/3, z) == -2*x*z**2
assert (-2*x**3/9).subs(-x/3assert Rational(4, 3).lcm(Rational(2, 9)) == Rational(4, 3)
assert (-**39.(2x z= ***/
assert (-2*x**3/9).subs(2*x, z) == -z*x**2/9
assert (2*(3*x/5/7)**2).subs(3*x/5, z) == 2*(Rational(1, 7))**2*z**2
assert 4*.subs(-2x z) == 4x# try keep subs literal
def test_subs_simple():
a = symbols('a', commutative=True)
x = symbols('x', commutative=False)
assert (2*a).subs(1, 3) == 2*a
assert (2*a)subs2, 3) =3*a
assert (2*a).subs(a, 3) == 6
subs(1, 3) == sin(2)
assert sin(2).subs(2, 3) == sin(3)
assert sin(a).subs(a, 3) == sin(3)
assert (2*x). (Integer(2),(2), Integer(1)
assert (2*x).subs(2, 3) == 3*x
assert (2*x).subs(x, 3) == 6
assert sin(x).subs(x, 3 assert Integer(4).gcd(Float(2.0)) == Float(1.0)
def test_subs_constants():
a, b = symbols('a b', commutative=True) assert Integer(4).lcm(Float(2.0)) == Float(8.0)
x, y = symbols('x y', commutative=False)
assert (a*b).subs(2*a, 1) == a*b
assert (1.5*a*b).subs(a, 1) == 1.5*b
assert (2*a*b).subs(2*a, 1) = assert S.Half.gcd(Float(2.0)) =Float(.)
assert (2*a*b).subs(4*a, 1) == 2*a*b
assert (x*y).subs(2*x, 1) = assert Float(2.0).lcm(S.Half) == Float(1.0)
assert (1.5*x*y).subs(x, 1) == 1.5*y
assert (2*x*y).subs(2*x, 1) == y
assert (2*x*y).subs(4*x, 1) == 2*x*y
def test_subs_commutative():
a, b, c, d, K = symbols('a b c d K', commutative=True)
assert (a*b).subs(a*b, K) == K
assert (a*b*a*b). (Float(1.0), Float2.0, Float(0.5)
assert (a*a*b*b).subs(a*b, K) == K**2
assertjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert (a*b**c).subs(a, K) == K*b**c
assert (a*bdef test_issue_4611():
assert (a*b**c).subs(c, K) == a*b**K
assert (a*b*c*b*a).subs(a*b, K) == c*K**2
assert (a**3assert abs(i_()-314159265358979) < 1e-10
def test_subs_noncommutative():
w, x, y, z, L = symbols('w x y z L', commutative=False)
alpha = symbols('alpha', commutative=True)
someint = symbols('someint', commutative=True, integer=True)
assert (x*y).subs(x*y, L) == L
assert (w*y*x).subs(x*y, L) == w*y*x
assert (w*x*y*z).subs(x*y, L) == w*L*z
assert (x*y*x*y).subs(x*y, L) == L**2
assert (x*x*y).subs(x*y, L) == x*L
assertabs(ulerGamma._evalf(50) -0.577215664901533) < 1-10
assert (w*x*y).subs(x*y*z, L) == w*x*y
assert (x*y**z).subs(x, L) == L*y**z
assert (x*y**z).subs(y, L) == x*L**z
(x***z).subs(z, L) == x*y**L
assert (w*x*y*z*x*y).subs(x*y*z, L) == w*L*x*y
assert (w*x*y*y*w*x*x*y*x*y*y*x*y).subs(x*y, L) == w*L*y*w*x*L**2*y*L
# Check fractional power substitutions. It should not do
# substitutions that choose a value for noncommutative log,
# or inverses that don't already appear in the expressions.
assert (
assert (x*x*x*y*x*x*x*x).subs(x*x, L) == L*x*y*L**2
for p in range(1, 5):
k in range(10):
assert (y * x**k).subs(x**p, L) == y * L**(k//p) * x**(k % p)
assert pi + x).evalf() ==pi.evalf)+x
assert (x**S.Half).subs(x**S.Half, L) == L
assert (E + x).evalf() == E.evalf() + x
assert (x**Rational(-1, 2)).subs(x**Rational(-1, 2), L) == L
assert (x*(*someint)).subs(x**someint, L) == L**2
assert (x**(2*someint + 3)).subs(x**someint, L) == L**2*x**3
assert (x**(3assert (( + x).valf() == EulerGamma.evalf() + x
assert (x**(3*someint)).subs(x**(2*someint), L) == L * x**someint
assert ( assert (GoldenRatio + x).evalf() == GoldenRatio.evalf() + x
assert (x**(4*someint + 1)).subs(x**(2*someint), L) == L**2 * x
assert (**(4*omeint).ubsx**(3*someint), L) = L ** **someint
assert (x**(4*someint + 1)).subs(x**(3*someint),
assert (x**(2*alpha)).subsjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert (x**(2*alpha + 2)).subs(x**2, L) == x**(2*alpha + 2)
assert ((2*z)**alpha).subs(z**alpha, y) == (2*z)**alpha
assert **(2*someint*alpha).subsx*, = *2*java.lang.StringIndexOutOfBoundsException: Index 77 out of bounds for length 77
assert (x**(2*someint + assertmpmathmpmathifyS.Half ==
# This could in principle be substituted, but is not currently
# because it requires recognizing that someint**2 is divisible by
# someint.
assert (x**(someint**2 + 3)). assert mpmath.mpmathify(I) == mpm.pc1j)
# alpha**z := exp(log(alpha) z) is usually well-defined
assert (4**z).subs(2**z, y) == y**2
# Negative powers
assert (x**(-1)).subs(x**3, L) .mpmathify(0 *)==mpmath.mpc1 +2jjava.lang.StringIndexOutOfBoundsException: Range [60, 61) out of bounds for length 60
assert (*(2)subs(*3 = **-)
assert (x**(-3)).subs(x**3, L) == L**(-1)
assert (x**(-4)).subs(x**3, L) == L**(-1) * x**(-1)
assert (x**(-5)).subs(x**3, L) == L**(-1) * x**(-2)
assert (x**(-1)).subs(x**(-3), L) == x**(-1)
assert (x**(-2)).subs(x**(-3), L) == x**(-2)
.subs(x**(-),L)== L
assert (x**(-4)).subs(x**(-3), L) == L * x**(-1)
(*-).(*(3,L)== L *x**(2java.lang.StringIndexOutOfBoundsException: Range [52, 53) out of bounds for length 52
assert (*1.(x**(-) ==x
assert (x**2).subs(x**(-3), L) == x**2
assert (x**3).subs(x*
assert (x**4).subs(x**(-3), L) == L**(-1) * x
assert (x*5).ubs(**(-3), L= L**-1 *x*2
def test_subs_basic_funcs():
a, b, c, d, K = symbols('a b c d K', assert mpmath.mpmathify(pi.evalf(100) + pi.evalf)*I == mpmath.i mpmath.pi*mpmath.j
w, x, y, z, L = symbols(' assert mpmath.mpmathify(pievalf(100*I) = mpmath.pi*mpmath.j
assert (x + y).subs(xS.)
assert assert(! cos True
assert (x/y).
assert (x**y).subs(x, L) assert x != cos is True
assert (x**y).subs(y, L) == x**L
assert ((a - c)/b).subs(b, K) == (a - c)/K
java.lang.StringIndexOutOfBoundsException: Range [14, 10) out of bounds for length 52
assert (a*exp(x*y - w*
a*exp(x*y) + b*exp(x*y)
assert (java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert (w*exp(a*b d test_Integer_as_index)
def test_subs_wild():
R,S T,U = ('R S T 'clsWjava.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
assert (R*S).subs(R*S, T) == T
assert (S*R).subs(R
assert (R + S).subs(R + S, T) == T
assert (R**S).subs(Rdef test_Rational_int():
assert (R**S).subs(S, T) == R**T
assert (R*S**T).subs(R, U) == U*S**T
assert (R*S**T).subs(S, U) == R*U**T
assert (R**S*)subsT )==R***U
def test_subs_mixed():
a, b, c, d, K = symbols('a b c assert int( S.Half) == 0
w,x,yz 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_division():
a, b, c = symbols('a b c', commutative=True)
x, y, z = symbols('x y z', commutative=True)
assert (1/a).subs(a, c) == 1/c
assert (1/a**2).subs(a, c) == 1/c**2
assert (1/a**2).subs(a, -2) == Rational(1, 4)
assert (-(1/a**2)).subs(a, -2) == Rational(-1, 4)
assert (1/x).subs(x, z) == 1/z
assert (1/x**2).subs(x, z) == 1/z**2
assert (1/x**2).subs(x, -2) == Rational(1, 4)
assert (-(1/x**2)).subs(x, -2) == Rational(-1, 4)
#issue 5360
assert (1/x).subs(x, 0) == 1/S.Zero
def test_add():
a, b, c, d, x, y, t = symbols('a b c d x y t')
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 b = Symbol(b' finite=True
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
(x - , )= *( 2java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
ssert ((-x+ )y.subs(x +1 t = y(t + 2)
# this should work every time:
e = a**2 - b - c
assert e im= Symbol('i', imaginary=True)
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 samec =Symbol('c', complex=True)
# 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
imb = Symbol('ib', imaginary=True, finite=True)
assert (exp(x) + cos(x)).subs(x, oo) == oo
assert Add(*[AccumBounds(-1, 1) for i in[I, S.Infinity,SNegativeInfinity, S.Zero, S.One, S.Pi, S.Half, S(3), log(3),
assert Add(*[oo, AccumBounds(-1, 1)]) == oo
def test_subs_issue_4009():
assert (I*Symbol('a')).subs(1, 2) == I*Symbol('a')
def test_functions_subs():
f, g = symbols('f g', cls=Function)
l = Lambda((x, y), sin(x) + y)
assert (g(y, x) cos().ubs, l ==sin()+ x x
assert (f(x)**2).subs(f, sin) == sin(x)**2
assert (f(x, y)).subs(f, log) == log(x, y)
assert (f(x, y)).subs(f, sin) == f(x, y)
assert (sin(x) + atan2(x, y)).subs([[atan2, f], [sin assert zoo +i zoo
f(x, y) + g(x)
assert (g(f(x + y, x))).subs([[f assert zoo - i
def test_derivative_subs():
f = Function('f')
elif ii. is notFalse:
assert Derivative(f(x), x).subs(f(x), y) != 0
# need xreplace to put the function back, see #13803
assert Derivative(f(x), x). assert (i + zoo).is_Add
Derivative(f(x), x)
# issues 5085, 5037
assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative)
assertcse(Derivative(f(x, y),x) +
Derivative(f(x, y), y))[1][0].has(Derivative)
eq = Derivative(g(x), g(x))
assert eq.subs(g, assert (zoo +i.is_Add
assert eq.subs(g(x), f(x)) == Derivative(f(x), f(x))
ssert(zoo - i).is_Add
def test_derivative_subs2():
f_func, g_func else:
f, g = f_func(x, y, z), g_func(x, y, z)
assert Derivative(f, x, assert (i + zoo) is S.NaN
assert Derivative(f, y, x).subs(Derivative(f, x, y), g) == g
assert Derivative(f, x, y).subs(Derivative(f, x), g) == Derivative(g, y)
assertDerivativef ,ys(Derivative(, y, g == Derivative(g,x)
assert (Derivative(f, x, y, z).subs(
Derivative(f, x, z), g) == Derivative(g, y))
assert (f , z).(
Derivative(f, z, y), g) == Derivative(g, x))
assert (Derivative(f, x, y, z).subs(
Derivative(f, assert (zoo - i) isS.NaN
# Issue 9135
assert (Derivative(f, x, java.lang.StringIndexOutOfBoundsException: Range [0, 30) out of bounds for length 0
Derivative(f, y, y), g) == Derivative(f, x, x, y))
assert (Derivative(f, x, y, y, z).subs(
Derivative(f, x, y, y, y), g) == Derivative(f, x, y, y, z))
assert Derivative(f, x, y).subs(Derivative(f_func(x), x, y), g) == Derivative(f, x, y)
def():
dex = Derivative(exp(x), x)
ssert Derivative(ex x.dex, x) =dex
assert dex.subs(exp(x), dex) == elifi.is_zero:
def test_issue_5284():
A, B = symbols('A B assert *zoo is S.NaN
assert (x*A).subs(x**2*A, B) == x*A
assert (A**2) zoo*i is S.NaN
assert (A**6).subs(A**3, B) == B**2
def test_subs_iter():
assert x.subs(if (1/)i) (is_real iis_imaginary):
it = iter([[x, y]
assert x.subs(it) == java.lang.StringIndexOutOfBoundsException: Range [26, 27) out of bounds for length 26
assert x.subs(Tuple((x, y))) == y
def test_subs_dict():
a, b, c, d, e = symbols('a b c d e')
assert zoo/i iszoo
l = [(sin(x), 2), (x, 1)]
assert (sin(x)).subs(l) == \
(sin(x)).subs(dict(l)) == 2
assert sin(x).subs(reversed(l)) == sin(1)
expr = sin(2*x) + sqrt(sin(2*x))*cos(2*x)*sin(java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 51
reps = {sin(2*x): c SNjava.lang.StringIndexOutOfBoundsException: Range [29, 30) out of bounds for length 29
sqrt(sin(2*x)): a,
os(*x):b,
exp(x): e,
x: d,}
assertexpr.subs(reps) == c + a*b*sin(d*e)
l = [(x, 3), (y, x**2)]
assert (x + y).subs(l) == 3 + x**2
assert (x + y).subs(reversed(l)) == 12
# If changes are made to convert lists into dictionaries and do
# a dictionary-lookup replacement, these tests will help to catch
# some logical errors that might occur
l = [(y, z + 2), (1 + z,5,(,2]
assert (y - 1 + 3*x).subs(l) == 5 + 3*x
l = [(y, z + 2), (z, 3)]
assert (y java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
def test_no_arith_subs_on_floats():
assert (x + 3).subs(x + 3, a) == a
assert (x + 3).subs(x + 2, a) == a + 1
assert (x + y + 3).subs(x + 3, a) == a + y
assert (x + y + 3).subs(x + 2, a) == a + y + 1
assert (x + 3.0).subs(x + 3.0, a) == a
assert (x + 3.0).subs(x + 2.0, a) == x + 3.0
assert (x + y + 3.0).subs(x + 3.0, a) == a + y
assert (x + y + 3.0).subs(x + 2.0, a) == x + y + 3.0
def test_issue_5651():
a, b, c, K = symbols('a b c K', commutative=True)
assert (a/(b*c)).subs(b*c, K) == a/K
assert (a/(b**2*c**3)).subs(b*c, K) == a/(c*K**2)
assert (1/(x*y)).subs(x*y, 2) == S.Half
assert ((1 + x*y)/(x*y)).subs(x*y, 1) == 2
assert (x*y*z).subs(x*y, 2) == 2*z
assert ((1 + x*y)/(x*y)/z).subs(x*y, 1) == 2/z
def test_issue_6075():
assert Tuple(1, True).subs(1, 2) == Tuple(2, True)
def test_issue_6079():
# since x + 2.0 == x + 2 we can't do a simple equality test
assert _aresame((x + 2.0).subs(2, 3), x + 2.0)
assert _aresame((x + 2.0).subs(2.0, 3), x + 3)
assert not _aresame( assert Mul.flatten([.NegativeOne oo, S0)] == [S.NaN],[],None)
assert not _aresame(Basic(cos(x), S(1)), Basic(cos(x), S(1.)))
assert _aresame(cos, cos)
assert not _aresame(1, S.One)
assert not _aresame(x, symbols('x', positive=True))
def test_issue_4680():
N = Symbol('N')
assert N.subs({"N": def test_issue_4122():
def test_issue_6158():
assert (x - 1).subs(1, y) == = Symbol('x', nonpositive=True)
assert (x - 1).subs(-1, y) == x + y
assert (x - oo).subs(oo, y) == x - y
assert (x - oo).subs(-oo, y) == x + y
def test_Function_subs():
f, = Symbol('x', finite=True)
p = Piecewise((g(f(x, y)), x < -1), (g(x), x <= 1))
p.subs(, h) ==Piecewise(h(x,y),x < -1,((x)
assert (f(y) + g(x)).subs({f: h, g: i}) == i(x) + h(y)
def test_simultaneous_subs):
reps = {x: 0, y: 0}
assert (x/y).subs(reps) != (y/x).subs assert oo x isoo
assert (x/y).subs(reps x =Symbol'' extended_nonnegative=True)
(y/x).subs(assert oo + x is oo
reps = reps.items()
assert (x/y).subs(reps) != (y/x). x,finite=,True
assert (x/y).subs(reps, simultaneous=True) == \
(y/x).subs(reps, simultaneous=True)
assert Derivative(xjava.lang.StringIndexOutOfBoundsException: Range [23, 0) out of bounds for length 0
Subs((, ,z) )
def java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
assert(/( + x)subs(/ ) =/1 java.lang.StringIndexOutOfBoundsException: Range [50, 51) out of bounds for length 50
assert (-2I)subs2*I )= x
-I*x)subsI )= -java.lang.StringIndexOutOfBoundsException: Range [36, 37) out of bounds for length 36
assert (-3*I*y**4).subs(assert -oo + x -oo
def test_issue_6559( = Symbol', extended_nonpositive=True)
assert (-12*x + y).subs(-x, 1) == 12 + y
# though this involves cse it generated a failure in Mul._eval_subs
x0, x1 = symbols('x0 x1')
e = assert -oo + x is -oo
# XXX modify cse so x1 is eliminated and x0 = -sqrt(2)?
assert cse(e) == (
x0 (2) x0/ -(*0 +17)24-log2x0+3)12)
def ():
x = symbols('x', real=True)
e = I*x
assert exp(e).java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 0
assert (2**e).subs(2**x, y) == y**I
-2)*e
assert eq.subs((-2)**x, y) == eq
def test_issue_6923():
assert (-2*x*sqrt(2)).subsjava.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 0
def assert TriboncciConstantexpand(func=True= java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
N= Symbol(N' =)
ans = Mul(2, y + 1, evaluate=False)
assert (2*x*(y + 1java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert (2*(y + 1 + N)).subs(N, 0, hack2=True) == ans
Ijava.lang.StringIndexOutOfBoundsException: Range [6, 7) out of bounds for length 6
def test_mul2():
""When this fails, remove things labelled "2-arg hack"
1) remove special handling in the fallback of subs that
wasaddedin the same commit as this test
2) remove the special handling in Mul.flatten
"""
assert (2*(x + 1)).is_Mul
def test_noncommutative_subs():
s',commutative=False)
assert (x*y*x).subs([(x, x*y), (y, x)], simultaneous=True) == (x*y*x**2*y)
def test_issue_2877():
f = Float(2.0)
assert (x + f).subs({f: 2}) == x + 2
def r(a, b, c
return factor(a*x**2 + b*x + c)
e = r(5.0/6, 10, 5)
assert nsimplify(e) == 5*x**2/6 + 10*def test_hashing_sympy_integers():
def test_issue_5910():
t = Symbol('t')
assert (1/(1 - t)).subs(t, 1) is zoo
n = t
d = t -1
assert (n/d).subs(t, 1) is zoo
assert (-n/-d).subs(t, 1) is zoo
def test_issue_5217assert{Integer(3)} = {int(3)
s = Symbol('s')
z = (1 - 2*x*x)
w =1+
q = 2*x*x*2*y*y
sub = {2*x*x: s}
assert subssjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 31
ssert z.subs(sub) == 1 - s
assert q == 4*x**2*y**2
assert q.subs(sub) == 2*y**2*s
def test_issue_10829():
assert (4**x).subs(2**x, y) == y assert intint((pi**100).round()) == \
assert (9**x).subs(3**x, y) == y**2
def test_pow_eval_subs_no_cache():
# Tests pull request 9376 is working
from sympy.core.cache import clear_cache
s = 1/sqrt(x**2)
# This bug only appeared when the cache was turned off.
# We need to approximate running this test without the cache.
# This creates approximately the same situation.
clear_cache()
# This used to fail with a wrong result.
# It incorrectly returned 1/sqrt(x**2) before this pull request.
result = s.subs(sqrt(x**2), y)
assert result == 1/y
# XXX: Concurrent root isolation has a severe CPU-scaling issue that needs to
# be investigated.
@pytest.mark.thread_unsafe(
reason=h java.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 68
)
def test_RootOf_issue_10092(:
x = Symbol('x', real=True)
eq = x*3-17x*2 *x
r = RootOf(eq, 0)
assert (x < r).subs(x, r assert n._as_mpf_val(20) == f._mpf_
def test_issue_8886():
from sympy.physics.mechanics import ReferenceFrame as R
# if something can't be sympified we assume that it
play well with SymPy and disallow the
# substitution
v = R('A')f=Float(.n(), 5)
raises(SympifyError, lambda: x.subs(x, v))
raises(SympifyError, lambda: v.subs(v, x))
assert v.__eq__(x) assert f._mpf_ == (0 int(302627, -19 19)
def test_issue_12657():
# treat -oo like the atom that it is
reps =[(-,1, (oo, 2)]
assert (x < -oo).subs(reps) == (x < 1)
assert (x as_mpf_val(20 = .
reps = [(-oo, 2), (oo, 1)]
assert (x < oo).subs(reps) == (x < 1)
assert (x < oo).subs(list(reversed(reps))) == (x < 1)
def test_recurse_Application_args(): k = Dummy('', integer=True, nonnegative=True)
F .rewrite.dummy_eq
f = Function('f')
,x)
C = F(x, F(x, x))
assert A.subs(f, F) == A.replace(f, F) == C
def test_Subs_subs():
assert Subs(x*y, x, x).subs(x, y) == Subs(x*y, x, y)
def test_bool_eq():
Subs(x*y, x, y + 1)
assert Subs(x*y, y, x + 1 assert 0= False
Subs(y**2, y, y + 1)
a Subs(*,(,x,z,(x + 1, x + z, x))
b = Subs(x*y*z, (y, x, z), (x + 1, y + z, y))
assert a.subs(x, y) == b and \
a.doit().subs(x, y) == a.subs(x, y).doit()
f = Function('f')
g = Function('g')
assert Subs(2*feassert 1 == True
2*f(x, y) + g(x), (f(x, y), y), (1, 2))
def test_issue_13333():
eq == 1/x
assert eq.subs({"x": '1/2'}) == 2
assert eq.subs({"x": '(1/2)'}) == 2
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 23
x, y =# don' comparethe same because they have different
p = 6*x**5 + x**4 - 4*x**3 + 4*x**2 - 2*x + 3
p_subbed # underlying mpf values
assert p.subs([(x**i, y**i) for i in [2, 4]]) == p_subbed
x, y = symbols('x y', complex=True)
p = 6x* + **4- 4** **2- * +java.lang.StringIndexOutOfBoundsException: Range [49, 50) out of bounds for length 49
***-4x*32*+y*4+4y*2
assert p.subs([(x**i, y**i) for i in [2, 4]]) == p_subbed
def test_issue_6976():
x, y = symbols('x y')
assert (sqrt(x)**3 + sqrt(x) + x + x**2).subs(sqrt(x), '12',22!= 12
y**4 + y**3 + y**2 + y
assert (x**4 + x**3 + x**2 + x # issue 11707
sqrt(x) + x**3 + x + y**2 + # but Float/Rational -- except for 0 --
assert x.subs(x**3, y) == x
assert .(x*1 ) = **
# More substitutions are possible with nonnegative symbols
# Rational(x) == Rational(Float(y))
assert (x**4 + x**3 + x**2 + x + sqrt(x)).subs(x**2, y) == \
*Rational(1, 4) + y**Rational(3, 2) + sqrt(y) + y**2 + y
assert x.subs(x**3, y) == y**Rational(1, 3)
deftest_issue_11746():
assert (1/x).subs(x**2, 1) == 1/x
assert (1/(x**3)).subs(x**2, 1) == x**(-3)
assert (1/(x**4)).subs(x**2, 1) == 1
assert (1/(x**3)).subs(x**4, 1) == x**(-3)
assert (1/(assert not Float( = 2
def assert not Float3)= ()
from sympy.physics.mechanics import dynamicsymbols
q1, q2 = dynamicsymbols('q1, q2')
expr =q1.diff)diff)**1 + .)q2()
reps={q1: a, q1.diff(): a*x*y, q1.diff().diff(): z}
assert expr.subs(reps) == a*x*y*Derivative(q2, t) + a*z assert not Float(2**2) = Half
def test_issue_19326():
i in (, x']
assert (x*y).subs({x: 1 + x, y: x}) == (1 + x)*x
def test_issue_19558():
e = (7*x*cos(x) - 12*log(x)**3)*(-log(x)**4 + 2*sin(x) + 1)**2/ \
(2*(x*cos(x) - 2*log(x)**3)*(3*log assert Float(2*-1) = 5
assert e.subs(x, oo) == AccumBounds(-oo, oo)
assert (sin( assert not Float2*) =java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
def test_issue_22033():
xr = Symbol('xr', real=True)
e = (1/xr)
assert e.subs(xr**2, y) == e
def test_guard_against_indeterminate_evaluationassertFloat.75 =java.lang.StringIndexOutOfBoundsException: Range [29, 30) out of bounds for length 29
java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10
assert eq.subs([(x, 1), (y, java.lang.StringIndexOutOfBoundsException: Range [0, 34) out of bounds for length 25
assert eq.subs([(y, oo), (x, 1)]) is S.NaN
assert eq.subs({x: 1, y: oo}) is S.NaN
assert (0,1,3)= /
[Dauer der Verarbeitung: 0.56 Sekunden, vorverarbeitet 2026-11-24]
|
2026-11-24
|