1from sympy import limit, Symbol, oo, sqrt, Rational, log, exp, cos, sin, tan, \
2    pi, asin, together, root, S
3
4# Numbers listed with the tests refer to problem numbers in the book
5# "Anti-demidovich, problemas resueltos, Ed. URSS"
6
7x = Symbol("x")
8
9
10def test_leadterm():
11    assert (3 + 2*x**(log(3)/log(2) - 1)).leadterm(x) == (3, 0)
12
13
14def root3(x):
15    return root(x, 3)
16
17
18def root4(x):
19    return root(x, 4)
20
21
22def test_Limits_simple_0():
23    assert limit((2**(x + 1) + 3**(x + 1))/(2**x + 3**x), x, oo) == 3  # 175
24
25
26def test_Limits_simple_1():
27    assert limit((x + 1)*(x + 2)*(x + 3)/x**3, x, oo) == 1  # 172
28    assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0  # 179
29    assert limit((2*x - 3)*(3*x + 5)*(4*x - 6)/(3*x**3 + x - 1), x, oo) == 8  # Primjer 1
30    assert limit(x/root3(x**3 + 10), x, oo) == 1  # Primjer 2
31    assert limit((x + 1)**2/(x**2 + 1), x, oo) == 1  # 181
32
33
34def test_Limits_simple_2():
35    assert limit(1000*x/(x**2 - 1), x, oo) == 0  # 182
36    assert limit((x**2 - 5*x + 1)/(3*x + 7), x, oo) is oo  # 183
37    assert limit((2*x**2 - x + 3)/(x**3 - 8*x + 5), x, oo) == 0  # 184
38    assert limit((2*x**2 - 3*x - 4)/sqrt(x**4 + 1), x, oo) == 2  # 186
39    assert limit((2*x + 3)/(x + root3(x)), x, oo) == 2  # 187
40    assert limit(x**2/(10 + x*sqrt(x)), x, oo) is oo  # 188
41    assert limit(root3(x**2 + 1)/(x + 1), x, oo) == 0  # 189
42    assert limit(sqrt(x)/sqrt(x + sqrt(x + sqrt(x))), x, oo) == 1  # 190
43
44
45def test_Limits_simple_3a():
46    a = Symbol('a')
47    #issue 3513
48    assert together(limit((x**2 - (a + 1)*x + a)/(x**3 - a**3), x, a)) == \
49        (a - 1)/(3*a**2)  # 196
50
51
52def test_Limits_simple_3b():
53    h = Symbol("h")
54    assert limit(((x + h)**3 - x**3)/h, h, 0) == 3*x**2  # 197
55    assert limit((1/(1 - x) - 3/(1 - x**3)), x, 1) == -1  # 198
56    assert limit((sqrt(1 + x) - 1)/(root3(1 + x) - 1), x, 0) == Rational(3)/2  # Primer 4
57    assert limit((sqrt(x) - 1)/(x - 1), x, 1) == Rational(1)/2  # 199
58    assert limit((sqrt(x) - 8)/(root3(x) - 4), x, 64) == 3  # 200
59    assert limit((root3(x) - 1)/(root4(x) - 1), x, 1) == Rational(4)/3  # 201
60    assert limit(
61        (root3(x**2) - 2*root3(x) + 1)/(x - 1)**2, x, 1) == Rational(1)/9  # 202
62
63
64def test_Limits_simple_4a():
65    a = Symbol('a')
66    assert limit((sqrt(x) - sqrt(a))/(x - a), x, a) == 1/(2*sqrt(a))  # Primer 5
67    assert limit((sqrt(x) - 1)/(root3(x) - 1), x, 1) == Rational(3, 2)  # 205
68    assert limit((sqrt(1 + x) - sqrt(1 - x))/x, x, 0) == 1  # 207
69    assert limit(sqrt(x**2 - 5*x + 6) - x, x, oo) == Rational(-5, 2)  # 213
70
71
72def test_limits_simple_4aa():
73    assert limit(x*(sqrt(x**2 + 1) - x), x, oo) == Rational(1)/2  # 214
74
75
76def test_Limits_simple_4b():
77    #issue 3511
78    assert limit(x - root3(x**3 - 1), x, oo) == 0  # 215
79
80
81def test_Limits_simple_4c():
82    assert limit(log(1 + exp(x))/x, x, -oo) == 0  # 267a
83    assert limit(log(1 + exp(x))/x, x, oo) == 1  # 267b
84
85
86def test_bounded():
87    assert limit(sin(x)/x, x, oo) == 0  # 216b
88    assert limit(x*sin(1/x), x, 0) == 0  # 227a
89
90
91def test_f1a():
92    #issue 3508:
93    assert limit((sin(2*x)/x)**(1 + x), x, 0) == 2  # Primer 7
94
95
96def test_f1a2():
97    #issue 3509:
98    assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)  # Primer 9
99
100
101def test_f1b():
102    m = Symbol("m")
103    n = Symbol("n")
104    h = Symbol("h")
105    a = Symbol("a")
106    assert limit(sin(x)/x, x, 2) == sin(2)/2  # 216a
107    assert limit(sin(3*x)/x, x, 0) == 3  # 217
108    assert limit(sin(5*x)/sin(2*x), x, 0) == Rational(5, 2)  # 218
109    assert limit(sin(pi*x)/sin(3*pi*x), x, 0) == Rational(1, 3)  # 219
110    assert limit(x*sin(pi/x), x, oo) == pi  # 220
111    assert limit((1 - cos(x))/x**2, x, 0) == S.Half  # 221
112    assert limit(x*sin(1/x), x, oo) == 1  # 227b
113    assert limit((cos(m*x) - cos(n*x))/x**2, x, 0) == -m**2/2 + n**2/2  # 232
114    assert limit((tan(x) - sin(x))/x**3, x, 0) == S.Half  # 233
115    assert limit((x - sin(2*x))/(x + sin(3*x)), x, 0) == -Rational(1, 4)  # 237
116    assert limit((1 - sqrt(cos(x)))/x**2, x, 0) == Rational(1, 4)  # 239
117    assert limit((sqrt(1 + sin(x)) - sqrt(1 - sin(x)))/x, x, 0) == 1  # 240
118
119    assert limit((1 + h/x)**x, x, oo) == exp(h)  # Primer 9
120    assert limit((sin(x) - sin(a))/(x - a), x, a) == cos(a)  # 222, *176
121    assert limit((cos(x) - cos(a))/(x - a), x, a) == -sin(a)  # 223
122    assert limit((sin(x + h) - sin(x))/h, h, 0) == cos(x)  # 225
123
124
125def test_f2a():
126    assert limit(((x + 1)/(2*x + 1))**(x**2), x, oo) == 0  # Primer 8
127
128
129def test_f2():
130    assert limit((sqrt(
131        cos(x)) - root3(cos(x)))/(sin(x)**2), x, 0) == -Rational(1, 12)  # *184
132
133
134def test_f3():
135    a = Symbol('a')
136    #issue 3504
137    assert limit(asin(a*x)/x, x, 0) == a
138