def test_root(): n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == cbrt(2) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(4, 2, 1, evaluate=False) == -Pow(4, Rational(1, 2), evaluate=False) assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == cbrt(x) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_minpoly_fraction_field(): assert minimal_polynomial(1/x)(y) == x*y - 1 assert minimal_polynomial(1/(x + 1))(y) == x*y + y - 1 assert minimal_polynomial(sqrt(x))(y) == y**2 - x assert minimal_polynomial(sqrt(x), method='groebner')(y) == y**2 - x assert minimal_polynomial(sqrt(x + 1))(y) == y**2 - x - 1 assert minimal_polynomial(sqrt(x)/x)(y) == x*y**2 - 1 assert minimal_polynomial(sqrt(2)*sqrt(x))(y) == y**2 - 2 * x assert minimal_polynomial(sqrt(2) + sqrt(x))(y) == \ y**4 - 2*x*y**2 - 4*y**2 + x**2 - 4*x + 4 assert minimal_polynomial(sqrt(2) + sqrt(x), method='groebner')(y) == \ y**4 - 2*x*y**2 - 4*y**2 + x**2 - 4*x + 4 assert minimal_polynomial(cbrt(x))(y) == y**3 - x assert minimal_polynomial(cbrt(x) + sqrt(x))(y) == \ y**6 - 3*x*y**4 - 2*x*y**3 + 3*x**2*y**2 - 6*x**2*y - x**3 + x**2 assert minimal_polynomial(sqrt(x)/z)(y) == z**2*y**2 - x assert minimal_polynomial(sqrt(x)/(z + 1))(y) == z**2*y**2 + 2*z*y**2 + y**2 - x assert minimal_polynomial(1/x) == PurePoly(x*y - 1, y) assert minimal_polynomial(1/(x + 1)) == PurePoly((x + 1)*y - 1, y) assert minimal_polynomial(sqrt(x)) == PurePoly(y**2 - x, y) assert minimal_polynomial(sqrt(x) / z) == PurePoly(z**2*y**2 - x, y) # this is (sqrt(1 + x**3)/x).integrate(x).diff(x) - sqrt(1 + x**3)/x a = sqrt(x)/sqrt(1 + x**(-3)) - sqrt(x**3 + 1)/x + 1/(x**Rational(5, 2) * (1 + x**(-3))**Rational(3, 2)) + 1/(x**Rational(11, 2)*(1 + x**(-3))**Rational(3, 2)) assert minimal_polynomial(a)(y) == y pytest.raises(NotAlgebraic, lambda: minimal_polynomial(exp(x)))
def test_nfloat(): x = Symbol("x") eq = x**Rational(4, 3) + 4 * cbrt(x) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * cbrt(x)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**Rational(4, 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big * x), Float_big * x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue sympy/sympy#6342 lamda = Symbol('lamda') f = x * lamda + lamda**3 * (x / 2 + Rational(1, 2)) + lamda**2 + Rational( 1, 4) assert not any(a[lamda].free_symbols for a in solve(f.subs({x: -0.139}))) # issue sympy/sympy#6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue sympy/sympy#7122 eq = cos(3 * x**4 + y) * RootOf(x**5 + 3 * x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_nfloat(): x = Symbol("x") eq = x**Rational(4, 3) + 4*cbrt(x)/3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0/3)*cbrt(x)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**Rational(4, 3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big*x), Float_big*x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue sympy/sympy#6342 lamda = Symbol('lamda') f = x*lamda + lamda**3*(x/2 + Rational(1, 2)) + lamda**2 + Rational(1, 4) assert not any(a[lamda].free_symbols for a in solve(f.subs({x: -0.139}))) # issue sympy/sympy#6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue sympy/sympy#7122 eq = cos(3*x**4 + y)*RootOf(x**5 + 3*x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_sympyissue_3449(): # test if powers are simplified correctly # see also issue sympy/sympy#3995 assert cbrt(x)**2 == x**Rational(2, 3) assert (x**3)**Rational(2, 5) == Pow(x**3, Rational(2, 5), evaluate=False) a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert (a**2)**b == (abs(a)**b)**2 assert sqrt(1 / a) != 1 / sqrt(a) # e.g. for a = -1 assert cbrt(a**3) != a assert (x**a)**b != x**(a * b) # e.g. x = -1, a=2, b=1/2 assert (x**.5)**b == x**(.5 * b) assert (x**.5)**.5 == x**.25 assert (x**2.5)**.5 != x**1.25 # e.g. for x = 5*I k = Symbol('k', integer=True) m = Symbol('m', integer=True) assert (x**k)**m == x**(k * m) assert Number(5)**Rational(2, 3) == cbrt(Number(25)) assert (x**.5)**2 == x**1.0 assert (x**2)**k == (x**k)**2 == x**(2 * k) a = Symbol('a', positive=True) assert (a**3)**Rational(2, 5) == a**Rational(6, 5) assert (a**2)**b == (a**b)**2 assert (a**Rational(2, 3))**x == (a**(2 * x / 3)) != (a**x)**Rational(2, 3)
def test_sympyissue_from_PR1599(): n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True) assert simplify(I*sqrt(n1)) == -sqrt(-n1) assert (powsimp(sqrt(n1)*sqrt(n2)*sqrt(n3)) == -I*sqrt(-n1)*sqrt(-n2)*sqrt(-n3)) assert (powsimp(root(n1, 3)*root(n2, 3)*root(n3, 3)*root(n4, 3)) == -cbrt(-1)*cbrt(-n1)*cbrt(-n2)*cbrt(-n3)*cbrt(-n4))
def test_sympyissue_7638(): f = pi / log(sqrt(2)) assert ((1 + I)**(I * f / 2))**0.3 == (1 + I)**(0.15 * I * f) # if 1/3 -> 1.0/3 this should fail since it cannot be shown that the # sign will be +/-1; for the previous "small arg" case, it didn't matter # that this could not be proved assert (1 + I)**(4 * I * f) == cbrt((1 + I)**(12 * I * f)) assert cbrt((1 + I)**(I * (1 + 7 * f))).exp == Rational(1, 3) r = symbols('r', extended_real=True) assert sqrt(r**2) == abs(r) assert cbrt(r**3) != r assert sqrt(Pow(2 * I, Rational(5, 2))) != (2 * I)**Rational(5, 4) p = symbols('p', positive=True) assert cbrt(p**2) == p**Rational(2, 3) assert NS(((0.2 + 0.7 * I)**(0.7 + 1.0 * I))**(0.5 - 0.1 * I), 1) == '0.4 + 0.2*I' assert sqrt(1 / (1 + I)) == sqrt((1 - I) / 2) # or 1/sqrt(1 + I) e = 1 / (1 - sqrt(2)) assert sqrt(e) == I / sqrt(-1 + sqrt(2)) assert e**Rational(-1, 2) == -I * sqrt(-1 + sqrt(2)) assert sqrt((cos(1)**2 + sin(1)**2 - 1)**(3 + I)).exp == Rational(1, 2) assert sqrt(r**Rational(4, 3)) != r**Rational(2, 3) assert sqrt((p + I)**Rational(4, 3)) == (p + I)**Rational(2, 3) assert sqrt((p - p**2 * I)**2) == p - p**2 * I assert sqrt((p + r * I)**2) != p + r * I
def test_python_functions(): # Simple assert python((2*x + exp(x))) in "x = Symbol('x')\ne = E**x + 2*x" assert python(sqrt(2)) == 'e = sqrt(2)' assert python(cbrt(2)) == 'e = 2**Rational(1, 3)' assert python(sqrt(2 + pi)) == 'e = sqrt(2 + pi)' assert python(cbrt(2 + pi)) == 'e = (2 + pi)**Rational(1, 3)' assert python(root(2, 4)) == 'e = 2**Rational(1, 4)' assert python(Abs(x)) == "x = Symbol('x')\ne = Abs(x)" assert python( Abs(x/(x**2 + 1))) in ["x = Symbol('x')\ne = Abs(x/(1 + x**2))", "x = Symbol('x')\ne = Abs(x/(x**2 + 1))"] # Univariate/Multivariate functions f = Function('f') assert python(f(x)) == "x = Symbol('x')\nf = Function('f')\ne = f(x)" assert python(f(x, y)) == "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x, y)" assert python(f(x/(y + 1), y)) in [ "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(1 + y), y)", "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(y + 1), y)"] # Nesting of square roots assert python(sqrt((sqrt(x + 1)) + 1)) in [ "x = Symbol('x')\ne = sqrt(1 + sqrt(1 + x))", "x = Symbol('x')\ne = sqrt(sqrt(x + 1) + 1)"] # Nesting of powers assert python(cbrt(cbrt(x + 1) + 1)) in [ "x = Symbol('x')\ne = (1 + (1 + x)**Rational(1, 3))**Rational(1, 3)", "x = Symbol('x')\ne = ((x + 1)**Rational(1, 3) + 1)**Rational(1, 3)"] # Function powers assert python(sin(x)**2) == "x = Symbol('x')\ne = sin(x)**2"
def test_sympyissue_4823(): e = cbrt(-1) assert e.conjugate().evalf() == e.evalf().conjugate() e = (Rational(-2, 3) - cbrt(Rational(-29, 54) + sqrt(93) / 18) - 1 / (9 * cbrt(Rational(-29, 54) + sqrt(93) / 18))) assert e.conjugate().evalf() == e.evalf().conjugate() e = 2**I assert e.conjugate().evalf() == e.evalf().conjugate()
def test_pow_sympyissue_4823(): e = cbrt(-1) assert e.conjugate().evalf() == e.evalf().conjugate() e = (Rational(-2, 3) - cbrt(Rational(-29, 54) + sqrt(93)/18) - 1/(9*cbrt(Rational(-29, 54) + sqrt(93)/18))) assert e.conjugate().evalf() == e.evalf().conjugate() e = 2**I assert e.conjugate().evalf() == e.evalf().conjugate()
def test_basic1(): assert limit(x, x, oo) == oo assert limit(x, x, -oo) == -oo assert limit(-x, x, oo) == -oo assert limit(x**2, x, -oo) == oo assert limit(-x**2, x, oo) == -oo assert limit(x*log(x), x, 0, dir="+") == 0 assert limit(1/x, x, oo) == 0 assert limit(exp(x), x, oo) == oo assert limit(-exp(x), x, oo) == -oo assert limit(exp(x)/x, x, oo) == oo assert limit(1/x - exp(-x), x, oo) == 0 assert limit(x + 1/x, x, oo) == oo assert limit(x - x**2, x, oo) == -oo assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1 assert limit((1 + x)**oo, x, 0) == oo assert limit((1 + x)**oo, x, 0, dir='-') == 0 assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo assert limit(y/x/log(x), x, 0) == -oo*sign(y) assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo limit(Sum(1/x, (x, 1, y)) - log(y), y, oo) limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo) assert limit(gamma(1/x + 3), x, oo) == 2 assert limit(nan, x, -oo) == nan assert limit(O(2)*x, x, nan) == nan assert limit(sin(O(x)), x, 0) == 0 assert limit(1/(x - 1), x, 1, dir="+") == oo assert limit(1/(x - 1), x, 1, dir="-") == -oo assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo assert limit(1/(5 - x)**3, x, 5, dir="-") == oo assert limit(1/sin(x), x, pi, dir="+") == -oo assert limit(1/sin(x), x, pi, dir="-") == oo assert limit(1/cos(x), x, pi/2, dir="+") == -oo assert limit(1/cos(x), x, pi/2, dir="-") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="+") == oo assert limit(1/tan(x**3), x, cbrt(2*pi), dir="-") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="+") == -oo assert limit(1/cot(x)**3, x, 3*pi/2, dir="-") == oo # approaching 0 # from dir="+" assert limit(1 + 1/x, x, 0) == oo # from dir='-' # Add assert limit(1 + 1/x, x, 0, dir='-') == -oo # Pow assert limit(x**(-2), x, 0, dir='-') == oo assert limit(x**(-3), x, 0, dir='-') == -oo assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I assert limit(x**2, x, 0, dir='-') == 0 assert limit(sqrt(x), x, 0, dir='-') == 0 assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi)) assert limit((1 + cos(x))**oo, x, 0) == oo assert limit(x**2, x, 0, dir='real') == 0 assert limit(exp(x), x, 0, dir='real') == 1 pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
def test_factor_terms(): A = Symbol('A', commutative=False) assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \ 9*x*y + 9*x + _keep_coeff(Integer(3), x + 1)**_keep_coeff(Integer(2), x + 1) + 9 assert factor_terms(9*(x + x*y + 1) + 3**(2 + 2*x)) == \ _keep_coeff(Integer(9), 3**(2*x) + x*y + x + 1) assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \ 9*3**(2*x)*(a + 1) assert factor_terms(x + x*A) == \ x*(1 + A) assert factor_terms(sin(x + x*A)) == \ sin(x*(1 + A)) assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \ _keep_coeff(Integer(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1) assert factor_terms(x + (x*y + x)**(3*x + 3)) == \ x + (x*(y + 1))**_keep_coeff(Integer(3), x + 1) assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \ x*(a + 2*b)*(y + 1) i = Integral(x, (x, 0, oo)) assert factor_terms(i) == i # check radical extraction eq = sqrt(2) + sqrt(10) assert factor_terms(eq) == eq assert factor_terms(eq, radical=True) == sqrt(2) * (1 + sqrt(5)) eq = root(-6, 3) + root(6, 3) assert factor_terms(eq, radical=True) == cbrt(6) * (1 + cbrt(-1)) eq = [x + x * y] ans = [x * (y + 1)] for c in [list, tuple, set]: assert factor_terms(c(eq)) == c(ans) assert factor_terms(Tuple(x + x * y)) == Tuple(x * (y + 1)) assert factor_terms(Interval(0, 1)) == Interval(0, 1) e = 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=False) == 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=True) == sqrt(2) / sqrt(a + 2) eq = x / (x + 1 / x) + 1 / (x**2 + 1) assert factor_terms(eq, fraction=False) == eq assert factor_terms(eq, fraction=True) == 1 assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \ y*(2 + 1/(x + 1))/x**2 # if not True, then processesing for this in factor_terms is not necessary assert gcd_terms(-x - y) == -x - y assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False) # if not True, then "special" processesing in factor_terms is not necessary assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1) e = exp(-x - 2) + x assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x assert factor_terms(e, sign=False) == e assert factor_terms(exp(-4 * x - 2) - x) == -x + exp(Mul(-2, 2 * x + 1, evaluate=False))
def test_minimal_polynomial_sq(): p = expand_multinomial((1 + 5*sqrt(2) + 2*sqrt(3))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**4 - 4*x**3 - 118*x**2 + 244*x + 1321 p = expand_multinomial((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**8 - 8*x**7 - 56*x**6 + 448*x**5 + 480*x**4 - 5056*x**3 + 1984*x**2 + 7424*x - 3008 p = Add(*[sqrt(i) for i in range(1, 12)]) mp = minimal_polynomial(p)(x) assert mp.subs({x: 0}) == -71965773323122507776
def test_minimal_polynomial_sq(): p = expand_multinomial((1 + 5 * sqrt(2) + 2 * sqrt(3))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**4 - 4 * x**3 - 118 * x**2 + 244 * x + 1321 p = expand_multinomial((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008 p = Add(*[sqrt(i) for i in range(1, 12)]) mp = minimal_polynomial(p)(x) assert mp.subs({x: 0}) == -71965773323122507776
def test_factor_terms(): A = Symbol('A', commutative=False) assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \ 9*x*y + 9*x + _keep_coeff(Integer(3), x + 1)**_keep_coeff(Integer(2), x + 1) + 9 assert factor_terms(9*(x + x*y + 1) + 3**(2 + 2*x)) == \ _keep_coeff(Integer(9), 3**(2*x) + x*y + x + 1) assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \ 9*3**(2*x)*(a + 1) assert factor_terms(x + x*A) == \ x*(1 + A) assert factor_terms(sin(x + x*A)) == \ sin(x*(1 + A)) assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \ _keep_coeff(Integer(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1) assert factor_terms(x + (x*y + x)**(3*x + 3)) == \ x + (x*(y + 1))**_keep_coeff(Integer(3), x + 1) assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \ x*(a + 2*b)*(y + 1) i = Integral(x, (x, 0, oo)) assert factor_terms(i) == i # check radical extraction eq = sqrt(2) + sqrt(10) assert factor_terms(eq) == eq assert factor_terms(eq, radical=True) == sqrt(2)*(1 + sqrt(5)) eq = root(-6, 3) + root(6, 3) assert factor_terms(eq, radical=True) == cbrt(6)*(1 + cbrt(-1)) eq = [x + x*y] ans = [x*(y + 1)] for c in [list, tuple, set]: assert factor_terms(c(eq)) == c(ans) assert factor_terms(Tuple(x + x*y)) == Tuple(x*(y + 1)) assert factor_terms(Interval(0, 1)) == Interval(0, 1) e = 1/sqrt(a/2 + 1) assert factor_terms(e, clear=False) == 1/sqrt(a/2 + 1) assert factor_terms(e, clear=True) == sqrt(2)/sqrt(a + 2) eq = x/(x + 1/x) + 1/(x**2 + 1) assert factor_terms(eq, fraction=False) == eq assert factor_terms(eq, fraction=True) == 1 assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \ y*(2 + 1/(x + 1))/x**2 # if not True, then processesing for this in factor_terms is not necessary assert gcd_terms(-x - y) == -x - y assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False) # if not True, then "special" processesing in factor_terms is not necessary assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1) e = exp(-x - 2) + x assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x assert factor_terms(e, sign=False) == e assert factor_terms(exp(-4*x - 2) - x) == -x + exp(Mul(-2, 2*x + 1, evaluate=False))
def test_sympyissue_4956_5204(): # issue sympy/sympy#4956 v = ((-27*cbrt(12)*sqrt(31)*I + 27*2**Rational(2, 3)*cbrt(3)*sqrt(31)*I) / (-2511*2**Rational(2, 3)*cbrt(3) + (29*cbrt(18) + 9*cbrt(2)*3**Rational(2, 3)*sqrt(31)*I + 87*cbrt(2)*root(3, 6)*I)**2)) assert NS(v, 1, strict=False) == '0.e-198 - 0.e-198*I' # issue sympy/sympy#5204 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = symbols('x:10') v = ((-18873261792*x0 + 3110400000*I*x1*x5 + 1239810624*x1*x8 - 97043832*x1*x9 + 304403832*x2*x6*(4*x0 + 1422)**Rational(2, 3) - 56619785376*x2 - 41281887168*x5 - 1274950152*x6*x7 - 13478400000*I*x8 + 5276370456*I*x9 - 357587765856 - 108755765856*sqrt(3)*I)/((25596*x0 + 76788*x2 + 1106028)**2 + 175732658352)) v = v.subs(((x9, 2**Rational(2, 3)*root(3, 6)*x7), (x8, cbrt(2)*3**Rational(5, 6)*x4), (x7, x3**Rational(2, 3)), (x6, 6**Rational(2, 3)), (x5, cbrt(6)*x4), (x4, cbrt(x3)), (x3, 54*x0 + 1422), (x2, I*x1), (x1, sqrt(83)), (x0, sqrt(249)))) assert NS(v, 5) == '0.077284 + 1.1104*I' assert NS(v, 1) == '0.08 + 1.*I'
def test_sympyissue_4956_5204(): # issue sympy/sympy#4956 v = ((-27 * cbrt(12) * sqrt(31) * I + 27 * 2**Rational(2, 3) * cbrt(3) * sqrt(31) * I) / (-2511 * 2**Rational(2, 3) * cbrt(3) + (29 * cbrt(18) + 9 * cbrt(2) * 3**Rational(2, 3) * sqrt(31) * I + 87 * cbrt(2) * root(3, 6) * I)**2)) assert NS(v, 1, strict=False) == '0.e-198 - 0.e-198*I' # issue sympy/sympy#5204 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = symbols('x:10') v = ((-18873261792 * x0 + 3110400000 * I * x1 * x5 + 1239810624 * x1 * x8 - 97043832 * x1 * x9 + 304403832 * x2 * x6 * (4 * x0 + 1422)**Rational(2, 3) - 56619785376 * x2 - 41281887168 * x5 - 1274950152 * x6 * x7 - 13478400000 * I * x8 + 5276370456 * I * x9 - 357587765856 - 108755765856 * sqrt(3) * I) / ((25596 * x0 + 76788 * x2 + 1106028)**2 + 175732658352)) v = v.subs( ((x9, 2**Rational(2, 3) * root(3, 6) * x7), (x8, cbrt(2) * 3**Rational(5, 6) * x4), (x7, x3**Rational(2, 3)), (x6, 6**Rational(2, 3)), (x5, cbrt(6) * x4), (x4, cbrt(x3)), (x3, 54 * x0 + 1422), (x2, I * x1), (x1, sqrt(83)), (x0, sqrt(249)))) assert NS(v, 5) == '0.077284 + 1.1104*I' assert NS(v, 1) == '0.08 + 1.*I'
def test_roots1(): assert roots(1) == {} assert roots(1, multiple=True) == [] q = Symbol('q', real=True) assert roots(x**3 - q, x) == {cbrt(q): 1, -cbrt(q)/2 - sqrt(3)*I*cbrt(q)/2: 1, -cbrt(q)/2 + sqrt(3)*I*cbrt(q)/2: 1} assert roots_cubic(Poly(x**3 - 1)) == [1, Rational(-1, 2) + sqrt(3)*I/2, Rational(-1, 2) - sqrt(3)*I/2] assert roots([1, x, y]) == {-x/2 - sqrt(x**2 - 4*y)/2: 1, -x/2 + sqrt(x**2 - 4*y)/2: 1} pytest.raises(ValueError, lambda: roots([1, x, y], z))
def test_sympyissue_5183(): # using list(...) so py.test can recalculate values tests = list( itertools.product( [x, -x], [-1, 1], [2, 3, Rational(1, 2), Rational(2, 3)], ['-', '+'])) results = (oo, oo, -oo, oo, -oo * I, oo, -oo * sign(cbrt(-1)), oo, 0, 0, 0, 0, 0, 0, 0, 0, oo, oo, oo, -oo, oo, -oo * I, oo, -oo * sign(cbrt(-1)), 0, 0, 0, 0, 0, 0, 0, 0) assert len(tests) == len(results) for i, (args, res) in enumerate(zip(tests, results)): y, s, e, d = args eq = y**(s * e) assert limit(eq, x, 0, dir=d) == res
def test_airybiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == root(3, 6) / gamma(Rational(1, 3)) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z * airybi(z) assert series(airybiprime(z), z, 0, 3) == (root(3, 6) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * z**2 / (6 * gamma(Rational(2, 3))) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**Rational(5, 6) * z**2 * hyper((), (Rational(5, 3), ), z**3 / 9) / (6 * gamma(Rational(2, 3))) + root(3, 6) * hyper( (), (Rational(1, 3), ), z**3 / 9) / gamma(Rational(1, 3))) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert (airybiprime(t).rewrite(besselj) == -sqrt(3) * t * (besselj(-Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3) + besselj(Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3) * (z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) / (z**Rational(3, 2))**Rational(2, 3) + (z**Rational(3, 2))**Rational(2, 3) * besseli(-Rational(2, 3), 2 * z**Rational(3, 2) / 3)) / 3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3) * p * (besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert airybiprime(p).rewrite(besselj) == airybiprime(p) assert expand_func(airybiprime( 2 * cbrt(3 * z**5))) == (sqrt(3) * (z**Rational(5, 3) / cbrt(z**5) - 1) * airyaiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2 + (z**Rational(5, 3) / cbrt(z**5) + 1) * airybiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2) assert expand_func(airybiprime(x * y)) == airybiprime(x * y) assert expand_func(airybiprime(log(x))) == airybiprime(log(x)) assert expand_func(airybiprime(2 * root(3 * z**5, 5))) == airybiprime( 2 * root(3 * z**5, 5)) assert airybiprime(-2).evalf(50) == Float( '0.27879516692116952268509756941098324140300059345163131', dps=50)
def test_powers(): assert sqrt(1 - sqrt(x)).subs(x, 4) == I assert (sqrt(1 - x**2)**3).subs(x, 2) == -3 * I * sqrt(3) assert cbrt(x).subs(x, 27) == 3 assert cbrt(x).subs(x, -27) == 3 * cbrt(-1) assert cbrt(-x).subs(x, 27) == 3 * cbrt(-1) n = Symbol('n', negative=True) assert (x**n).subs(x, 0) is zoo assert exp(-1).subs(E, 0) is zoo assert (x**(4.0 * y)).subs(x**(2.0 * y), n) == n**2.0 assert (2**(x + 2)).subs(2, 3) == 3**(x + 3) # issue sympy/sympy#10829 assert (4**x).subs(2**x, y) == y**2 assert (9**x).subs(3**x, y) == y**2
def test_Pow(): assert str(x**-1) == "1/x" assert str(x**-2) == "x**(-2)" assert str(x**2) == "x**2" assert str((x + y)**-1) == "1/(x + y)" assert str((x + y)**-2) == "(x + y)**(-2)" assert str((x + y)**2) == "(x + y)**2" assert str((x + y)**(1 + x)) == "(x + y)**(x + 1)" assert str(cbrt(x)) == "x**(1/3)" assert str(1 / cbrt(x)) == "x**(-1/3)" assert str(sqrt(sqrt(x))) == "x**(1/4)" # not the same as x**-1 assert str(x**-1.0) == 'x**(-1.0)' # see issue sympy/sympy#2860 assert str(Pow(Integer(2), -1.0, evaluate=False)) == '2**(-1.0)'
def test_Pow(): assert str(x**-1) == "1/x" assert str(x**-2) == "x**(-2)" assert str(x**2) == "x**2" assert str((x + y)**-1) == "1/(x + y)" assert str((x + y)**-2) == "(x + y)**(-2)" assert str((x + y)**2) == "(x + y)**2" assert str((x + y)**(1 + x)) == "(x + y)**(x + 1)" assert str(cbrt(x)) == "x**(1/3)" assert str(1/cbrt(x)) == "x**(-1/3)" assert str(sqrt(sqrt(x))) == "x**(1/4)" # not the same as x**-1 assert str(x**-1.0) == 'x**(-1.0)' # see issue sympy/sympy#2860 assert str(Pow(Integer(2), -1.0, evaluate=False)) == '2**(-1.0)'
def test_powers(): assert sqrt(1 - sqrt(x)).subs({x: 4}) == I assert (sqrt(1 - x**2)**3).subs({x: 2}) == - 3*I*sqrt(3) assert cbrt(x).subs({x: 27}) == 3 assert cbrt(x).subs({x: -27}) == 3*cbrt(-1) assert cbrt(-x).subs({x: 27}) == 3*cbrt(-1) n = Symbol('n', negative=True) assert (x**n).subs({x: 0}) is zoo assert exp(-1).subs({E: 0}) is zoo assert (x**(4.0*y)).subs({x**(2.0*y): n}) == n**2.0 assert (2**(x + 2)).subs({2: 3}) == 3**(x + 3) # issue sympy/sympy#10829 assert (4**x).subs({2**x: y}) == y**2 assert (9**x).subs({3**x: y}) == y**2
def test_powers(): assert sqrt(1 - sqrt(x)).subs({x: 4}) == I assert (sqrt(1 - x**2)**3).subs({x: 2}) == -3 * I * sqrt(3) assert cbrt(x).subs({x: 27}) == 3 assert cbrt(x).subs({x: -27}) == 3 * cbrt(-1) assert cbrt(-x).subs({x: 27}) == 3 * cbrt(-1) n = Symbol('n', negative=True) assert (x**n).subs({x: 0}) is zoo assert exp(-1).subs({E: 0}) is zoo assert (x**(4.0 * y)).subs({x**(2.0 * y): n}) == n**2.0 assert (2**(x + 2)).subs({2: 3}) == 3**(x + 3) # issue sympy/sympy#10829 assert (4**x).subs({2**x: y}) == y**2 assert (9**x).subs({3**x: y}) == y**2
def test_sympyissue_5183(): # using list(...) so py.test can recalculate values tests = list(itertools.product([x, -x], [-1, 1], [2, 3, Rational(1, 2), Rational(2, 3)], ['-', '+'])) results = (oo, oo, -oo, oo, -oo*I, oo, -oo*sign(cbrt(-1)), oo, 0, 0, 0, 0, 0, 0, 0, 0, oo, oo, oo, -oo, oo, -oo*I, oo, -oo*sign(cbrt(-1)), 0, 0, 0, 0, 0, 0, 0, 0) assert len(tests) == len(results) for i, (args, res) in enumerate(zip(tests, results)): y, s, e, d = args eq = y**(s*e) assert limit(eq, x, 0, dir=d) == res
def test_sympyissue_5183(): # using list(...) so pytest can recalculate values tests = list(itertools.product([x, -x], [-1, 1], [2, 3, Rational(1, 2), Rational(2, 3)], [1, -1])) results = (oo, oo, -oo, oo, -oo*I, oo, -oo*cbrt(-1), oo, 0, 0, 0, 0, 0, 0, 0, 0, oo, oo, oo, -oo, oo, -oo*I, oo, -oo*cbrt(-1), 0, 0, 0, 0, 0, 0, 0, 0) assert len(tests) == len(results) for args, res in zip(tests, results): y, s, e, d = args eq = y**(s*e) assert limit(eq, x, 0, dir=d) == res
def test_airyai(): z = Symbol('z', extended_real=False) r = Symbol('r', extended_real=True) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3))) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert airyai(z).series(z, 0, 3) == ( 3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0) assert l.doit() == l # cover _airyais._eval_aseries assert airyai(z).rewrite(hyper) == ( -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) + cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyai(z).rewrite(besseli) == ( -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyai(p).rewrite(besseli) == ( sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) - besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert expand_func(airyai(2*cbrt(3*z**5))) == ( -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 + (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyai(x*y)) == airyai(x*y) assert expand_func(airyai(log(x))) == airyai(log(x)) assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5)) assert (airyai(r).as_real_imag() == airyai(r).as_real_imag(deep=False) == (airyai(r), 0)) assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False) assert (airyai(x).as_real_imag() == (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert airyai(x).taylor_term(-1, x) == 0
def test_airyai(): z = Symbol('z', extended_real=False) r = Symbol('r', extended_real=True) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3))) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert series(airyai(z), z, 0, 3) == ( 3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0) assert l.doit() == l # cover _airyais._eval_aseries assert airyai(z).rewrite(hyper) == ( -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) + cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyai(z).rewrite(besseli) == ( -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyai(p).rewrite(besseli) == ( sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) - besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert expand_func(airyai(2*cbrt(3*z**5))) == ( -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 + (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyai(x*y)) == airyai(x*y) assert expand_func(airyai(log(x))) == airyai(log(x)) assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5)) assert (airyai(r).as_real_imag() == airyai(r).as_real_imag(deep=False) == (airyai(r), 0)) assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False) assert (airyai(x).as_real_imag() == (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - airyai(re(x) + I*re(x)*abs(im(x))/Abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert airyai(x).taylor_term(-1, x) == 0
def test_intractable(): assert limit(1/gamma(x), x, oo) == 0 assert limit(1/loggamma(x), x, oo) == 0 assert limit(gamma(x)/loggamma(x), x, oo) == oo assert limit(exp(gamma(x))/gamma(x), x, oo) == oo assert limit(gamma(3 + 1/x), x, oo) == 2 assert limit(gamma(Rational(1, 7) + 1/x), x, oo) == gamma(Rational(1, 7)) assert limit(log(x**x)/log(gamma(x)), x, oo) == 1 assert limit(log(gamma(gamma(x)))/exp(x), x, oo) == oo assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2) # issue sympy/sympy#10804 assert limit(2*airyai(x)*root(x, 4) * exp(2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi) assert limit(airybi(x)*root(x, 4) * exp(-2*x**Rational(3, 2)/3), x, oo) == 1/sqrt(pi) assert limit(airyai(1/x), x, oo) == (3**Rational(5, 6) * gamma(Rational(1, 3))/(6*pi)) assert limit(airybi(1/x), x, oo) == cbrt(3)*gamma(Rational(1, 3))/(2*pi) assert limit(airyai(2 + 1/x), x, oo) == airyai(2) assert limit(airybi(2 + 1/x), x, oo) == airybi(2) # issue sympy/sympy#10976 assert limit(erf(m/x)/erf(1/x), x, oo) == m assert limit(Max(x**2, x, exp(x))/x, x, oo) == oo
def test_AssocOp_Function(): e = Min(-sqrt(3)*cos(pi/18)/6 + re(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 + sin(pi/18)/2 + 2 + I*(-cos(pi/18)/2 - sqrt(3)*sin(pi/18)/6 + im(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3), re(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 - sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 + I*(im(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 - sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2)) # the following should not raise a recursion error; it # should raise a value error because the first arg computes # a non-comparable (prec=1) imaginary part pytest.raises(ValueError, lambda: e.evalf(2, strict=False))
def test_sympyissue_6252(): expr = 1/x/cbrt(a + b*x) anti = integrate(expr, x, meijerg=True) assert not expr.has(hyper) # XXX the expression is a mess, but actually upon differentiation and # putting in numerical values seems to work... assert not anti.has(hyper)
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic(Poly(x**3 + 2 * a / 27, x)) assert res == [ -root(a + sqrt(a**2), 3) / 3, Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False), Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False) ]
def test_branch_bug(): assert hyperexpand(hyper((-Rational(1, 3), Rational(1, 2)), (Rational(2, 3), Rational(3, 2)), -z)) == \ -cbrt(z)*lowergamma(exp_polar(I*pi)/3, z)/5 \ + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z)) assert hyperexpand(meijerg([Rational(7, 6), 1], [], [Rational(2, 3)], [Rational(1, 6), 0], z)) == \ 2*z**Rational(2, 3)*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) - 2*lowergamma(Rational(2, 3), z)/z**Rational(2, 3))*gamma(Rational(2, 3))/gamma(Rational(5, 3))
def test_nsimplify(): assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5))/2 assert nsimplify((1 + sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2 assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == Rational(1, 2) - sqrt(3)*I/2 assert nsimplify(sin(3*pi/5, evaluate=False)) == sqrt(sqrt(5)/8 + Rational(5, 8)) assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2*atan('1/4')*I)) == Rational(49, 17) + 8*I/17 assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == cbrt(2) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).evalf(), rational=True) == Rational(109861228866811, 100000000000000) assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).evalf(3), [pi, log(2)]) == \ -pi/4 - log(2) + Rational(7, 4) assert nsimplify(x/7.0) == x/7 assert nsimplify(pi/1e2) == pi/100 assert nsimplify(pi/1e2, rational=False) == pi/100.0 assert nsimplify(pi/1e-7) == 10000000*pi assert not nsimplify( factor(-3.0*z**2*(z**2)**(-2.5) + 3*(z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == 33 assert nsimplify(33.33, tolerance=10, rational=True) == 30 assert nsimplify(37.76, tolerance=10, rational=True) == 40 assert nsimplify(-203.1) == -Rational(2031, 10) assert nsimplify(+.2, tolerance=0) == Rational(+1, 5) assert nsimplify(-.2, tolerance=0) == Rational(-1, 5) assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == -Rational(1111, 5000) # issue sympy/sympy#7211, PR sympy/sympy#4112 assert nsimplify(Float(2e-8)) == Rational(1, 50000000) # issue sympy/sympy#7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue sympy/sympy#10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i)*oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(Sum(1/n**2, (n, 1, oo)), [pi]) == pi**2/6
def test_sympyissue_6252(): expr = 1 / x / cbrt(a + b * x) anti = integrate(expr, x, meijerg=True) assert not expr.has(hyper) # XXX the expression is a mess, but actually upon differentiation and # putting in numerical values seems to work... assert not anti.has(hyper)
def test_mathml_pow(): mml = mp._print(cbrt(x)) assert mml.childNodes[0].nodeName == 'root' assert mml.childNodes[1].nodeName == 'degree' assert mml.childNodes[1].childNodes[0].nodeName == 'ci' mml = mp._print(sqrt(x)) assert mml.childNodes[0].nodeName == 'root' assert mml.childNodes[1].nodeName == 'ci'
def test_airybi(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybi(z), airybi) assert airybi(0) == 3**Rational(5, 6) / (3 * gamma(Rational(2, 3))) assert airybi(oo) == oo assert airybi(-oo) == 0 assert diff(airybi(z), z) == airybiprime(z) assert series(airybi(z), z, 0, 3) == (cbrt(3) * gamma(Rational(1, 3)) / (2 * pi) + 3**Rational(2, 3) * z * gamma(Rational(2, 3)) / (2 * pi) + O(z**3)) l = Limit( airybi(I / x) / (exp(Rational(2, 3) * (I / x)**Rational(3, 2)) * sqrt(pi * sqrt(I / x))), x, 0) assert l.doit() == l assert airybi(z).rewrite(hyper) == (root(3, 6) * z * hyper( (), (Rational(4, 3), ), z**3 / 9) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * hyper( (), (Rational(2, 3), ), z**3 / 9) / (3 * gamma(Rational(2, 3)))) assert isinstance(airybi(z).rewrite(besselj), airybi) assert (airybi(t).rewrite(besselj) == sqrt(3) * sqrt(-t) * (besselj(-1 / 3, 2 * (-t)**Rational(3, 2) / 3) - besselj(Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3) assert airybi(z).rewrite(besseli) == ( sqrt(3) * (z * besseli(Rational(1, 3), 2 * z**Rational(3, 2) / 3) / cbrt(z**Rational(3, 2)) + cbrt(z**Rational(3, 2)) * besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3)) / 3) assert airybi(p).rewrite(besseli) == ( sqrt(3) * sqrt(p) * (besseli(-Rational(1, 3), 2 * p**Rational(3, 2) / 3) + besseli(Rational(1, 3), 2 * p**Rational(3, 2) / 3)) / 3) assert airybi(p).rewrite(besselj) == airybi(p) assert expand_func(airybi( 2 * cbrt(3 * z**5))) == (sqrt(3) * (1 - cbrt(z**5) / z**Rational(5, 3)) * airyai(2 * cbrt(3) * z**Rational(5, 3)) / 2 + (1 + cbrt(z**5) / z**Rational(5, 3)) * airybi(2 * cbrt(3) * z**Rational(5, 3)) / 2) assert expand_func(airybi(x * y)) == airybi(x * y) assert expand_func(airybi(log(x))) == airybi(log(x)) assert expand_func(airybi(2 * root(3 * z**5, 5))) == airybi( 2 * root(3 * z**5, 5)) assert airybi(x).taylor_term(-1, x) == 0
def test_roots1(): assert roots(1) == {} assert roots(1, multiple=True) == [] q = Symbol('q', real=True) assert roots(x**3 - q, x) == { cbrt(q): 1, -cbrt(q) / 2 - sqrt(3) * I * cbrt(q) / 2: 1, -cbrt(q) / 2 + sqrt(3) * I * cbrt(q) / 2: 1 } assert roots_cubic(Poly(x**3 - 1)) == [ 1, Rational(-1, 2) + sqrt(3) * I / 2, Rational(-1, 2) - sqrt(3) * I / 2 ] assert roots([1, x, y]) == { -x / 2 - sqrt(x**2 - 4 * y) / 2: 1, -x / 2 + sqrt(x**2 - 4 * y) / 2: 1 } pytest.raises(ValueError, lambda: roots([1, x, y], z))
def test_sympyissue_4956(): v = ((-27 * cbrt(12) * sqrt(31) * I + 27 * 2**Rational(2, 3) * cbrt(3) * sqrt(31) * I) / (-2511 * 2**Rational(2, 3) * cbrt(3) + (29 * cbrt(18) + 9 * cbrt(2) * 3**Rational(2, 3) * sqrt(31) * I + 87 * cbrt(2) * root(3, 6) * I)**2)) assert NS(v, 1, strict=False) == '0.e-198 - 0.e-198*I'
def test_airybiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == root(3, 6)/gamma(Rational(1, 3)) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z*airybi(z) assert series(airybiprime(z), z, 0, 3) == ( root(3, 6)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*z**2/(6*gamma(Rational(2, 3))) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**Rational(5, 6)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) + root(3, 6)*hyper((), (Rational(1, 3),), z**3/9)/gamma(Rational(1, 3))) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert (airybiprime(t).rewrite(besselj) == -sqrt(3)*t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) + besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3)*(z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(z**Rational(3, 2))**Rational(2, 3) + (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(2, 3), 2*z**Rational(3, 2)/3))/3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3)*p*(besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3) assert airybiprime(p).rewrite(besselj) == airybiprime(p) assert expand_func(airybiprime(2*cbrt(3*z**5))) == ( sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2 + (z**Rational(5, 3)/cbrt(z**5) + 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airybiprime(x*y)) == airybiprime(x*y) assert expand_func(airybiprime(log(x))) == airybiprime(log(x)) assert expand_func(airybiprime(2*root(3*z**5, 5))) == airybiprime(2*root(3*z**5, 5)) assert airybiprime(-2).evalf(50) == Float('0.27879516692116952268509756941098324140300059345163131', dps=50)
def test_python_functions(): # Simple assert python((2 * x + exp(x))) in "x = Symbol('x')\ne = E**x + 2*x" assert python(sqrt(2)) == 'e = sqrt(2)' assert python(cbrt(2)) == 'e = 2**Rational(1, 3)' assert python(sqrt(2 + pi)) == 'e = sqrt(2 + pi)' assert python(cbrt(2 + pi)) == 'e = (2 + pi)**Rational(1, 3)' assert python(root(2, 4)) == 'e = 2**Rational(1, 4)' assert python(abs(x)) == "x = Symbol('x')\ne = Abs(x)" assert python(abs(x / (x**2 + 1))) in [ "x = Symbol('x')\ne = Abs(x/(1 + x**2))", "x = Symbol('x')\ne = Abs(x/(x**2 + 1))" ] # Univariate/Multivariate functions f = Function('f') assert python(f(x)) == "x = Symbol('x')\nf = Function('f')\ne = f(x)" assert python( f(x, y) ) == "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x, y)" assert python(f(x / (y + 1), y)) in [ "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(1 + y), y)", "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(y + 1), y)" ] # Nesting of square roots assert python(sqrt((sqrt(x + 1)) + 1)) in [ "x = Symbol('x')\ne = sqrt(1 + sqrt(1 + x))", "x = Symbol('x')\ne = sqrt(sqrt(x + 1) + 1)" ] # Nesting of powers assert python(cbrt(cbrt(x + 1) + 1)) in [ "x = Symbol('x')\ne = (1 + (1 + x)**Rational(1, 3))**Rational(1, 3)", "x = Symbol('x')\ne = ((x + 1)**Rational(1, 3) + 1)**Rational(1, 3)" ] # Function powers assert python(sin(x)**2) == "x = Symbol('x')\ne = sin(x)**2"
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ]
def test_pow_eval(): # XXX Pow does not fully support conversion of negative numbers # to their complex equivalent assert sqrt(-1) == I assert sqrt(-4) == 2*I assert sqrt(+4) == 2 assert cbrt(+8) == 2 assert cbrt(-8) == 2*cbrt(-1) assert sqrt(-2) == I*sqrt(2) assert cbrt(-1) != I assert cbrt(-10) != I*cbrt(10) assert root(-2, 4) != root(2, 4) assert cbrt(64) == 4 assert 64**Rational(2, 3) == 16 assert 24/sqrt(64) == 3 assert cbrt(-27) == 3*cbrt(-1) assert (cos(2) / tan(2))**2 == (cos(2) / tan(2))**2
def test_airybi(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybi(z), airybi) assert airybi(0) == 3**Rational(5, 6)/(3*gamma(Rational(2, 3))) assert airybi(oo) == oo assert airybi(-oo) == 0 assert diff(airybi(z), z) == airybiprime(z) assert series(airybi(z), z, 0, 3) == ( cbrt(3)*gamma(Rational(1, 3))/(2*pi) + 3**Rational(2, 3)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3)) l = Limit(airybi(I/x)/(exp(Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))), x, 0) assert l.doit() == l assert airybi(z).rewrite(hyper) == ( root(3, 6)*z*hyper((), (Rational(4, 3),), z**3/9)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3)))) assert isinstance(airybi(z).rewrite(besselj), airybi) assert (airybi(t).rewrite(besselj) == sqrt(3)*sqrt(-t)*(besselj(-1/3, 2*(-t)**Rational(3, 2)/3) - besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airybi(z).rewrite(besseli) == ( sqrt(3)*(z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/cbrt(z**Rational(3, 2)) + cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3))/3) assert airybi(p).rewrite(besseli) == ( sqrt(3)*sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3) assert airybi(p).rewrite(besselj) == airybi(p) assert expand_func(airybi(2*cbrt(3*z**5))) == ( sqrt(3)*(1 - cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2 + (1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airybi(x*y)) == airybi(x*y) assert expand_func(airybi(log(x))) == airybi(log(x)) assert expand_func(airybi(2*root(3*z**5, 5))) == airybi(2*root(3*z**5, 5)) assert airybi(x).taylor_term(-1, x) == 0
def test_expand_radicals(): a = sqrt(x + y) assert (a**1).expand() == a assert (a**3).expand() == x*a + y*a assert (a**5).expand() == x**2*a + 2*x*y*a + y**2*a assert (1/a**1).expand() == 1/a assert (1/a**3).expand() == 1/(x*a + y*a) assert (1/a**5).expand() == 1/(x**2*a + 2*x*y*a + y**2*a) a = cbrt(x + y) assert (a**1).expand() == a assert (a**2).expand() == a**2 assert (a**4).expand() == x*a + y*a assert (a**5).expand() == x*a**2 + y*a**2 assert (a**7).expand() == x**2*a + 2*x*y*a + y**2*a
def test_intractable(): assert gruntz(1/gamma(x), x) == 0 assert gruntz(1/loggamma(x), x) == 0 assert gruntz(gamma(x)/loggamma(x), x) == oo assert gruntz(exp(gamma(x))/gamma(x), x) == oo assert gruntz(gamma(3 + 1/x), x) == 2 assert gruntz(gamma(Rational(1, 7) + 1/x), x) == gamma(Rational(1, 7)) assert gruntz(log(x**x)/log(gamma(x)), x) == 1 assert gruntz(log(gamma(gamma(x)))/exp(x), x) == oo # issue sympy/sympy#10804 assert gruntz(2*airyai(x)*root(x, 4) * exp(2*x**Rational(3, 2)/3), x) == 1/sqrt(pi) assert gruntz(airybi(x)*root(x, 4) * exp(-2*x**Rational(3, 2)/3), x) == 1/sqrt(pi) assert gruntz(airyai(1/x), x) == (3**Rational(5, 6) * gamma(Rational(1, 3))/(6*pi)) assert gruntz(airybi(1/x), x) == cbrt(3)*gamma(Rational(1, 3))/(2*pi) assert gruntz(airyai(2 + 1/x), x) == airyai(2) assert gruntz(airybi(2 + 1/x), x) == airybi(2)
def test_roots_cubic(): assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2*x**2 + 3*x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic(Poly(x**3 + 2*a/27, x)) assert res == [-root(a + sqrt(a**2), 3)/3, Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3)*I/2, root(a + sqrt(a**2), 3), evaluate=False), Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3)*I/2, root(a + sqrt(a**2), 3), evaluate=False)]
def test_airyaiprime(): z = Symbol('z', extended_real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyaiprime(z), airyaiprime) assert airyaiprime(0) == -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) assert airyaiprime(oo) == 0 assert diff(airyaiprime(z), z) == z*airyai(z) assert series(airyaiprime(z), z, 0, 3) == ( -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) + cbrt(3)*z**2/(6*gamma(Rational(2, 3))) + O(z**3)) assert airyaiprime(z).rewrite(hyper) == ( cbrt(3)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) - 3**Rational(2, 3)*hyper((), (Rational(1, 3),), z**3/9)/(3*gamma(Rational(1, 3)))) assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime) assert (airyaiprime(t).rewrite(besselj) == t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) - besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3) assert airyaiprime(z).rewrite(besseli) == ( z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(3*(z**Rational(3, 2))**Rational(2, 3)) - (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3) assert airyaiprime(p).rewrite(besseli) == ( p*(-besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3) assert airyaiprime(p).rewrite(besselj) == airyaiprime(p) assert expand_func(airyaiprime(2*cbrt(3*z**5))) == ( sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/6 + (z**Rational(5, 3)/cbrt(z**5) + 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2) assert expand_func(airyaiprime(x*y)) == airyaiprime(x*y) assert expand_func(airyaiprime(log(x))) == airyaiprime(log(x)) assert expand_func(airyaiprime(2*root(3*z**5, 5))) == airyaiprime(2*root(3*z**5, 5)) assert airyaiprime(-2).evalf(50) == Float('0.61825902074169104140626429133247528291577794512414753', dps=50)
def test_sympyissue_3505(): e = sin(x)**(-4)*(sqrt(cos(x))*sin(x)**2 - cbrt(cos(x))*sin(x)**2) assert e.nseries(x, n=8) == -Rational(1, 12) - 7*x**2/288 - \ 43*x**4/10368 + O(x**6)
def test_Abs(): pytest.raises(TypeError, lambda: Abs(Interval(2, 3))) # issue sympy/sympy#8717 x, y = symbols('x,y') assert sign(sign(x)) == sign(x) assert isinstance(sign(x*y), sign) assert Abs(0) == 0 assert Abs(1) == 1 assert Abs(-1) == 1 assert Abs(I) == 1 assert Abs(-I) == 1 assert Abs(nan) == nan assert Abs(I * pi) == pi assert Abs(-I * pi) == pi assert Abs(I * x) == Abs(x) assert Abs(-I * x) == Abs(x) assert Abs(-2*x) == 2*Abs(x) assert Abs(-2.0*x) == 2.0*Abs(x) assert Abs(2*pi*x*y) == 2*pi*Abs(x*y) assert Abs(conjugate(x)) == Abs(x) assert conjugate(Abs(x)) == Abs(x) a = cos(1)**2 + sin(1)**2 - 1 assert Abs(a*x).series(x).simplify() == 0 a = Symbol('a', positive=True) assert Abs(2*pi*x*a) == 2*pi*a*Abs(x) assert Abs(2*pi*I*x*a) == 2*pi*a*Abs(x) x = Symbol('x', extended_real=True) n = Symbol('n', integer=True) assert Abs((-1)**n) == 1 assert x**(2*n) == Abs(x)**(2*n) assert Abs(x).diff(x) == sign(x) assert Abs(-x).fdiff() == sign(x) assert abs(x) == Abs(x) # Python built-in assert Abs(x)**3 == x**2*Abs(x) assert Abs(x)**4 == x**4 assert ( Abs(x)**(3*n)).args == (Abs(x), 3*n) # leave symbolic odd unchanged assert (1/Abs(x)).args == (Abs(x), -1) assert 1/Abs(x)**3 == 1/(x**2*Abs(x)) assert Abs(x)**-3 == Abs(x)/(x**4) assert Abs(x**3) == x**2*Abs(x) assert Abs(x**pi) == Abs(x**pi, evaluate=False) x = Symbol('x', imaginary=True) assert Abs(x).diff(x) == -sign(x) pytest.raises(ArgumentIndexError, lambda: Abs(z).fdiff(2)) eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) # if there is a fast way to know when you can and when you cannot prove an # expression like this is zero then the equality to zero is ok assert abs(eq) == 0 q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = cbrt(expand(q**3)) d = p - q assert abs(d) == 0 assert Abs(4*exp(pi*I/4)) == 4 assert Abs(3**(2 + I)) == 9 assert Abs((-3)**(1 - I)) == 3*exp(pi) assert Abs(oo) is oo assert Abs(-oo) is oo assert Abs(oo + I) is oo assert Abs(oo + I*oo) is oo a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert abs(sign(z)) == Abs(sign(z), evaluate=False)
def test_sign(): assert sign(1.2) == 1 assert sign(-1.2) == -1 assert sign(3*I) == I assert sign(-3*I) == -I assert sign(0) == 0 assert sign(nan) == nan assert sign(2 + 2*I).doit() == sqrt(2)*(2 + 2*I)/4 assert sign(2 + 3*I).simplify() == sign(2 + 3*I) assert sign(2 + 2*I).simplify() == sign(1 + I) assert sign(im(sqrt(1 - sqrt(3)))) == 1 assert sign(sqrt(1 - sqrt(3))) == I x = Symbol('x') assert sign(x).is_finite is True assert sign(x).is_complex is True assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).doit() == sign(x) assert sign(1.2*x) == sign(x) assert sign(2*x) == sign(x) assert sign(I*x) == I*sign(x) assert sign(-2*I*x) == -I*sign(x) assert sign(conjugate(x)) == conjugate(sign(x)) p = Symbol('p', positive=True) n = Symbol('n', negative=True) m = Symbol('m', negative=True) assert sign(2*p*x) == sign(x) assert sign(n*x) == -sign(x) assert sign(n*m*x) == sign(x) x = Symbol('x', imaginary=True) xn = Symbol('xn', imaginary=True, nonzero=True) assert sign(x).is_imaginary is True assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).diff(x) == 2*DiracDelta(-I*x) assert sign(xn).doit() == xn / Abs(xn) assert conjugate(sign(x)) == -sign(x) x = Symbol('x', extended_real=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is None assert sign(x).diff(x) == 2*DiracDelta(x) assert sign(x).doit() == sign(x) assert conjugate(sign(x)) == sign(x) assert sign(sin(x)).nseries(x) == 1 y = Symbol('y') assert sign(x*y).nseries(x).removeO() == sign(y) x = Symbol('x', nonzero=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = Symbol('x', positive=True) assert sign(x).is_imaginary is False assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = 0 assert sign(x).is_imaginary is True assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is True assert sign(x).doit() == 0 assert sign(Abs(x)) == 0 assert Abs(sign(x)) == 0 nz = Symbol('nz', nonzero=True, integer=True) assert sign(nz).is_imaginary is False assert sign(nz).is_integer is True assert sign(nz).is_extended_real is True assert sign(nz).is_zero is False assert sign(nz)**2 == 1 assert (sign(nz)**3).args == (sign(nz), 3) assert sign(Symbol('x', nonnegative=True)).is_nonnegative assert sign(Symbol('x', nonnegative=True)).is_nonpositive is None assert sign(Symbol('x', nonpositive=True)).is_nonnegative is None assert sign(Symbol('x', nonpositive=True)).is_nonpositive assert sign(Symbol('x', extended_real=True)).is_nonnegative is None assert sign(Symbol('x', extended_real=True)).is_nonpositive is None assert sign(Symbol('x', extended_real=True, zero=False)).is_nonpositive is None x, y = Symbol('x', extended_real=True), Symbol('y') assert sign(x).rewrite(Piecewise) == \ Piecewise((1, x > 0), (-1, x < 0), (0, True)) assert sign(y).rewrite(Piecewise) == sign(y) assert sign(x).rewrite(Heaviside) == 2*Heaviside(x)-1 assert sign(y).rewrite(Heaviside) == sign(y) # evaluate what can be evaluated assert sign(exp_polar(I*pi)*pi) is Integer(-1) eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) # if there is a fast way to know when and when you cannot prove an # expression like this is zero then the equality to zero is ok assert sign(eq) == 0 q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = cbrt(expand(q**3)) d = p - q assert sign(d) == 0 assert abs(sign(z)) == Abs(sign(z), evaluate=False)
def test_unrad1(): pytest.raises(NotImplementedError, lambda: unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x)) + 3)) pytest.raises(NotImplementedError, lambda: unrad(sqrt(x) + cbrt(x + 1) + 2*sqrt(y))) s = symbols('s', cls=Dummy) # checkers to deal with possibility of answer coming # back with a sign change (cf issue sympy/sympy#5203) def check(rv, ans): assert bool(rv[1]) == bool(ans[1]) if ans[1]: return s_check(rv, ans) e = rv[0].expand() a = ans[0].expand() return e in [a, -a] and rv[1] == ans[1] def s_check(rv, ans): # get the dummy rv = list(rv) d = rv[0].atoms(Dummy) reps = list(zip(d, [s]*len(d))) # replace s with this dummy rv = (rv[0].subs(reps).expand(), [rv[1][0].subs(reps), rv[1][1].subs(reps)]) ans = (ans[0].subs(reps).expand(), [ans[1][0].subs(reps), ans[1][1].subs(reps)]) return str(rv[0]) in [str(ans[0]), str(-ans[0])] and \ str(rv[1]) == str(ans[1]) assert check(unrad(sqrt(x)), (x, [])) assert check(unrad(sqrt(x) + 1), (x - 1, [])) assert check(unrad(sqrt(x) + root(x, 3) + 2), (s**3 + s**2 + 2, [s, s**6 - x])) assert check(unrad(sqrt(x)*root(x, 3) + 2), (x**5 - 64, [])) assert check(unrad(sqrt(x) + cbrt(x + 1)), (x**3 - (x + 1)**2, [])) assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(2*x)), (-2*sqrt(2)*x - 2*x + 1, [])) assert check(unrad(sqrt(x) + sqrt(x + 1) + 2), (16*x - 9, [])) assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - x)), (5*x**2 - 4*x, [])) assert check(unrad(a*sqrt(x) + b*sqrt(x) + c*sqrt(y) + d*sqrt(y)), ((a*sqrt(x) + b*sqrt(x))**2 - (c*sqrt(y) + d*sqrt(y))**2, [])) assert check(unrad(sqrt(x) + sqrt(1 - x)), (2*x - 1, [])) assert check(unrad(sqrt(x) + sqrt(1 - x) - 3), (x**2 - x + 16, [])) assert check(unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x)), (5*x**2 - 2*x + 1, [])) assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - 3) in [ (25*x**4 + 376*x**3 + 1256*x**2 - 2272*x + 784, []), (25*x**8 - 476*x**6 + 2534*x**4 - 1468*x**2 + 169, [])] assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - sqrt(1 - 2*x)) == \ (41*x**4 + 40*x**3 + 232*x**2 - 160*x + 16, []) # orig root at 0.487 assert check(unrad(sqrt(x) + sqrt(x + 1)), (Integer(1), [])) eq = sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x)) assert check(unrad(eq), (16*x**2 - 9*x, [])) assert {s[x] for s in solve(eq, check=False)} == {0, Rational(9, 16)} assert solve(eq) == [] # but this one really does have those solutions assert ({s[x] for s in solve(sqrt(x) - sqrt(x + 1) + sqrt(1 - sqrt(x)))} == {0, Rational(9, 16)}) assert check(unrad(sqrt(x) + root(x + 1, 3) + 2*sqrt(y), y), (2*sqrt(x)*cbrt(x + 1) + x - 4*y + (x + 1)**Rational(2, 3), [])) assert check(unrad(sqrt(x/(1 - x)) + cbrt(x + 1)), (x**5 - x**4 - x**3 + 2*x**2 + x - 1, [])) assert check(unrad(sqrt(x/(1 - x)) + 2*sqrt(y), y), (4*x*y + x - 4*y, [])) assert check(unrad(sqrt(x)*sqrt(1 - x) + 2, x), (x**2 - x + 4, [])) # http://tutorial.math.lamar.edu/ # Classes/Alg/SolveRadicalEqns.aspx#Solve_Rad_Ex2_a assert solve(Eq(x, sqrt(x + 6))) == [{x: 3}] assert solve(Eq(x + sqrt(x - 4), 4)) == [{x: 4}] assert solve(Eq(1, x + sqrt(2*x - 3))) == [] assert {s[x] for s in solve(Eq(sqrt(5*x + 6) - 2, x))} == {-1, 2} assert {s[x] for s in solve(Eq(sqrt(2*x - 1) - sqrt(x - 4), 2))} == {5, 13} assert solve(Eq(sqrt(x + 7) + 2, sqrt(3 - x))) == [{x: -6}] # http://www.purplemath.com/modules/solverad.htm assert solve(cbrt(2*x - 5) - 3) == [{x: 16}] assert {s[x] for s in solve(x + 1 - root(x**4 + 4*x**3 - x, 4))} == {-Rational(1, 2), -Rational(1, 3)} assert {s[x] for s in solve(sqrt(2*x**2 - 7) - (3 - x))} == {-8, 2} assert solve(sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4)) == [{x: 0}] assert solve(sqrt(x + 4) + sqrt(2*x - 1) - 3*sqrt(x - 1)) == [{x: 5}] assert solve(sqrt(x)*sqrt(x - 7) - 12) == [{x: 16}] assert solve(sqrt(x - 3) + sqrt(x) - 3) == [{x: 4}] assert solve(sqrt(9*x**2 + 4) - (3*x + 2)) == [{x: 0}] assert solve(sqrt(x) - 2 - 5) == [{x: 49}] assert solve(sqrt(x - 3) - sqrt(x) - 3) == [] assert solve(sqrt(x - 1) - x + 7) == [{x: 10}] assert solve(sqrt(x - 2) - 5) == [{x: 27}] assert solve(sqrt(17*x - sqrt(x**2 - 5)) - 7) == [{x: 3}] assert solve(sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x))) == [] # don't posify the expression in unrad and do use _mexpand z = sqrt(2*x + 1)/sqrt(x) - sqrt(2 + 1/x) p = posify(z)[0] assert solve(p) == [] assert solve(z) == [] assert solve(z + 6*I) == [{x: -Rational(1, 11)}] assert solve(p + 6*I) == [] # issue sympy/sympy#8622 assert unrad((root(x + 1, 5) - root(x, 3))) == ( x**5 - x**3 - 3*x**2 - 3*x - 1, []) # issue sympy/sympy#8679 assert check(unrad(x + root(x, 3) + root(x, 3)**2 + sqrt(y), x), (s**3 + s**2 + s + sqrt(y), [s, s**3 - x])) # for coverage assert check(unrad(sqrt(x) + root(x, 3) + y), (s**3 + s**2 + y, [s, s**6 - x])) assert solve(sqrt(x) + root(x, 3) - 2) == [{x: 1}] pytest.raises(NotImplementedError, lambda: solve(sqrt(x) + root(x, 3) + root(x + 1, 5) - 2)) # fails through a different code path pytest.raises(NotImplementedError, lambda: solve(-sqrt(2) + cosh(x)/x)) # unrad some e = root(x + 1, 3) + root(x, 3) assert unrad(e) == (2*x + 1, []) eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5) assert check(unrad(eq), (15625*x**4 + 173000*x**3 + 355600*x**2 - 817920*x + 331776, [])) assert check(unrad(root(x, 4) + root(x, 4)**3 - 1), (s**3 + s - 1, [s, s**4 - x])) assert check(unrad(root(x, 2) + root(x, 2)**3 - 1), (x**3 + 2*x**2 + x - 1, [])) assert unrad(x**0.5) is None assert check(unrad(t + root(x + y, 5) + root(x + y, 5)**3), (s**3 + s + t, [s, s**5 - x - y])) assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, y), (s**3 + s + x, [s, s**5 - x - y])) assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, x), (s**5 + s**3 + s - y, [s, s**5 - x - y])) assert check(unrad(root(x - 1, 3) + root(x + 1, 5) + root(2, 5)), (s**5 + 5*root(2, 5)*s**4 + s**3 + 10*2**Rational(2, 5)*s**3 + 10*2**Rational(3, 5)*s**2 + 5*2**Rational(4, 5)*s + 4, [s, s**3 - x + 1])) pytest.raises(NotImplementedError, lambda: unrad((root(x, 2) + root(x, 3) + root(x, 4)).subs({x: x**5 - x + 1}))) # the simplify flag should be reset to False for unrad results; # if it's not then this next test will take a long time assert solve(root(x, 3) + root(x, 5) - 2) == [{x: 1}] eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5) assert check(unrad(eq), ((5*x - 4)*(3125*x**3 + 37100*x**2 + 100800*x - 82944), [])) ans = [{x: Rational(4, 5)}, {x: Rational(-1484, 375) + 172564/(140625*cbrt(114*sqrt(12657)/78125 + Rational(12459439, 52734375))) + 4*cbrt(114*sqrt(12657)/78125 + Rational(12459439, 52734375))}] assert solve(eq) == ans # duplicate radical handling assert check(unrad(sqrt(x + root(x + 1, 3)) - root(x + 1, 3) - 2), (s**3 - s**2 - 3*s - 5, [s, s**3 - x - 1])) # cov post-processing e = root(x**2 + 1, 3) - root(x**2 - 1, 5) - 2 assert check(unrad(e), (s**5 - 10*s**4 + 39*s**3 - 80*s**2 + 80*s - 30, [s, s**3 - x**2 - 1])) e = sqrt(x + root(x + 1, 2)) - root(x + 1, 3) - 2 assert check(unrad(e), (s**6 - 2*s**5 - 7*s**4 - 3*s**3 + 26*s**2 + 40*s + 25, [s, s**3 - x - 1])) assert check(unrad(e, _reverse=True), (s**6 - 14*s**5 + 73*s**4 - 187*s**3 + 276*s**2 - 228*s + 89, [s, s**2 - x - sqrt(x + 1)])) # this one needs r0, r1 reversal to work assert check(unrad(sqrt(x + sqrt(root(x, 3) - 1)) - root(x, 6) - 2), (s**12 - 2*s**8 - 8*s**7 - 8*s**6 + s**4 + 8*s**3 + 23*s**2 + 32*s + 17, [s, s**6 - x])) # is this needed? # assert unrad(root(cosh(x), 3)/x*root(x + 1, 5) - 1) == ( # x**15 - x**3*cosh(x)**5 - 3*x**2*cosh(x)**5 - 3*x*cosh(x)**5 - cosh(x)**5, []) pytest.raises(NotImplementedError, lambda: unrad(sqrt(cosh(x)/x) + root(x + 1, 3)*sqrt(x) - 1)) assert unrad((x+y)**(2*y/3) + cbrt(x+y) + 1) is None assert check(unrad((x+y)**(2*y/3) + cbrt(x+y) + 1, x), (s**(2*y) + s + 1, [s, s**3 - x - y])) # This tests two things: that if full unrad is attempted and fails # the solution should still be found; also it tests that the use of # composite assert len(solve(sqrt(y)*x + x**3 - 1, x)) == 3 assert len(solve(-512*y**3 + 1344*cbrt(x + 2)*y**2 - 1176*(x + 2)**Rational(2, 3)*y - 169*x + 686, y, _unrad=False)) == 3 # watch out for when the cov doesn't involve the symbol of interest eq = -x + (7*y/8 - cbrt(27*x/2 + 27*sqrt(x**2)/2)/3)**3 - 1 assert solve(eq, y) == [ {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) + 2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) - 768*sqrt(x**2) - 1536, y, 0, evaluate=False)}, {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) + 2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) - 768*sqrt(x**2) - 1536, y, 1, evaluate=False)}, {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) + 2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) - 768*sqrt(x**2) - 1536, y, 2, evaluate=False)}] eq = root(x + 1, 3) - (root(x, 3) + root(x, 5)) assert check(unrad(eq), (3*s**13 + 3*s**11 + s**9 - 1, [s, s**15 - x])) assert check(unrad(eq - 2), (3*s**13 + 3*s**11 + 6*s**10 + s**9 + 12*s**8 + 6*s**6 + 12*s**5 + 12*s**3 + 7, [s, s**15 - x])) assert check(unrad(root(x, 3) - root(x + 1, 4)/2 + root(x + 2, 3)), (4096*s**13 + 960*s**12 + 48*s**11 - s**10 - 1728*s**4, [s, s**4 - x - 1])) # orig expr has two real roots: -1, -.389 assert check(unrad(root(x, 3) + root(x + 1, 4) - root(x + 2, 3)/2), (343*s**13 + 2904*s**12 + 1344*s**11 + 512*s**10 - 1323*s**9 - 3024*s**8 - 1728*s**7 + 1701*s**5 + 216*s**4 - 729*s, [s, s**4 - x - 1])) # orig expr has one real root: -0.048 assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3)), (729*s**13 - 216*s**12 + 1728*s**11 - 512*s**10 + 1701*s**9 - 3024*s**8 + 1344*s**7 + 1323*s**5 - 2904*s**4 + 343*s, [s, s**4 - x - 1])) # orig expr has 2 real roots: -0.91, -0.15 # orig expr has 1 real root: 19.53 assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3) - 2), (729*s**13 + 1242*s**12 + 18496*s**10 + 129701*s**9 + 388602*s**8 + 453312*s**7 - 612864*s**6 - 3337173*s**5 - 6332418*s**4 - 7134912*s**3 - 5064768*s**2 - 2111913*s - 398034, [s, s**4 - x - 1])) ans = solve(sqrt(x) + sqrt(x + 1) - sqrt(1 - x) - sqrt(2 + x)) assert len(ans) == 1 and NS(ans[0][x])[:4] == '0.73' # the fence optimization problem # https://github.com/sympy/sympy/issues/4793#issuecomment-36994519 eq = F - (2*x + 2*y + sqrt(x**2 + y**2)) ans = 2*F/7 - sqrt(2)*F/14 X = solve(eq, x, check=False) for xi in reversed(X): # reverse since currently, ans is the 2nd one Y = solve((x*y).subs(xi).diff(y), y, simplify=False, check=False) if any((a[y] - ans).expand().is_zero for a in Y): break else: assert None # no answer was found assert (solve(sqrt(x + 1) + root(x, 3) - 2) == [{x: (-11/(9*cbrt(Rational(47, 54) + sqrt(93)/6)) + Rational(1, 3) + cbrt(Rational(47, 54) + sqrt(93)/6))**3}]) assert (solve(sqrt(sqrt(x + 1)) + cbrt(x) - 2) == [{x: (-sqrt(-2*cbrt(Rational(-1, 16) + sqrt(6913)/16) + 6/cbrt(Rational(-1, 16) + sqrt(6913)/16) + Rational(17, 2) + 121/(4*sqrt(-6/cbrt(Rational(-1, 16) + sqrt(6913)/16) + 2*cbrt(Rational(-1, 16) + sqrt(6913)/16) + Rational(17, 4))))/2 + sqrt(-6/cbrt(Rational(-1, 16) + sqrt(6913)/16) + 2*cbrt(Rational(-1, 16) + sqrt(6913)/16) + Rational(17, 4))/2 + Rational(9, 4))**3}]) assert (solve(sqrt(x) + root(sqrt(x) + 1, 3) - 2) == [{x: (-cbrt(Rational(81, 2) + 3*sqrt(741)/2)/3 + (Rational(81, 2) + 3*sqrt(741)/2)**Rational(-1, 3) + 2)**2}]) eq = (-x + (Rational(1, 2) - sqrt(3)*I/2)*cbrt(3*x**3/2 - x*(3*x**2 - 34)/2 + sqrt((-3*x**3 + x*(3*x**2 - 34) + 90)**2/4 - Rational(39304, 27)) - 45) + 34/(3*(Rational(1, 2) - sqrt(3)*I/2)*cbrt(3*x**3/2 - x*(3*x**2 - 34)/2 + sqrt((-3*x**3 + x*(3*x**2 - 34) + 90)**2/4 - Rational(39304, 27)) - 45))) assert check(unrad(eq), (s**6 - sqrt(3)*s**6*I + 102*cbrt(12)*s**4 + 102*2**Rational(2, 3)*3**Rational(5, 6)*s**4*I + 1620*s**3 - 1620*sqrt(3)*s**3*I - 13872*cbrt(18)*s**2 + 471648 - 471648*sqrt(3)*I, [s, s**3 - 306*x - sqrt(3)*sqrt(31212*x**2 - 165240*x + 61484) + 810])) assert solve(eq, x, check=False) != [] # not other code errors
def test_sympyissue_3204(): x = Symbol("x", nonnegative=True) f = cbrt(sin(x**3)) assert f.nseries(x) == x - x**7/18 - x**13/3240 + O(x**19)