def test_add_1(): assert O(x + x) == O(x) assert O(3 * x - 2 * x**2) == O(x) assert O(1 + x) == O(1, x) assert O(1 + 1 / x) == O(1 / x) assert O(ln(x) + 1 / ln(x)) == O(ln(x)) assert O(exp(1 / x) + x) == O(exp(1 / x)) assert O(exp(1 / x) + 1 / x**20) == O(exp(1 / x))
def test_simple_1(): o = Rational(0) assert O(2 * x) == O(x) assert O(x) * 3 == O(x) assert -28 * O(x) == O(x) assert O(O(x)) == O(x) assert O(O(x), y) == O(O(x), x, y) assert O(-23) == O(1) assert O(exp(x)) == O(1, x) assert O(exp(1 / x)).expr == exp(1 / x) assert O(x * exp(1 / x)).expr == x * exp(1 / x) assert O(x**(o / 3)).expr == x**(o / 3) assert O(x**(5 * o / 3)).expr == x**(5 * o / 3) assert O(x**2 + x + y, x) == O(1, x) assert O(x**2 + x + y, y) == O(1, y) pytest.raises(ValueError, lambda: O(exp(x), x, x)) pytest.raises(TypeError, lambda: O(x, 2 - x)) pytest.raises(ValueError, lambda: O(x, (x, x**2))) assert O(x**2).is_commutative
def test_coth_series(): x = Symbol('x') assert coth(x).series(x, 0, 8) == \ 1/x + x/3 - x**3/45 + 2*x**5/945 - x**7/4725 + O(x**8)
def test_order_leadterm(): assert O(x**2)._eval_as_leading_term(x) == O(x**2)
def test_nan(): assert O(nan) == nan assert not O(x).contains(nan)
def test_multivar_0a(): assert O(exp(1 / x) * exp(1 / y)).expr == exp(1 / x + 1 / y)
def test_multivar_mul_1(): assert O(x + y) * x == O(x**2 + y * x, x, y)
def test_sympyissue_6782(): assert sqrt(sin(x**3)).series(x, 0, 7) == x**Rational(3, 2) + O(x**7) assert sqrt(sin(x**4)).series(x, 0, 3) == x**2 + O(x**3)
def test_sympyissue_6653(): assert (1 / sqrt(1 + sin(x**2))).series(x, 0, 3) == 1 - x**2 / 2 + O(x**3)
def test_acoth_series(): x = Symbol('x') assert acoth(x).series(x, 0, 10) == \ I*pi/2 + x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
def test_sinh_series(): x = Symbol('x') assert sinh(x).series(x, 0, 10) == \ x + x**3/6 + x**5/120 + x**7/5040 + x**9/362880 + O(x**10)
def test_atanh_series(): x = Symbol('x') assert atanh(x).series(x, 0, 10) == \ x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
def test_sech_series(): x = Symbol('x') assert sech(x).series(x, 0, 10) == \ 1 - x**2/2 + 5*x**4/24 - 61*x**6/720 + 277*x**8/8064 + O(x**10)
def test_csch_series(): x = Symbol('x') assert csch(x).series(x, 0, 10) == \ 1/x - x/6 + 7*x**3/360 - 31*x**5/15120 + 127*x**7/604800 \ - 73*x**9/3421440 + O(x**10)
def test_ln_args(): assert O(log(x)) + O(log(2 * x)) == O(log(x)) assert O(log(x)) + O(log(x**3)) == O(log(x)) assert O(log(x * y)) + O(log(x) + log(y)) == O(log(x * y))
def test_sympyissue_6429(): f = (c**2 + x)**(0.5) assert f.series(x, x0=0, n=1) == (c**2)**0.5 + O(x) assert f.taylor_term(0, x) == (c**2)**0.5 assert f.taylor_term(1, x) == 0.5 * x * (c**2)**(-0.5) assert f.taylor_term(2, x) == -0.125 * x**2 * (c**2)**(-1.5)
def test_free_symbols(): assert O(1).free_symbols == set() assert O(x).free_symbols == {x} assert O(1, x).free_symbols == {x} assert O(x * y).free_symbols == {x, y} assert O(x, x, y).free_symbols == {x, y}
def test_ignore_order_terms(): eq = exp(x).series(x, 0, 3) + sin(y + x**3) - 1 assert cse(eq) == ([], [sin(x**3 + y) + x + x**2 / 2 + O(x**3)])
def test_multivar_2(): assert O(x**2 * y + y**2 * x, x, y).expr == x**2 * y + y**2 * x
def test_piecewise_series(): from diofant import sin, cos, O p1 = Piecewise((sin(x), x < 0), (cos(x), x > 0)) assert p1.nseries(x, n=2) == 1 + O(x**2)
def test_multivar_3(): assert (O(x) + O(y)).args in [(O(x), O(y)), (O(y), O(x))] assert O(x) + O(y) + O(x + y) == O(x + y) assert (O(x**2 * y) + O(y**2 * x)).args in [(O(x * y**2), O(y * x**2)), (O(y * x**2), O(x * y**2))] assert (O(x**2 * y) + O(y * x)) == O(x * y)
def test_expand_arit(): a = Symbol('a') b = Symbol('b', positive=True) c = Symbol('c') p = Integer(5) e = (a + b) * c assert e == c * (a + b) assert (e.expand() - a * c - b * c) == 0 e = (a + b) * (a + b) assert e == (a + b)**2 assert e.expand() == 2 * a * b + a**2 + b**2 e = (a + b) * (a + b)**2 assert e == (a + b)**3 assert e.expand() == 3 * b * a**2 + 3 * a * b**2 + a**3 + b**3 assert e.expand() == 3 * b * a**2 + 3 * a * b**2 + a**3 + b**3 e = (a + b) * (a + c) * (b + c) assert e == (a + c) * (a + b) * (b + c) assert e.expand( ) == 2 * a * b * c + b * a**2 + c * a**2 + b * c**2 + a * c**2 + c * b**2 + a * b**2 e = (a + 1)**p assert e == (1 + a)**5 assert e.expand() == 1 + 5 * a + 10 * a**2 + 10 * a**3 + 5 * a**4 + a**5 e = (a + b + c) * (a + c + p) assert e == (5 + a + c) * (a + b + c) assert e.expand( ) == 5 * a + 5 * b + 5 * c + 2 * a * c + b * c + a * b + a**2 + c**2 x = Symbol('x') s = exp(x * x) - 1 e = s.series(x) / x**2 assert e.expand() == 1 + x**2 / 2 + O(x**4) e = (x * (y + z))**(x * (y + z)) * (x + y) assert e.expand(power_exp=False, power_base=False) == x * ( x * y + x * z)**(x * y + x * z) + y * (x * y + x * z)**(x * y + x * z) assert e.expand(power_exp=False, power_base=False, deep=False) == x * \ (x*(y + z))**(x*(y + z)) + y*(x*(y + z))**(x*(y + z)) e = (x * (y + z))**z assert e.expand(power_base=True, mul=True, deep=True) in [x**z * (y + z)**z, (x * y + x * z)**z] assert ((2 * y)**z).expand() == 2**z * y**z p = Symbol('p', positive=True) assert sqrt(-x).expand().is_Pow assert sqrt(-x).expand(force=True) == I * sqrt(x) assert ((2 * y * p)**z).expand() == 2**z * p**z * y**z assert ((2 * y * p * x)**z).expand() == 2**z * p**z * (x * y)**z assert ((2 * y * p * x)**z).expand(force=True) == 2**z * p**z * x**z * y**z assert ((2 * y * p * -pi)**z).expand() == 2**z * pi**z * p**z * (-y)**z assert ((2 * y * p * -pi * x)**z).expand() == 2**z * pi**z * p**z * (-x * y)**z n = Symbol('n', negative=True) m = Symbol('m', negative=True) assert ((-2 * x * y * n)**z).expand() == 2**z * (-n)**z * (x * y)**z assert ((-2 * x * y * n * m)**z).expand() == 2**z * (-m)**z * (-n)**z * (-x * y)**z # issue sympy/sympy#5482 assert sqrt(-2 * x * n) == sqrt(2) * sqrt(-n) * sqrt(x) # issue sympy/sympy#5605 (2) assert (cos(x + y)**2).expand(trig=True) in [ (-sin(x) * sin(y) + cos(x) * cos(y))**2, sin(x)**2 * sin(y)**2 - 2 * sin(x) * sin(y) * cos(x) * cos(y) + cos(x)**2 * cos(y)**2 ] # Check that this isn't too slow x = Symbol('x') W = 1 for i in range(1, 21): W = W * (x - i) W = W.expand() assert W.has(-1672280820 * x**15)
def test_order_symbols(): e = x * y * sin(x) * Integral(x, (x, 1, 2)) assert O(e) == O(x**2 * y) assert O(e, x) == O(x**2)
def test_expand_multinomial(): assert expand_multinomial((x + 1 + O(z))**2) == 1 + 2 * x + x**2 + O(z) assert expand_multinomial( (x + 1 + O(z))**3) == 1 + 3 * x + 3 * x**2 + x**3 + O(z)
def test_O1(): assert O(1, x) * x == O(x) assert O(1, y) * x == O(1, y)
def test_contains(): assert O(1, x).contains(O(1, x)) assert O(1, x).contains(O(1)) assert O(1).contains(O(1, x)) is False assert O(x).contains(O(x)) assert O(x).contains(O(x**2)) assert not O(x**2).contains(O(x)) assert not O(x).contains(O(1 / x)) assert not O(1 / x).contains(O(exp(1 / x))) assert not O(x).contains(O(exp(1 / x))) assert O(1 / x).contains(O(x)) assert O(exp(1 / x)).contains(O(x)) assert O(exp(1 / x)).contains(O(1 / x)) assert O(exp(1 / x)).contains(O(exp(1 / x))) assert O(exp(2 / x)).contains(O(exp(1 / x))) assert not O(exp(1 / x)).contains(O(exp(2 / x))) assert O(x).contains(O(y)) is None assert O(x).contains(O(y * x)) assert O(y * x).contains(O(x)) assert O(y).contains(O(x * y)) assert O(x).contains(O(y**2 * x)) assert O(x * y**2).contains(O(x**2 * y)) is None assert O(x**2 * y).contains(O(x * y**2)) is None assert O(sin(1 / x**2)).contains(O(cos(1 / x**2))) is None assert O(cos(1 / x**2)).contains(O(sin(1 / x**2))) is None q = Symbol('q', positive=True) assert O(x**8).contains(x**(q + 7)) is None assert O(x**8).contains(x**(q + 8)) assert O(1, x) not in O(1) assert O(1) in O(1, x) pytest.raises(TypeError, lambda: O(x * y**2) in O(x**2 * y))
def test_diff(): assert O(1).diff(x) == 0 assert O(1, x).diff(x) == Derivative(O(1, x), x) assert O(x**2).diff(x) == Derivative(O(x**2), x)
def test_tanh_series(): x = Symbol('x') assert tanh(x).series(x, 0, 10) == \ x - x**3/3 + 2*x**5/15 - 17*x**7/315 + 62*x**9/2835 + O(x**10)