MTerryJack commited on
Commit
1ef8baa
·
verified ·
1 Parent(s): 4b8d352

Add files using upload-large-folder tool

Browse files
Files changed (20) hide show
  1. .venv/lib/python3.13/site-packages/sympy/core/benchmarks/__init__.py +0 -0
  2. .venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_assumptions.py +12 -0
  3. .venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_basic.py +15 -0
  4. .venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_sympify.py +11 -0
  5. .venv/lib/python3.13/site-packages/sympy/crypto/tests/__init__.py +0 -0
  6. .venv/lib/python3.13/site-packages/sympy/crypto/tests/test_crypto.py +562 -0
  7. .venv/lib/python3.13/site-packages/sympy/series/tests/__init__.py +0 -0
  8. .venv/lib/python3.13/site-packages/sympy/series/tests/test_aseries.py +55 -0
  9. .venv/lib/python3.13/site-packages/sympy/series/tests/test_demidovich.py +143 -0
  10. .venv/lib/python3.13/site-packages/sympy/series/tests/test_formal.py +618 -0
  11. .venv/lib/python3.13/site-packages/sympy/series/tests/test_fourier.py +165 -0
  12. .venv/lib/python3.13/site-packages/sympy/series/tests/test_kauers.py +23 -0
  13. .venv/lib/python3.13/site-packages/sympy/series/tests/test_limits.py +1440 -0
  14. .venv/lib/python3.13/site-packages/sympy/series/tests/test_limitseq.py +177 -0
  15. .venv/lib/python3.13/site-packages/sympy/series/tests/test_lseries.py +65 -0
  16. .venv/lib/python3.13/site-packages/sympy/series/tests/test_nseries.py +557 -0
  17. .venv/lib/python3.13/site-packages/sympy/series/tests/test_order.py +503 -0
  18. .venv/lib/python3.13/site-packages/sympy/series/tests/test_residues.py +101 -0
  19. .venv/lib/python3.13/site-packages/sympy/series/tests/test_sequences.py +312 -0
  20. .venv/lib/python3.13/site-packages/sympy/series/tests/test_series.py +421 -0
.venv/lib/python3.13/site-packages/sympy/core/benchmarks/__init__.py ADDED
File without changes
.venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_assumptions.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import Symbol, Integer
2
+
3
+ x = Symbol('x')
4
+ i3 = Integer(3)
5
+
6
+
7
+ def timeit_x_is_integer():
8
+ x.is_integer
9
+
10
+
11
+ def timeit_Integer_is_irrational():
12
+ i3.is_irrational
.venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_basic.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import symbols, S
2
+
3
+ x, y = symbols('x,y')
4
+
5
+
6
+ def timeit_Symbol_meth_lookup():
7
+ x.diff # no call, just method lookup
8
+
9
+
10
+ def timeit_S_lookup():
11
+ S.Exp1
12
+
13
+
14
+ def timeit_Symbol_eq_xy():
15
+ x == y
.venv/lib/python3.13/site-packages/sympy/core/benchmarks/bench_sympify.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import sympify, Symbol
2
+
3
+ x = Symbol('x')
4
+
5
+
6
+ def timeit_sympify_1():
7
+ sympify(1)
8
+
9
+
10
+ def timeit_sympify_x():
11
+ sympify(x)
.venv/lib/python3.13/site-packages/sympy/crypto/tests/__init__.py ADDED
File without changes
.venv/lib/python3.13/site-packages/sympy/crypto/tests/test_crypto.py ADDED
@@ -0,0 +1,562 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import symbols
2
+ from sympy.crypto.crypto import (cycle_list,
3
+ encipher_shift, encipher_affine, encipher_substitution,
4
+ check_and_join, encipher_vigenere, decipher_vigenere,
5
+ encipher_hill, decipher_hill, encipher_bifid5, encipher_bifid6,
6
+ bifid5_square, bifid6_square, bifid5, bifid6,
7
+ decipher_bifid5, decipher_bifid6, encipher_kid_rsa,
8
+ decipher_kid_rsa, kid_rsa_private_key, kid_rsa_public_key,
9
+ decipher_rsa, rsa_private_key, rsa_public_key, encipher_rsa,
10
+ lfsr_connection_polynomial, lfsr_autocorrelation, lfsr_sequence,
11
+ encode_morse, decode_morse, elgamal_private_key, elgamal_public_key,
12
+ encipher_elgamal, decipher_elgamal, dh_private_key, dh_public_key,
13
+ dh_shared_key, decipher_shift, decipher_affine, encipher_bifid,
14
+ decipher_bifid, bifid_square, padded_key, uniq, decipher_gm,
15
+ encipher_gm, gm_public_key, gm_private_key, encipher_bg, decipher_bg,
16
+ bg_private_key, bg_public_key, encipher_rot13, decipher_rot13,
17
+ encipher_atbash, decipher_atbash, NonInvertibleCipherWarning,
18
+ encipher_railfence, decipher_railfence)
19
+ from sympy.external.gmpy import gcd
20
+ from sympy.matrices import Matrix
21
+ from sympy.ntheory import isprime, is_primitive_root
22
+ from sympy.polys.domains import FF
23
+
24
+ from sympy.testing.pytest import raises, warns
25
+
26
+ from sympy.core.random import randrange
27
+
28
+ def test_encipher_railfence():
29
+ assert encipher_railfence("hello world",2) == "hlowrdel ol"
30
+ assert encipher_railfence("hello world",3) == "horel ollwd"
31
+ assert encipher_railfence("hello world",4) == "hwe olordll"
32
+
33
+ def test_decipher_railfence():
34
+ assert decipher_railfence("hlowrdel ol",2) == "hello world"
35
+ assert decipher_railfence("horel ollwd",3) == "hello world"
36
+ assert decipher_railfence("hwe olordll",4) == "hello world"
37
+
38
+
39
+ def test_cycle_list():
40
+ assert cycle_list(3, 4) == [3, 0, 1, 2]
41
+ assert cycle_list(-1, 4) == [3, 0, 1, 2]
42
+ assert cycle_list(1, 4) == [1, 2, 3, 0]
43
+
44
+
45
+ def test_encipher_shift():
46
+ assert encipher_shift("ABC", 0) == "ABC"
47
+ assert encipher_shift("ABC", 1) == "BCD"
48
+ assert encipher_shift("ABC", -1) == "ZAB"
49
+ assert decipher_shift("ZAB", -1) == "ABC"
50
+
51
+ def test_encipher_rot13():
52
+ assert encipher_rot13("ABC") == "NOP"
53
+ assert encipher_rot13("NOP") == "ABC"
54
+ assert decipher_rot13("ABC") == "NOP"
55
+ assert decipher_rot13("NOP") == "ABC"
56
+
57
+
58
+ def test_encipher_affine():
59
+ assert encipher_affine("ABC", (1, 0)) == "ABC"
60
+ assert encipher_affine("ABC", (1, 1)) == "BCD"
61
+ assert encipher_affine("ABC", (-1, 0)) == "AZY"
62
+ assert encipher_affine("ABC", (-1, 1), symbols="ABCD") == "BAD"
63
+ assert encipher_affine("123", (-1, 1), symbols="1234") == "214"
64
+ assert encipher_affine("ABC", (3, 16)) == "QTW"
65
+ assert decipher_affine("QTW", (3, 16)) == "ABC"
66
+
67
+ def test_encipher_atbash():
68
+ assert encipher_atbash("ABC") == "ZYX"
69
+ assert encipher_atbash("ZYX") == "ABC"
70
+ assert decipher_atbash("ABC") == "ZYX"
71
+ assert decipher_atbash("ZYX") == "ABC"
72
+
73
+ def test_encipher_substitution():
74
+ assert encipher_substitution("ABC", "BAC", "ABC") == "BAC"
75
+ assert encipher_substitution("123", "1243", "1234") == "124"
76
+
77
+
78
+ def test_check_and_join():
79
+ assert check_and_join("abc") == "abc"
80
+ assert check_and_join(uniq("aaabc")) == "abc"
81
+ assert check_and_join("ab c".split()) == "abc"
82
+ assert check_and_join("abc", "a", filter=True) == "a"
83
+ raises(ValueError, lambda: check_and_join('ab', 'a'))
84
+
85
+
86
+ def test_encipher_vigenere():
87
+ assert encipher_vigenere("ABC", "ABC") == "ACE"
88
+ assert encipher_vigenere("ABC", "ABC", symbols="ABCD") == "ACA"
89
+ assert encipher_vigenere("ABC", "AB", symbols="ABCD") == "ACC"
90
+ assert encipher_vigenere("AB", "ABC", symbols="ABCD") == "AC"
91
+ assert encipher_vigenere("A", "ABC", symbols="ABCD") == "A"
92
+
93
+
94
+ def test_decipher_vigenere():
95
+ assert decipher_vigenere("ABC", "ABC") == "AAA"
96
+ assert decipher_vigenere("ABC", "ABC", symbols="ABCD") == "AAA"
97
+ assert decipher_vigenere("ABC", "AB", symbols="ABCD") == "AAC"
98
+ assert decipher_vigenere("AB", "ABC", symbols="ABCD") == "AA"
99
+ assert decipher_vigenere("A", "ABC", symbols="ABCD") == "A"
100
+
101
+
102
+ def test_encipher_hill():
103
+ A = Matrix(2, 2, [1, 2, 3, 5])
104
+ assert encipher_hill("ABCD", A) == "CFIV"
105
+ A = Matrix(2, 2, [1, 0, 0, 1])
106
+ assert encipher_hill("ABCD", A) == "ABCD"
107
+ assert encipher_hill("ABCD", A, symbols="ABCD") == "ABCD"
108
+ A = Matrix(2, 2, [1, 2, 3, 5])
109
+ assert encipher_hill("ABCD", A, symbols="ABCD") == "CBAB"
110
+ assert encipher_hill("AB", A, symbols="ABCD") == "CB"
111
+ # message length, n, does not need to be a multiple of k;
112
+ # it is padded
113
+ assert encipher_hill("ABA", A) == "CFGC"
114
+ assert encipher_hill("ABA", A, pad="Z") == "CFYV"
115
+
116
+
117
+ def test_decipher_hill():
118
+ A = Matrix(2, 2, [1, 2, 3, 5])
119
+ assert decipher_hill("CFIV", A) == "ABCD"
120
+ A = Matrix(2, 2, [1, 0, 0, 1])
121
+ assert decipher_hill("ABCD", A) == "ABCD"
122
+ assert decipher_hill("ABCD", A, symbols="ABCD") == "ABCD"
123
+ A = Matrix(2, 2, [1, 2, 3, 5])
124
+ assert decipher_hill("CBAB", A, symbols="ABCD") == "ABCD"
125
+ assert decipher_hill("CB", A, symbols="ABCD") == "AB"
126
+ # n does not need to be a multiple of k
127
+ assert decipher_hill("CFA", A) == "ABAA"
128
+
129
+
130
+ def test_encipher_bifid5():
131
+ assert encipher_bifid5("AB", "AB") == "AB"
132
+ assert encipher_bifid5("AB", "CD") == "CO"
133
+ assert encipher_bifid5("ab", "c") == "CH"
134
+ assert encipher_bifid5("a bc", "b") == "BAC"
135
+
136
+
137
+ def test_bifid5_square():
138
+ A = bifid5
139
+ f = lambda i, j: symbols(A[5*i + j])
140
+ M = Matrix(5, 5, f)
141
+ assert bifid5_square("") == M
142
+
143
+
144
+ def test_decipher_bifid5():
145
+ assert decipher_bifid5("AB", "AB") == "AB"
146
+ assert decipher_bifid5("CO", "CD") == "AB"
147
+ assert decipher_bifid5("ch", "c") == "AB"
148
+ assert decipher_bifid5("b ac", "b") == "ABC"
149
+
150
+
151
+ def test_encipher_bifid6():
152
+ assert encipher_bifid6("AB", "AB") == "AB"
153
+ assert encipher_bifid6("AB", "CD") == "CP"
154
+ assert encipher_bifid6("ab", "c") == "CI"
155
+ assert encipher_bifid6("a bc", "b") == "BAC"
156
+
157
+
158
+ def test_decipher_bifid6():
159
+ assert decipher_bifid6("AB", "AB") == "AB"
160
+ assert decipher_bifid6("CP", "CD") == "AB"
161
+ assert decipher_bifid6("ci", "c") == "AB"
162
+ assert decipher_bifid6("b ac", "b") == "ABC"
163
+
164
+
165
+ def test_bifid6_square():
166
+ A = bifid6
167
+ f = lambda i, j: symbols(A[6*i + j])
168
+ M = Matrix(6, 6, f)
169
+ assert bifid6_square("") == M
170
+
171
+
172
+ def test_rsa_public_key():
173
+ assert rsa_public_key(2, 3, 1) == (6, 1)
174
+ assert rsa_public_key(5, 3, 3) == (15, 3)
175
+
176
+ with warns(NonInvertibleCipherWarning):
177
+ assert rsa_public_key(2, 2, 1) == (4, 1)
178
+ assert rsa_public_key(8, 8, 8) is False
179
+
180
+
181
+ def test_rsa_private_key():
182
+ assert rsa_private_key(2, 3, 1) == (6, 1)
183
+ assert rsa_private_key(5, 3, 3) == (15, 3)
184
+ assert rsa_private_key(23,29,5) == (667,493)
185
+
186
+ with warns(NonInvertibleCipherWarning):
187
+ assert rsa_private_key(2, 2, 1) == (4, 1)
188
+ assert rsa_private_key(8, 8, 8) is False
189
+
190
+
191
+ def test_rsa_large_key():
192
+ # Sample from
193
+ # http://www.herongyang.com/Cryptography/JCE-Public-Key-RSA-Private-Public-Key-Pair-Sample.html
194
+ p = int('101565610013301240713207239558950144682174355406589305284428666'\
195
+ '903702505233009')
196
+ q = int('894687191887545488935455605955948413812376003053143521429242133'\
197
+ '12069293984003')
198
+ e = int('65537')
199
+ d = int('893650581832704239530398858744759129594796235440844479456143566'\
200
+ '6999402846577625762582824202269399672579058991442587406384754958587'\
201
+ '400493169361356902030209')
202
+ assert rsa_public_key(p, q, e) == (p*q, e)
203
+ assert rsa_private_key(p, q, e) == (p*q, d)
204
+
205
+
206
+ def test_encipher_rsa():
207
+ puk = rsa_public_key(2, 3, 1)
208
+ assert encipher_rsa(2, puk) == 2
209
+ puk = rsa_public_key(5, 3, 3)
210
+ assert encipher_rsa(2, puk) == 8
211
+
212
+ with warns(NonInvertibleCipherWarning):
213
+ puk = rsa_public_key(2, 2, 1)
214
+ assert encipher_rsa(2, puk) == 2
215
+
216
+
217
+ def test_decipher_rsa():
218
+ prk = rsa_private_key(2, 3, 1)
219
+ assert decipher_rsa(2, prk) == 2
220
+ prk = rsa_private_key(5, 3, 3)
221
+ assert decipher_rsa(8, prk) == 2
222
+
223
+ with warns(NonInvertibleCipherWarning):
224
+ prk = rsa_private_key(2, 2, 1)
225
+ assert decipher_rsa(2, prk) == 2
226
+
227
+
228
+ def test_mutltiprime_rsa_full_example():
229
+ # Test example from
230
+ # https://iopscience.iop.org/article/10.1088/1742-6596/995/1/012030
231
+ puk = rsa_public_key(2, 3, 5, 7, 11, 13, 7)
232
+ prk = rsa_private_key(2, 3, 5, 7, 11, 13, 7)
233
+ assert puk == (30030, 7)
234
+ assert prk == (30030, 823)
235
+
236
+ msg = 10
237
+ encrypted = encipher_rsa(2 * msg - 15, puk)
238
+ assert encrypted == 18065
239
+ decrypted = (decipher_rsa(encrypted, prk) + 15) / 2
240
+ assert decrypted == msg
241
+
242
+ # Test example from
243
+ # https://www.scirp.org/pdf/JCC_2018032215502008.pdf
244
+ puk1 = rsa_public_key(53, 41, 43, 47, 41)
245
+ prk1 = rsa_private_key(53, 41, 43, 47, 41)
246
+ puk2 = rsa_public_key(53, 41, 43, 47, 97)
247
+ prk2 = rsa_private_key(53, 41, 43, 47, 97)
248
+
249
+ assert puk1 == (4391633, 41)
250
+ assert prk1 == (4391633, 294041)
251
+ assert puk2 == (4391633, 97)
252
+ assert prk2 == (4391633, 455713)
253
+
254
+ msg = 12321
255
+ encrypted = encipher_rsa(encipher_rsa(msg, puk1), puk2)
256
+ assert encrypted == 1081588
257
+ decrypted = decipher_rsa(decipher_rsa(encrypted, prk2), prk1)
258
+ assert decrypted == msg
259
+
260
+
261
+ def test_rsa_crt_extreme():
262
+ p = int(
263
+ '10177157607154245068023861503693082120906487143725062283406501' \
264
+ '54082258226204046999838297167140821364638180697194879500245557' \
265
+ '65445186962893346463841419427008800341257468600224049986260471' \
266
+ '92257248163014468841725476918639415726709736077813632961290911' \
267
+ '0256421232977833028677441206049309220354796014376698325101693')
268
+
269
+ q = int(
270
+ '28752342353095132872290181526607275886182793241660805077850801' \
271
+ '75689512797754286972952273553128181861830576836289738668745250' \
272
+ '34028199691128870676414118458442900035778874482624765513861643' \
273
+ '27966696316822188398336199002306588703902894100476186823849595' \
274
+ '103239410527279605442148285816149368667083114802852804976893')
275
+
276
+ r = int(
277
+ '17698229259868825776879500736350186838850961935956310134378261' \
278
+ '89771862186717463067541369694816245225291921138038800171125596' \
279
+ '07315449521981157084370187887650624061033066022458512942411841' \
280
+ '18747893789972315277160085086164119879536041875335384844820566' \
281
+ '0287479617671726408053319619892052000850883994343378882717849')
282
+
283
+ s = int(
284
+ '68925428438585431029269182233502611027091755064643742383515623' \
285
+ '64321310582896893395529367074942808353187138794422745718419645' \
286
+ '28291231865157212604266903677599180789896916456120289112752835' \
287
+ '98502265889669730331688206825220074713977607415178738015831030' \
288
+ '364290585369150502819743827343552098197095520550865360159439'
289
+ )
290
+
291
+ t = int(
292
+ '69035483433453632820551311892368908779778144568711455301541094' \
293
+ '31487047642322695357696860925747923189635033183069823820910521' \
294
+ '71172909106797748883261493224162414050106920442445896819806600' \
295
+ '15448444826108008217972129130625571421904893252804729877353352' \
296
+ '739420480574842850202181462656251626522910618936534699566291'
297
+ )
298
+
299
+ e = 65537
300
+ puk = rsa_public_key(p, q, r, s, t, e)
301
+ prk = rsa_private_key(p, q, r, s, t, e)
302
+
303
+ plaintext = 1000
304
+ ciphertext_1 = encipher_rsa(plaintext, puk)
305
+ ciphertext_2 = encipher_rsa(plaintext, puk, [p, q, r, s, t])
306
+ assert ciphertext_1 == ciphertext_2
307
+ assert decipher_rsa(ciphertext_1, prk) == \
308
+ decipher_rsa(ciphertext_1, prk, [p, q, r, s, t])
309
+
310
+
311
+ def test_rsa_exhaustive():
312
+ p, q = 61, 53
313
+ e = 17
314
+ puk = rsa_public_key(p, q, e, totient='Carmichael')
315
+ prk = rsa_private_key(p, q, e, totient='Carmichael')
316
+
317
+ for msg in range(puk[0]):
318
+ encrypted = encipher_rsa(msg, puk)
319
+ decrypted = decipher_rsa(encrypted, prk)
320
+ try:
321
+ assert decrypted == msg
322
+ except AssertionError:
323
+ raise AssertionError(
324
+ "The RSA is not correctly decrypted " \
325
+ "(Original : {}, Encrypted : {}, Decrypted : {})" \
326
+ .format(msg, encrypted, decrypted)
327
+ )
328
+
329
+
330
+ def test_rsa_multiprime_exhanstive():
331
+ primes = [3, 5, 7, 11]
332
+ e = 7
333
+ args = primes + [e]
334
+ puk = rsa_public_key(*args, totient='Carmichael')
335
+ prk = rsa_private_key(*args, totient='Carmichael')
336
+ n = puk[0]
337
+
338
+ for msg in range(n):
339
+ encrypted = encipher_rsa(msg, puk)
340
+ decrypted = decipher_rsa(encrypted, prk)
341
+ try:
342
+ assert decrypted == msg
343
+ except AssertionError:
344
+ raise AssertionError(
345
+ "The RSA is not correctly decrypted " \
346
+ "(Original : {}, Encrypted : {}, Decrypted : {})" \
347
+ .format(msg, encrypted, decrypted)
348
+ )
349
+
350
+
351
+ def test_rsa_multipower_exhanstive():
352
+ primes = [5, 5, 7]
353
+ e = 7
354
+ args = primes + [e]
355
+ puk = rsa_public_key(*args, multipower=True)
356
+ prk = rsa_private_key(*args, multipower=True)
357
+ n = puk[0]
358
+
359
+ for msg in range(n):
360
+ if gcd(msg, n) != 1:
361
+ continue
362
+
363
+ encrypted = encipher_rsa(msg, puk)
364
+ decrypted = decipher_rsa(encrypted, prk)
365
+ try:
366
+ assert decrypted == msg
367
+ except AssertionError:
368
+ raise AssertionError(
369
+ "The RSA is not correctly decrypted " \
370
+ "(Original : {}, Encrypted : {}, Decrypted : {})" \
371
+ .format(msg, encrypted, decrypted)
372
+ )
373
+
374
+
375
+ def test_kid_rsa_public_key():
376
+ assert kid_rsa_public_key(1, 2, 1, 1) == (5, 2)
377
+ assert kid_rsa_public_key(1, 2, 2, 1) == (8, 3)
378
+ assert kid_rsa_public_key(1, 2, 1, 2) == (7, 2)
379
+
380
+
381
+ def test_kid_rsa_private_key():
382
+ assert kid_rsa_private_key(1, 2, 1, 1) == (5, 3)
383
+ assert kid_rsa_private_key(1, 2, 2, 1) == (8, 3)
384
+ assert kid_rsa_private_key(1, 2, 1, 2) == (7, 4)
385
+
386
+
387
+ def test_encipher_kid_rsa():
388
+ assert encipher_kid_rsa(1, (5, 2)) == 2
389
+ assert encipher_kid_rsa(1, (8, 3)) == 3
390
+ assert encipher_kid_rsa(1, (7, 2)) == 2
391
+
392
+
393
+ def test_decipher_kid_rsa():
394
+ assert decipher_kid_rsa(2, (5, 3)) == 1
395
+ assert decipher_kid_rsa(3, (8, 3)) == 1
396
+ assert decipher_kid_rsa(2, (7, 4)) == 1
397
+
398
+
399
+ def test_encode_morse():
400
+ assert encode_morse('ABC') == '.-|-...|-.-.'
401
+ assert encode_morse('SMS ') == '...|--|...||'
402
+ assert encode_morse('SMS\n') == '...|--|...||'
403
+ assert encode_morse('') == ''
404
+ assert encode_morse(' ') == '||'
405
+ assert encode_morse(' ', sep='`') == '``'
406
+ assert encode_morse(' ', sep='``') == '````'
407
+ assert encode_morse('!@#$%^&*()_+') == '-.-.--|.--.-.|...-..-|-.--.|-.--.-|..--.-|.-.-.'
408
+ assert encode_morse('12345') == '.----|..---|...--|....-|.....'
409
+ assert encode_morse('67890') == '-....|--...|---..|----.|-----'
410
+
411
+
412
+ def test_decode_morse():
413
+ assert decode_morse('-.-|.|-.--') == 'KEY'
414
+ assert decode_morse('.-.|..-|-.||') == 'RUN'
415
+ raises(KeyError, lambda: decode_morse('.....----'))
416
+
417
+
418
+ def test_lfsr_sequence():
419
+ raises(TypeError, lambda: lfsr_sequence(1, [1], 1))
420
+ raises(TypeError, lambda: lfsr_sequence([1], 1, 1))
421
+ F = FF(2)
422
+ assert lfsr_sequence([F(1)], [F(1)], 2) == [F(1), F(1)]
423
+ assert lfsr_sequence([F(0)], [F(1)], 2) == [F(1), F(0)]
424
+ F = FF(3)
425
+ assert lfsr_sequence([F(1)], [F(1)], 2) == [F(1), F(1)]
426
+ assert lfsr_sequence([F(0)], [F(2)], 2) == [F(2), F(0)]
427
+ assert lfsr_sequence([F(1)], [F(2)], 2) == [F(2), F(2)]
428
+
429
+
430
+ def test_lfsr_autocorrelation():
431
+ raises(TypeError, lambda: lfsr_autocorrelation(1, 2, 3))
432
+ F = FF(2)
433
+ s = lfsr_sequence([F(1), F(0)], [F(0), F(1)], 5)
434
+ assert lfsr_autocorrelation(s, 2, 0) == 1
435
+ assert lfsr_autocorrelation(s, 2, 1) == -1
436
+
437
+
438
+ def test_lfsr_connection_polynomial():
439
+ F = FF(2)
440
+ x = symbols("x")
441
+ s = lfsr_sequence([F(1), F(0)], [F(0), F(1)], 5)
442
+ assert lfsr_connection_polynomial(s) == x**2 + 1
443
+ s = lfsr_sequence([F(1), F(1)], [F(0), F(1)], 5)
444
+ assert lfsr_connection_polynomial(s) == x**2 + x + 1
445
+
446
+
447
+ def test_elgamal_private_key():
448
+ a, b, _ = elgamal_private_key(digit=100)
449
+ assert isprime(a)
450
+ assert is_primitive_root(b, a)
451
+ assert len(bin(a)) >= 102
452
+
453
+
454
+ def test_elgamal():
455
+ dk = elgamal_private_key(5)
456
+ ek = elgamal_public_key(dk)
457
+ P = ek[0]
458
+ assert P - 1 == decipher_elgamal(encipher_elgamal(P - 1, ek), dk)
459
+ raises(ValueError, lambda: encipher_elgamal(P, dk))
460
+ raises(ValueError, lambda: encipher_elgamal(-1, dk))
461
+
462
+
463
+ def test_dh_private_key():
464
+ p, g, _ = dh_private_key(digit = 100)
465
+ assert isprime(p)
466
+ assert is_primitive_root(g, p)
467
+ assert len(bin(p)) >= 102
468
+
469
+
470
+ def test_dh_public_key():
471
+ p1, g1, a = dh_private_key(digit = 100)
472
+ p2, g2, ga = dh_public_key((p1, g1, a))
473
+ assert p1 == p2
474
+ assert g1 == g2
475
+ assert ga == pow(g1, a, p1)
476
+
477
+
478
+ def test_dh_shared_key():
479
+ prk = dh_private_key(digit = 100)
480
+ p, _, ga = dh_public_key(prk)
481
+ b = randrange(2, p)
482
+ sk = dh_shared_key((p, _, ga), b)
483
+ assert sk == pow(ga, b, p)
484
+ raises(ValueError, lambda: dh_shared_key((1031, 14, 565), 2000))
485
+
486
+
487
+ def test_padded_key():
488
+ assert padded_key('b', 'ab') == 'ba'
489
+ raises(ValueError, lambda: padded_key('ab', 'ace'))
490
+ raises(ValueError, lambda: padded_key('ab', 'abba'))
491
+
492
+
493
+ def test_bifid():
494
+ raises(ValueError, lambda: encipher_bifid('abc', 'b', 'abcde'))
495
+ assert encipher_bifid('abc', 'b', 'abcd') == 'bdb'
496
+ raises(ValueError, lambda: decipher_bifid('bdb', 'b', 'abcde'))
497
+ assert encipher_bifid('bdb', 'b', 'abcd') == 'abc'
498
+ raises(ValueError, lambda: bifid_square('abcde'))
499
+ assert bifid5_square("B") == \
500
+ bifid5_square('BACDEFGHIKLMNOPQRSTUVWXYZ')
501
+ assert bifid6_square('B0') == \
502
+ bifid6_square('B0ACDEFGHIJKLMNOPQRSTUVWXYZ123456789')
503
+
504
+
505
+ def test_encipher_decipher_gm():
506
+ ps = [131, 137, 139, 149, 151, 157, 163, 167,
507
+ 173, 179, 181, 191, 193, 197, 199]
508
+ qs = [89, 97, 101, 103, 107, 109, 113, 127,
509
+ 131, 137, 139, 149, 151, 157, 47]
510
+ messages = [
511
+ 0, 32855, 34303, 14805, 1280, 75859, 38368,
512
+ 724, 60356, 51675, 76697, 61854, 18661,
513
+ ]
514
+ for p, q in zip(ps, qs):
515
+ pri = gm_private_key(p, q)
516
+ for msg in messages:
517
+ pub = gm_public_key(p, q)
518
+ enc = encipher_gm(msg, pub)
519
+ dec = decipher_gm(enc, pri)
520
+ assert dec == msg
521
+
522
+
523
+ def test_gm_private_key():
524
+ raises(ValueError, lambda: gm_public_key(13, 15))
525
+ raises(ValueError, lambda: gm_public_key(0, 0))
526
+ raises(ValueError, lambda: gm_public_key(0, 5))
527
+ assert 17, 19 == gm_public_key(17, 19)
528
+
529
+
530
+ def test_gm_public_key():
531
+ assert 323 == gm_public_key(17, 19)[1]
532
+ assert 15 == gm_public_key(3, 5)[1]
533
+ raises(ValueError, lambda: gm_public_key(15, 19))
534
+
535
+ def test_encipher_decipher_bg():
536
+ ps = [67, 7, 71, 103, 11, 43, 107, 47,
537
+ 79, 19, 83, 23, 59, 127, 31]
538
+ qs = qs = [7, 71, 103, 11, 43, 107, 47,
539
+ 79, 19, 83, 23, 59, 127, 31, 67]
540
+ messages = [
541
+ 0, 328, 343, 148, 1280, 758, 383,
542
+ 724, 603, 516, 766, 618, 186,
543
+ ]
544
+
545
+ for p, q in zip(ps, qs):
546
+ pri = bg_private_key(p, q)
547
+ for msg in messages:
548
+ pub = bg_public_key(p, q)
549
+ enc = encipher_bg(msg, pub)
550
+ dec = decipher_bg(enc, pri)
551
+ assert dec == msg
552
+
553
+ def test_bg_private_key():
554
+ raises(ValueError, lambda: bg_private_key(8, 16))
555
+ raises(ValueError, lambda: bg_private_key(8, 8))
556
+ raises(ValueError, lambda: bg_private_key(13, 17))
557
+ assert 23, 31 == bg_private_key(23, 31)
558
+
559
+ def test_bg_public_key():
560
+ assert 5293 == bg_public_key(67, 79)
561
+ assert 713 == bg_public_key(23, 31)
562
+ raises(ValueError, lambda: bg_private_key(13, 17))
.venv/lib/python3.13/site-packages/sympy/series/tests/__init__.py ADDED
File without changes
.venv/lib/python3.13/site-packages/sympy/series/tests/test_aseries.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.function import PoleError
2
+ from sympy.core.numbers import oo
3
+ from sympy.core.symbol import Symbol
4
+ from sympy.functions.elementary.exponential import (exp, log)
5
+ from sympy.functions.elementary.miscellaneous import sqrt
6
+ from sympy.functions.elementary.trigonometric import (cos, sin)
7
+ from sympy.series.order import O
8
+ from sympy.abc import x
9
+
10
+ from sympy.testing.pytest import raises
11
+
12
+ def test_simple():
13
+ # Gruntz' theses pp. 91 to 96
14
+ # 6.6
15
+ e = sin(1/x + exp(-x)) - sin(1/x)
16
+ assert e.aseries(x) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
17
+
18
+ e = exp(x) * (exp(1/x + exp(-x)) - exp(1/x))
19
+ assert e.aseries(x, n=4) == 1/(6*x**3) + 1/(2*x**2) + 1/x + 1 + O(x**(-4), (x, oo))
20
+
21
+ e = exp(exp(x) / (1 - 1/x))
22
+ assert e.aseries(x) == exp(exp(x) / (1 - 1/x))
23
+
24
+ # The implementation of bound in aseries is incorrect currently. This test
25
+ # should be commented out when that is fixed.
26
+ # assert e.aseries(x, bound=3) == exp(exp(x) / x**2)*exp(exp(x) / x)*exp(-exp(x) + exp(x)/(1 - 1/x) - \
27
+ # exp(x) / x - exp(x) / x**2) * exp(exp(x))
28
+
29
+ e = exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))
30
+ assert e.aseries(x, n=4) == (-1/(2*x**3) + 1/x + 1 + O(x**(-4), (x, oo)))*exp(-exp(x))
31
+
32
+ e3 = lambda x:exp(exp(exp(x)))
33
+ e = e3(x)/e3(x - 1/e3(x))
34
+ assert e.aseries(x, n=3) == 1 + exp(2*x + 2*exp(x))*exp(-2*exp(exp(x)))/2\
35
+ - exp(2*x + exp(x))*exp(-2*exp(exp(x)))/2 - exp(x + exp(x))*exp(-2*exp(exp(x)))/2\
36
+ + exp(x + exp(x))*exp(-exp(exp(x))) + O(exp(-3*exp(exp(x))), (x, oo))
37
+
38
+ e = exp(exp(x)) * (exp(sin(1/x + 1/exp(exp(x)))) - exp(sin(1/x)))
39
+ assert e.aseries(x, n=4) == -1/(2*x**3) + 1/x + 1 + O(x**(-4), (x, oo))
40
+
41
+ n = Symbol('n', integer=True)
42
+ e = (sqrt(n)*log(n)**2*exp(sqrt(log(n))*log(log(n))**2*exp(sqrt(log(log(n)))*log(log(log(n)))**3)))/n
43
+ assert e.aseries(n) == \
44
+ exp(exp(sqrt(log(log(n)))*log(log(log(n)))**3)*sqrt(log(n))*log(log(n))**2)*log(n)**2/sqrt(n)
45
+
46
+
47
+ def test_hierarchical():
48
+ e = sin(1/x + exp(-x))
49
+ assert e.aseries(x, n=3, hir=True) == -exp(-2*x)*sin(1/x)/2 + \
50
+ exp(-x)*cos(1/x) + sin(1/x) + O(exp(-3*x), (x, oo))
51
+
52
+ e = sin(x) * cos(exp(-x))
53
+ assert e.aseries(x, hir=True) == exp(-4*x)*sin(x)/24 - \
54
+ exp(-2*x)*sin(x)/2 + sin(x) + O(exp(-6*x), (x, oo))
55
+ raises(PoleError, lambda: e.aseries(x))
.venv/lib/python3.13/site-packages/sympy/series/tests/test_demidovich.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import (Rational, oo, pi)
2
+ from sympy.core.singleton import S
3
+ from sympy.core.symbol import Symbol
4
+ from sympy.functions.elementary.exponential import (exp, log)
5
+ from sympy.functions.elementary.miscellaneous import (root, sqrt)
6
+ from sympy.functions.elementary.trigonometric import (asin, cos, sin, tan)
7
+ from sympy.polys.rationaltools import together
8
+ from sympy.series.limits import limit
9
+
10
+ # Numbers listed with the tests refer to problem numbers in the book
11
+ # "Anti-demidovich, problemas resueltos, Ed. URSS"
12
+
13
+ x = Symbol("x")
14
+
15
+
16
+ def test_leadterm():
17
+ assert (3 + 2*x**(log(3)/log(2) - 1)).leadterm(x) == (3, 0)
18
+
19
+
20
+ def root3(x):
21
+ return root(x, 3)
22
+
23
+
24
+ def root4(x):
25
+ return root(x, 4)
26
+
27
+
28
+ def test_Limits_simple_0():
29
+ assert limit((2**(x + 1) + 3**(x + 1))/(2**x + 3**x), x, oo) == 3 # 175
30
+
31
+
32
+ def test_Limits_simple_1():
33
+ assert limit((x + 1)*(x + 2)*(x + 3)/x**3, x, oo) == 1 # 172
34
+ assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0 # 179
35
+ assert limit((2*x - 3)*(3*x + 5)*(4*x - 6)/(3*x**3 + x - 1), x, oo) == 8 # Primjer 1
36
+ assert limit(x/root3(x**3 + 10), x, oo) == 1 # Primjer 2
37
+ assert limit((x + 1)**2/(x**2 + 1), x, oo) == 1 # 181
38
+
39
+
40
+ def test_Limits_simple_2():
41
+ assert limit(1000*x/(x**2 - 1), x, oo) == 0 # 182
42
+ assert limit((x**2 - 5*x + 1)/(3*x + 7), x, oo) is oo # 183
43
+ assert limit((2*x**2 - x + 3)/(x**3 - 8*x + 5), x, oo) == 0 # 184
44
+ assert limit((2*x**2 - 3*x - 4)/sqrt(x**4 + 1), x, oo) == 2 # 186
45
+ assert limit((2*x + 3)/(x + root3(x)), x, oo) == 2 # 187
46
+ assert limit(x**2/(10 + x*sqrt(x)), x, oo) is oo # 188
47
+ assert limit(root3(x**2 + 1)/(x + 1), x, oo) == 0 # 189
48
+ assert limit(sqrt(x)/sqrt(x + sqrt(x + sqrt(x))), x, oo) == 1 # 190
49
+
50
+
51
+ def test_Limits_simple_3a():
52
+ a = Symbol('a')
53
+ #issue 3513
54
+ assert together(limit((x**2 - (a + 1)*x + a)/(x**3 - a**3), x, a)) == \
55
+ (a - 1)/(3*a**2) # 196
56
+
57
+
58
+ def test_Limits_simple_3b():
59
+ h = Symbol("h")
60
+ assert limit(((x + h)**3 - x**3)/h, h, 0) == 3*x**2 # 197
61
+ assert limit((1/(1 - x) - 3/(1 - x**3)), x, 1) == -1 # 198
62
+ assert limit((sqrt(1 + x) - 1)/(root3(1 + x) - 1), x, 0) == Rational(3)/2 # Primer 4
63
+ assert limit((sqrt(x) - 1)/(x - 1), x, 1) == Rational(1)/2 # 199
64
+ assert limit((sqrt(x) - 8)/(root3(x) - 4), x, 64) == 3 # 200
65
+ assert limit((root3(x) - 1)/(root4(x) - 1), x, 1) == Rational(4)/3 # 201
66
+ assert limit(
67
+ (root3(x**2) - 2*root3(x) + 1)/(x - 1)**2, x, 1) == Rational(1)/9 # 202
68
+
69
+
70
+ def test_Limits_simple_4a():
71
+ a = Symbol('a')
72
+ assert limit((sqrt(x) - sqrt(a))/(x - a), x, a) == 1/(2*sqrt(a)) # Primer 5
73
+ assert limit((sqrt(x) - 1)/(root3(x) - 1), x, 1) == Rational(3, 2) # 205
74
+ assert limit((sqrt(1 + x) - sqrt(1 - x))/x, x, 0) == 1 # 207
75
+ assert limit(sqrt(x**2 - 5*x + 6) - x, x, oo) == Rational(-5, 2) # 213
76
+
77
+
78
+ def test_limits_simple_4aa():
79
+ assert limit(x*(sqrt(x**2 + 1) - x), x, oo) == Rational(1)/2 # 214
80
+
81
+
82
+ def test_Limits_simple_4b():
83
+ #issue 3511
84
+ assert limit(x - root3(x**3 - 1), x, oo) == 0 # 215
85
+
86
+
87
+ def test_Limits_simple_4c():
88
+ assert limit(log(1 + exp(x))/x, x, -oo) == 0 # 267a
89
+ assert limit(log(1 + exp(x))/x, x, oo) == 1 # 267b
90
+
91
+
92
+ def test_bounded():
93
+ assert limit(sin(x)/x, x, oo) == 0 # 216b
94
+ assert limit(x*sin(1/x), x, 0) == 0 # 227a
95
+
96
+
97
+ def test_f1a():
98
+ #issue 3508:
99
+ assert limit((sin(2*x)/x)**(1 + x), x, 0) == 2 # Primer 7
100
+
101
+
102
+ def test_f1a2():
103
+ #issue 3509:
104
+ assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2) # Primer 9
105
+
106
+
107
+ def test_f1b():
108
+ m = Symbol("m")
109
+ n = Symbol("n")
110
+ h = Symbol("h")
111
+ a = Symbol("a")
112
+ assert limit(sin(x)/x, x, 2) == sin(2)/2 # 216a
113
+ assert limit(sin(3*x)/x, x, 0) == 3 # 217
114
+ assert limit(sin(5*x)/sin(2*x), x, 0) == Rational(5, 2) # 218
115
+ assert limit(sin(pi*x)/sin(3*pi*x), x, 0) == Rational(1, 3) # 219
116
+ assert limit(x*sin(pi/x), x, oo) == pi # 220
117
+ assert limit((1 - cos(x))/x**2, x, 0) == S.Half # 221
118
+ assert limit(x*sin(1/x), x, oo) == 1 # 227b
119
+ assert limit((cos(m*x) - cos(n*x))/x**2, x, 0) == -m**2/2 + n**2/2 # 232
120
+ assert limit((tan(x) - sin(x))/x**3, x, 0) == S.Half # 233
121
+ assert limit((x - sin(2*x))/(x + sin(3*x)), x, 0) == -Rational(1, 4) # 237
122
+ assert limit((1 - sqrt(cos(x)))/x**2, x, 0) == Rational(1, 4) # 239
123
+ assert limit((sqrt(1 + sin(x)) - sqrt(1 - sin(x)))/x, x, 0) == 1 # 240
124
+
125
+ assert limit((1 + h/x)**x, x, oo) == exp(h) # Primer 9
126
+ assert limit((sin(x) - sin(a))/(x - a), x, a) == cos(a) # 222, *176
127
+ assert limit((cos(x) - cos(a))/(x - a), x, a) == -sin(a) # 223
128
+ assert limit((sin(x + h) - sin(x))/h, h, 0) == cos(x) # 225
129
+
130
+
131
+ def test_f2a():
132
+ assert limit(((x + 1)/(2*x + 1))**(x**2), x, oo) == 0 # Primer 8
133
+
134
+
135
+ def test_f2():
136
+ assert limit((sqrt(
137
+ cos(x)) - root3(cos(x)))/(sin(x)**2), x, 0) == -Rational(1, 12) # *184
138
+
139
+
140
+ def test_f3():
141
+ a = Symbol('a')
142
+ #issue 3504
143
+ assert limit(asin(a*x)/x, x, 0) == a
.venv/lib/python3.13/site-packages/sympy/series/tests/test_formal.py ADDED
@@ -0,0 +1,618 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.concrete.summations import Sum
2
+ from sympy.core.add import Add
3
+ from sympy.core.function import (Derivative, Function)
4
+ from sympy.core.mul import Mul
5
+ from sympy.core.numbers import (I, Rational, oo, pi)
6
+ from sympy.core.singleton import S
7
+ from sympy.core.symbol import symbols
8
+ from sympy.functions.combinatorial.factorials import factorial
9
+ from sympy.functions.elementary.exponential import (exp, log)
10
+ from sympy.functions.elementary.hyperbolic import (acosh, asech)
11
+ from sympy.functions.elementary.miscellaneous import sqrt
12
+ from sympy.functions.elementary.trigonometric import (acos, asin, atan, cos, sin)
13
+ from sympy.functions.special.bessel import airyai
14
+ from sympy.functions.special.error_functions import erf
15
+ from sympy.functions.special.gamma_functions import gamma
16
+ from sympy.integrals.integrals import integrate
17
+ from sympy.series.formal import fps
18
+ from sympy.series.order import O
19
+ from sympy.series.formal import (rational_algorithm, FormalPowerSeries,
20
+ FormalPowerSeriesProduct, FormalPowerSeriesCompose,
21
+ FormalPowerSeriesInverse, simpleDE,
22
+ rational_independent, exp_re, hyper_re)
23
+ from sympy.testing.pytest import raises, XFAIL, slow
24
+
25
+ x, y, z = symbols('x y z')
26
+ n, m, k = symbols('n m k', integer=True)
27
+ f, r = Function('f'), Function('r')
28
+
29
+
30
+ def test_rational_algorithm():
31
+ f = 1 / ((x - 1)**2 * (x - 2))
32
+ assert rational_algorithm(f, x, k) == \
33
+ (-2**(-k - 1) + 1 - (factorial(k + 1) / factorial(k)), 0, 0)
34
+
35
+ f = (1 + x + x**2 + x**3) / ((x - 1) * (x - 2))
36
+ assert rational_algorithm(f, x, k) == \
37
+ (-15*2**(-k - 1) + 4, x + 4, 0)
38
+
39
+ f = z / (y*m - m*x - y*x + x**2)
40
+ assert rational_algorithm(f, x, k) == \
41
+ (((-y**(-k - 1)*z) / (y - m)) + ((m**(-k - 1)*z) / (y - m)), 0, 0)
42
+
43
+ f = x / (1 - x - x**2)
44
+ assert rational_algorithm(f, x, k) is None
45
+ assert rational_algorithm(f, x, k, full=True) == \
46
+ (((Rational(-1, 2) + sqrt(5)/2)**(-k - 1) *
47
+ (-sqrt(5)/10 + S.Half)) +
48
+ ((-sqrt(5)/2 - S.Half)**(-k - 1) *
49
+ (sqrt(5)/10 + S.Half)), 0, 0)
50
+
51
+ f = 1 / (x**2 + 2*x + 2)
52
+ assert rational_algorithm(f, x, k) is None
53
+ assert rational_algorithm(f, x, k, full=True) == \
54
+ ((I*(-1 + I)**(-k - 1)) / 2 - (I*(-1 - I)**(-k - 1)) / 2, 0, 0)
55
+
56
+ f = log(1 + x)
57
+ assert rational_algorithm(f, x, k) == \
58
+ (-(-1)**(-k) / k, 0, 1)
59
+
60
+ f = atan(x)
61
+ assert rational_algorithm(f, x, k) is None
62
+ assert rational_algorithm(f, x, k, full=True) == \
63
+ (((I*I**(-k)) / 2 - (I*(-I)**(-k)) / 2) / k, 0, 1)
64
+
65
+ f = x*atan(x) - log(1 + x**2) / 2
66
+ assert rational_algorithm(f, x, k) is None
67
+ assert rational_algorithm(f, x, k, full=True) == \
68
+ (((I*I**(-k + 1)) / 2 - (I*(-I)**(-k + 1)) / 2) /
69
+ (k*(k - 1)), 0, 2)
70
+
71
+ f = log((1 + x) / (1 - x)) / 2 - atan(x)
72
+ assert rational_algorithm(f, x, k) is None
73
+ assert rational_algorithm(f, x, k, full=True) == \
74
+ ((-(-1)**(-k) / 2 - (I*I**(-k)) / 2 + (I*(-I)**(-k)) / 2 +
75
+ S.Half) / k, 0, 1)
76
+
77
+ assert rational_algorithm(cos(x), x, k) is None
78
+
79
+
80
+ def test_rational_independent():
81
+ ri = rational_independent
82
+ assert ri([], x) == []
83
+ assert ri([cos(x), sin(x)], x) == [cos(x), sin(x)]
84
+ assert ri([x**2, sin(x), x*sin(x), x**3], x) == \
85
+ [x**3 + x**2, x*sin(x) + sin(x)]
86
+ assert ri([S.One, x*log(x), log(x), sin(x)/x, cos(x), sin(x), x], x) == \
87
+ [x + 1, x*log(x) + log(x), sin(x)/x + sin(x), cos(x)]
88
+
89
+
90
+ def test_simpleDE():
91
+ # Tests just the first valid DE
92
+ for DE in simpleDE(exp(x), x, f):
93
+ assert DE == (-f(x) + Derivative(f(x), x), 1)
94
+ break
95
+ for DE in simpleDE(sin(x), x, f):
96
+ assert DE == (f(x) + Derivative(f(x), x, x), 2)
97
+ break
98
+ for DE in simpleDE(log(1 + x), x, f):
99
+ assert DE == ((x + 1)*Derivative(f(x), x, 2) + Derivative(f(x), x), 2)
100
+ break
101
+ for DE in simpleDE(asin(x), x, f):
102
+ assert DE == (x*Derivative(f(x), x) + (x**2 - 1)*Derivative(f(x), x, x),
103
+ 2)
104
+ break
105
+ for DE in simpleDE(exp(x)*sin(x), x, f):
106
+ assert DE == (2*f(x) - 2*Derivative(f(x)) + Derivative(f(x), x, x), 2)
107
+ break
108
+ for DE in simpleDE(((1 + x)/(1 - x))**n, x, f):
109
+ assert DE == (2*n*f(x) + (x**2 - 1)*Derivative(f(x), x), 1)
110
+ break
111
+ for DE in simpleDE(airyai(x), x, f):
112
+ assert DE == (-x*f(x) + Derivative(f(x), x, x), 2)
113
+ break
114
+
115
+
116
+ def test_exp_re():
117
+ d = -f(x) + Derivative(f(x), x)
118
+ assert exp_re(d, r, k) == -r(k) + r(k + 1)
119
+
120
+ d = f(x) + Derivative(f(x), x, x)
121
+ assert exp_re(d, r, k) == r(k) + r(k + 2)
122
+
123
+ d = f(x) + Derivative(f(x), x) + Derivative(f(x), x, x)
124
+ assert exp_re(d, r, k) == r(k) + r(k + 1) + r(k + 2)
125
+
126
+ d = Derivative(f(x), x) + Derivative(f(x), x, x)
127
+ assert exp_re(d, r, k) == r(k) + r(k + 1)
128
+
129
+ d = Derivative(f(x), x, 3) + Derivative(f(x), x, 4) + Derivative(f(x))
130
+ assert exp_re(d, r, k) == r(k) + r(k + 2) + r(k + 3)
131
+
132
+
133
+ def test_hyper_re():
134
+ d = f(x) + Derivative(f(x), x, x)
135
+ assert hyper_re(d, r, k) == r(k) + (k+1)*(k+2)*r(k + 2)
136
+
137
+ d = -x*f(x) + Derivative(f(x), x, x)
138
+ assert hyper_re(d, r, k) == (k + 2)*(k + 3)*r(k + 3) - r(k)
139
+
140
+ d = 2*f(x) - 2*Derivative(f(x), x) + Derivative(f(x), x, x)
141
+ assert hyper_re(d, r, k) == \
142
+ (-2*k - 2)*r(k + 1) + (k + 1)*(k + 2)*r(k + 2) + 2*r(k)
143
+
144
+ d = 2*n*f(x) + (x**2 - 1)*Derivative(f(x), x)
145
+ assert hyper_re(d, r, k) == \
146
+ k*r(k) + 2*n*r(k + 1) + (-k - 2)*r(k + 2)
147
+
148
+ d = (x**10 + 4)*Derivative(f(x), x) + x*(x**10 - 1)*Derivative(f(x), x, x)
149
+ assert hyper_re(d, r, k) == \
150
+ (k*(k - 1) + k)*r(k) + (4*k - (k + 9)*(k + 10) + 40)*r(k + 10)
151
+
152
+ d = ((x**2 - 1)*Derivative(f(x), x, 3) + 3*x*Derivative(f(x), x, x) +
153
+ Derivative(f(x), x))
154
+ assert hyper_re(d, r, k) == \
155
+ ((k*(k - 2)*(k - 1) + 3*k*(k - 1) + k)*r(k) +
156
+ (-k*(k + 1)*(k + 2))*r(k + 2))
157
+
158
+
159
+ def test_fps():
160
+ assert fps(1) == 1
161
+ assert fps(2, x) == 2
162
+ assert fps(2, x, dir='+') == 2
163
+ assert fps(2, x, dir='-') == 2
164
+ assert fps(1/x + 1/x**2) == 1/x + 1/x**2
165
+ assert fps(log(1 + x), hyper=False, rational=False) == log(1 + x)
166
+
167
+ f = fps(x**2 + x + 1)
168
+ assert isinstance(f, FormalPowerSeries)
169
+ assert f.function == x**2 + x + 1
170
+ assert f[0] == 1
171
+ assert f[2] == x**2
172
+ assert f.truncate(4) == x**2 + x + 1 + O(x**4)
173
+ assert f.polynomial() == x**2 + x + 1
174
+
175
+ f = fps(log(1 + x))
176
+ assert isinstance(f, FormalPowerSeries)
177
+ assert f.function == log(1 + x)
178
+ assert f.subs(x, y) == f
179
+ assert f[:5] == [0, x, -x**2/2, x**3/3, -x**4/4]
180
+ assert f.as_leading_term(x) == x
181
+ assert f.polynomial(6) == x - x**2/2 + x**3/3 - x**4/4 + x**5/5
182
+
183
+ k = f.ak.variables[0]
184
+ assert f.infinite == Sum((-(-1)**(-k)*x**k)/k, (k, 1, oo))
185
+
186
+ ft, s = f.truncate(n=None), f[:5]
187
+ for i, t in enumerate(ft):
188
+ if i == 5:
189
+ break
190
+ assert s[i] == t
191
+
192
+ f = sin(x).fps(x)
193
+ assert isinstance(f, FormalPowerSeries)
194
+ assert f.truncate() == x - x**3/6 + x**5/120 + O(x**6)
195
+
196
+ raises(NotImplementedError, lambda: fps(y*x))
197
+ raises(ValueError, lambda: fps(x, dir=0))
198
+
199
+
200
+ @slow
201
+ def test_fps__rational():
202
+ assert fps(1/x) == (1/x)
203
+ assert fps((x**2 + x + 1) / x**3, dir=-1) == (x**2 + x + 1) / x**3
204
+
205
+ f = 1 / ((x - 1)**2 * (x - 2))
206
+ assert fps(f, x).truncate() == \
207
+ (Rational(-1, 2) - x*Rational(5, 4) - 17*x**2/8 - 49*x**3/16 - 129*x**4/32 -
208
+ 321*x**5/64 + O(x**6))
209
+
210
+ f = (1 + x + x**2 + x**3) / ((x - 1) * (x - 2))
211
+ assert fps(f, x).truncate() == \
212
+ (S.Half + x*Rational(5, 4) + 17*x**2/8 + 49*x**3/16 + 113*x**4/32 +
213
+ 241*x**5/64 + O(x**6))
214
+
215
+ f = x / (1 - x - x**2)
216
+ assert fps(f, x, full=True).truncate() == \
217
+ x + x**2 + 2*x**3 + 3*x**4 + 5*x**5 + O(x**6)
218
+
219
+ f = 1 / (x**2 + 2*x + 2)
220
+ assert fps(f, x, full=True).truncate() == \
221
+ S.Half - x/2 + x**2/4 - x**4/8 + x**5/8 + O(x**6)
222
+
223
+ f = log(1 + x)
224
+ assert fps(f, x).truncate() == \
225
+ x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
226
+ assert fps(f, x, dir=1).truncate() == fps(f, x, dir=-1).truncate()
227
+ assert fps(f, x, 2).truncate() == \
228
+ (log(3) - Rational(2, 3) - (x - 2)**2/18 + (x - 2)**3/81 -
229
+ (x - 2)**4/324 + (x - 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2)))
230
+ assert fps(f, x, 2, dir=-1).truncate() == \
231
+ (log(3) - Rational(2, 3) - (-x + 2)**2/18 - (-x + 2)**3/81 -
232
+ (-x + 2)**4/324 - (-x + 2)**5/1215 + x/3 + O((x - 2)**6, (x, 2)))
233
+
234
+ f = atan(x)
235
+ assert fps(f, x, full=True).truncate() == x - x**3/3 + x**5/5 + O(x**6)
236
+ assert fps(f, x, full=True, dir=1).truncate() == \
237
+ fps(f, x, full=True, dir=-1).truncate()
238
+ assert fps(f, x, 2, full=True).truncate() == \
239
+ (atan(2) - Rational(2, 5) - 2*(x - 2)**2/25 + 11*(x - 2)**3/375 -
240
+ 6*(x - 2)**4/625 + 41*(x - 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2)))
241
+ assert fps(f, x, 2, full=True, dir=-1).truncate() == \
242
+ (atan(2) - Rational(2, 5) - 2*(-x + 2)**2/25 - 11*(-x + 2)**3/375 -
243
+ 6*(-x + 2)**4/625 - 41*(-x + 2)**5/15625 + x/5 + O((x - 2)**6, (x, 2)))
244
+
245
+ f = x*atan(x) - log(1 + x**2) / 2
246
+ assert fps(f, x, full=True).truncate() == x**2/2 - x**4/12 + O(x**6)
247
+
248
+ f = log((1 + x) / (1 - x)) / 2 - atan(x)
249
+ assert fps(f, x, full=True).truncate(n=10) == 2*x**3/3 + 2*x**7/7 + O(x**10)
250
+
251
+
252
+ @slow
253
+ def test_fps__hyper():
254
+ f = sin(x)
255
+ assert fps(f, x).truncate() == x - x**3/6 + x**5/120 + O(x**6)
256
+
257
+ f = cos(x)
258
+ assert fps(f, x).truncate() == 1 - x**2/2 + x**4/24 + O(x**6)
259
+
260
+ f = exp(x)
261
+ assert fps(f, x).truncate() == \
262
+ 1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + O(x**6)
263
+
264
+ f = atan(x)
265
+ assert fps(f, x).truncate() == x - x**3/3 + x**5/5 + O(x**6)
266
+
267
+ f = exp(acos(x))
268
+ assert fps(f, x).truncate() == \
269
+ (exp(pi/2) - x*exp(pi/2) + x**2*exp(pi/2)/2 - x**3*exp(pi/2)/3 +
270
+ 5*x**4*exp(pi/2)/24 - x**5*exp(pi/2)/6 + O(x**6))
271
+
272
+ f = exp(acosh(x))
273
+ assert fps(f, x).truncate() == I + x - I*x**2/2 - I*x**4/8 + O(x**6)
274
+
275
+ f = atan(1/x)
276
+ assert fps(f, x).truncate() == pi/2 - x + x**3/3 - x**5/5 + O(x**6)
277
+
278
+ f = x*atan(x) - log(1 + x**2) / 2
279
+ assert fps(f, x, rational=False).truncate() == x**2/2 - x**4/12 + O(x**6)
280
+
281
+ f = log(1 + x)
282
+ assert fps(f, x, rational=False).truncate() == \
283
+ x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
284
+
285
+ f = airyai(x**2)
286
+ assert fps(f, x).truncate() == \
287
+ (3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) -
288
+ 3**Rational(2, 3)*x**2/(3*gamma(Rational(1, 3))) + O(x**6))
289
+
290
+ f = exp(x)*sin(x)
291
+ assert fps(f, x).truncate() == x + x**2 + x**3/3 - x**5/30 + O(x**6)
292
+
293
+ f = exp(x)*sin(x)/x
294
+ assert fps(f, x).truncate() == 1 + x + x**2/3 - x**4/30 - x**5/90 + O(x**6)
295
+
296
+ f = sin(x) * cos(x)
297
+ assert fps(f, x).truncate() == x - 2*x**3/3 + 2*x**5/15 + O(x**6)
298
+
299
+
300
+ def test_fps_shift():
301
+ f = x**-5*sin(x)
302
+ assert fps(f, x).truncate() == \
303
+ 1/x**4 - 1/(6*x**2) + Rational(1, 120) - x**2/5040 + x**4/362880 + O(x**6)
304
+
305
+ f = x**2*atan(x)
306
+ assert fps(f, x, rational=False).truncate() == \
307
+ x**3 - x**5/3 + O(x**6)
308
+
309
+ f = cos(sqrt(x))*x
310
+ assert fps(f, x).truncate() == \
311
+ x - x**2/2 + x**3/24 - x**4/720 + x**5/40320 + O(x**6)
312
+
313
+ f = x**2*cos(sqrt(x))
314
+ assert fps(f, x).truncate() == \
315
+ x**2 - x**3/2 + x**4/24 - x**5/720 + O(x**6)
316
+
317
+
318
+ def test_fps__Add_expr():
319
+ f = x*atan(x) - log(1 + x**2) / 2
320
+ assert fps(f, x).truncate() == x**2/2 - x**4/12 + O(x**6)
321
+
322
+ f = sin(x) + cos(x) - exp(x) + log(1 + x)
323
+ assert fps(f, x).truncate() == x - 3*x**2/2 - x**4/4 + x**5/5 + O(x**6)
324
+
325
+ f = 1/x + sin(x)
326
+ assert fps(f, x).truncate() == 1/x + x - x**3/6 + x**5/120 + O(x**6)
327
+
328
+ f = sin(x) - cos(x) + 1/(x - 1)
329
+ assert fps(f, x).truncate() == \
330
+ -2 - x**2/2 - 7*x**3/6 - 25*x**4/24 - 119*x**5/120 + O(x**6)
331
+
332
+
333
+ def test_fps__asymptotic():
334
+ f = exp(x)
335
+ assert fps(f, x, oo) == f
336
+ assert fps(f, x, -oo).truncate() == O(1/x**6, (x, oo))
337
+
338
+ f = erf(x)
339
+ assert fps(f, x, oo).truncate() == 1 + O(1/x**6, (x, oo))
340
+ assert fps(f, x, -oo).truncate() == -1 + O(1/x**6, (x, oo))
341
+
342
+ f = atan(x)
343
+ assert fps(f, x, oo, full=True).truncate() == \
344
+ -1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2 + O(1/x**6, (x, oo))
345
+ assert fps(f, x, -oo, full=True).truncate() == \
346
+ -1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2 + O(1/x**6, (x, oo))
347
+
348
+ f = log(1 + x)
349
+ assert fps(f, x, oo) != \
350
+ (-1/(5*x**5) - 1/(4*x**4) + 1/(3*x**3) - 1/(2*x**2) + 1/x - log(1/x) +
351
+ O(1/x**6, (x, oo)))
352
+ assert fps(f, x, -oo) != \
353
+ (-1/(5*x**5) - 1/(4*x**4) + 1/(3*x**3) - 1/(2*x**2) + 1/x + I*pi -
354
+ log(-1/x) + O(1/x**6, (x, oo)))
355
+
356
+
357
+ def test_fps__fractional():
358
+ f = sin(sqrt(x)) / x
359
+ assert fps(f, x).truncate() == \
360
+ (1/sqrt(x) - sqrt(x)/6 + x**Rational(3, 2)/120 -
361
+ x**Rational(5, 2)/5040 + x**Rational(7, 2)/362880 -
362
+ x**Rational(9, 2)/39916800 + x**Rational(11, 2)/6227020800 + O(x**6))
363
+
364
+ f = sin(sqrt(x)) * x
365
+ assert fps(f, x).truncate() == \
366
+ (x**Rational(3, 2) - x**Rational(5, 2)/6 + x**Rational(7, 2)/120 -
367
+ x**Rational(9, 2)/5040 + x**Rational(11, 2)/362880 + O(x**6))
368
+
369
+ f = atan(sqrt(x)) / x**2
370
+ assert fps(f, x).truncate() == \
371
+ (x**Rational(-3, 2) - x**Rational(-1, 2)/3 + x**S.Half/5 -
372
+ x**Rational(3, 2)/7 + x**Rational(5, 2)/9 - x**Rational(7, 2)/11 +
373
+ x**Rational(9, 2)/13 - x**Rational(11, 2)/15 + O(x**6))
374
+
375
+ f = exp(sqrt(x))
376
+ assert fps(f, x).truncate().expand() == \
377
+ (1 + x/2 + x**2/24 + x**3/720 + x**4/40320 + x**5/3628800 + sqrt(x) +
378
+ x**Rational(3, 2)/6 + x**Rational(5, 2)/120 + x**Rational(7, 2)/5040 +
379
+ x**Rational(9, 2)/362880 + x**Rational(11, 2)/39916800 + O(x**6))
380
+
381
+ f = exp(sqrt(x))*x
382
+ assert fps(f, x).truncate().expand() == \
383
+ (x + x**2/2 + x**3/24 + x**4/720 + x**5/40320 + x**Rational(3, 2) +
384
+ x**Rational(5, 2)/6 + x**Rational(7, 2)/120 + x**Rational(9, 2)/5040 +
385
+ x**Rational(11, 2)/362880 + O(x**6))
386
+
387
+
388
+ def test_fps__logarithmic_singularity():
389
+ f = log(1 + 1/x)
390
+ assert fps(f, x) != \
391
+ -log(x) + x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
392
+ assert fps(f, x, rational=False) != \
393
+ -log(x) + x - x**2/2 + x**3/3 - x**4/4 + x**5/5 + O(x**6)
394
+
395
+
396
+ @XFAIL
397
+ def test_fps__logarithmic_singularity_fail():
398
+ f = asech(x) # Algorithms for computing limits probably needs improvements
399
+ assert fps(f, x) == log(2) - log(x) - x**2/4 - 3*x**4/64 + O(x**6)
400
+
401
+
402
+ def test_fps_symbolic():
403
+ f = x**n*sin(x**2)
404
+ assert fps(f, x).truncate(8) == x**(n + 2) - x**(n + 6)/6 + O(x**(n + 8), x)
405
+
406
+ f = x**n*log(1 + x)
407
+ fp = fps(f, x)
408
+ k = fp.ak.variables[0]
409
+ assert fp.infinite == \
410
+ Sum((-(-1)**(-k)*x**(k + n))/k, (k, 1, oo))
411
+
412
+ f = (x - 2)**n*log(1 + x)
413
+ assert fps(f, x, 2).truncate() == \
414
+ ((x - 2)**n*log(3) + (x - 2)**(n + 1)/3 - (x - 2)**(n + 2)/18 + (x - 2)**(n + 3)/81 -
415
+ (x - 2)**(n + 4)/324 + (x - 2)**(n + 5)/1215 + O((x - 2)**(n + 6), (x, 2)))
416
+
417
+ f = x**(n - 2)*cos(x)
418
+ assert fps(f, x).truncate() == \
419
+ (x**(n - 2) - x**n/2 + x**(n + 2)/24 + O(x**(n + 4), x))
420
+
421
+ f = x**(n - 2)*sin(x) + x**n*exp(x)
422
+ assert fps(f, x).truncate() == \
423
+ (x**(n - 1) + x**(n + 1) + x**(n + 2)/2 + x**n +
424
+ x**(n + 4)/24 + x**(n + 5)/60 + O(x**(n + 6), x))
425
+
426
+ f = x**n*atan(x)
427
+ assert fps(f, x, oo).truncate() == \
428
+ (-x**(n - 5)/5 + x**(n - 3)/3 + x**n*(pi/2 - 1/x) +
429
+ O((1/x)**(-n)/x**6, (x, oo)))
430
+
431
+ f = x**(n/2)*cos(x)
432
+ assert fps(f, x).truncate() == \
433
+ x**(n/2) - x**(n/2 + 2)/2 + x**(n/2 + 4)/24 + O(x**(n/2 + 6), x)
434
+
435
+ f = x**(n + m)*sin(x)
436
+ assert fps(f, x).truncate() == \
437
+ x**(m + n + 1) - x**(m + n + 3)/6 + x**(m + n + 5)/120 + O(x**(m + n + 6), x)
438
+
439
+
440
+ def test_fps__slow():
441
+ f = x*exp(x)*sin(2*x) # TODO: rsolve needs improvement
442
+ assert fps(f, x).truncate() == 2*x**2 + 2*x**3 - x**4/3 - x**5 + O(x**6)
443
+
444
+
445
+ def test_fps__operations():
446
+ f1, f2 = fps(sin(x)), fps(cos(x))
447
+
448
+ fsum = f1 + f2
449
+ assert fsum.function == sin(x) + cos(x)
450
+ assert fsum.truncate() == \
451
+ 1 + x - x**2/2 - x**3/6 + x**4/24 + x**5/120 + O(x**6)
452
+
453
+ fsum = f1 + 1
454
+ assert fsum.function == sin(x) + 1
455
+ assert fsum.truncate() == 1 + x - x**3/6 + x**5/120 + O(x**6)
456
+
457
+ fsum = 1 + f2
458
+ assert fsum.function == cos(x) + 1
459
+ assert fsum.truncate() == 2 - x**2/2 + x**4/24 + O(x**6)
460
+
461
+ assert (f1 + x) == Add(f1, x)
462
+
463
+ assert -f2.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
464
+ assert (f1 - f1) is S.Zero
465
+
466
+ fsub = f1 - f2
467
+ assert fsub.function == sin(x) - cos(x)
468
+ assert fsub.truncate() == \
469
+ -1 + x + x**2/2 - x**3/6 - x**4/24 + x**5/120 + O(x**6)
470
+
471
+ fsub = f1 - 1
472
+ assert fsub.function == sin(x) - 1
473
+ assert fsub.truncate() == -1 + x - x**3/6 + x**5/120 + O(x**6)
474
+
475
+ fsub = 1 - f2
476
+ assert fsub.function == -cos(x) + 1
477
+ assert fsub.truncate() == x**2/2 - x**4/24 + O(x**6)
478
+
479
+ raises(ValueError, lambda: f1 + fps(exp(x), dir=-1))
480
+ raises(ValueError, lambda: f1 + fps(exp(x), x0=1))
481
+
482
+ fm = f1 * 3
483
+
484
+ assert fm.function == 3*sin(x)
485
+ assert fm.truncate() == 3*x - x**3/2 + x**5/40 + O(x**6)
486
+
487
+ fm = 3 * f2
488
+
489
+ assert fm.function == 3*cos(x)
490
+ assert fm.truncate() == 3 - 3*x**2/2 + x**4/8 + O(x**6)
491
+
492
+ assert (f1 * f2) == Mul(f1, f2)
493
+ assert (f1 * x) == Mul(f1, x)
494
+
495
+ fd = f1.diff()
496
+ assert fd.function == cos(x)
497
+ assert fd.truncate() == 1 - x**2/2 + x**4/24 + O(x**6)
498
+
499
+ fd = f2.diff()
500
+ assert fd.function == -sin(x)
501
+ assert fd.truncate() == -x + x**3/6 - x**5/120 + O(x**6)
502
+
503
+ fd = f2.diff().diff()
504
+ assert fd.function == -cos(x)
505
+ assert fd.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
506
+
507
+ f3 = fps(exp(sqrt(x)))
508
+ fd = f3.diff()
509
+ assert fd.truncate().expand() == \
510
+ (1/(2*sqrt(x)) + S.Half + x/12 + x**2/240 + x**3/10080 + x**4/725760 +
511
+ x**5/79833600 + sqrt(x)/4 + x**Rational(3, 2)/48 + x**Rational(5, 2)/1440 +
512
+ x**Rational(7, 2)/80640 + x**Rational(9, 2)/7257600 + x**Rational(11, 2)/958003200 +
513
+ O(x**6))
514
+
515
+ assert f1.integrate((x, 0, 1)) == -cos(1) + 1
516
+ assert integrate(f1, (x, 0, 1)) == -cos(1) + 1
517
+
518
+ fi = integrate(f1, x)
519
+ assert fi.function == -cos(x)
520
+ assert fi.truncate() == -1 + x**2/2 - x**4/24 + O(x**6)
521
+
522
+ fi = f2.integrate(x)
523
+ assert fi.function == sin(x)
524
+ assert fi.truncate() == x - x**3/6 + x**5/120 + O(x**6)
525
+
526
+ def test_fps__product():
527
+ f1, f2, f3 = fps(sin(x)), fps(exp(x)), fps(cos(x))
528
+
529
+ raises(ValueError, lambda: f1.product(exp(x), x))
530
+ raises(ValueError, lambda: f1.product(fps(exp(x), dir=-1), x, 4))
531
+ raises(ValueError, lambda: f1.product(fps(exp(x), x0=1), x, 4))
532
+ raises(ValueError, lambda: f1.product(fps(exp(y)), x, 4))
533
+
534
+ fprod = f1.product(f2, x)
535
+ assert isinstance(fprod, FormalPowerSeriesProduct)
536
+ assert isinstance(fprod.ffps, FormalPowerSeries)
537
+ assert isinstance(fprod.gfps, FormalPowerSeries)
538
+ assert fprod.f == sin(x)
539
+ assert fprod.g == exp(x)
540
+ assert fprod.function == sin(x) * exp(x)
541
+ assert fprod._eval_terms(4) == x + x**2 + x**3/3
542
+ assert fprod.truncate(4) == x + x**2 + x**3/3 + O(x**4)
543
+ assert fprod.polynomial(4) == x + x**2 + x**3/3
544
+
545
+ raises(NotImplementedError, lambda: fprod._eval_term(5))
546
+ raises(NotImplementedError, lambda: fprod.infinite)
547
+ raises(NotImplementedError, lambda: fprod._eval_derivative(x))
548
+ raises(NotImplementedError, lambda: fprod.integrate(x))
549
+
550
+ assert f1.product(f3, x)._eval_terms(4) == x - 2*x**3/3
551
+ assert f1.product(f3, x).truncate(4) == x - 2*x**3/3 + O(x**4)
552
+
553
+
554
+ def test_fps__compose():
555
+ f1, f2, f3 = fps(exp(x)), fps(sin(x)), fps(cos(x))
556
+
557
+ raises(ValueError, lambda: f1.compose(sin(x), x))
558
+ raises(ValueError, lambda: f1.compose(fps(sin(x), dir=-1), x, 4))
559
+ raises(ValueError, lambda: f1.compose(fps(sin(x), x0=1), x, 4))
560
+ raises(ValueError, lambda: f1.compose(fps(sin(y)), x, 4))
561
+
562
+ raises(ValueError, lambda: f1.compose(f3, x))
563
+ raises(ValueError, lambda: f2.compose(f3, x))
564
+
565
+ fcomp = f1.compose(f2, x)
566
+ assert isinstance(fcomp, FormalPowerSeriesCompose)
567
+ assert isinstance(fcomp.ffps, FormalPowerSeries)
568
+ assert isinstance(fcomp.gfps, FormalPowerSeries)
569
+ assert fcomp.f == exp(x)
570
+ assert fcomp.g == sin(x)
571
+ assert fcomp.function == exp(sin(x))
572
+ assert fcomp._eval_terms(6) == 1 + x + x**2/2 - x**4/8 - x**5/15
573
+ assert fcomp.truncate() == 1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6)
574
+ assert fcomp.truncate(5) == 1 + x + x**2/2 - x**4/8 + O(x**5)
575
+
576
+ raises(NotImplementedError, lambda: fcomp._eval_term(5))
577
+ raises(NotImplementedError, lambda: fcomp.infinite)
578
+ raises(NotImplementedError, lambda: fcomp._eval_derivative(x))
579
+ raises(NotImplementedError, lambda: fcomp.integrate(x))
580
+
581
+ assert f1.compose(f2, x).truncate(4) == 1 + x + x**2/2 + O(x**4)
582
+ assert f1.compose(f2, x).truncate(8) == \
583
+ 1 + x + x**2/2 - x**4/8 - x**5/15 - x**6/240 + x**7/90 + O(x**8)
584
+ assert f1.compose(f2, x).truncate(6) == \
585
+ 1 + x + x**2/2 - x**4/8 - x**5/15 + O(x**6)
586
+
587
+ assert f2.compose(f2, x).truncate(4) == x - x**3/3 + O(x**4)
588
+ assert f2.compose(f2, x).truncate(8) == x - x**3/3 + x**5/10 - 8*x**7/315 + O(x**8)
589
+ assert f2.compose(f2, x).truncate(6) == x - x**3/3 + x**5/10 + O(x**6)
590
+
591
+
592
+ def test_fps__inverse():
593
+ f1, f2, f3 = fps(sin(x)), fps(exp(x)), fps(cos(x))
594
+
595
+ raises(ValueError, lambda: f1.inverse(x))
596
+
597
+ finv = f2.inverse(x)
598
+ assert isinstance(finv, FormalPowerSeriesInverse)
599
+ assert isinstance(finv.ffps, FormalPowerSeries)
600
+ raises(ValueError, lambda: finv.gfps)
601
+
602
+ assert finv.f == exp(x)
603
+ assert finv.function == exp(-x)
604
+ assert finv._eval_terms(5) == 1 - x + x**2/2 - x**3/6 + x**4/24
605
+ assert finv.truncate() == 1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + O(x**6)
606
+ assert finv.truncate(5) == 1 - x + x**2/2 - x**3/6 + x**4/24 + O(x**5)
607
+
608
+ raises(NotImplementedError, lambda: finv._eval_term(5))
609
+ raises(ValueError, lambda: finv.g)
610
+ raises(NotImplementedError, lambda: finv.infinite)
611
+ raises(NotImplementedError, lambda: finv._eval_derivative(x))
612
+ raises(NotImplementedError, lambda: finv.integrate(x))
613
+
614
+ assert f2.inverse(x).truncate(8) == \
615
+ 1 - x + x**2/2 - x**3/6 + x**4/24 - x**5/120 + x**6/720 - x**7/5040 + O(x**8)
616
+
617
+ assert f3.inverse(x).truncate() == 1 + x**2/2 + 5*x**4/24 + O(x**6)
618
+ assert f3.inverse(x).truncate(8) == 1 + x**2/2 + 5*x**4/24 + 61*x**6/720 + O(x**8)
.venv/lib/python3.13/site-packages/sympy/series/tests/test_fourier.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.add import Add
2
+ from sympy.core.numbers import (Rational, oo, pi)
3
+ from sympy.core.singleton import S
4
+ from sympy.core.symbol import symbols
5
+ from sympy.functions.elementary.exponential import (exp, log)
6
+ from sympy.functions.elementary.piecewise import Piecewise
7
+ from sympy.functions.elementary.trigonometric import (cos, sin, sinc, tan)
8
+ from sympy.series.fourier import fourier_series
9
+ from sympy.series.fourier import FourierSeries
10
+ from sympy.testing.pytest import raises
11
+ from functools import lru_cache
12
+
13
+ x, y, z = symbols('x y z')
14
+
15
+ # Don't declare these during import because they are slow
16
+ @lru_cache()
17
+ def _get_examples():
18
+ fo = fourier_series(x, (x, -pi, pi))
19
+ fe = fourier_series(x**2, (-pi, pi))
20
+ fp = fourier_series(Piecewise((0, x < 0), (pi, True)), (x, -pi, pi))
21
+ return fo, fe, fp
22
+
23
+
24
+ def test_FourierSeries():
25
+ fo, fe, fp = _get_examples()
26
+
27
+ assert fourier_series(1, (-pi, pi)) == 1
28
+ assert (Piecewise((0, x < 0), (pi, True)).
29
+ fourier_series((x, -pi, pi)).truncate()) == fp.truncate()
30
+ assert isinstance(fo, FourierSeries)
31
+ assert fo.function == x
32
+ assert fo.x == x
33
+ assert fo.period == (-pi, pi)
34
+
35
+ assert fo.term(3) == 2*sin(3*x) / 3
36
+ assert fe.term(3) == -4*cos(3*x) / 9
37
+ assert fp.term(3) == 2*sin(3*x) / 3
38
+
39
+ assert fo.as_leading_term(x) == 2*sin(x)
40
+ assert fe.as_leading_term(x) == pi**2 / 3
41
+ assert fp.as_leading_term(x) == pi / 2
42
+
43
+ assert fo.truncate() == 2*sin(x) - sin(2*x) + (2*sin(3*x) / 3)
44
+ assert fe.truncate() == -4*cos(x) + cos(2*x) + pi**2 / 3
45
+ assert fp.truncate() == 2*sin(x) + (2*sin(3*x) / 3) + pi / 2
46
+
47
+ fot = fo.truncate(n=None)
48
+ s = [0, 2*sin(x), -sin(2*x)]
49
+ for i, t in enumerate(fot):
50
+ if i == 3:
51
+ break
52
+ assert s[i] == t
53
+
54
+ def _check_iter(f, i):
55
+ for ind, t in enumerate(f):
56
+ assert t == f[ind] # noqa: PLR1736
57
+ if ind == i:
58
+ break
59
+
60
+ _check_iter(fo, 3)
61
+ _check_iter(fe, 3)
62
+ _check_iter(fp, 3)
63
+
64
+ assert fo.subs(x, x**2) == fo
65
+
66
+ raises(ValueError, lambda: fourier_series(x, (0, 1, 2)))
67
+ raises(ValueError, lambda: fourier_series(x, (x, 0, oo)))
68
+ raises(ValueError, lambda: fourier_series(x*y, (0, oo)))
69
+
70
+
71
+ def test_FourierSeries_2():
72
+ p = Piecewise((0, x < 0), (x, True))
73
+ f = fourier_series(p, (x, -2, 2))
74
+
75
+ assert f.term(3) == (2*sin(3*pi*x / 2) / (3*pi) -
76
+ 4*cos(3*pi*x / 2) / (9*pi**2))
77
+ assert f.truncate() == (2*sin(pi*x / 2) / pi - sin(pi*x) / pi -
78
+ 4*cos(pi*x / 2) / pi**2 + S.Half)
79
+
80
+
81
+ def test_square_wave():
82
+ """Test if fourier_series approximates discontinuous function correctly."""
83
+ square_wave = Piecewise((1, x < pi), (-1, True))
84
+ s = fourier_series(square_wave, (x, 0, 2*pi))
85
+
86
+ assert s.truncate(3) == 4 / pi * sin(x) + 4 / (3 * pi) * sin(3 * x) + \
87
+ 4 / (5 * pi) * sin(5 * x)
88
+ assert s.sigma_approximation(4) == 4 / pi * sin(x) * sinc(pi / 4) + \
89
+ 4 / (3 * pi) * sin(3 * x) * sinc(3 * pi / 4)
90
+
91
+
92
+ def test_sawtooth_wave():
93
+ s = fourier_series(x, (x, 0, pi))
94
+ assert s.truncate(4) == \
95
+ pi/2 - sin(2*x) - sin(4*x)/2 - sin(6*x)/3
96
+ s = fourier_series(x, (x, 0, 1))
97
+ assert s.truncate(4) == \
98
+ S.Half - sin(2*pi*x)/pi - sin(4*pi*x)/(2*pi) - sin(6*pi*x)/(3*pi)
99
+
100
+
101
+ def test_FourierSeries__operations():
102
+ fo, fe, fp = _get_examples()
103
+
104
+ fes = fe.scale(-1).shift(pi**2)
105
+ assert fes.truncate() == 4*cos(x) - cos(2*x) + 2*pi**2 / 3
106
+
107
+ assert fp.shift(-pi/2).truncate() == (2*sin(x) + (2*sin(3*x) / 3) +
108
+ (2*sin(5*x) / 5))
109
+
110
+ fos = fo.scale(3)
111
+ assert fos.truncate() == 6*sin(x) - 3*sin(2*x) + 2*sin(3*x)
112
+
113
+ fx = fe.scalex(2).shiftx(1)
114
+ assert fx.truncate() == -4*cos(2*x + 2) + cos(4*x + 4) + pi**2 / 3
115
+
116
+ fl = fe.scalex(3).shift(-pi).scalex(2).shiftx(1).scale(4)
117
+ assert fl.truncate() == (-16*cos(6*x + 6) + 4*cos(12*x + 12) -
118
+ 4*pi + 4*pi**2 / 3)
119
+
120
+ raises(ValueError, lambda: fo.shift(x))
121
+ raises(ValueError, lambda: fo.shiftx(sin(x)))
122
+ raises(ValueError, lambda: fo.scale(x*y))
123
+ raises(ValueError, lambda: fo.scalex(x**2))
124
+
125
+
126
+ def test_FourierSeries__neg():
127
+ fo, fe, fp = _get_examples()
128
+
129
+ assert (-fo).truncate() == -2*sin(x) + sin(2*x) - (2*sin(3*x) / 3)
130
+ assert (-fe).truncate() == +4*cos(x) - cos(2*x) - pi**2 / 3
131
+
132
+
133
+ def test_FourierSeries__add__sub():
134
+ fo, fe, fp = _get_examples()
135
+
136
+ assert fo + fo == fo.scale(2)
137
+ assert fo - fo == 0
138
+ assert -fe - fe == fe.scale(-2)
139
+
140
+ assert (fo + fe).truncate() == 2*sin(x) - sin(2*x) - 4*cos(x) + cos(2*x) \
141
+ + pi**2 / 3
142
+ assert (fo - fe).truncate() == 2*sin(x) - sin(2*x) + 4*cos(x) - cos(2*x) \
143
+ - pi**2 / 3
144
+
145
+ assert isinstance(fo + 1, Add)
146
+
147
+ raises(ValueError, lambda: fo + fourier_series(x, (x, 0, 2)))
148
+
149
+
150
+ def test_FourierSeries_finite():
151
+
152
+ assert fourier_series(sin(x)).truncate(1) == sin(x)
153
+ # assert type(fourier_series(sin(x)*log(x))).truncate() == FourierSeries
154
+ # assert type(fourier_series(sin(x**2+6))).truncate() == FourierSeries
155
+ assert fourier_series(sin(x)*log(y)*exp(z),(x,pi,-pi)).truncate() == sin(x)*log(y)*exp(z)
156
+ assert fourier_series(sin(x)**6).truncate(oo) == -15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 \
157
+ + Rational(5, 16)
158
+ assert fourier_series(sin(x) ** 6).truncate() == -15 * cos(2 * x) / 32 + 3 * cos(4 * x) / 16 \
159
+ + Rational(5, 16)
160
+ assert fourier_series(sin(4*x+3) + cos(3*x+4)).truncate(oo) == -sin(4)*sin(3*x) + sin(4*x)*cos(3) \
161
+ + cos(4)*cos(3*x) + sin(3)*cos(4*x)
162
+ assert fourier_series(sin(x)+cos(x)*tan(x)).truncate(oo) == 2*sin(x)
163
+ assert fourier_series(cos(pi*x), (x, -1, 1)).truncate(oo) == cos(pi*x)
164
+ assert fourier_series(cos(3*pi*x + 4) - sin(4*pi*x)*log(pi*y), (x, -1, 1)).truncate(oo) == -log(pi*y)*sin(4*pi*x)\
165
+ - sin(4)*sin(3*pi*x) + cos(4)*cos(3*pi*x)
.venv/lib/python3.13/site-packages/sympy/series/tests/test_kauers.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.series.kauers import finite_diff
2
+ from sympy.series.kauers import finite_diff_kauers
3
+ from sympy.abc import x, y, z, m, n, w
4
+ from sympy.core.numbers import pi
5
+ from sympy.functions.elementary.trigonometric import (cos, sin)
6
+ from sympy.concrete.summations import Sum
7
+
8
+
9
+ def test_finite_diff():
10
+ assert finite_diff(x**2 + 2*x + 1, x) == 2*x + 3
11
+ assert finite_diff(y**3 + 2*y**2 + 3*y + 5, y) == 3*y**2 + 7*y + 6
12
+ assert finite_diff(z**2 - 2*z + 3, z) == 2*z - 1
13
+ assert finite_diff(w**2 + 3*w - 2, w) == 2*w + 4
14
+ assert finite_diff(sin(x), x, pi/6) == -sin(x) + sin(x + pi/6)
15
+ assert finite_diff(cos(y), y, pi/3) == -cos(y) + cos(y + pi/3)
16
+ assert finite_diff(x**2 - 2*x + 3, x, 2) == 4*x
17
+ assert finite_diff(n**2 - 2*n + 3, n, 3) == 6*n + 3
18
+
19
+ def test_finite_diff_kauers():
20
+ assert finite_diff_kauers(Sum(x**2, (x, 1, n))) == (n + 1)**2
21
+ assert finite_diff_kauers(Sum(y, (y, 1, m))) == (m + 1)
22
+ assert finite_diff_kauers(Sum((x*y), (x, 1, m), (y, 1, n))) == (m + 1)*(n + 1)
23
+ assert finite_diff_kauers(Sum((x*y**2), (x, 1, m), (y, 1, n))) == (n + 1)**2*(m + 1)
.venv/lib/python3.13/site-packages/sympy/series/tests/test_limits.py ADDED
@@ -0,0 +1,1440 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import product
2
+
3
+ from sympy.concrete.summations import Sum
4
+ from sympy.core.function import (Function, diff)
5
+ from sympy.core import EulerGamma, GoldenRatio
6
+ from sympy.core.mod import Mod
7
+ from sympy.core.numbers import (E, I, Rational, oo, pi, zoo)
8
+ from sympy.core.singleton import S
9
+ from sympy.core.symbol import (Symbol, symbols)
10
+ from sympy.functions.combinatorial.numbers import fibonacci
11
+ from sympy.functions.combinatorial.factorials import (binomial, factorial, subfactorial)
12
+ from sympy.functions.elementary.complexes import (Abs, re, sign)
13
+ from sympy.functions.elementary.exponential import (LambertW, exp, log)
14
+ from sympy.functions.elementary.hyperbolic import (atanh, asinh, acosh, acoth, acsch, asech, tanh, sinh)
15
+ from sympy.functions.elementary.integers import (ceiling, floor, frac)
16
+ from sympy.functions.elementary.miscellaneous import (cbrt, real_root, sqrt)
17
+ from sympy.functions.elementary.piecewise import Piecewise
18
+ from sympy.functions.elementary.trigonometric import (acos, acot, acsc, asec, asin,
19
+ atan, cos, cot, csc, sec, sin, tan)
20
+ from sympy.functions.special.bessel import (besseli, bessely, besselj, besselk)
21
+ from sympy.functions.special.error_functions import (Ei, erf, erfc, erfi, fresnelc, fresnels)
22
+ from sympy.functions.special.gamma_functions import (digamma, gamma, uppergamma)
23
+ from sympy.functions.special.hyper import meijerg
24
+ from sympy.integrals.integrals import (Integral, integrate)
25
+ from sympy.series.limits import (Limit, limit)
26
+ from sympy.simplify.simplify import (logcombine, simplify)
27
+ from sympy.simplify.hyperexpand import hyperexpand
28
+
29
+ from sympy.calculus.accumulationbounds import AccumBounds
30
+ from sympy.core.mul import Mul
31
+ from sympy.series.limits import heuristics
32
+ from sympy.series.order import Order
33
+ from sympy.testing.pytest import XFAIL, raises
34
+
35
+ from sympy import elliptic_e, elliptic_k
36
+
37
+ from sympy.abc import x, y, z, k
38
+ n = Symbol('n', integer=True, positive=True)
39
+
40
+
41
+ def test_basic1():
42
+ assert limit(x, x, oo) is oo
43
+ assert limit(x, x, -oo) is -oo
44
+ assert limit(-x, x, oo) is -oo
45
+ assert limit(x**2, x, -oo) is oo
46
+ assert limit(-x**2, x, oo) is -oo
47
+ assert limit(x*log(x), x, 0, dir="+") == 0
48
+ assert limit(1/x, x, oo) == 0
49
+ assert limit(exp(x), x, oo) is oo
50
+ assert limit(-exp(x), x, oo) is -oo
51
+ assert limit(exp(x)/x, x, oo) is oo
52
+ assert limit(1/x - exp(-x), x, oo) == 0
53
+ assert limit(x + 1/x, x, oo) is oo
54
+ assert limit(x - x**2, x, oo) is -oo
55
+ assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
56
+ assert limit((1 + x)**oo, x, 0) == Limit((x + 1)**oo, x, 0)
57
+ assert limit((1 + x)**oo, x, 0, dir='-') == Limit((x + 1)**oo, x, 0, dir='-')
58
+ assert limit((1 + x + y)**oo, x, 0, dir='-') == Limit((1 + x + y)**oo, x, 0, dir='-')
59
+ assert limit(y/x/log(x), x, 0) == -oo*sign(y)
60
+ assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
61
+ assert limit(gamma(1/x + 3), x, oo) == 2
62
+ assert limit(S.NaN, x, -oo) is S.NaN
63
+ assert limit(Order(2)*x, x, S.NaN) is S.NaN
64
+ assert limit(1/(x - 1), x, 1, dir="+") is oo
65
+ assert limit(1/(x - 1), x, 1, dir="-") is -oo
66
+ assert limit(1/(5 - x)**3, x, 5, dir="+") is -oo
67
+ assert limit(1/(5 - x)**3, x, 5, dir="-") is oo
68
+ assert limit(1/sin(x), x, pi, dir="+") is -oo
69
+ assert limit(1/sin(x), x, pi, dir="-") is oo
70
+ assert limit(1/cos(x), x, pi/2, dir="+") is -oo
71
+ assert limit(1/cos(x), x, pi/2, dir="-") is oo
72
+ assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") is oo
73
+ assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") is -oo
74
+ assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="+") is -oo
75
+ assert limit(1/cot(x)**3, x, (pi*Rational(3, 2)), dir="-") is oo
76
+ assert limit(tan(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
77
+ assert limit(cot(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
78
+ assert limit(sec(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
79
+ assert limit(csc(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
80
+
81
+ # test bi-directional limits
82
+ assert limit(sin(x)/x, x, 0, dir="+-") == 1
83
+ assert limit(x**2, x, 0, dir="+-") == 0
84
+ assert limit(1/x**2, x, 0, dir="+-") is oo
85
+
86
+ # test failing bi-directional limits
87
+ assert limit(1/x, x, 0, dir="+-") is zoo
88
+ # approaching 0
89
+ # from dir="+"
90
+ assert limit(1 + 1/x, x, 0) is oo
91
+ # from dir='-'
92
+ # Add
93
+ assert limit(1 + 1/x, x, 0, dir='-') is -oo
94
+ # Pow
95
+ assert limit(x**(-2), x, 0, dir='-') is oo
96
+ assert limit(x**(-3), x, 0, dir='-') is -oo
97
+ assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
98
+ assert limit(x**2, x, 0, dir='-') == 0
99
+ assert limit(sqrt(x), x, 0, dir='-') == 0
100
+ assert limit(x**-pi, x, 0, dir='-') == -oo*(-1)**(1 - pi)
101
+ assert limit((1 + cos(x))**oo, x, 0) == Limit((cos(x) + 1)**oo, x, 0)
102
+
103
+ # test pull request 22491
104
+ assert limit(1/asin(x), x, 0, dir = '+') == oo
105
+ assert limit(1/asin(x), x, 0, dir = '-') == -oo
106
+ assert limit(1/sinh(x), x, 0, dir = '+') == oo
107
+ assert limit(1/sinh(x), x, 0, dir = '-') == -oo
108
+ assert limit(log(1/x) + 1/sin(x), x, 0, dir = '+') == oo
109
+ assert limit(log(1/x) + 1/x, x, 0, dir = '+') == oo
110
+
111
+
112
+ def test_basic2():
113
+ assert limit(x**x, x, 0, dir="+") == 1
114
+ assert limit((exp(x) - 1)/x, x, 0) == 1
115
+ assert limit(1 + 1/x, x, oo) == 1
116
+ assert limit(-exp(1/x), x, oo) == -1
117
+ assert limit(x + exp(-x), x, oo) is oo
118
+ assert limit(x + exp(-x**2), x, oo) is oo
119
+ assert limit(x + exp(-exp(x)), x, oo) is oo
120
+ assert limit(13 + 1/x - exp(-x), x, oo) == 13
121
+
122
+
123
+ def test_basic3():
124
+ assert limit(1/x, x, 0, dir="+") is oo
125
+ assert limit(1/x, x, 0, dir="-") is -oo
126
+
127
+
128
+ def test_basic4():
129
+ assert limit(2*x + y*x, x, 0) == 0
130
+ assert limit(2*x + y*x, x, 1) == 2 + y
131
+ assert limit(2*x**8 + y*x**(-3), x, -2) == 512 - y/8
132
+ assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0
133
+ assert integrate(1/(x**3 + 1), (x, 0, oo)) == 2*pi*sqrt(3)/9
134
+
135
+
136
+ def test_log():
137
+ # https://github.com/sympy/sympy/issues/21598
138
+ a, b, c = symbols('a b c', positive=True)
139
+ A = log(a/b) - (log(a) - log(b))
140
+ assert A.limit(a, oo) == 0
141
+ assert (A * c).limit(a, oo) == 0
142
+
143
+ tau, x = symbols('tau x', positive=True)
144
+ # The value of manualintegrate in the issue
145
+ expr = tau**2*((tau - 1)*(tau + 1)*log(x + 1)/(tau**2 + 1)**2 + 1/((tau**2\
146
+ + 1)*(x + 1)) - (-2*tau*atan(x/tau) + (tau**2/2 - 1/2)*log(tau**2\
147
+ + x**2))/(tau**2 + 1)**2)
148
+ assert limit(expr, x, oo) == pi*tau**3/(tau**2 + 1)**2
149
+
150
+
151
+ def test_piecewise():
152
+ # https://github.com/sympy/sympy/issues/18363
153
+ assert limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+') == Rational(1, 12)
154
+
155
+
156
+ def test_piecewise2():
157
+ func1 = 2*sqrt(x)*Piecewise(((4*x - 2)/Abs(sqrt(4 - 4*(2*x - 1)**2)), 4*x - 2\
158
+ >= 0), ((2 - 4*x)/Abs(sqrt(4 - 4*(2*x - 1)**2)), True))
159
+ func2 = Piecewise((x**2/2, x <= 0.5), (x/2 - 0.125, True))
160
+ func3 = Piecewise(((x - 9) / 5, x < -1), ((x - 9) / 5, x > 4), (sqrt(Abs(x - 3)), True))
161
+ assert limit(func1, x, 0) == 1
162
+ assert limit(func2, x, 0) == 0
163
+ assert limit(func3, x, -1) == 2
164
+
165
+
166
+ def test_basic5():
167
+ class my(Function):
168
+ @classmethod
169
+ def eval(cls, arg):
170
+ if arg is S.Infinity:
171
+ return S.NaN
172
+ assert limit(my(x), x, oo) == Limit(my(x), x, oo)
173
+
174
+
175
+ def test_issue_3885():
176
+ assert limit(x*y + x*z, z, 2) == x*y + 2*x
177
+
178
+
179
+ def test_Limit():
180
+ assert Limit(sin(x)/x, x, 0) != 1
181
+ assert Limit(sin(x)/x, x, 0).doit() == 1
182
+ assert Limit(x, x, 0, dir='+-').args == (x, x, 0, Symbol('+-'))
183
+
184
+
185
+ def test_floor():
186
+ assert limit(floor(x), x, -2, "+") == -2
187
+ assert limit(floor(x), x, -2, "-") == -3
188
+ assert limit(floor(x), x, -1, "+") == -1
189
+ assert limit(floor(x), x, -1, "-") == -2
190
+ assert limit(floor(x), x, 0, "+") == 0
191
+ assert limit(floor(x), x, 0, "-") == -1
192
+ assert limit(floor(x), x, 1, "+") == 1
193
+ assert limit(floor(x), x, 1, "-") == 0
194
+ assert limit(floor(x), x, 2, "+") == 2
195
+ assert limit(floor(x), x, 2, "-") == 1
196
+ assert limit(floor(x), x, 248, "+") == 248
197
+ assert limit(floor(x), x, 248, "-") == 247
198
+
199
+ # https://github.com/sympy/sympy/issues/14478
200
+ assert limit(x*floor(3/x)/2, x, 0, '+') == Rational(3, 2)
201
+ assert limit(floor(x + 1/2) - floor(x), x, oo) == AccumBounds(-S.Half, S(3)/2)
202
+
203
+ # test issue 9158
204
+ assert limit(floor(atan(x)), x, oo) == 1
205
+ assert limit(floor(atan(x)), x, -oo) == -2
206
+ assert limit(ceiling(atan(x)), x, oo) == 2
207
+ assert limit(ceiling(atan(x)), x, -oo) == -1
208
+
209
+
210
+ def test_floor_requires_robust_assumptions():
211
+ assert limit(floor(sin(x)), x, 0, "+") == 0
212
+ assert limit(floor(sin(x)), x, 0, "-") == -1
213
+ assert limit(floor(cos(x)), x, 0, "+") == 0
214
+ assert limit(floor(cos(x)), x, 0, "-") == 0
215
+ assert limit(floor(5 + sin(x)), x, 0, "+") == 5
216
+ assert limit(floor(5 + sin(x)), x, 0, "-") == 4
217
+ assert limit(floor(5 + cos(x)), x, 0, "+") == 5
218
+ assert limit(floor(5 + cos(x)), x, 0, "-") == 5
219
+
220
+
221
+ def test_ceiling():
222
+ assert limit(ceiling(x), x, -2, "+") == -1
223
+ assert limit(ceiling(x), x, -2, "-") == -2
224
+ assert limit(ceiling(x), x, -1, "+") == 0
225
+ assert limit(ceiling(x), x, -1, "-") == -1
226
+ assert limit(ceiling(x), x, 0, "+") == 1
227
+ assert limit(ceiling(x), x, 0, "-") == 0
228
+ assert limit(ceiling(x), x, 1, "+") == 2
229
+ assert limit(ceiling(x), x, 1, "-") == 1
230
+ assert limit(ceiling(x), x, 2, "+") == 3
231
+ assert limit(ceiling(x), x, 2, "-") == 2
232
+ assert limit(ceiling(x), x, 248, "+") == 249
233
+ assert limit(ceiling(x), x, 248, "-") == 248
234
+
235
+ # https://github.com/sympy/sympy/issues/14478
236
+ assert limit(x*ceiling(3/x)/2, x, 0, '+') == Rational(3, 2)
237
+ assert limit(ceiling(x + 1/2) - ceiling(x), x, oo) == AccumBounds(-S.Half, S(3)/2)
238
+
239
+
240
+ def test_ceiling_requires_robust_assumptions():
241
+ assert limit(ceiling(sin(x)), x, 0, "+") == 1
242
+ assert limit(ceiling(sin(x)), x, 0, "-") == 0
243
+ assert limit(ceiling(cos(x)), x, 0, "+") == 1
244
+ assert limit(ceiling(cos(x)), x, 0, "-") == 1
245
+ assert limit(ceiling(5 + sin(x)), x, 0, "+") == 6
246
+ assert limit(ceiling(5 + sin(x)), x, 0, "-") == 5
247
+ assert limit(ceiling(5 + cos(x)), x, 0, "+") == 6
248
+ assert limit(ceiling(5 + cos(x)), x, 0, "-") == 6
249
+
250
+
251
+ def test_frac():
252
+ assert limit(frac(x), x, oo) == AccumBounds(0, 1)
253
+ assert limit(frac(x)**(1/x), x, oo) == AccumBounds(0, 1)
254
+ assert limit(frac(x)**(1/x), x, -oo) == AccumBounds(1, oo)
255
+ assert limit(frac(x)**x, x, oo) == AccumBounds(0, oo) # wolfram gives (0, 1)
256
+ assert limit(frac(sin(x)), x, 0, "+") == 0
257
+ assert limit(frac(sin(x)), x, 0, "-") == 1
258
+ assert limit(frac(cos(x)), x, 0, "+-") == 1
259
+ assert limit(frac(x**2), x, 0, "+-") == 0
260
+ raises(ValueError, lambda: limit(frac(x), x, 0, '+-'))
261
+ assert limit(frac(-2*x + 1), x, 0, "+") == 1
262
+ assert limit(frac(-2*x + 1), x, 0, "-") == 0
263
+ assert limit(frac(x + S.Half), x, 0, "+-") == S(1)/2
264
+ assert limit(frac(1/x), x, 0) == AccumBounds(0, 1)
265
+
266
+
267
+ def test_issue_14355():
268
+ assert limit(floor(sin(x)/x), x, 0, '+') == 0
269
+ assert limit(floor(sin(x)/x), x, 0, '-') == 0
270
+ # test comment https://github.com/sympy/sympy/issues/14355#issuecomment-372121314
271
+ assert limit(floor(-tan(x)/x), x, 0, '+') == -2
272
+ assert limit(floor(-tan(x)/x), x, 0, '-') == -2
273
+
274
+
275
+ def test_atan():
276
+ x = Symbol("x", real=True)
277
+ assert limit(atan(x)*sin(1/x), x, 0) == 0
278
+ assert limit(atan(x) + sqrt(x + 1) - sqrt(x), x, oo) == pi/2
279
+
280
+
281
+ def test_set_signs():
282
+ assert limit(abs(x), x, 0) == 0
283
+ assert limit(abs(sin(x)), x, 0) == 0
284
+ assert limit(abs(cos(x)), x, 0) == 1
285
+ assert limit(abs(sin(x + 1)), x, 0) == sin(1)
286
+
287
+ # https://github.com/sympy/sympy/issues/9449
288
+ assert limit((Abs(x + y) - Abs(x - y))/(2*x), x, 0) == sign(y)
289
+
290
+ # https://github.com/sympy/sympy/issues/12398
291
+ assert limit(Abs(log(x)/x**3), x, oo) == 0
292
+ assert limit(x*(Abs(log(x)/x**3)/Abs(log(x + 1)/(x + 1)**3) - 1), x, oo) == 3
293
+
294
+ # https://github.com/sympy/sympy/issues/18501
295
+ assert limit(Abs(log(x - 1)**3 - 1), x, 1, '+') == oo
296
+
297
+ # https://github.com/sympy/sympy/issues/18997
298
+ assert limit(Abs(log(x)), x, 0) == oo
299
+ assert limit(Abs(log(Abs(x))), x, 0) == oo
300
+
301
+ # https://github.com/sympy/sympy/issues/19026
302
+ z = Symbol('z', positive=True)
303
+ assert limit(Abs(log(z) + 1)/log(z), z, oo) == 1
304
+
305
+ # https://github.com/sympy/sympy/issues/20704
306
+ assert limit(z*(Abs(1/z + y) - Abs(y - 1/z))/2, z, 0) == 0
307
+
308
+ # https://github.com/sympy/sympy/issues/21606
309
+ assert limit(cos(z)/sign(z), z, pi, '-') == -1
310
+
311
+
312
+ def test_heuristic():
313
+ x = Symbol("x", real=True)
314
+ assert heuristics(sin(1/x) + atan(x), x, 0, '+') == AccumBounds(-1, 1)
315
+ assert limit(log(2 + sqrt(atan(x))*sqrt(sin(1/x))), x, 0) == log(2)
316
+
317
+
318
+ def test_issue_3871():
319
+ z = Symbol("z", positive=True)
320
+ f = -1/z*exp(-z*x)
321
+ assert limit(f, x, oo) == 0
322
+ assert f.limit(x, oo) == 0
323
+
324
+
325
+ def test_exponential():
326
+ n = Symbol('n')
327
+ x = Symbol('x', real=True)
328
+ assert limit((1 + x/n)**n, n, oo) == exp(x)
329
+ assert limit((1 + x/(2*n))**n, n, oo) == exp(x/2)
330
+ assert limit((1 + x/(2*n + 1))**n, n, oo) == exp(x/2)
331
+ assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)
332
+ assert limit(1 + (1 + 1/x)**x, x, oo) == 1 + S.Exp1
333
+ assert limit((2 + 6*x)**x/(6*x)**x, x, oo) == exp(S('1/3'))
334
+
335
+
336
+ def test_exponential2():
337
+ n = Symbol('n')
338
+ assert limit((1 + x/(n + sin(n)))**n, n, oo) == exp(x)
339
+
340
+
341
+ def test_doit():
342
+ f = Integral(2 * x, x)
343
+ l = Limit(f, x, oo)
344
+ assert l.doit() is oo
345
+
346
+
347
+ def test_series_AccumBounds():
348
+ assert limit(sin(k) - sin(k + 1), k, oo) == AccumBounds(-2, 2)
349
+ assert limit(cos(k) - cos(k + 1) + 1, k, oo) == AccumBounds(-1, 3)
350
+
351
+ # not the exact bound
352
+ assert limit(sin(k) - sin(k)*cos(k), k, oo) == AccumBounds(-2, 2)
353
+
354
+ # test for issue #9934
355
+ lo = (-3 + cos(1))/2
356
+ hi = (1 + cos(1))/2
357
+ t1 = Mul(AccumBounds(lo, hi), 1/(-1 + cos(1)), evaluate=False)
358
+ assert limit(simplify(Sum(cos(n).rewrite(exp), (n, 0, k)).doit().rewrite(sin)), k, oo) == t1
359
+
360
+ t2 = Mul(AccumBounds(-1 + sin(1)/2, sin(1)/2 + 1), 1/(1 - cos(1)))
361
+ assert limit(simplify(Sum(sin(n).rewrite(exp), (n, 0, k)).doit().rewrite(sin)), k, oo) == t2
362
+
363
+ assert limit(((sin(x) + 1)/2)**x, x, oo) == AccumBounds(0, oo) # wolfram says 0
364
+
365
+ # https://github.com/sympy/sympy/issues/12312
366
+ e = 2**(-x)*(sin(x) + 1)**x
367
+ assert limit(e, x, oo) == AccumBounds(0, oo)
368
+
369
+
370
+ def test_bessel_functions_at_infinity():
371
+ # Pull Request 23844 implements limits for all bessel and modified bessel
372
+ # functions approaching infinity along any direction i.e. abs(z0) tends to oo
373
+
374
+ assert limit(besselj(1, x), x, oo) == 0
375
+ assert limit(besselj(1, x), x, -oo) == 0
376
+ assert limit(besselj(1, x), x, I*oo) == oo*I
377
+ assert limit(besselj(1, x), x, -I*oo) == -oo*I
378
+ assert limit(bessely(1, x), x, oo) == 0
379
+ assert limit(bessely(1, x), x, -oo) == 0
380
+ assert limit(bessely(1, x), x, I*oo) == -oo
381
+ assert limit(bessely(1, x), x, -I*oo) == -oo
382
+ assert limit(besseli(1, x), x, oo) == oo
383
+ assert limit(besseli(1, x), x, -oo) == -oo
384
+ assert limit(besseli(1, x), x, I*oo) == 0
385
+ assert limit(besseli(1, x), x, -I*oo) == 0
386
+ assert limit(besselk(1, x), x, oo) == 0
387
+ assert limit(besselk(1, x), x, -oo) == -oo*I
388
+ assert limit(besselk(1, x), x, I*oo) == 0
389
+ assert limit(besselk(1, x), x, -I*oo) == 0
390
+
391
+ # test issue 14874
392
+ assert limit(besselk(0, x), x, oo) == 0
393
+
394
+
395
+ @XFAIL
396
+ def test_doit2():
397
+ f = Integral(2 * x, x)
398
+ l = Limit(f, x, oo)
399
+ # limit() breaks on the contained Integral.
400
+ assert l.doit(deep=False) == l
401
+
402
+
403
+ def test_issue_2929():
404
+ assert limit((x * exp(x))/(exp(x) - 1), x, -oo) == 0
405
+
406
+
407
+ def test_issue_3792():
408
+ assert limit((1 - cos(x))/x**2, x, S.Half) == 4 - 4*cos(S.Half)
409
+ assert limit(sin(sin(x + 1) + 1), x, 0) == sin(1 + sin(1))
410
+ assert limit(abs(sin(x + 1) + 1), x, 0) == 1 + sin(1)
411
+
412
+
413
+ def test_issue_4090():
414
+ assert limit(1/(x + 3), x, 2) == Rational(1, 5)
415
+ assert limit(1/(x + pi), x, 2) == S.One/(2 + pi)
416
+ assert limit(log(x)/(x**2 + 3), x, 2) == log(2)/7
417
+ assert limit(log(x)/(x**2 + pi), x, 2) == log(2)/(4 + pi)
418
+
419
+
420
+ def test_issue_4547():
421
+ assert limit(cot(x), x, 0, dir='+') is oo
422
+ assert limit(cot(x), x, pi/2, dir='+') == 0
423
+
424
+
425
+ def test_issue_5164():
426
+ assert limit(x**0.5, x, oo) == oo**0.5 is oo
427
+ assert limit(x**0.5, x, 16) == 4 # Should this be a float?
428
+ assert limit(x**0.5, x, 0) == 0
429
+ assert limit(x**(-0.5), x, oo) == 0
430
+ assert limit(x**(-0.5), x, 4) == S.Half # Should this be a float?
431
+
432
+
433
+ def test_issue_5383():
434
+ func = (1.0 * 1 + 1.0 * x)**(1.0 * 1 / x)
435
+ assert limit(func, x, 0) == E
436
+
437
+
438
+ def test_issue_14793():
439
+ expr = ((x + S(1)/2) * log(x) - x + log(2*pi)/2 - \
440
+ log(factorial(x)) + S(1)/(12*x))*x**3
441
+ assert limit(expr, x, oo) == S(1)/360
442
+
443
+
444
+ def test_issue_5183():
445
+ # using list(...) so py.test can recalculate values
446
+ tests = list(product([x, -x],
447
+ [-1, 1],
448
+ [2, 3, S.Half, Rational(2, 3)],
449
+ ['-', '+']))
450
+ results = (oo, oo, -oo, oo, -oo*I, oo, -oo*(-1)**Rational(1, 3), oo,
451
+ 0, 0, 0, 0, 0, 0, 0, 0,
452
+ oo, oo, oo, -oo, oo, -oo*I, oo, -oo*(-1)**Rational(1, 3),
453
+ 0, 0, 0, 0, 0, 0, 0, 0)
454
+ assert len(tests) == len(results)
455
+ for i, (args, res) in enumerate(zip(tests, results)):
456
+ y, s, e, d = args
457
+ eq = y**(s*e)
458
+ try:
459
+ assert limit(eq, x, 0, dir=d) == res
460
+ except AssertionError:
461
+ if 0: # change to 1 if you want to see the failing tests
462
+ print()
463
+ print(i, res, eq, d, limit(eq, x, 0, dir=d))
464
+ else:
465
+ assert None
466
+
467
+
468
+ def test_issue_5184():
469
+ assert limit(sin(x)/x, x, oo) == 0
470
+ assert limit(atan(x), x, oo) == pi/2
471
+ assert limit(gamma(x), x, oo) is oo
472
+ assert limit(cos(x)/x, x, oo) == 0
473
+ assert limit(gamma(x), x, S.Half) == sqrt(pi)
474
+
475
+ r = Symbol('r', real=True)
476
+ assert limit(r*sin(1/r), r, 0) == 0
477
+
478
+
479
+ def test_issue_5229():
480
+ assert limit((1 + y)**(1/y) - S.Exp1, y, 0) == 0
481
+
482
+
483
+ def test_issue_4546():
484
+ # using list(...) so py.test can recalculate values
485
+ tests = list(product([cot, tan],
486
+ [-pi/2, 0, pi/2, pi, pi*Rational(3, 2)],
487
+ ['-', '+']))
488
+ results = (0, 0, -oo, oo, 0, 0, -oo, oo, 0, 0,
489
+ oo, -oo, 0, 0, oo, -oo, 0, 0, oo, -oo)
490
+ assert len(tests) == len(results)
491
+ for i, (args, res) in enumerate(zip(tests, results)):
492
+ f, l, d = args
493
+ eq = f(x)
494
+ try:
495
+ assert limit(eq, x, l, dir=d) == res
496
+ except AssertionError:
497
+ if 0: # change to 1 if you want to see the failing tests
498
+ print()
499
+ print(i, res, eq, l, d, limit(eq, x, l, dir=d))
500
+ else:
501
+ assert None
502
+
503
+
504
+ def test_issue_3934():
505
+ assert limit((1 + x**log(3))**(1/x), x, 0) == 1
506
+ assert limit((5**(1/x) + 3**(1/x))**x, x, 0) == 5
507
+
508
+
509
+ def test_issue_5955():
510
+ assert limit((x**16)/(1 + x**16), x, oo) == 1
511
+ assert limit((x**100)/(1 + x**100), x, oo) == 1
512
+ assert limit((x**1885)/(1 + x**1885), x, oo) == 1
513
+ assert limit((x**1000/((x + 1)**1000 + exp(-x))), x, oo) == 1
514
+
515
+
516
+ def test_newissue():
517
+ assert limit(exp(1/sin(x))/exp(cot(x)), x, 0) == 1
518
+
519
+
520
+ def test_extended_real_line():
521
+ assert limit(x - oo, x, oo) == Limit(x - oo, x, oo)
522
+ assert limit(1/(x + sin(x)) - oo, x, 0) == Limit(1/(x + sin(x)) - oo, x, 0)
523
+ assert limit(oo/x, x, oo) == Limit(oo/x, x, oo)
524
+ assert limit(x - oo + 1/x, x, oo) == Limit(x - oo + 1/x, x, oo)
525
+
526
+
527
+ @XFAIL
528
+ def test_order_oo():
529
+ x = Symbol('x', positive=True)
530
+ assert Order(x)*oo != Order(1, x)
531
+ assert limit(oo/(x**2 - 4), x, oo) is oo
532
+
533
+
534
+ def test_issue_5436():
535
+ raises(NotImplementedError, lambda: limit(exp(x*y), x, oo))
536
+ raises(NotImplementedError, lambda: limit(exp(-x*y), x, oo))
537
+
538
+
539
+ def test_Limit_dir():
540
+ raises(TypeError, lambda: Limit(x, x, 0, dir=0))
541
+ raises(ValueError, lambda: Limit(x, x, 0, dir='0'))
542
+
543
+
544
+ def test_polynomial():
545
+ assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, oo) == 1
546
+ assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, -oo) == 1
547
+ assert limit(x ** Rational(77, 3) / (1 + x ** Rational(77, 3)), x, oo) == 1
548
+ assert limit(x ** 101.1 / (1 + x ** 101.1), x, oo) == 1
549
+
550
+
551
+ def test_rational():
552
+ assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, oo) == (z - 1)/(y*z)
553
+ assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, -oo) == (z - 1)/(y*z)
554
+
555
+
556
+ def test_issue_5740():
557
+ assert limit(log(x)*z - log(2*x)*y, x, 0) == oo*sign(y - z)
558
+
559
+
560
+ def test_issue_6366():
561
+ n = Symbol('n', integer=True, positive=True)
562
+ r = (n + 1)*x**(n + 1)/(x**(n + 1) - 1) - x/(x - 1)
563
+ assert limit(r, x, 1).cancel() == n/2
564
+
565
+
566
+ def test_factorial():
567
+ f = factorial(x)
568
+ assert limit(f, x, oo) is oo
569
+ assert limit(x/f, x, oo) == 0
570
+ # see Stirling's approximation:
571
+ # https://en.wikipedia.org/wiki/Stirling's_approximation
572
+ assert limit(f/(sqrt(2*pi*x)*(x/E)**x), x, oo) == 1
573
+ assert limit(f, x, -oo) == gamma(-oo)
574
+
575
+
576
+ def test_issue_6560():
577
+ e = (5*x**3/4 - x*Rational(3, 4) + (y*(3*x**2/2 - S.Half) +
578
+ 35*x**4/8 - 15*x**2/4 + Rational(3, 8))/(2*(y + 1)))
579
+ assert limit(e, y, oo) == 5*x**3/4 + 3*x**2/4 - 3*x/4 - Rational(1, 4)
580
+
581
+ @XFAIL
582
+ def test_issue_5172():
583
+ n = Symbol('n')
584
+ r = Symbol('r', positive=True)
585
+ c = Symbol('c')
586
+ p = Symbol('p', positive=True)
587
+ m = Symbol('m', negative=True)
588
+ expr = ((2*n*(n - r + 1)/(n + r*(n - r + 1)))**c +
589
+ (r - 1)*(n*(n - r + 2)/(n + r*(n - r + 1)))**c - n)/(n**c - n)
590
+ expr = expr.subs(c, c + 1)
591
+ raises(NotImplementedError, lambda: limit(expr, n, oo))
592
+ assert limit(expr.subs(c, m), n, oo) == 1
593
+ assert limit(expr.subs(c, p), n, oo).simplify() == \
594
+ (2**(p + 1) + r - 1)/(r + 1)**(p + 1)
595
+
596
+
597
+ def test_issue_7088():
598
+ a = Symbol('a')
599
+ assert limit(sqrt(x/(x + a)), x, oo) == 1
600
+
601
+
602
+ def test_branch_cuts():
603
+ assert limit(asin(I*x + 2), x, 0) == pi - asin(2)
604
+ assert limit(asin(I*x + 2), x, 0, '-') == asin(2)
605
+ assert limit(asin(I*x - 2), x, 0) == -asin(2)
606
+ assert limit(asin(I*x - 2), x, 0, '-') == -pi + asin(2)
607
+ assert limit(acos(I*x + 2), x, 0) == -acos(2)
608
+ assert limit(acos(I*x + 2), x, 0, '-') == acos(2)
609
+ assert limit(acos(I*x - 2), x, 0) == acos(-2)
610
+ assert limit(acos(I*x - 2), x, 0, '-') == 2*pi - acos(-2)
611
+ assert limit(atan(x + 2*I), x, 0) == I*atanh(2)
612
+ assert limit(atan(x + 2*I), x, 0, '-') == -pi + I*atanh(2)
613
+ assert limit(atan(x - 2*I), x, 0) == pi - I*atanh(2)
614
+ assert limit(atan(x - 2*I), x, 0, '-') == -I*atanh(2)
615
+ assert limit(atan(1/x), x, 0) == pi/2
616
+ assert limit(atan(1/x), x, 0, '-') == -pi/2
617
+ assert limit(atan(x), x, oo) == pi/2
618
+ assert limit(atan(x), x, -oo) == -pi/2
619
+ assert limit(acot(x + S(1)/2*I), x, 0) == pi - I*acoth(S(1)/2)
620
+ assert limit(acot(x + S(1)/2*I), x, 0, '-') == -I*acoth(S(1)/2)
621
+ assert limit(acot(x - S(1)/2*I), x, 0) == I*acoth(S(1)/2)
622
+ assert limit(acot(x - S(1)/2*I), x, 0, '-') == -pi + I*acoth(S(1)/2)
623
+ assert limit(acot(x), x, 0) == pi/2
624
+ assert limit(acot(x), x, 0, '-') == -pi/2
625
+ assert limit(asec(I*x + S(1)/2), x, 0) == asec(S(1)/2)
626
+ assert limit(asec(I*x + S(1)/2), x, 0, '-') == -asec(S(1)/2)
627
+ assert limit(asec(I*x - S(1)/2), x, 0) == 2*pi - asec(-S(1)/2)
628
+ assert limit(asec(I*x - S(1)/2), x, 0, '-') == asec(-S(1)/2)
629
+ assert limit(acsc(I*x + S(1)/2), x, 0) == acsc(S(1)/2)
630
+ assert limit(acsc(I*x + S(1)/2), x, 0, '-') == pi - acsc(S(1)/2)
631
+ assert limit(acsc(I*x - S(1)/2), x, 0) == -pi + acsc(S(1)/2)
632
+ assert limit(acsc(I*x - S(1)/2), x, 0, '-') == -acsc(S(1)/2)
633
+
634
+ assert limit(log(I*x - 1), x, 0) == I*pi
635
+ assert limit(log(I*x - 1), x, 0, '-') == -I*pi
636
+ assert limit(log(-I*x - 1), x, 0) == -I*pi
637
+ assert limit(log(-I*x - 1), x, 0, '-') == I*pi
638
+
639
+ assert limit(sqrt(I*x - 1), x, 0) == I
640
+ assert limit(sqrt(I*x - 1), x, 0, '-') == -I
641
+ assert limit(sqrt(-I*x - 1), x, 0) == -I
642
+ assert limit(sqrt(-I*x - 1), x, 0, '-') == I
643
+
644
+ assert limit(cbrt(I*x - 1), x, 0) == (-1)**(S(1)/3)
645
+ assert limit(cbrt(I*x - 1), x, 0, '-') == -(-1)**(S(2)/3)
646
+ assert limit(cbrt(-I*x - 1), x, 0) == -(-1)**(S(2)/3)
647
+ assert limit(cbrt(-I*x - 1), x, 0, '-') == (-1)**(S(1)/3)
648
+
649
+
650
+ def test_issue_6364():
651
+ a = Symbol('a')
652
+ e = z/(1 - sqrt(1 + z)*sin(a)**2 - sqrt(1 - z)*cos(a)**2)
653
+ assert limit(e, z, 0) == 1/(cos(a)**2 - S.Half)
654
+
655
+
656
+ def test_issue_6682():
657
+ assert limit(exp(2*Ei(-x))/x**2, x, 0) == exp(2*EulerGamma)
658
+
659
+
660
+ def test_issue_4099():
661
+ a = Symbol('a')
662
+ assert limit(a/x, x, 0) == oo*sign(a)
663
+ assert limit(-a/x, x, 0) == -oo*sign(a)
664
+ assert limit(-a*x, x, oo) == -oo*sign(a)
665
+ assert limit(a*x, x, oo) == oo*sign(a)
666
+
667
+
668
+ def test_issue_4503():
669
+ dx = Symbol('dx')
670
+ assert limit((sqrt(1 + exp(x + dx)) - sqrt(1 + exp(x)))/dx, dx, 0) == \
671
+ exp(x)/(2*sqrt(exp(x) + 1))
672
+
673
+
674
+ def test_issue_6052():
675
+ G = meijerg((), (), (1,), (0,), -x)
676
+ g = hyperexpand(G)
677
+ assert limit(g, x, 0, '+-') == 0
678
+ assert limit(g, x, oo) == -oo
679
+
680
+
681
+ def test_issue_7224():
682
+ expr = sqrt(x)*besseli(1,sqrt(8*x))
683
+ assert limit(x*diff(expr, x, x)/expr, x, 0) == 2
684
+ assert limit(x*diff(expr, x, x)/expr, x, 1).evalf() == 2.0
685
+
686
+
687
+ def test_issue_7391_8166():
688
+ f = Function('f')
689
+ # limit should depend on the continuity of the expression at the point passed
690
+ assert limit(f(x), x, 4) == Limit(f(x), x, 4, dir='+')
691
+ assert limit(x*f(x)**2/(x**2 + f(x)**4), x, 0) == Limit(x*f(x)**2/(x**2 + f(x)**4), x, 0, dir='+')
692
+
693
+
694
+ def test_issue_8208():
695
+ assert limit(n**(Rational(1, 1e9) - 1), n, oo) == 0
696
+
697
+
698
+ def test_issue_8229():
699
+ assert limit((x**Rational(1, 4) - 2)/(sqrt(x) - 4)**Rational(2, 3), x, 16) == 0
700
+
701
+
702
+ def test_issue_8433():
703
+ d, t = symbols('d t', positive=True)
704
+ assert limit(erf(1 - t/d), t, oo) == -1
705
+
706
+
707
+ def test_issue_8481():
708
+ k = Symbol('k', integer=True, nonnegative=True)
709
+ lamda = Symbol('lamda', positive=True)
710
+ assert limit(lamda**k * exp(-lamda) / factorial(k), k, oo) == 0
711
+
712
+
713
+ def test_issue_8462():
714
+ assert limit(binomial(n, n/2), n, oo) == oo
715
+ assert limit(binomial(n, n/2) * 3 ** (-n), n, oo) == 0
716
+
717
+
718
+ def test_issue_8634():
719
+ n = Symbol('n', integer=True, positive=True)
720
+ x = Symbol('x')
721
+ assert limit(x**n, x, -oo) == oo*sign((-1)**n)
722
+
723
+
724
+ def test_issue_8635_18176():
725
+ x = Symbol('x', real=True)
726
+ k = Symbol('k', positive=True)
727
+ assert limit(x**n - x**(n - 0), x, oo) == 0
728
+ assert limit(x**n - x**(n - 5), x, oo) == oo
729
+ assert limit(x**n - x**(n - 2.5), x, oo) == oo
730
+ assert limit(x**n - x**(n - k - 1), x, oo) == oo
731
+ x = Symbol('x', positive=True)
732
+ assert limit(x**n - x**(n - 1), x, oo) == oo
733
+ assert limit(x**n - x**(n + 2), x, oo) == -oo
734
+
735
+
736
+ def test_issue_8730():
737
+ assert limit(subfactorial(x), x, oo) is oo
738
+
739
+
740
+ def test_issue_9252():
741
+ n = Symbol('n', integer=True)
742
+ c = Symbol('c', positive=True)
743
+ assert limit((log(n))**(n/log(n)) / (1 + c)**n, n, oo) == 0
744
+ # limit should depend on the value of c
745
+ raises(NotImplementedError, lambda: limit((log(n))**(n/log(n)) / c**n, n, oo))
746
+
747
+
748
+ def test_issue_9558():
749
+ assert limit(sin(x)**15, x, 0, '-') == 0
750
+
751
+
752
+ def test_issue_10801():
753
+ # make sure limits work with binomial
754
+ assert limit(16**k / (k * binomial(2*k, k)**2), k, oo) == pi
755
+
756
+
757
+ def test_issue_10976():
758
+ s, x = symbols('s x', real=True)
759
+ assert limit(erf(s*x)/erf(s), s, 0) == x
760
+
761
+
762
+ def test_issue_9041():
763
+ assert limit(factorial(n) / ((n/exp(1))**n * sqrt(2*pi*n)), n, oo) == 1
764
+
765
+
766
+ def test_issue_9205():
767
+ x, y, a = symbols('x, y, a')
768
+ assert Limit(x, x, a).free_symbols == {a}
769
+ assert Limit(x, x, a, '-').free_symbols == {a}
770
+ assert Limit(x + y, x + y, a).free_symbols == {a}
771
+ assert Limit(-x**2 + y, x**2, a).free_symbols == {y, a}
772
+
773
+
774
+ def test_issue_9471():
775
+ assert limit(((27**(log(n,3)))/n**3),n,oo) == 1
776
+ assert limit(((27**(log(n,3)+1))/n**3),n,oo) == 27
777
+
778
+
779
+ def test_issue_10382():
780
+ assert limit(fibonacci(n + 1)/fibonacci(n), n, oo) == GoldenRatio
781
+
782
+
783
+ def test_issue_11496():
784
+ assert limit(erfc(log(1/x)), x, oo) == 2
785
+
786
+
787
+ def test_issue_11879():
788
+ assert simplify(limit(((x+y)**n-x**n)/y, y, 0)) == n*x**(n-1)
789
+
790
+
791
+ def test_limit_with_Float():
792
+ k = symbols("k")
793
+ assert limit(1.0 ** k, k, oo) == 1
794
+ assert limit(0.3*1.0**k, k, oo) == Rational(3, 10)
795
+
796
+
797
+ def test_issue_10610():
798
+ assert limit(3**x*3**(-x - 1)*(x + 1)**2/x**2, x, oo) == Rational(1, 3)
799
+
800
+
801
+ def test_issue_10868():
802
+ assert limit(log(x) + asech(x), x, 0, '+') == log(2)
803
+ assert limit(log(x) + asech(x), x, 0, '-') == log(2) + 2*I*pi
804
+ raises(ValueError, lambda: limit(log(x) + asech(x), x, 0, '+-'))
805
+ assert limit(log(x) + asech(x), x, oo) == oo
806
+ assert limit(log(x) + acsch(x), x, 0, '+') == log(2)
807
+ assert limit(log(x) + acsch(x), x, 0, '-') == -oo
808
+ raises(ValueError, lambda: limit(log(x) + acsch(x), x, 0, '+-'))
809
+ assert limit(log(x) + acsch(x), x, oo) == oo
810
+
811
+
812
+ def test_issue_6599():
813
+ assert limit((n + cos(n))/n, n, oo) == 1
814
+
815
+
816
+ def test_issue_12555():
817
+ assert limit((3**x + 2* x**10) / (x**10 + exp(x)), x, -oo) == 2
818
+ assert limit((3**x + 2* x**10) / (x**10 + exp(x)), x, oo) is oo
819
+
820
+
821
+ def test_issue_12769():
822
+ r, z, x = symbols('r z x', real=True)
823
+ a, b, s0, K, F0, s, T = symbols('a b s0 K F0 s T', positive=True, real=True)
824
+ fx = (F0**b*K**b*r*s0 - sqrt((F0**2*K**(2*b)*a**2*(b - 1) + \
825
+ F0**(2*b)*K**2*a**2*(b - 1) + F0**(2*b)*K**(2*b)*s0**2*(b - 1)*(b**2 - 2*b + 1) - \
826
+ 2*F0**(2*b)*K**(b + 1)*a*r*s0*(b**2 - 2*b + 1) + \
827
+ 2*F0**(b + 1)*K**(2*b)*a*r*s0*(b**2 - 2*b + 1) - \
828
+ 2*F0**(b + 1)*K**(b + 1)*a**2*(b - 1))/((b - 1)*(b**2 - 2*b + 1))))*(b*r - b - r + 1)
829
+
830
+ assert fx.subs(K, F0).factor(deep=True) == limit(fx, K, F0).factor(deep=True)
831
+
832
+
833
+ def test_issue_13332():
834
+ assert limit(sqrt(30)*5**(-5*x - 1)*(46656*x)**x*(5*x + 2)**(5*x + 5*S.Half) *
835
+ (6*x + 2)**(-6*x - 5*S.Half), x, oo) == Rational(25, 36)
836
+
837
+
838
+ def test_issue_12564():
839
+ assert limit(x**2 + x*sin(x) + cos(x), x, -oo) is oo
840
+ assert limit(x**2 + x*sin(x) + cos(x), x, oo) is oo
841
+ assert limit(((x + cos(x))**2).expand(), x, oo) is oo
842
+ assert limit(((x + sin(x))**2).expand(), x, oo) is oo
843
+ assert limit(((x + cos(x))**2).expand(), x, -oo) is oo
844
+ assert limit(((x + sin(x))**2).expand(), x, -oo) is oo
845
+
846
+
847
+ def test_issue_14456():
848
+ raises(NotImplementedError, lambda: Limit(exp(x), x, zoo).doit())
849
+ raises(NotImplementedError, lambda: Limit(x**2/(x+1), x, zoo).doit())
850
+
851
+
852
+ def test_issue_14411():
853
+ assert limit(3*sec(4*pi*x - x/3), x, 3*pi/(24*pi - 2)) is -oo
854
+
855
+
856
+ def test_issue_13382():
857
+ assert limit(x*(((x + 1)**2 + 1)/(x**2 + 1) - 1), x, oo) == 2
858
+
859
+
860
+ def test_issue_13403():
861
+ assert limit(x*(-1 + (x + log(x + 1) + 1)/(x + log(x))), x, oo) == 1
862
+
863
+
864
+ def test_issue_13416():
865
+ assert limit((-x**3*log(x)**3 + (x - 1)*(x + 1)**2*log(x + 1)**3)/(x**2*log(x)**3), x, oo) == 1
866
+
867
+
868
+ def test_issue_13462():
869
+ assert limit(n**2*(2*n*(-(1 - 1/(2*n))**x + 1) - x - (-x**2/4 + x/4)/n), n, oo) == x**3/24 - x**2/8 + x/12
870
+
871
+
872
+ def test_issue_13750():
873
+ a = Symbol('a')
874
+ assert limit(erf(a - x), x, oo) == -1
875
+ assert limit(erf(sqrt(x) - x), x, oo) == -1
876
+
877
+
878
+ def test_issue_14276():
879
+ assert isinstance(limit(sin(x)**log(x), x, oo), Limit)
880
+ assert isinstance(limit(sin(x)**cos(x), x, oo), Limit)
881
+ assert isinstance(limit(sin(log(cos(x))), x, oo), Limit)
882
+ assert limit((1 + 1/(x**2 + cos(x)))**(x**2 + x), x, oo) == E
883
+
884
+
885
+ def test_issue_14514():
886
+ assert limit((1/(log(x)**log(x)))**(1/x), x, oo) == 1
887
+
888
+
889
+ def test_issues_14525():
890
+ assert limit(sin(x)**2 - cos(x) + tan(x)*csc(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
891
+ assert limit(sin(x)**2 - cos(x) + sin(x)*cot(x), x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
892
+ assert limit(cot(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.Infinity)
893
+ assert limit(cos(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
894
+ assert limit(sin(x) - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
895
+ assert limit(cos(x)**2 - tan(x)**2, x, oo) == AccumBounds(S.NegativeInfinity, S.One)
896
+ assert limit(tan(x)**2 + sin(x)**2 - cos(x), x, oo) == AccumBounds(-S.One, S.Infinity)
897
+
898
+
899
+ def test_issue_14574():
900
+ assert limit(sqrt(x)*cos(x - x**2) / (x + 1), x, oo) == 0
901
+
902
+
903
+ def test_issue_10102():
904
+ assert limit(fresnels(x), x, oo) == S.Half
905
+ assert limit(3 + fresnels(x), x, oo) == 3 + S.Half
906
+ assert limit(5*fresnels(x), x, oo) == Rational(5, 2)
907
+ assert limit(fresnelc(x), x, oo) == S.Half
908
+ assert limit(fresnels(x), x, -oo) == Rational(-1, 2)
909
+ assert limit(4*fresnelc(x), x, -oo) == -2
910
+
911
+
912
+ def test_issue_14377():
913
+ raises(NotImplementedError, lambda: limit(exp(I*x)*sin(pi*x), x, oo))
914
+
915
+
916
+ def test_issue_15146():
917
+ e = (x/2) * (-2*x**3 - 2*(x**3 - 1) * x**2 * digamma(x**3 + 1) + \
918
+ 2*(x**3 - 1) * x**2 * digamma(x**3 + x + 1) + x + 3)
919
+ assert limit(e, x, oo) == S(1)/3
920
+
921
+
922
+ def test_issue_15202():
923
+ e = (2**x*(2 + 2**(-x)*(-2*2**x + x + 2))/(x + 1))**(x + 1)
924
+ assert limit(e, x, oo) == exp(1)
925
+
926
+ e = (log(x, 2)**7 + 10*x*factorial(x) + 5**x) / (factorial(x + 1) + 3*factorial(x) + 10**x)
927
+ assert limit(e, x, oo) == 10
928
+
929
+
930
+ def test_issue_15282():
931
+ assert limit((x**2000 - (x + 1)**2000) / x**1999, x, oo) == -2000
932
+
933
+
934
+ def test_issue_15984():
935
+ assert limit((-x + log(exp(x) + 1))/x, x, oo, dir='-') == 0
936
+
937
+
938
+ def test_issue_13571():
939
+ assert limit(uppergamma(x, 1) / gamma(x), x, oo) == 1
940
+
941
+
942
+ def test_issue_13575():
943
+ assert limit(acos(erfi(x)), x, 1) == acos(erfi(S.One))
944
+
945
+
946
+ def test_issue_17325():
947
+ assert Limit(sin(x)/x, x, 0, dir="+-").doit() == 1
948
+ assert Limit(x**2, x, 0, dir="+-").doit() == 0
949
+ assert Limit(1/x**2, x, 0, dir="+-").doit() is oo
950
+ assert Limit(1/x, x, 0, dir="+-").doit() is zoo
951
+
952
+
953
+ def test_issue_10978():
954
+ assert LambertW(x).limit(x, 0) == 0
955
+
956
+
957
+ def test_issue_14313_comment():
958
+ assert limit(floor(n/2), n, oo) is oo
959
+
960
+
961
+ def test_issue_15323():
962
+ d = ((1 - 1/x)**x).diff(x)
963
+ assert limit(d, x, 1, dir='+') == 1
964
+
965
+
966
+ def test_issue_12571():
967
+ assert limit(-LambertW(-log(x))/log(x), x, 1) == 1
968
+
969
+
970
+ def test_issue_14590():
971
+ assert limit((x**3*((x + 1)/x)**x)/((x + 1)*(x + 2)*(x + 3)), x, oo) == exp(1)
972
+
973
+
974
+ def test_issue_14393():
975
+ a, b = symbols('a b')
976
+ assert limit((x**b - y**b)/(x**a - y**a), x, y) == b*y**(-a + b)/a
977
+
978
+
979
+ def test_issue_14556():
980
+ assert limit(factorial(n + 1)**(1/(n + 1)) - factorial(n)**(1/n), n, oo) == exp(-1)
981
+
982
+
983
+ def test_issue_14811():
984
+ assert limit(((1 + ((S(2)/3)**(x + 1)))**(2**x))/(2**((S(4)/3)**(x - 1))), x, oo) == oo
985
+
986
+
987
+ def test_issue_16222():
988
+ assert limit(exp(x), x, 1000000000) == exp(1000000000)
989
+
990
+
991
+ def test_issue_16714():
992
+ assert limit(((x**(x + 1) + (x + 1)**x) / x**(x + 1))**x, x, oo) == exp(exp(1))
993
+
994
+
995
+ def test_issue_16722():
996
+ z = symbols('z', positive=True)
997
+ assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
998
+ z = symbols('z', positive=True, integer=True)
999
+ assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
1000
+
1001
+
1002
+ def test_issue_17431():
1003
+ assert limit(((n + 1) + 1) / (((n + 1) + 2) * factorial(n + 1)) *
1004
+ (n + 2) * factorial(n) / (n + 1), n, oo) == 0
1005
+ assert limit((n + 2)**2*factorial(n)/((n + 1)*(n + 3)*factorial(n + 1))
1006
+ , n, oo) == 0
1007
+ assert limit((n + 1) * factorial(n) / (n * factorial(n + 1)), n, oo) == 0
1008
+
1009
+
1010
+ def test_issue_17671():
1011
+ assert limit(Ei(-log(x)) - log(log(x))/x, x, 1) == EulerGamma
1012
+
1013
+
1014
+ def test_issue_17751():
1015
+ a, b, c, x = symbols('a b c x', positive=True)
1016
+ assert limit((a + 1)*x - sqrt((a + 1)**2*x**2 + b*x + c), x, oo) == -b/(2*a + 2)
1017
+
1018
+
1019
+ def test_issue_17792():
1020
+ assert limit(factorial(n)/sqrt(n)*(exp(1)/n)**n, n, oo) == sqrt(2)*sqrt(pi)
1021
+
1022
+
1023
+ def test_issue_18118():
1024
+ assert limit(sign(sin(x)), x, 0, "-") == -1
1025
+ assert limit(sign(sin(x)), x, 0, "+") == 1
1026
+
1027
+
1028
+ def test_issue_18306():
1029
+ assert limit(sin(sqrt(x))/sqrt(sin(x)), x, 0, '+') == 1
1030
+
1031
+
1032
+ def test_issue_18378():
1033
+ assert limit(log(exp(3*x) + x)/log(exp(x) + x**100), x, oo) == 3
1034
+
1035
+
1036
+ def test_issue_18399():
1037
+ assert limit((1 - S(1)/2*x)**(3*x), x, oo) is zoo
1038
+ assert limit((-x)**x, x, oo) is zoo
1039
+
1040
+
1041
+ def test_issue_18442():
1042
+ assert limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-') == Limit(tan(x)**(2**(sqrt(pi))), x, oo, dir='-')
1043
+
1044
+
1045
+ def test_issue_18452():
1046
+ assert limit(abs(log(x))**x, x, 0) == 1
1047
+ assert limit(abs(log(x))**x, x, 0, "-") == 1
1048
+
1049
+
1050
+ def test_issue_18473():
1051
+ assert limit(sin(x)**(1/x), x, oo) == Limit(sin(x)**(1/x), x, oo, dir='-')
1052
+ assert limit(cos(x)**(1/x), x, oo) == Limit(cos(x)**(1/x), x, oo, dir='-')
1053
+ assert limit(tan(x)**(1/x), x, oo) == Limit(tan(x)**(1/x), x, oo, dir='-')
1054
+ assert limit((cos(x) + 2)**(1/x), x, oo) == 1
1055
+ assert limit((sin(x) + 10)**(1/x), x, oo) == 1
1056
+ assert limit((cos(x) - 2)**(1/x), x, oo) == Limit((cos(x) - 2)**(1/x), x, oo, dir='-')
1057
+ assert limit((cos(x) + 1)**(1/x), x, oo) == AccumBounds(0, 1)
1058
+ assert limit((tan(x)**2)**(2/x) , x, oo) == AccumBounds(0, oo)
1059
+ assert limit((sin(x)**2)**(1/x), x, oo) == AccumBounds(0, 1)
1060
+ # Tests for issue #23751
1061
+ assert limit((cos(x) + 1)**(1/x), x, -oo) == AccumBounds(1, oo)
1062
+ assert limit((sin(x)**2)**(1/x), x, -oo) == AccumBounds(1, oo)
1063
+ assert limit((tan(x)**2)**(2/x) , x, -oo) == AccumBounds(0, oo)
1064
+
1065
+
1066
+ def test_issue_18482():
1067
+ assert limit((2*exp(3*x)/(exp(2*x) + 1))**(1/x), x, oo) == exp(1)
1068
+
1069
+
1070
+ def test_issue_18508():
1071
+ assert limit(sin(x)/sqrt(1-cos(x)), x, 0) == sqrt(2)
1072
+ assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='+') == sqrt(2)
1073
+ assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='-') == -sqrt(2)
1074
+
1075
+
1076
+ def test_issue_18521():
1077
+ raises(NotImplementedError, lambda: limit(exp((2 - n) * x), x, oo))
1078
+
1079
+
1080
+ def test_issue_18969():
1081
+ a, b = symbols('a b', positive=True)
1082
+ assert limit(LambertW(a), a, b) == LambertW(b)
1083
+ assert limit(exp(LambertW(a)), a, b) == exp(LambertW(b))
1084
+
1085
+
1086
+ def test_issue_18992():
1087
+ assert limit(n/(factorial(n)**(1/n)), n, oo) == exp(1)
1088
+
1089
+
1090
+ def test_issue_19067():
1091
+ x = Symbol('x')
1092
+ assert limit(gamma(x)/(gamma(x - 1)*gamma(x + 2)), x, 0) == -1
1093
+
1094
+
1095
+ def test_issue_19586():
1096
+ assert limit(x**(2**x*3**(-x)), x, oo) == 1
1097
+
1098
+
1099
+ def test_issue_13715():
1100
+ n = Symbol('n')
1101
+ p = Symbol('p', zero=True)
1102
+ assert limit(n + p, n, 0) == 0
1103
+
1104
+
1105
+ def test_issue_15055():
1106
+ assert limit(n**3*((-n - 1)*sin(1/n) + (n + 2)*sin(1/(n + 1)))/(-n + 1), n, oo) == 1
1107
+
1108
+
1109
+ def test_issue_16708():
1110
+ m, vi = symbols('m vi', positive=True)
1111
+ B, ti, d = symbols('B ti d')
1112
+ assert limit((B*ti*vi - sqrt(m)*sqrt(-2*B*d*vi + m*(vi)**2) + m*vi)/(B*vi), B, 0) == (d + ti*vi)/vi
1113
+
1114
+
1115
+ def test_issue_19154():
1116
+ assert limit(besseli(1, 3 *x)/(x *besseli(1, x)**3), x , oo) == 2*sqrt(3)*pi/3
1117
+ assert limit(besseli(1, 3 *x)/(x *besseli(1, x)**3), x , -oo) == -2*sqrt(3)*pi/3
1118
+
1119
+
1120
+ def test_issue_19453():
1121
+ beta = Symbol("beta", positive=True)
1122
+ h = Symbol("h", positive=True)
1123
+ m = Symbol("m", positive=True)
1124
+ w = Symbol("omega", positive=True)
1125
+ g = Symbol("g", positive=True)
1126
+
1127
+ e = exp(1)
1128
+ q = 3*h**2*beta*g*e**(0.5*h*beta*w)
1129
+ p = m**2*w**2
1130
+ s = e**(h*beta*w) - 1
1131
+ Z = -q/(4*p*s) - q/(2*p*s**2) - q*(e**(h*beta*w) + 1)/(2*p*s**3)\
1132
+ + e**(0.5*h*beta*w)/s
1133
+ E = -diff(log(Z), beta)
1134
+
1135
+ assert limit(E - 0.5*h*w, beta, oo) == 0
1136
+ assert limit(E.simplify() - 0.5*h*w, beta, oo) == 0
1137
+
1138
+
1139
+ def test_issue_19739():
1140
+ assert limit((-S(1)/4)**x, x, oo) == 0
1141
+
1142
+
1143
+ def test_issue_19766():
1144
+ assert limit(2**(-x)*sqrt(4**(x + 1) + 1), x, oo) == 2
1145
+
1146
+
1147
+ def test_issue_19770():
1148
+ m = Symbol('m')
1149
+ # the result is not 0 for non-real m
1150
+ assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
1151
+ m = Symbol('m', real=True)
1152
+ # can be improved to give the correct result 0
1153
+ assert limit(cos(m*x)/x, x, oo) == Limit(cos(m*x)/x, x, oo, dir='-')
1154
+ m = Symbol('m', nonzero=True)
1155
+ assert limit(cos(m*x), x, oo) == AccumBounds(-1, 1)
1156
+ assert limit(cos(m*x)/x, x, oo) == 0
1157
+
1158
+
1159
+ def test_issue_7535():
1160
+ assert limit(tan(x)/sin(tan(x)), x, pi/2) == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+')
1161
+ assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='-')
1162
+ assert limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-') == Limit(tan(x)/sin(tan(x)), x, pi/2, dir='+-')
1163
+ assert limit(sin(tan(x)),x,pi/2) == AccumBounds(-1, 1)
1164
+ assert -oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
1165
+ assert oo*(1/sin(oo)) == AccumBounds(-oo, oo)
1166
+ assert oo*(1/sin(-oo)) == AccumBounds(-oo, oo)
1167
+ assert -oo*(1/sin(oo)) == AccumBounds(-oo, oo)
1168
+
1169
+
1170
+ def test_issue_20365():
1171
+ assert limit(((x + 1)**(1/x) - E)/x, x, 0) == -E/2
1172
+
1173
+
1174
+ def test_issue_21031():
1175
+ assert limit(((1 + x)**(1/x) - (1 + 2*x)**(1/(2*x)))/asin(x), x, 0) == E/2
1176
+
1177
+
1178
+ def test_issue_21038():
1179
+ assert limit(sin(pi*x)/(3*x - 12), x, 4) == pi/3
1180
+
1181
+
1182
+ def test_issue_20578():
1183
+ expr = abs(x) * sin(1/x)
1184
+ assert limit(expr,x,0,'+') == 0
1185
+ assert limit(expr,x,0,'-') == 0
1186
+ assert limit(expr,x,0,'+-') == 0
1187
+
1188
+
1189
+ def test_issue_21227():
1190
+ f = log(x)
1191
+
1192
+ assert f.nseries(x, logx=y) == y
1193
+ assert f.nseries(x, logx=-x) == -x
1194
+
1195
+ f = log(-log(x))
1196
+
1197
+ assert f.nseries(x, logx=y) == log(-y)
1198
+ assert f.nseries(x, logx=-x) == log(x)
1199
+
1200
+ f = log(log(x))
1201
+
1202
+ assert f.nseries(x, logx=y) == log(y)
1203
+ assert f.nseries(x, logx=-x) == log(-x)
1204
+ assert f.nseries(x, logx=x) == log(x)
1205
+
1206
+ f = log(log(log(1/x)))
1207
+
1208
+ assert f.nseries(x, logx=y) == log(log(-y))
1209
+ assert f.nseries(x, logx=-y) == log(log(y))
1210
+ assert f.nseries(x, logx=x) == log(log(-x))
1211
+ assert f.nseries(x, logx=-x) == log(log(x))
1212
+
1213
+
1214
+ def test_issue_21415():
1215
+ exp = (x-1)*cos(1/(x-1))
1216
+ assert exp.limit(x,1) == 0
1217
+ assert exp.expand().limit(x,1) == 0
1218
+
1219
+
1220
+ def test_issue_21530():
1221
+ assert limit(sinh(n + 1)/sinh(n), n, oo) == E
1222
+
1223
+
1224
+ def test_issue_21550():
1225
+ r = (sqrt(5) - 1)/2
1226
+ assert limit((x - r)/(x**2 + x - 1), x, r) == sqrt(5)/5
1227
+
1228
+
1229
+ def test_issue_21661():
1230
+ out = limit((x**(x + 1) * (log(x) + 1) + 1) / x, x, 11)
1231
+ assert out == S(3138428376722)/11 + 285311670611*log(11)
1232
+
1233
+
1234
+ def test_issue_21701():
1235
+ assert limit((besselj(z, x)/x**z).subs(z, 7), x, 0) == S(1)/645120
1236
+
1237
+
1238
+ def test_issue_21721():
1239
+ a = Symbol('a', real=True)
1240
+ I = integrate(1/(pi*(1 + (x - a)**2)), x)
1241
+ assert I.limit(x, oo) == S.Half
1242
+
1243
+
1244
+ def test_issue_21756():
1245
+ term = (1 - exp(-2*I*pi*z))/(1 - exp(-2*I*pi*z/5))
1246
+ assert term.limit(z, 0) == 5
1247
+ assert re(term).limit(z, 0) == 5
1248
+
1249
+
1250
+ def test_issue_21785():
1251
+ a = Symbol('a')
1252
+ assert sqrt((-a**2 + x**2)/(1 - x**2)).limit(a, 1, '-') == I
1253
+
1254
+
1255
+ def test_issue_22181():
1256
+ assert limit((-1)**x * 2**(-x), x, oo) == 0
1257
+
1258
+
1259
+ def test_issue_22220():
1260
+ e1 = sqrt(30)*atan(sqrt(30)*tan(x/2)/6)/30
1261
+ e2 = sqrt(30)*I*(-log(sqrt(2)*tan(x/2) - 2*sqrt(15)*I/5) +
1262
+ +log(sqrt(2)*tan(x/2) + 2*sqrt(15)*I/5))/60
1263
+
1264
+ assert limit(e1, x, -pi) == -sqrt(30)*pi/60
1265
+ assert limit(e2, x, -pi) == -sqrt(30)*pi/30
1266
+
1267
+ assert limit(e1, x, -pi, '-') == sqrt(30)*pi/60
1268
+ assert limit(e2, x, -pi, '-') == 0
1269
+
1270
+ # test https://github.com/sympy/sympy/issues/22220#issuecomment-972727694
1271
+ expr = log(x - I) - log(-x - I)
1272
+ expr2 = logcombine(expr, force=True)
1273
+ assert limit(expr, x, oo) == limit(expr2, x, oo) == I*pi
1274
+
1275
+ # test https://github.com/sympy/sympy/issues/22220#issuecomment-1077618340
1276
+ expr = expr = (-log(tan(x/2) - I) +log(tan(x/2) + I))
1277
+ assert limit(expr, x, pi, '+') == 2*I*pi
1278
+ assert limit(expr, x, pi, '-') == 0
1279
+
1280
+
1281
+ def test_issue_22334():
1282
+ k, n = symbols('k, n', positive=True)
1283
+ assert limit((n+1)**k/((n+1)**(k+1) - (n)**(k+1)), n, oo) == 1/(k + 1)
1284
+ assert limit((n+1)**k/((n+1)**(k+1) - (n)**(k+1)).expand(), n, oo) == 1/(k + 1)
1285
+ assert limit((n+1)**k/(n*(-n**k + (n + 1)**k) + (n + 1)**k), n, oo) == 1/(k + 1)
1286
+
1287
+
1288
+ def test_issue_22836_limit():
1289
+ assert limit(2**(1/x)/factorial(1/(x)), x, 0) == S.Zero
1290
+
1291
+
1292
+ def test_sympyissue_22986():
1293
+ assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2)
1294
+
1295
+
1296
+ def test_issue_23231():
1297
+ f = (2**x - 2**(-x))/(2**x + 2**(-x))
1298
+ assert limit(f, x, -oo) == -1
1299
+
1300
+
1301
+ def test_issue_23596():
1302
+ assert integrate(((1 + x)/x**2)*exp(-1/x), (x, 0, oo)) == oo
1303
+
1304
+
1305
+ def test_issue_23752():
1306
+ expr1 = sqrt(-I*x**2 + x - 3)
1307
+ expr2 = sqrt(-I*x**2 + I*x - 3)
1308
+ assert limit(expr1, x, 0, '+') == -sqrt(3)*I
1309
+ assert limit(expr1, x, 0, '-') == -sqrt(3)*I
1310
+ assert limit(expr2, x, 0, '+') == sqrt(3)*I
1311
+ assert limit(expr2, x, 0, '-') == -sqrt(3)*I
1312
+
1313
+
1314
+ def test_issue_24276():
1315
+ fx = log(tan(pi/2*tanh(x))).diff(x)
1316
+ assert fx.limit(x, oo) == 2
1317
+ assert fx.simplify().limit(x, oo) == 2
1318
+ assert fx.rewrite(sin).limit(x, oo) == 2
1319
+ assert fx.rewrite(sin).simplify().limit(x, oo) == 2
1320
+
1321
+ def test_issue_25230():
1322
+ a = Symbol('a', real = True)
1323
+ b = Symbol('b', positive = True)
1324
+ c = Symbol('c', negative = True)
1325
+ n = Symbol('n', integer = True)
1326
+ raises(NotImplementedError, lambda: limit(Mod(x, a), x, a))
1327
+ assert limit(Mod(x, b), x, n*b, '+') == 0
1328
+ assert limit(Mod(x, b), x, n*b, '-') == b
1329
+ assert limit(Mod(x, c), x, n*c, '+') == c
1330
+ assert limit(Mod(x, c), x, n*c, '-') == 0
1331
+
1332
+
1333
+ def test_issue_25582():
1334
+
1335
+ assert limit(asin(exp(x)), x, oo, '-') == -oo*I
1336
+ assert limit(acos(exp(x)), x, oo, '-') == oo*I
1337
+ assert limit(atan(exp(x)), x, oo, '-') == pi/2
1338
+ assert limit(acot(exp(x)), x, oo, '-') == 0
1339
+ assert limit(asec(exp(x)), x, oo, '-') == pi/2
1340
+ assert limit(acsc(exp(x)), x, oo, '-') == 0
1341
+
1342
+
1343
+ def test_issue_25847():
1344
+ #atan
1345
+ assert limit(atan(sin(x)/x), x, 0, '+-') == pi/4
1346
+ assert limit(atan(exp(1/x)), x, 0, '+') == pi/2
1347
+ assert limit(atan(exp(1/x)), x, 0, '-') == 0
1348
+
1349
+ #asin
1350
+ assert limit(asin(sin(x)/x), x, 0, '+-') == pi/2
1351
+ assert limit(asin(exp(1/x)), x, 0, '+') == -oo*I
1352
+ assert limit(asin(exp(1/x)), x, 0, '-') == 0
1353
+
1354
+ #acos
1355
+ assert limit(acos(sin(x)/x), x, 0, '+-') == 0
1356
+ assert limit(acos(exp(1/x)), x, 0, '+') == oo*I
1357
+ assert limit(acos(exp(1/x)), x, 0, '-') == pi/2
1358
+
1359
+ #acot
1360
+ assert limit(acot(sin(x)/x), x, 0, '+-') == pi/4
1361
+ assert limit(acot(exp(1/x)), x, 0, '+') == 0
1362
+ assert limit(acot(exp(1/x)), x, 0, '-') == pi/2
1363
+
1364
+ #asec
1365
+ assert limit(asec(sin(x)/x), x, 0, '+-') == 0
1366
+ assert limit(asec(exp(1/x)), x, 0, '+') == pi/2
1367
+ assert limit(asec(exp(1/x)), x, 0, '-') == oo*I
1368
+
1369
+ #acsc
1370
+ assert limit(acsc(sin(x)/x), x, 0, '+-') == pi/2
1371
+ assert limit(acsc(exp(1/x)), x, 0, '+') == 0
1372
+ assert limit(acsc(exp(1/x)), x, 0, '-') == -oo*I
1373
+
1374
+ #atanh
1375
+ assert limit(atanh(sin(x)/x), x, 0, '+-') == oo
1376
+ assert limit(atanh(exp(1/x)), x, 0, '+') == -I*pi/2
1377
+ assert limit(atanh(exp(1/x)), x, 0, '-') == 0
1378
+
1379
+ #asinh
1380
+ assert limit(asinh(sin(x)/x), x, 0, '+-') == log(1 + sqrt(2))
1381
+ assert limit(asinh(exp(1/x)), x, 0, '+') == oo
1382
+ assert limit(asinh(exp(1/x)), x, 0, '-') == 0
1383
+
1384
+ #acosh
1385
+ assert limit(acosh(sin(x)/x), x, 0, '+-') == 0
1386
+ assert limit(acosh(exp(1/x)), x, 0, '+') == oo
1387
+ assert limit(acosh(exp(1/x)), x, 0, '-') == I*pi/2
1388
+
1389
+ #acoth
1390
+ assert limit(acoth(sin(x)/x), x, 0, '+-') == oo
1391
+ assert limit(acoth(exp(1/x)), x, 0, '+') == 0
1392
+ assert limit(acoth(exp(1/x)), x, 0, '-') == -I*pi/2
1393
+
1394
+ #asech
1395
+ assert limit(asech(sin(x)/x), x, 0, '+-') == 0
1396
+ assert limit(asech(exp(1/x)), x, 0, '+') == I*pi/2
1397
+ assert limit(asech(exp(1/x)), x, 0, '-') == oo
1398
+
1399
+ #acsch
1400
+ assert limit(acsch(sin(x)/x), x, 0, '+-') == log(1 + sqrt(2))
1401
+ assert limit(acsch(exp(1/x)), x, 0, '+') == 0
1402
+ assert limit(acsch(exp(1/x)), x, 0, '-') == oo
1403
+
1404
+
1405
+ def test_issue_26040():
1406
+ assert limit(besseli(0, x + 1)/besseli(0, x), x, oo) == S.Exp1
1407
+
1408
+
1409
+ def test_issue_26250():
1410
+ e = elliptic_e(4*x/(x**2 + 2*x + 1))
1411
+ k = elliptic_k(4*x/(x**2 + 2*x + 1))
1412
+ e1 = ((1-3*x**2)*e**2/2 - (x**2-2*x+1)*e*k/2)
1413
+ e2 = pi**2*(x**8 - 2*x**7 - x**6 + 4*x**5 - x**4 - 2*x**3 + x**2)
1414
+ assert limit(e1/e2, x, 0) == -S(1)/8
1415
+
1416
+
1417
+ def test_issue_26513():
1418
+ assert limit(abs((-x/(x+1))**x), x ,oo) == exp(-1)
1419
+ assert limit((x/(x + 1))**x, x, oo) == exp(-1)
1420
+ raises (NotImplementedError, lambda: limit((-x/(x+1))**x, x, oo))
1421
+
1422
+
1423
+ def test_issue_26916():
1424
+ assert limit(Ei(x)*exp(-x), x, +oo) == 0
1425
+ assert limit(Ei(x)*exp(-x), x, -oo) == 0
1426
+
1427
+
1428
+ def test_issue_22982_15323():
1429
+ assert limit((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x, oo) == oo
1430
+ assert limit((1 - 1/x)**x*(log(1 - 1/x) + 1/(x*(1 - 1/x))), x, 1, dir='+') == 1
1431
+ assert limit((log(E + 1/x) )**(1 - sqrt(E + 1/x)), x, oo) == 1
1432
+ assert limit((log(E + 1/x) - 1)**(- sqrt(E + 1/x)), x, oo) == oo
1433
+
1434
+
1435
+ def test_issue_26991():
1436
+ assert limit(x/((x - 6)*sinh(tanh(0.03*x)) + tanh(x) - 0.5), x, oo) == 1/sinh(1)
1437
+
1438
+ def test_issue_27278():
1439
+ expr = (1/(x*log((x + 3)/x)))**x*((x + 1)*log((x + 4)/(x + 1)))**(x + 1)/3
1440
+ assert limit(expr, x, oo) == 1
.venv/lib/python3.13/site-packages/sympy/series/tests/test_limitseq.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.concrete.summations import Sum
2
+ from sympy.core.add import Add
3
+ from sympy.core.numbers import (I, Rational, oo, pi)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.functions.combinatorial.factorials import (binomial, factorial, subfactorial)
7
+ from sympy.functions.combinatorial.numbers import (fibonacci, harmonic)
8
+ from sympy.functions.elementary.exponential import (exp, log)
9
+ from sympy.functions.elementary.miscellaneous import sqrt
10
+ from sympy.functions.elementary.trigonometric import (cos, sin)
11
+ from sympy.functions.special.gamma_functions import gamma
12
+ from sympy.series.limitseq import limit_seq
13
+ from sympy.series.limitseq import difference_delta as dd
14
+ from sympy.testing.pytest import raises, XFAIL
15
+ from sympy.calculus.accumulationbounds import AccumulationBounds
16
+
17
+ n, m, k = symbols('n m k', integer=True)
18
+
19
+
20
+ def test_difference_delta():
21
+ e = n*(n + 1)
22
+ e2 = e * k
23
+
24
+ assert dd(e) == 2*n + 2
25
+ assert dd(e2, n, 2) == k*(4*n + 6)
26
+
27
+ raises(ValueError, lambda: dd(e2))
28
+ raises(ValueError, lambda: dd(e2, n, oo))
29
+
30
+
31
+ def test_difference_delta__Sum():
32
+ e = Sum(1/k, (k, 1, n))
33
+ assert dd(e, n) == 1/(n + 1)
34
+ assert dd(e, n, 5) == Add(*[1/(i + n + 1) for i in range(5)])
35
+
36
+ e = Sum(1/k, (k, 1, 3*n))
37
+ assert dd(e, n) == Add(*[1/(i + 3*n + 1) for i in range(3)])
38
+
39
+ e = n * Sum(1/k, (k, 1, n))
40
+ assert dd(e, n) == 1 + Sum(1/k, (k, 1, n))
41
+
42
+ e = Sum(1/k, (k, 1, n), (m, 1, n))
43
+ assert dd(e, n) == harmonic(n)
44
+
45
+
46
+ def test_difference_delta__Add():
47
+ e = n + n*(n + 1)
48
+ assert dd(e, n) == 2*n + 3
49
+ assert dd(e, n, 2) == 4*n + 8
50
+
51
+ e = n + Sum(1/k, (k, 1, n))
52
+ assert dd(e, n) == 1 + 1/(n + 1)
53
+ assert dd(e, n, 5) == 5 + Add(*[1/(i + n + 1) for i in range(5)])
54
+
55
+
56
+ def test_difference_delta__Pow():
57
+ e = 4**n
58
+ assert dd(e, n) == 3*4**n
59
+ assert dd(e, n, 2) == 15*4**n
60
+
61
+ e = 4**(2*n)
62
+ assert dd(e, n) == 15*4**(2*n)
63
+ assert dd(e, n, 2) == 255*4**(2*n)
64
+
65
+ e = n**4
66
+ assert dd(e, n) == (n + 1)**4 - n**4
67
+
68
+ e = n**n
69
+ assert dd(e, n) == (n + 1)**(n + 1) - n**n
70
+
71
+
72
+ def test_limit_seq():
73
+ e = binomial(2*n, n) / Sum(binomial(2*k, k), (k, 1, n))
74
+ assert limit_seq(e) == S(3) / 4
75
+ assert limit_seq(e, m) == e
76
+
77
+ e = (5*n**3 + 3*n**2 + 4) / (3*n**3 + 4*n - 5)
78
+ assert limit_seq(e, n) == S(5) / 3
79
+
80
+ e = (harmonic(n) * Sum(harmonic(k), (k, 1, n))) / (n * harmonic(2*n)**2)
81
+ assert limit_seq(e, n) == 1
82
+
83
+ e = Sum(k**2 * Sum(2**m/m, (m, 1, k)), (k, 1, n)) / (2**n*n)
84
+ assert limit_seq(e, n) == 4
85
+
86
+ e = (Sum(binomial(3*k, k) * binomial(5*k, k), (k, 1, n)) /
87
+ (binomial(3*n, n) * binomial(5*n, n)))
88
+ assert limit_seq(e, n) == S(84375) / 83351
89
+
90
+ e = Sum(harmonic(k)**2/k, (k, 1, 2*n)) / harmonic(n)**3
91
+ assert limit_seq(e, n) == S.One / 3
92
+
93
+ raises(ValueError, lambda: limit_seq(e * m))
94
+
95
+
96
+ def test_alternating_sign():
97
+ assert limit_seq((-1)**n/n**2, n) == 0
98
+ assert limit_seq((-2)**(n+1)/(n + 3**n), n) == 0
99
+ assert limit_seq((2*n + (-1)**n)/(n + 1), n) == 2
100
+ assert limit_seq(sin(pi*n), n) == 0
101
+ assert limit_seq(cos(2*pi*n), n) == 1
102
+ assert limit_seq((S.NegativeOne/5)**n, n) == 0
103
+ assert limit_seq((Rational(-1, 5))**n, n) == 0
104
+ assert limit_seq((I/3)**n, n) == 0
105
+ assert limit_seq(sqrt(n)*(I/2)**n, n) == 0
106
+ assert limit_seq(n**7*(I/3)**n, n) == 0
107
+ assert limit_seq(n/(n + 1) + (I/2)**n, n) == 1
108
+
109
+
110
+ def test_accum_bounds():
111
+ assert limit_seq((-1)**n, n) == AccumulationBounds(-1, 1)
112
+ assert limit_seq(cos(pi*n), n) == AccumulationBounds(-1, 1)
113
+ assert limit_seq(sin(pi*n/2)**2, n) == AccumulationBounds(0, 1)
114
+ assert limit_seq(2*(-3)**n/(n + 3**n), n) == AccumulationBounds(-2, 2)
115
+ assert limit_seq(3*n/(n + 1) + 2*(-1)**n, n) == AccumulationBounds(1, 5)
116
+
117
+
118
+ def test_limitseq_sum():
119
+ from sympy.abc import x, y, z
120
+ assert limit_seq(Sum(1/x, (x, 1, y)) - log(y), y) == S.EulerGamma
121
+ assert limit_seq(Sum(1/x, (x, 1, y)) - 1/y, y) is S.Infinity
122
+ assert (limit_seq(binomial(2*x, x) / Sum(binomial(2*y, y), (y, 1, x)), x) ==
123
+ S(3) / 4)
124
+ assert (limit_seq(Sum(y**2 * Sum(2**z/z, (z, 1, y)), (y, 1, x)) /
125
+ (2**x*x), x) == 4)
126
+
127
+
128
+ def test_issue_9308():
129
+ assert limit_seq(subfactorial(n)/factorial(n), n) == exp(-1)
130
+
131
+
132
+ def test_issue_10382():
133
+ n = Symbol('n', integer=True)
134
+ assert limit_seq(fibonacci(n+1)/fibonacci(n), n).together() == S.GoldenRatio
135
+
136
+
137
+ def test_issue_11672():
138
+ assert limit_seq(Rational(-1, 2)**n, n) == 0
139
+
140
+
141
+ def test_issue_14196():
142
+ k, n = symbols('k, n', positive=True)
143
+ m = Symbol('m')
144
+ assert limit_seq(Sum(m**k, (m, 1, n)).doit()/(n**(k + 1)), n) == 1/(k + 1)
145
+
146
+
147
+ def test_issue_16735():
148
+ assert limit_seq(5**n/factorial(n), n) == 0
149
+
150
+
151
+ def test_issue_19868():
152
+ assert limit_seq(1/gamma(n + S.One/2), n) == 0
153
+
154
+
155
+ @XFAIL
156
+ def test_limit_seq_fail():
157
+ # improve Summation algorithm or add ad-hoc criteria
158
+ e = (harmonic(n)**3 * Sum(1/harmonic(k), (k, 1, n)) /
159
+ (n * Sum(harmonic(k)/k, (k, 1, n))))
160
+ assert limit_seq(e, n) == 2
161
+
162
+ # No unique dominant term
163
+ e = (Sum(2**k * binomial(2*k, k) / k**2, (k, 1, n)) /
164
+ (Sum(2**k/k*2, (k, 1, n)) * Sum(binomial(2*k, k), (k, 1, n))))
165
+ assert limit_seq(e, n) == S(3) / 7
166
+
167
+ # Simplifications of summations needs to be improved.
168
+ e = n**3*Sum(2**k/k**2, (k, 1, n))**2 / (2**n * Sum(2**k/k, (k, 1, n)))
169
+ assert limit_seq(e, n) == 2
170
+
171
+ e = (harmonic(n) * Sum(2**k/k, (k, 1, n)) /
172
+ (n * Sum(2**k*harmonic(k)/k**2, (k, 1, n))))
173
+ assert limit_seq(e, n) == 1
174
+
175
+ e = (Sum(2**k*factorial(k) / k**2, (k, 1, 2*n)) /
176
+ (Sum(4**k/k**2, (k, 1, n)) * Sum(factorial(k), (k, 1, 2*n))))
177
+ assert limit_seq(e, n) == S(3) / 16
.venv/lib/python3.13/site-packages/sympy/series/tests/test_lseries.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import E
2
+ from sympy.core.singleton import S
3
+ from sympy.functions.elementary.exponential import exp
4
+ from sympy.functions.elementary.hyperbolic import tanh
5
+ from sympy.functions.elementary.trigonometric import (cos, sin)
6
+ from sympy.series.order import Order
7
+ from sympy.abc import x, y
8
+
9
+
10
+ def test_sin():
11
+ e = sin(x).lseries(x)
12
+ assert next(e) == x
13
+ assert next(e) == -x**3/6
14
+ assert next(e) == x**5/120
15
+
16
+
17
+ def test_cos():
18
+ e = cos(x).lseries(x)
19
+ assert next(e) == 1
20
+ assert next(e) == -x**2/2
21
+ assert next(e) == x**4/24
22
+
23
+
24
+ def test_exp():
25
+ e = exp(x).lseries(x)
26
+ assert next(e) == 1
27
+ assert next(e) == x
28
+ assert next(e) == x**2/2
29
+ assert next(e) == x**3/6
30
+
31
+
32
+ def test_exp2():
33
+ e = exp(cos(x)).lseries(x)
34
+ assert next(e) == E
35
+ assert next(e) == -E*x**2/2
36
+ assert next(e) == E*x**4/6
37
+ assert next(e) == -31*E*x**6/720
38
+
39
+
40
+ def test_simple():
41
+ assert list(x.lseries()) == [x]
42
+ assert list(S.One.lseries(x)) == [1]
43
+ assert not next((x/(x + y)).lseries(y)).has(Order)
44
+
45
+
46
+ def test_issue_5183():
47
+ s = (x + 1/x).lseries()
48
+ assert list(s) == [1/x, x]
49
+ assert next((x + x**2).lseries()) == x
50
+ assert next(((1 + x)**7).lseries(x)) == 1
51
+ assert next((sin(x + y)).series(x, n=3).lseries(y)) == x
52
+ # it would be nice if all terms were grouped, but in the
53
+ # following case that would mean that all the terms would have
54
+ # to be known since, for example, every term has a constant in it.
55
+ s = ((1 + x)**7).series(x, 1, n=None)
56
+ assert [next(s) for i in range(2)] == [128, -448 + 448*x]
57
+
58
+
59
+ def test_issue_6999():
60
+ s = tanh(x).lseries(x, 1)
61
+ assert next(s) == tanh(1)
62
+ assert next(s) == x - (x - 1)*tanh(1)**2 - 1
63
+ assert next(s) == -(x - 1)**2*tanh(1) + (x - 1)**2*tanh(1)**3
64
+ assert next(s) == -(x - 1)**3*tanh(1)**4 - (x - 1)**3/3 + \
65
+ 4*(x - 1)**3*tanh(1)**2/3
.venv/lib/python3.13/site-packages/sympy/series/tests/test_nseries.py ADDED
@@ -0,0 +1,557 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.calculus.util import AccumBounds
2
+ from sympy.core.function import (Derivative, PoleError)
3
+ from sympy.core.numbers import (E, I, Integer, Rational, pi)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.functions.elementary.complexes import sign
7
+ from sympy.functions.elementary.exponential import (exp, log)
8
+ from sympy.functions.elementary.hyperbolic import (acosh, acoth, asinh, atanh, cosh, coth, sinh, tanh)
9
+ from sympy.functions.elementary.integers import (ceiling, floor, frac)
10
+ from sympy.functions.elementary.miscellaneous import (cbrt, sqrt)
11
+ from sympy.functions.elementary.trigonometric import (asin, cos, cot, sin, tan)
12
+ from sympy.series.limits import limit
13
+ from sympy.series.order import O
14
+ from sympy.abc import x, y, z
15
+
16
+ from sympy.testing.pytest import raises, XFAIL
17
+
18
+
19
+ def test_simple_1():
20
+ assert x.nseries(x, n=5) == x
21
+ assert y.nseries(x, n=5) == y
22
+ assert (1/(x*y)).nseries(y, n=5) == 1/(x*y)
23
+ assert Rational(3, 4).nseries(x, n=5) == Rational(3, 4)
24
+ assert x.nseries() == x
25
+
26
+
27
+ def test_mul_0():
28
+ assert (x*log(x)).nseries(x, n=5) == x*log(x)
29
+
30
+
31
+ def test_mul_1():
32
+ assert (x*log(2 + x)).nseries(x, n=5) == x*log(2) + x**2/2 - x**3/8 + \
33
+ x**4/24 + O(x**5)
34
+ assert (x*log(1 + x)).nseries(
35
+ x, n=5) == x**2 - x**3/2 + x**4/3 + O(x**5)
36
+
37
+
38
+ def test_pow_0():
39
+ assert (x**2).nseries(x, n=5) == x**2
40
+ assert (1/x).nseries(x, n=5) == 1/x
41
+ assert (1/x**2).nseries(x, n=5) == 1/x**2
42
+ assert (x**Rational(2, 3)).nseries(x, n=5) == (x**Rational(2, 3))
43
+ assert (sqrt(x)**3).nseries(x, n=5) == (sqrt(x)**3)
44
+
45
+
46
+ def test_pow_1():
47
+ assert ((1 + x)**2).nseries(x, n=5) == x**2 + 2*x + 1
48
+
49
+ # https://github.com/sympy/sympy/issues/21075
50
+ assert ((sqrt(x) + 1)**2).nseries(x) == 2*sqrt(x) + x + 1
51
+ assert ((sqrt(x) + cbrt(x))**2).nseries(x) == 2*x**Rational(5, 6)\
52
+ + x**Rational(2, 3) + x
53
+
54
+
55
+ def test_geometric_1():
56
+ assert (1/(1 - x)).nseries(x, n=5) == 1 + x + x**2 + x**3 + x**4 + O(x**5)
57
+ assert (x/(1 - x)).nseries(x, n=6) == x + x**2 + x**3 + x**4 + x**5 + O(x**6)
58
+ assert (x**3/(1 - x)).nseries(x, n=8) == x**3 + x**4 + x**5 + x**6 + \
59
+ x**7 + O(x**8)
60
+
61
+
62
+ def test_sqrt_1():
63
+ assert sqrt(1 + x).nseries(x, n=5) == 1 + x/2 - x**2/8 + x**3/16 - 5*x**4/128 + O(x**5)
64
+
65
+
66
+ def test_exp_1():
67
+ assert exp(x).nseries(x, n=5) == 1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)
68
+ assert exp(x).nseries(x, n=12) == 1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120 + \
69
+ x**6/720 + x**7/5040 + x**8/40320 + x**9/362880 + x**10/3628800 + \
70
+ x**11/39916800 + O(x**12)
71
+ assert exp(1/x).nseries(x, n=5) == exp(1/x)
72
+ assert exp(1/(1 + x)).nseries(x, n=4) == \
73
+ (E*(1 - x - 13*x**3/6 + 3*x**2/2)).expand() + O(x**4)
74
+ assert exp(2 + x).nseries(x, n=5) == \
75
+ (exp(2)*(1 + x + x**2/2 + x**3/6 + x**4/24)).expand() + O(x**5)
76
+
77
+
78
+ def test_exp_sqrt_1():
79
+ assert exp(1 + sqrt(x)).nseries(x, n=3) == \
80
+ (exp(1)*(1 + sqrt(x) + x/2 + sqrt(x)*x/6)).expand() + O(sqrt(x)**3)
81
+
82
+
83
+ def test_power_x_x1():
84
+ assert (exp(x*log(x))).nseries(x, n=4) == \
85
+ 1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
86
+
87
+
88
+ def test_power_x_x2():
89
+ assert (x**x).nseries(x, n=4) == \
90
+ 1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**4*log(x)**4)
91
+
92
+
93
+ def test_log_singular1():
94
+ assert log(1 + 1/x).nseries(x, n=5) == x - log(x) - x**2/2 + x**3/3 - \
95
+ x**4/4 + O(x**5)
96
+
97
+
98
+ def test_log_power1():
99
+ e = 1 / (1/x + x ** (log(3)/log(2)))
100
+ assert e.nseries(x, n=5) == -x**(log(3)/log(2) + 2) + x + O(x**5)
101
+
102
+
103
+ def test_log_series():
104
+ l = Symbol('l')
105
+ e = 1/(1 - log(x))
106
+ assert e.nseries(x, n=5, logx=l) == 1/(1 - l)
107
+
108
+
109
+ def test_log2():
110
+ e = log(-1/x)
111
+ assert e.nseries(x, n=5) == -log(x) + log(-1)
112
+
113
+
114
+ def test_log3():
115
+ l = Symbol('l')
116
+ e = 1/log(-1/x)
117
+ assert e.nseries(x, n=4, logx=l) == 1/(-l + log(-1))
118
+
119
+
120
+ def test_series1():
121
+ e = sin(x)
122
+ assert e.nseries(x, 0, 0) != 0
123
+ assert e.nseries(x, 0, 0) == O(1, x)
124
+ assert e.nseries(x, 0, 1) == O(x, x)
125
+ assert e.nseries(x, 0, 2) == x + O(x**2, x)
126
+ assert e.nseries(x, 0, 3) == x + O(x**3, x)
127
+ assert e.nseries(x, 0, 4) == x - x**3/6 + O(x**4, x)
128
+
129
+ e = (exp(x) - 1)/x
130
+ assert e.nseries(x, 0, 3) == 1 + x/2 + x**2/6 + O(x**3)
131
+
132
+ assert x.nseries(x, 0, 2) == x
133
+
134
+
135
+ @XFAIL
136
+ def test_series1_failing():
137
+ assert x.nseries(x, 0, 0) == O(1, x)
138
+ assert x.nseries(x, 0, 1) == O(x, x)
139
+
140
+
141
+ def test_seriesbug1():
142
+ assert (1/x).nseries(x, 0, 3) == 1/x
143
+ assert (x + 1/x).nseries(x, 0, 3) == x + 1/x
144
+
145
+
146
+ def test_series2x():
147
+ assert ((x + 1)**(-2)).nseries(x, 0, 4) == 1 - 2*x + 3*x**2 - 4*x**3 + O(x**4, x)
148
+ assert ((x + 1)**(-1)).nseries(x, 0, 4) == 1 - x + x**2 - x**3 + O(x**4, x)
149
+ assert ((x + 1)**0).nseries(x, 0, 3) == 1
150
+ assert ((x + 1)**1).nseries(x, 0, 3) == 1 + x
151
+ assert ((x + 1)**2).nseries(x, 0, 3) == x**2 + 2*x + 1
152
+ assert ((x + 1)**3).nseries(x, 0, 3) == 1 + 3*x + 3*x**2 + O(x**3)
153
+
154
+ assert (1/(1 + x)).nseries(x, 0, 4) == 1 - x + x**2 - x**3 + O(x**4, x)
155
+ assert (x + 3/(1 + 2*x)).nseries(x, 0, 4) == 3 - 5*x + 12*x**2 - 24*x**3 + O(x**4, x)
156
+
157
+ assert ((1/x + 1)**3).nseries(x, 0, 3) == 1 + 3/x + 3/x**2 + x**(-3)
158
+ assert (1/(1 + 1/x)).nseries(x, 0, 4) == x - x**2 + x**3 - O(x**4, x)
159
+ assert (1/(1 + 1/x**2)).nseries(x, 0, 6) == x**2 - x**4 + O(x**6, x)
160
+
161
+
162
+ def test_bug2(): # 1/log(0)*log(0) problem
163
+ w = Symbol("w")
164
+ e = (w**(-1) + w**(
165
+ -log(3)*log(2)**(-1)))**(-1)*(3*w**(-log(3)*log(2)**(-1)) + 2*w**(-1))
166
+ e = e.expand()
167
+ assert e.nseries(w, 0, 4).subs(w, 0) == 3
168
+
169
+
170
+ def test_exp():
171
+ e = (1 + x)**(1/x)
172
+ assert e.nseries(x, n=3) == exp(1) - x*exp(1)/2 + 11*exp(1)*x**2/24 + O(x**3)
173
+
174
+
175
+ def test_exp2():
176
+ w = Symbol("w")
177
+ e = w**(1 - log(x)/(log(2) + log(x)))
178
+ logw = Symbol("logw")
179
+ assert e.nseries(
180
+ w, 0, 1, logx=logw) == exp(logw*log(2)/(log(x) + log(2)))
181
+
182
+
183
+ def test_bug3():
184
+ e = (2/x + 3/x**2)/(1/x + 1/x**2)
185
+ assert e.nseries(x, n=3) == 3 - x + x**2 + O(x**3)
186
+
187
+
188
+ def test_generalexponent():
189
+ p = 2
190
+ e = (2/x + 3/x**p)/(1/x + 1/x**p)
191
+ assert e.nseries(x, 0, 3) == 3 - x + x**2 + O(x**3)
192
+ p = S.Half
193
+ e = (2/x + 3/x**p)/(1/x + 1/x**p)
194
+ assert e.nseries(x, 0, 2) == 2 - x + sqrt(x) + x**(S(3)/2) + O(x**2)
195
+
196
+ e = 1 + sqrt(x)
197
+ assert e.nseries(x, 0, 4) == 1 + sqrt(x)
198
+
199
+ # more complicated example
200
+
201
+
202
+ def test_genexp_x():
203
+ e = 1/(1 + sqrt(x))
204
+ assert e.nseries(x, 0, 2) == \
205
+ 1 + x - sqrt(x) - sqrt(x)**3 + O(x**2, x)
206
+
207
+ # more complicated example
208
+
209
+
210
+ def test_genexp_x2():
211
+ p = Rational(3, 2)
212
+ e = (2/x + 3/x**p)/(1/x + 1/x**p)
213
+ assert e.nseries(x, 0, 3) == 3 + x + x**2 - sqrt(x) - x**(S(3)/2) - x**(S(5)/2) + O(x**3)
214
+
215
+
216
+ def test_seriesbug2():
217
+ w = Symbol("w")
218
+ #simple case (1):
219
+ e = ((2*w)/w)**(1 + w)
220
+ assert e.nseries(w, 0, 1) == 2 + O(w, w)
221
+ assert e.nseries(w, 0, 1).subs(w, 0) == 2
222
+
223
+
224
+ def test_seriesbug2b():
225
+ w = Symbol("w")
226
+ #test sin
227
+ e = sin(2*w)/w
228
+ assert e.nseries(w, 0, 3) == 2 - 4*w**2/3 + O(w**3)
229
+
230
+
231
+ def test_seriesbug2d():
232
+ w = Symbol("w", real=True)
233
+ e = log(sin(2*w)/w)
234
+ assert e.series(w, n=5) == log(2) - 2*w**2/3 - 4*w**4/45 + O(w**5)
235
+
236
+
237
+ def test_seriesbug2c():
238
+ w = Symbol("w", real=True)
239
+ #more complicated case, but sin(x)~x, so the result is the same as in (1)
240
+ e = (sin(2*w)/w)**(1 + w)
241
+ assert e.series(w, 0, 1) == 2 + O(w)
242
+ assert e.series(w, 0, 3) == 2 + 2*w*log(2) + \
243
+ w**2*(Rational(-4, 3) + log(2)**2) + O(w**3)
244
+ assert e.series(w, 0, 2).subs(w, 0) == 2
245
+
246
+
247
+ def test_expbug4():
248
+ x = Symbol("x", real=True)
249
+ assert (log(
250
+ sin(2*x)/x)*(1 + x)).series(x, 0, 2) == log(2) + x*log(2) + O(x**2, x)
251
+ assert exp(
252
+ log(sin(2*x)/x)*(1 + x)).series(x, 0, 2) == 2 + 2*x*log(2) + O(x**2)
253
+
254
+ assert exp(log(2) + O(x)).nseries(x, 0, 2) == 2 + O(x)
255
+ assert ((2 + O(x))**(1 + x)).nseries(x, 0, 2) == 2 + O(x)
256
+
257
+
258
+ def test_logbug4():
259
+ assert log(2 + O(x)).nseries(x, 0, 2) == log(2) + O(x, x)
260
+
261
+
262
+ def test_expbug5():
263
+ assert exp(log(1 + x)/x).nseries(x, n=3) == exp(1) + -exp(1)*x/2 + 11*exp(1)*x**2/24 + O(x**3)
264
+
265
+ assert exp(O(x)).nseries(x, 0, 2) == 1 + O(x)
266
+
267
+
268
+ def test_sinsinbug():
269
+ assert sin(sin(x)).nseries(x, 0, 8) == x - x**3/3 + x**5/10 - 8*x**7/315 + O(x**8)
270
+
271
+
272
+ def test_issue_3258():
273
+ a = x/(exp(x) - 1)
274
+ assert a.nseries(x, 0, 5) == 1 - x/2 - x**4/720 + x**2/12 + O(x**5)
275
+
276
+
277
+ def test_issue_3204():
278
+ x = Symbol("x", nonnegative=True)
279
+ f = sin(x**3)**Rational(1, 3)
280
+ assert f.nseries(x, 0, 17) == x - x**7/18 - x**13/3240 + O(x**17)
281
+
282
+
283
+ def test_issue_3224():
284
+ f = sqrt(1 - sqrt(y))
285
+ assert f.nseries(y, 0, 2) == 1 - sqrt(y)/2 - y/8 - sqrt(y)**3/16 + O(y**2)
286
+
287
+
288
+ def test_issue_3463():
289
+ w, i = symbols('w,i')
290
+ r = log(5)/log(3)
291
+ p = w**(-1 + r)
292
+ e = 1/x*(-log(w**(1 + r)) + log(w + w**r))
293
+ e_ser = -r*log(w)/x + p/x - p**2/(2*x) + O(w)
294
+ assert e.nseries(w, n=1) == e_ser
295
+
296
+
297
+ def test_sin():
298
+ assert sin(8*x).nseries(x, n=4) == 8*x - 256*x**3/3 + O(x**4)
299
+ assert sin(x + y).nseries(x, n=1) == sin(y) + O(x)
300
+ assert sin(x + y).nseries(x, n=2) == sin(y) + cos(y)*x + O(x**2)
301
+ assert sin(x + y).nseries(x, n=5) == sin(y) + cos(y)*x - sin(y)*x**2/2 - \
302
+ cos(y)*x**3/6 + sin(y)*x**4/24 + O(x**5)
303
+
304
+
305
+ def test_issue_3515():
306
+ e = sin(8*x)/x
307
+ assert e.nseries(x, n=6) == 8 - 256*x**2/3 + 4096*x**4/15 + O(x**6)
308
+
309
+
310
+ def test_issue_3505():
311
+ e = sin(x)**(-4)*(sqrt(cos(x))*sin(x)**2 -
312
+ cos(x)**Rational(1, 3)*sin(x)**2)
313
+ assert e.nseries(x, n=9) == Rational(-1, 12) - 7*x**2/288 - \
314
+ 43*x**4/10368 - 1123*x**6/2488320 + 377*x**8/29859840 + O(x**9)
315
+
316
+
317
+ def test_issue_3501():
318
+ a = Symbol("a")
319
+ e = x**(-2)*(x*sin(a + x) - x*sin(a))
320
+ assert e.nseries(x, n=6) == cos(a) - sin(a)*x/2 - cos(a)*x**2/6 + \
321
+ x**3*sin(a)/24 + x**4*cos(a)/120 - x**5*sin(a)/720 + O(x**6)
322
+ e = x**(-2)*(x*cos(a + x) - x*cos(a))
323
+ assert e.nseries(x, n=6) == -sin(a) - cos(a)*x/2 + sin(a)*x**2/6 + \
324
+ cos(a)*x**3/24 - x**4*sin(a)/120 - x**5*cos(a)/720 + O(x**6)
325
+
326
+
327
+ def test_issue_3502():
328
+ e = sin(5*x)/sin(2*x)
329
+ assert e.nseries(x, n=2) == Rational(5, 2) + O(x**2)
330
+ assert e.nseries(x, n=6) == \
331
+ Rational(5, 2) - 35*x**2/4 + 329*x**4/48 + O(x**6)
332
+
333
+
334
+ def test_issue_3503():
335
+ e = sin(2 + x)/(2 + x)
336
+ assert e.nseries(x, n=2) == sin(2)/2 + x*cos(2)/2 - x*sin(2)/4 + O(x**2)
337
+
338
+
339
+ def test_issue_3506():
340
+ e = (x + sin(3*x))**(-2)*(x*(x + sin(3*x)) - (x + sin(3*x))*sin(2*x))
341
+ assert e.nseries(x, n=7) == \
342
+ Rational(-1, 4) + 5*x**2/96 + 91*x**4/768 + 11117*x**6/129024 + O(x**7)
343
+
344
+
345
+ def test_issue_3508():
346
+ x = Symbol("x", real=True)
347
+ assert log(sin(x)).series(x, n=5) == log(x) - x**2/6 - x**4/180 + O(x**5)
348
+ e = -log(x) + x*(-log(x) + log(sin(2*x))) + log(sin(2*x))
349
+ assert e.series(x, n=5) == \
350
+ log(2) + log(2)*x - 2*x**2/3 - 2*x**3/3 - 4*x**4/45 + O(x**5)
351
+
352
+
353
+ def test_issue_3507():
354
+ e = x**(-4)*(x**2 - x**2*sqrt(cos(x)))
355
+ assert e.nseries(x, n=9) == \
356
+ Rational(1, 4) + x**2/96 + 19*x**4/5760 + 559*x**6/645120 + 29161*x**8/116121600 + O(x**9)
357
+
358
+
359
+ def test_issue_3639():
360
+ assert sin(cos(x)).nseries(x, n=5) == \
361
+ sin(1) - x**2*cos(1)/2 - x**4*sin(1)/8 + x**4*cos(1)/24 + O(x**5)
362
+
363
+
364
+ def test_hyperbolic():
365
+ assert sinh(x).nseries(x, n=6) == x + x**3/6 + x**5/120 + O(x**6)
366
+ assert cosh(x).nseries(x, n=5) == 1 + x**2/2 + x**4/24 + O(x**5)
367
+ assert tanh(x).nseries(x, n=6) == x - x**3/3 + 2*x**5/15 + O(x**6)
368
+ assert coth(x).nseries(x, n=6) == \
369
+ 1/x - x**3/45 + x/3 + 2*x**5/945 + O(x**6)
370
+ assert asinh(x).nseries(x, n=6) == x - x**3/6 + 3*x**5/40 + O(x**6)
371
+ assert acosh(x).nseries(x, n=6) == \
372
+ pi*I/2 - I*x - 3*I*x**5/40 - I*x**3/6 + O(x**6)
373
+ assert atanh(x).nseries(x, n=6) == x + x**3/3 + x**5/5 + O(x**6)
374
+ assert acoth(x).nseries(x, n=6) == -I*pi/2 + x + x**3/3 + x**5/5 + O(x**6)
375
+
376
+
377
+ def test_series2():
378
+ w = Symbol("w", real=True)
379
+ x = Symbol("x", real=True)
380
+ e = w**(-2)*(w*exp(1/x - w) - w*exp(1/x))
381
+ assert e.nseries(w, n=4) == -exp(1/x) + w*exp(1/x)/2 - w**2*exp(1/x)/6 + w**3*exp(1/x)/24 + O(w**4)
382
+
383
+
384
+ def test_series3():
385
+ w = Symbol("w", real=True)
386
+ e = w**(-6)*(w**3*tan(w) - w**3*sin(w))
387
+ assert e.nseries(w, n=8) == Integer(1)/2 + w**2/8 + 13*w**4/240 + 529*w**6/24192 + O(w**8)
388
+
389
+
390
+ def test_bug4():
391
+ w = Symbol("w")
392
+ e = x/(w**4 + x**2*w**4 + 2*x*w**4)*w**4
393
+ assert e.nseries(w, n=2).removeO().expand() in [x/(1 + 2*x + x**2),
394
+ 1/(1 + x/2 + 1/x/2)/2, 1/x/(1 + 2/x + x**(-2))]
395
+
396
+
397
+ def test_bug5():
398
+ w = Symbol("w")
399
+ l = Symbol('l')
400
+ e = (-log(w) + log(1 + w*log(x)))**(-2)*w**(-2)*((-log(w) +
401
+ log(1 + x*w))*(-log(w) + log(1 + w*log(x)))*w - x*(-log(w) +
402
+ log(1 + w*log(x)))*w)
403
+ assert e.nseries(w, n=0, logx=l) == x/w/l + 1/w + O(1, w)
404
+ assert e.nseries(w, n=1, logx=l) == x/w/l + 1/w - x/l + 1/l*log(x) \
405
+ + x*log(x)/l**2 + O(w)
406
+
407
+
408
+ def test_issue_4115():
409
+ assert (sin(x)/(1 - cos(x))).nseries(x, n=1) == 2/x + O(x)
410
+ assert (sin(x)**2/(1 - cos(x))).nseries(x, n=1) == 2 + O(x)
411
+
412
+
413
+ def test_pole():
414
+ raises(PoleError, lambda: sin(1/x).series(x, 0, 5))
415
+ raises(PoleError, lambda: sin(1 + 1/x).series(x, 0, 5))
416
+ raises(PoleError, lambda: (x*sin(1/x)).series(x, 0, 5))
417
+
418
+
419
+ def test_expsinbug():
420
+ assert exp(sin(x)).series(x, 0, 0) == O(1, x)
421
+ assert exp(sin(x)).series(x, 0, 1) == 1 + O(x)
422
+ assert exp(sin(x)).series(x, 0, 2) == 1 + x + O(x**2)
423
+ assert exp(sin(x)).series(x, 0, 3) == 1 + x + x**2/2 + O(x**3)
424
+ assert exp(sin(x)).series(x, 0, 4) == 1 + x + x**2/2 + O(x**4)
425
+ assert exp(sin(x)).series(x, 0, 5) == 1 + x + x**2/2 - x**4/8 + O(x**5)
426
+
427
+
428
+ def test_floor():
429
+ x = Symbol('x')
430
+ assert floor(x).series(x) == 0
431
+ assert floor(-x).series(x) == -1
432
+ assert floor(sin(x)).series(x) == 0
433
+ assert floor(sin(-x)).series(x) == -1
434
+ assert floor(x**3).series(x) == 0
435
+ assert floor(-x**3).series(x) == -1
436
+ assert floor(cos(x)).series(x) == 0
437
+ assert floor(cos(-x)).series(x) == 0
438
+ assert floor(5 + sin(x)).series(x) == 5
439
+ assert floor(5 + sin(-x)).series(x) == 4
440
+
441
+ assert floor(x).series(x, 2) == 2
442
+ assert floor(-x).series(x, 2) == -3
443
+
444
+ x = Symbol('x', negative=True)
445
+ assert floor(x + 1.5).series(x) == 1
446
+
447
+
448
+ def test_frac():
449
+ assert frac(x).series(x, cdir=1) == x
450
+ assert frac(x).series(x, cdir=-1) == 1 + x
451
+ assert frac(2*x + 1).series(x, cdir=1) == 2*x
452
+ assert frac(2*x + 1).series(x, cdir=-1) == 1 + 2*x
453
+ assert frac(x**2).series(x, cdir=1) == x**2
454
+ assert frac(x**2).series(x, cdir=-1) == x**2
455
+ assert frac(sin(x) + 5).series(x, cdir=1) == x - x**3/6 + x**5/120 + O(x**6)
456
+ assert frac(sin(x) + 5).series(x, cdir=-1) == 1 + x - x**3/6 + x**5/120 + O(x**6)
457
+ assert frac(sin(x) + S.Half).series(x) == S.Half + x - x**3/6 + x**5/120 + O(x**6)
458
+ assert frac(x**8).series(x, cdir=1) == O(x**6)
459
+ assert frac(1/x).series(x) == AccumBounds(0, 1) + O(x**6)
460
+
461
+
462
+ def test_ceiling():
463
+ assert ceiling(x).series(x) == 1
464
+ assert ceiling(-x).series(x) == 0
465
+ assert ceiling(sin(x)).series(x) == 1
466
+ assert ceiling(sin(-x)).series(x) == 0
467
+ assert ceiling(1 - cos(x)).series(x) == 1
468
+ assert ceiling(1 - cos(-x)).series(x) == 1
469
+ assert ceiling(x).series(x, 2) == 3
470
+ assert ceiling(-x).series(x, 2) == -2
471
+
472
+
473
+ def test_abs():
474
+ a = Symbol('a')
475
+ assert abs(x).nseries(x, n=4) == x
476
+ assert abs(-x).nseries(x, n=4) == x
477
+ assert abs(x + 1).nseries(x, n=4) == x + 1
478
+ assert abs(sin(x)).nseries(x, n=4) == x - Rational(1, 6)*x**3 + O(x**4)
479
+ assert abs(sin(-x)).nseries(x, n=4) == x - Rational(1, 6)*x**3 + O(x**4)
480
+ assert abs(x - a).nseries(x, 1) == -a*sign(1 - a) + (x - 1)*sign(1 - a) + sign(1 - a)
481
+
482
+
483
+ def test_dir():
484
+ assert abs(x).series(x, 0, dir="+") == x
485
+ assert abs(x).series(x, 0, dir="-") == -x
486
+ assert floor(x + 2).series(x, 0, dir='+') == 2
487
+ assert floor(x + 2).series(x, 0, dir='-') == 1
488
+ assert floor(x + 2.2).series(x, 0, dir='-') == 2
489
+ assert ceiling(x + 2.2).series(x, 0, dir='-') == 3
490
+ assert sin(x + y).series(x, 0, dir='-') == sin(x + y).series(x, 0, dir='+')
491
+
492
+
493
+ def test_cdir():
494
+ assert abs(x).series(x, 0, cdir=1) == x
495
+ assert abs(x).series(x, 0, cdir=-1) == -x
496
+ assert floor(x + 2).series(x, 0, cdir=1) == 2
497
+ assert floor(x + 2).series(x, 0, cdir=-1) == 1
498
+ assert floor(x + 2.2).series(x, 0, cdir=1) == 2
499
+ assert ceiling(x + 2.2).series(x, 0, cdir=-1) == 3
500
+ assert sin(x + y).series(x, 0, cdir=-1) == sin(x + y).series(x, 0, cdir=1)
501
+
502
+
503
+ def test_issue_3504():
504
+ a = Symbol("a")
505
+ e = asin(a*x)/x
506
+ assert e.series(x, 4, n=2).removeO() == \
507
+ (x - 4)*(a/(4*sqrt(-16*a**2 + 1)) - asin(4*a)/16) + asin(4*a)/4
508
+
509
+
510
+ def test_issue_4441():
511
+ a, b = symbols('a,b')
512
+ f = 1/(1 + a*x)
513
+ assert f.series(x, 0, 5) == 1 - a*x + a**2*x**2 - a**3*x**3 + \
514
+ a**4*x**4 + O(x**5)
515
+ f = 1/(1 + (a + b)*x)
516
+ assert f.series(x, 0, 3) == 1 + x*(-a - b)\
517
+ + x**2*(a + b)**2 + O(x**3)
518
+
519
+
520
+ def test_issue_4329():
521
+ assert tan(x).series(x, pi/2, n=3).removeO() == \
522
+ -pi/6 + x/3 - 1/(x - pi/2)
523
+ assert cot(x).series(x, pi, n=3).removeO() == \
524
+ -x/3 + pi/3 + 1/(x - pi)
525
+ assert limit(tan(x)**tan(2*x), x, pi/4) == exp(-1)
526
+
527
+
528
+ def test_issue_5183():
529
+ assert abs(x + x**2).series(n=1) == O(x)
530
+ assert abs(x + x**2).series(n=2) == x + O(x**2)
531
+ assert ((1 + x)**2).series(x, n=6) == x**2 + 2*x + 1
532
+ assert (1 + 1/x).series() == 1 + 1/x
533
+ assert Derivative(exp(x).series(), x).doit() == \
534
+ 1 + x + x**2/2 + x**3/6 + x**4/24 + O(x**5)
535
+
536
+
537
+ def test_issue_5654():
538
+ a = Symbol('a')
539
+ assert (1/(x**2+a**2)**2).nseries(x, x0=I*a, n=0) == \
540
+ -I/(4*a**3*(-I*a + x)) - 1/(4*a**2*(-I*a + x)**2) + O(1, (x, I*a))
541
+ assert (1/(x**2+a**2)**2).nseries(x, x0=I*a, n=1) == 3/(16*a**4) \
542
+ -I/(4*a**3*(-I*a + x)) - 1/(4*a**2*(-I*a + x)**2) + O(-I*a + x, (x, I*a))
543
+
544
+
545
+ def test_issue_5925():
546
+ sx = sqrt(x + z).series(z, 0, 1)
547
+ sxy = sqrt(x + y + z).series(z, 0, 1)
548
+ s1, s2 = sx.subs(x, x + y), sxy
549
+ assert (s1 - s2).expand().removeO().simplify() == 0
550
+
551
+ sx = sqrt(x + z).series(z, 0, 1)
552
+ sxy = sqrt(x + y + z).series(z, 0, 1)
553
+ assert sxy.subs({x:1, y:2}) == sx.subs(x, 3)
554
+
555
+
556
+ def test_exp_2():
557
+ assert exp(x**3).nseries(x, 0, 14) == 1 + x**3 + x**6/2 + x**9/6 + x**12/24 + O(x**14)
.venv/lib/python3.13/site-packages/sympy/series/tests/test_order.py ADDED
@@ -0,0 +1,503 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.add import Add
2
+ from sympy.core.function import (Function, expand)
3
+ from sympy.core.numbers import (I, Rational, nan, oo, pi)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.functions.combinatorial.factorials import factorial
7
+ from sympy.functions.elementary.complexes import (conjugate, transpose)
8
+ from sympy.functions.elementary.exponential import (exp, log)
9
+ from sympy.functions.elementary.miscellaneous import sqrt
10
+ from sympy.functions.elementary.trigonometric import (cos, sin)
11
+ from sympy.integrals.integrals import Integral
12
+ from sympy.series.order import O, Order
13
+ from sympy.core.expr import unchanged
14
+ from sympy.testing.pytest import raises
15
+ from sympy.abc import w, x, y, z
16
+ from sympy.testing.pytest import XFAIL
17
+
18
+
19
+ def test_caching_bug():
20
+ #needs to be a first test, so that all caches are clean
21
+ #cache it
22
+ O(w)
23
+ #and test that this won't raise an exception
24
+ O(w**(-1/x/log(3)*log(5)), w)
25
+
26
+
27
+ def test_free_symbols():
28
+ assert Order(1).free_symbols == set()
29
+ assert Order(x).free_symbols == {x}
30
+ assert Order(1, x).free_symbols == {x}
31
+ assert Order(x*y).free_symbols == {x, y}
32
+ assert Order(x, x, y).free_symbols == {x, y}
33
+
34
+
35
+ def test_simple_1():
36
+ o = Rational(0)
37
+ assert Order(2*x) == Order(x)
38
+ assert Order(x)*3 == Order(x)
39
+ assert -28*Order(x) == Order(x)
40
+ assert Order(Order(x)) == Order(x)
41
+ assert Order(Order(x), y) == Order(Order(x), x, y)
42
+ assert Order(-23) == Order(1)
43
+ assert Order(exp(x)) == Order(1, x)
44
+ assert Order(exp(1/x)).expr == exp(1/x)
45
+ assert Order(x*exp(1/x)).expr == x*exp(1/x)
46
+ assert Order(x**(o/3)).expr == x**(o/3)
47
+ assert Order(x**(o*Rational(5, 3))).expr == x**(o*Rational(5, 3))
48
+ assert Order(x**2 + x + y, x) == O(1, x)
49
+ assert Order(x**2 + x + y, y) == O(1, y)
50
+ raises(ValueError, lambda: Order(exp(x), x, x))
51
+ raises(TypeError, lambda: Order(x, 2 - x))
52
+
53
+
54
+ def test_simple_2():
55
+ assert Order(2*x)*x == Order(x**2)
56
+ assert Order(2*x)/x == Order(1, x)
57
+ assert Order(2*x)*x*exp(1/x) == Order(x**2*exp(1/x))
58
+ assert (Order(2*x)*x*exp(1/x)/log(x)**3).expr == x**2*exp(1/x)*log(x)**-3
59
+
60
+
61
+ def test_simple_3():
62
+ assert Order(x) + x == Order(x)
63
+ assert Order(x) + 2 == 2 + Order(x)
64
+ assert Order(x) + x**2 == Order(x)
65
+ assert Order(x) + 1/x == 1/x + Order(x)
66
+ assert Order(1/x) + 1/x**2 == 1/x**2 + Order(1/x)
67
+ assert Order(x) + exp(1/x) == Order(x) + exp(1/x)
68
+
69
+
70
+ def test_simple_4():
71
+ assert Order(x)**2 == Order(x**2)
72
+
73
+
74
+ def test_simple_5():
75
+ assert Order(x) + Order(x**2) == Order(x)
76
+ assert Order(x) + Order(x**-2) == Order(x**-2)
77
+ assert Order(x) + Order(1/x) == Order(1/x)
78
+
79
+
80
+ def test_simple_6():
81
+ assert Order(x) - Order(x) == Order(x)
82
+ assert Order(x) + Order(1) == Order(1)
83
+ assert Order(x) + Order(x**2) == Order(x)
84
+ assert Order(1/x) + Order(1) == Order(1/x)
85
+ assert Order(x) + Order(exp(1/x)) == Order(exp(1/x))
86
+ assert Order(x**3) + Order(exp(2/x)) == Order(exp(2/x))
87
+ assert Order(x**-3) + Order(exp(2/x)) == Order(exp(2/x))
88
+
89
+
90
+ def test_simple_7():
91
+ assert 1 + O(1) == O(1)
92
+ assert 2 + O(1) == O(1)
93
+ assert x + O(1) == O(1)
94
+ assert 1/x + O(1) == 1/x + O(1)
95
+
96
+
97
+ def test_simple_8():
98
+ assert O(sqrt(-x)) == O(sqrt(x))
99
+ assert O(x**2*sqrt(x)) == O(x**Rational(5, 2))
100
+ assert O(x**3*sqrt(-(-x)**3)) == O(x**Rational(9, 2))
101
+ assert O(x**Rational(3, 2)*sqrt((-x)**3)) == O(x**3)
102
+ assert O(x*(-2*x)**(I/2)) == O(x*(-x)**(I/2))
103
+
104
+
105
+ def test_as_expr_variables():
106
+ assert Order(x).as_expr_variables(None) == (x, ((x, 0),))
107
+ assert Order(x).as_expr_variables(((x, 0),)) == (x, ((x, 0),))
108
+ assert Order(y).as_expr_variables(((x, 0),)) == (y, ((x, 0), (y, 0)))
109
+ assert Order(y).as_expr_variables(((x, 0), (y, 0))) == (y, ((x, 0), (y, 0)))
110
+
111
+
112
+ def test_contains_0():
113
+ assert Order(1, x).contains(Order(1, x))
114
+ assert Order(1, x).contains(Order(1))
115
+ assert Order(1).contains(Order(1, x)) is False
116
+
117
+
118
+ def test_contains_1():
119
+ assert Order(x).contains(Order(x))
120
+ assert Order(x).contains(Order(x**2))
121
+ assert not Order(x**2).contains(Order(x))
122
+ assert not Order(x).contains(Order(1/x))
123
+ assert not Order(1/x).contains(Order(exp(1/x)))
124
+ assert not Order(x).contains(Order(exp(1/x)))
125
+ assert Order(1/x).contains(Order(x))
126
+ assert Order(exp(1/x)).contains(Order(x))
127
+ assert Order(exp(1/x)).contains(Order(1/x))
128
+ assert Order(exp(1/x)).contains(Order(exp(1/x)))
129
+ assert Order(exp(2/x)).contains(Order(exp(1/x)))
130
+ assert not Order(exp(1/x)).contains(Order(exp(2/x)))
131
+
132
+
133
+ def test_contains_2():
134
+ assert Order(x).contains(Order(y)) is None
135
+ assert Order(x).contains(Order(y*x))
136
+ assert Order(y*x).contains(Order(x))
137
+ assert Order(y).contains(Order(x*y))
138
+ assert Order(x).contains(Order(y**2*x))
139
+
140
+
141
+ def test_contains_3():
142
+ assert Order(x*y**2).contains(Order(x**2*y)) is None
143
+ assert Order(x**2*y).contains(Order(x*y**2)) is None
144
+
145
+
146
+ def test_contains_4():
147
+ assert Order(sin(1/x**2)).contains(Order(cos(1/x**2))) is True
148
+ assert Order(cos(1/x**2)).contains(Order(sin(1/x**2))) is True
149
+
150
+
151
+ def test_contains():
152
+ assert Order(1, x) not in Order(1)
153
+ assert Order(1) in Order(1, x)
154
+ raises(TypeError, lambda: Order(x*y**2) in Order(x**2*y))
155
+
156
+
157
+ def test_add_1():
158
+ assert Order(x + x) == Order(x)
159
+ assert Order(3*x - 2*x**2) == Order(x)
160
+ assert Order(1 + x) == Order(1, x)
161
+ assert Order(1 + 1/x) == Order(1/x)
162
+ # TODO : A better output for Order(log(x) + 1/log(x))
163
+ # could be Order(log(x)). Currently Order for expressions
164
+ # where all arguments would involve a log term would fall
165
+ # in this category and outputs for these should be improved.
166
+ assert Order(log(x) + 1/log(x)) == Order((log(x)**2 + 1)/log(x))
167
+ assert Order(exp(1/x) + x) == Order(exp(1/x))
168
+ assert Order(exp(1/x) + 1/x**20) == Order(exp(1/x))
169
+
170
+
171
+ def test_ln_args():
172
+ assert O(log(x)) + O(log(2*x)) == O(log(x))
173
+ assert O(log(x)) + O(log(x**3)) == O(log(x))
174
+ assert O(log(x*y)) + O(log(x) + log(y)) == O(log(x) + log(y), x, y)
175
+
176
+
177
+ def test_multivar_0():
178
+ assert Order(x*y).expr == x*y
179
+ assert Order(x*y**2).expr == x*y**2
180
+ assert Order(x*y, x).expr == x
181
+ assert Order(x*y**2, y).expr == y**2
182
+ assert Order(x*y*z).expr == x*y*z
183
+ assert Order(x/y).expr == x/y
184
+ assert Order(x*exp(1/y)).expr == x*exp(1/y)
185
+ assert Order(exp(x)*exp(1/y)).expr == exp(x)*exp(1/y)
186
+
187
+
188
+ def test_multivar_0a():
189
+ assert Order(exp(1/x)*exp(1/y)).expr == exp(1/x)*exp(1/y)
190
+
191
+
192
+ def test_multivar_1():
193
+ assert Order(x + y).expr == x + y
194
+ assert Order(x + 2*y).expr == x + y
195
+ assert (Order(x + y) + x).expr == (x + y)
196
+ assert (Order(x + y) + x**2) == Order(x + y)
197
+ assert (Order(x + y) + 1/x) == 1/x + Order(x + y)
198
+ assert Order(x**2 + y*x).expr == x**2 + y*x
199
+
200
+
201
+ def test_multivar_2():
202
+ assert Order(x**2*y + y**2*x, x, y).expr == x**2*y + y**2*x
203
+
204
+
205
+ def test_multivar_mul_1():
206
+ assert Order(x + y)*x == Order(x**2 + y*x, x, y)
207
+
208
+
209
+ def test_multivar_3():
210
+ assert (Order(x) + Order(y)).args in [
211
+ (Order(x), Order(y)),
212
+ (Order(y), Order(x))]
213
+ assert Order(x) + Order(y) + Order(x + y) == Order(x + y)
214
+ assert (Order(x**2*y) + Order(y**2*x)).args in [
215
+ (Order(x*y**2), Order(y*x**2)),
216
+ (Order(y*x**2), Order(x*y**2))]
217
+ assert (Order(x**2*y) + Order(y*x)) == Order(x*y)
218
+
219
+
220
+ def test_issue_3468():
221
+ y = Symbol('y', negative=True)
222
+ z = Symbol('z', complex=True)
223
+
224
+ # check that Order does not modify assumptions about symbols
225
+ Order(x)
226
+ Order(y)
227
+ Order(z)
228
+
229
+ assert x.is_positive is None
230
+ assert y.is_positive is False
231
+ assert z.is_positive is None
232
+
233
+
234
+ def test_leading_order():
235
+ assert (x + 1 + 1/x**5).extract_leading_order(x) == ((1/x**5, O(1/x**5)),)
236
+ assert (1 + 1/x).extract_leading_order(x) == ((1/x, O(1/x)),)
237
+ assert (1 + x).extract_leading_order(x) == ((1, O(1, x)),)
238
+ assert (1 + x**2).extract_leading_order(x) == ((1, O(1, x)),)
239
+ assert (2 + x**2).extract_leading_order(x) == ((2, O(1, x)),)
240
+ assert (x + x**2).extract_leading_order(x) == ((x, O(x)),)
241
+
242
+
243
+ def test_leading_order2():
244
+ assert set((2 + pi + x**2).extract_leading_order(x)) == {(pi, O(1, x)),
245
+ (S(2), O(1, x))}
246
+ assert set((2*x + pi*x + x**2).extract_leading_order(x)) == {(2*x, O(x)),
247
+ (x*pi, O(x))}
248
+
249
+
250
+ def test_order_leadterm():
251
+ assert O(x**2)._eval_as_leading_term(x, None, 1) == O(x**2)
252
+
253
+
254
+ def test_order_symbols():
255
+ e = x*y*sin(x)*Integral(x, (x, 1, 2))
256
+ assert O(e) == O(x**2*y, x, y)
257
+ assert O(e, x) == O(x**2)
258
+
259
+
260
+ def test_nan():
261
+ assert O(nan) is nan
262
+ assert not O(x).contains(nan)
263
+
264
+
265
+ def test_O1():
266
+ assert O(1, x) * x == O(x)
267
+ assert O(1, y) * x == O(1, y)
268
+
269
+
270
+ def test_getn():
271
+ # other lines are tested incidentally by the suite
272
+ assert O(x).getn() == 1
273
+ assert O(x/log(x)).getn() == 1
274
+ assert O(x**2/log(x)**2).getn() == 2
275
+ assert O(x*log(x)).getn() == 1
276
+ raises(NotImplementedError, lambda: (O(x) + O(y)).getn())
277
+
278
+
279
+ def test_diff():
280
+ assert O(x**2).diff(x) == O(x)
281
+
282
+
283
+ def test_getO():
284
+ assert (x).getO() is None
285
+ assert (x).removeO() == x
286
+ assert (O(x)).getO() == O(x)
287
+ assert (O(x)).removeO() == 0
288
+ assert (z + O(x) + O(y)).getO() == O(x) + O(y)
289
+ assert (z + O(x) + O(y)).removeO() == z
290
+ raises(NotImplementedError, lambda: (O(x) + O(y)).getn())
291
+
292
+
293
+ def test_leading_term():
294
+ from sympy.functions.special.gamma_functions import digamma
295
+ assert O(1/digamma(1/x)) == O(1/log(x))
296
+
297
+
298
+ def test_eval():
299
+ assert Order(x).subs(Order(x), 1) == 1
300
+ assert Order(x).subs(x, y) == Order(y)
301
+ assert Order(x).subs(y, x) == Order(x)
302
+ assert Order(x).subs(x, x + y) == Order(x + y, (x, -y))
303
+ assert (O(1)**x).is_Pow
304
+
305
+
306
+ def test_issue_4279():
307
+ a, b = symbols('a b')
308
+ assert O(a, a, b) + O(1, a, b) == O(1, a, b)
309
+ assert O(b, a, b) + O(1, a, b) == O(1, a, b)
310
+ assert O(a + b, a, b) + O(1, a, b) == O(1, a, b)
311
+ assert O(1, a, b) + O(a, a, b) == O(1, a, b)
312
+ assert O(1, a, b) + O(b, a, b) == O(1, a, b)
313
+ assert O(1, a, b) + O(a + b, a, b) == O(1, a, b)
314
+
315
+
316
+ def test_issue_4855():
317
+ assert 1/O(1) != O(1)
318
+ assert 1/O(x) != O(1/x)
319
+ assert 1/O(x, (x, oo)) != O(1/x, (x, oo))
320
+
321
+ f = Function('f')
322
+ assert 1/O(f(x)) != O(1/x)
323
+
324
+
325
+ def test_order_conjugate_transpose():
326
+ x = Symbol('x', real=True)
327
+ y = Symbol('y', imaginary=True)
328
+ assert conjugate(Order(x)) == Order(conjugate(x))
329
+ assert conjugate(Order(y)) == Order(conjugate(y))
330
+ assert conjugate(Order(x**2)) == Order(conjugate(x)**2)
331
+ assert conjugate(Order(y**2)) == Order(conjugate(y)**2)
332
+ assert transpose(Order(x)) == Order(transpose(x))
333
+ assert transpose(Order(y)) == Order(transpose(y))
334
+ assert transpose(Order(x**2)) == Order(transpose(x)**2)
335
+ assert transpose(Order(y**2)) == Order(transpose(y)**2)
336
+
337
+
338
+ def test_order_noncommutative():
339
+ A = Symbol('A', commutative=False)
340
+ assert Order(A + A*x, x) == Order(1, x)
341
+ assert (A + A*x)*Order(x) == Order(x)
342
+ assert (A*x)*Order(x) == Order(x**2, x)
343
+ assert expand((1 + Order(x))*A*A*x) == A*A*x + Order(x**2, x)
344
+ assert expand((A*A + Order(x))*x) == A*A*x + Order(x**2, x)
345
+ assert expand((A + Order(x))*A*x) == A*A*x + Order(x**2, x)
346
+
347
+
348
+ def test_issue_6753():
349
+ assert (1 + x**2)**10000*O(x) == O(x)
350
+
351
+
352
+ def test_order_at_infinity():
353
+ assert Order(1 + x, (x, oo)) == Order(x, (x, oo))
354
+ assert Order(3*x, (x, oo)) == Order(x, (x, oo))
355
+ assert Order(x, (x, oo))*3 == Order(x, (x, oo))
356
+ assert -28*Order(x, (x, oo)) == Order(x, (x, oo))
357
+ assert Order(Order(x, (x, oo)), (x, oo)) == Order(x, (x, oo))
358
+ assert Order(Order(x, (x, oo)), (y, oo)) == Order(x, (x, oo), (y, oo))
359
+ assert Order(3, (x, oo)) == Order(1, (x, oo))
360
+ assert Order(x**2 + x + y, (x, oo)) == O(x**2, (x, oo))
361
+ assert Order(x**2 + x + y, (y, oo)) == O(y, (y, oo))
362
+
363
+ assert Order(2*x, (x, oo))*x == Order(x**2, (x, oo))
364
+ assert Order(2*x, (x, oo))/x == Order(1, (x, oo))
365
+ assert Order(2*x, (x, oo))*x*exp(1/x) == Order(x**2*exp(1/x), (x, oo))
366
+ assert Order(2*x, (x, oo))*x*exp(1/x)/log(x)**3 == Order(x**2*exp(1/x)*log(x)**-3, (x, oo))
367
+
368
+ assert Order(x, (x, oo)) + 1/x == 1/x + Order(x, (x, oo)) == Order(x, (x, oo))
369
+ assert Order(x, (x, oo)) + 1 == 1 + Order(x, (x, oo)) == Order(x, (x, oo))
370
+ assert Order(x, (x, oo)) + x == x + Order(x, (x, oo)) == Order(x, (x, oo))
371
+ assert Order(x, (x, oo)) + x**2 == x**2 + Order(x, (x, oo))
372
+ assert Order(1/x, (x, oo)) + 1/x**2 == 1/x**2 + Order(1/x, (x, oo)) == Order(1/x, (x, oo))
373
+ assert Order(x, (x, oo)) + exp(1/x) == exp(1/x) + Order(x, (x, oo))
374
+
375
+ assert Order(x, (x, oo))**2 == Order(x**2, (x, oo))
376
+
377
+ assert Order(x, (x, oo)) + Order(x**2, (x, oo)) == Order(x**2, (x, oo))
378
+ assert Order(x, (x, oo)) + Order(x**-2, (x, oo)) == Order(x, (x, oo))
379
+ assert Order(x, (x, oo)) + Order(1/x, (x, oo)) == Order(x, (x, oo))
380
+
381
+ assert Order(x, (x, oo)) - Order(x, (x, oo)) == Order(x, (x, oo))
382
+ assert Order(x, (x, oo)) + Order(1, (x, oo)) == Order(x, (x, oo))
383
+ assert Order(x, (x, oo)) + Order(x**2, (x, oo)) == Order(x**2, (x, oo))
384
+ assert Order(1/x, (x, oo)) + Order(1, (x, oo)) == Order(1, (x, oo))
385
+ assert Order(x, (x, oo)) + Order(exp(1/x), (x, oo)) == Order(x, (x, oo))
386
+ assert Order(x**3, (x, oo)) + Order(exp(2/x), (x, oo)) == Order(x**3, (x, oo))
387
+ assert Order(x**-3, (x, oo)) + Order(exp(2/x), (x, oo)) == Order(exp(2/x), (x, oo))
388
+
389
+ # issue 7207
390
+ assert Order(exp(x), (x, oo)).expr == Order(2*exp(x), (x, oo)).expr == exp(x)
391
+ assert Order(y**x, (x, oo)).expr == Order(2*y**x, (x, oo)).expr == exp(x*log(y))
392
+
393
+ # issue 19545
394
+ assert Order(1/x - 3/(3*x + 2), (x, oo)).expr == x**(-2)
395
+
396
+ def test_mixing_order_at_zero_and_infinity():
397
+ assert (Order(x, (x, 0)) + Order(x, (x, oo))).is_Add
398
+ assert Order(x, (x, 0)) + Order(x, (x, oo)) == Order(x, (x, oo)) + Order(x, (x, 0))
399
+ assert Order(Order(x, (x, oo))) == Order(x, (x, oo))
400
+
401
+ # not supported (yet)
402
+ raises(NotImplementedError, lambda: Order(x, (x, 0))*Order(x, (x, oo)))
403
+ raises(NotImplementedError, lambda: Order(x, (x, oo))*Order(x, (x, 0)))
404
+ raises(NotImplementedError, lambda: Order(Order(x, (x, oo)), y))
405
+ raises(NotImplementedError, lambda: Order(Order(x), (x, oo)))
406
+
407
+
408
+ def test_order_at_some_point():
409
+ assert Order(x, (x, 1)) == Order(1, (x, 1))
410
+ assert Order(2*x - 2, (x, 1)) == Order(x - 1, (x, 1))
411
+ assert Order(-x + 1, (x, 1)) == Order(x - 1, (x, 1))
412
+ assert Order(x - 1, (x, 1))**2 == Order((x - 1)**2, (x, 1))
413
+ assert Order(x - 2, (x, 2)) - O(x - 2, (x, 2)) == Order(x - 2, (x, 2))
414
+
415
+
416
+ def test_order_subs_limits():
417
+ # issue 3333
418
+ assert (1 + Order(x)).subs(x, 1/x) == 1 + Order(1/x, (x, oo))
419
+ assert (1 + Order(x)).limit(x, 0) == 1
420
+ # issue 5769
421
+ assert ((x + Order(x**2))/x).limit(x, 0) == 1
422
+
423
+ assert Order(x**2).subs(x, y - 1) == Order((y - 1)**2, (y, 1))
424
+ assert Order(10*x**2, (x, 2)).subs(x, y - 1) == Order(1, (y, 3))
425
+
426
+ #issue 19120
427
+ assert O(x).subs(x, O(x)) == O(x)
428
+ assert O(x**2).subs(x, x + O(x)) == O(x**2)
429
+ assert O(x, (x, oo)).subs(x, O(x, (x, oo))) == O(x, (x, oo))
430
+ assert O(x**2, (x, oo)).subs(x, x + O(x, (x, oo))) == O(x**2, (x, oo))
431
+ assert (x + O(x**2)).subs(x, x + O(x**2)) == x + O(x**2)
432
+ assert (x**2 + O(x**2) + 1/x**2).subs(x, x + O(x**2)) == (x + O(x**2))**(-2) + O(x**2)
433
+ assert (x**2 + O(x**2) + 1).subs(x, x + O(x**2)) == 1 + O(x**2)
434
+ assert O(x, (x, oo)).subs(x, x + O(x**2, (x, oo))) == O(x**2, (x, oo))
435
+ assert sin(x).series(n=8).subs(x,sin(x).series(n=8)).expand() == x - x**3/3 + x**5/10 - 8*x**7/315 + O(x**8)
436
+ assert cos(x).series(n=8).subs(x,sin(x).series(n=8)).expand() == 1 - x**2/2 + 5*x**4/24 - 37*x**6/720 + O(x**8)
437
+ assert O(x).subs(x, O(1/x, (x, oo))) == O(1/x, (x, oo))
438
+
439
+ @XFAIL
440
+ def test_order_failing_due_to_solveset():
441
+ assert O(x**3).subs(x, exp(-x**2)) == O(exp(-3*x**2), (x, -oo))
442
+ raises(NotImplementedError, lambda: O(x).subs(x, O(1/x))) # mixing of order at different points
443
+
444
+
445
+ def test_issue_9351():
446
+ assert exp(x).series(x, 10, 1) == exp(10) + Order(x - 10, (x, 10))
447
+
448
+
449
+ def test_issue_9192():
450
+ assert O(1)*O(1) == O(1)
451
+ assert O(1)**O(1) == O(1)
452
+
453
+
454
+ def test_issue_9910():
455
+ assert O(x*log(x) + sin(x), (x, oo)) == O(x*log(x), (x, oo))
456
+
457
+
458
+ def test_performance_of_adding_order():
459
+ l = [x**i for i in range(1000)]
460
+ l.append(O(x**1001))
461
+ assert Add(*l).subs(x,1) == O(1)
462
+
463
+ def test_issue_14622():
464
+ assert (x**(-4) + x**(-3) + x**(-1) + O(x**(-6), (x, oo))).as_numer_denom() == (
465
+ x**4 + x**5 + x**7 + O(x**2, (x, oo)), x**8)
466
+ assert (x**3 + O(x**2, (x, oo))).is_Add
467
+ assert O(x**2, (x, oo)).contains(x**3) is False
468
+ assert O(x, (x, oo)).contains(O(x, (x, 0))) is None
469
+ assert O(x, (x, 0)).contains(O(x, (x, oo))) is None
470
+ raises(NotImplementedError, lambda: O(x**3).contains(x**w))
471
+
472
+
473
+ def test_issue_15539():
474
+ assert O(1/x**2 + 1/x**4, (x, -oo)) == O(1/x**2, (x, -oo))
475
+ assert O(1/x**4 + exp(x), (x, -oo)) == O(1/x**4, (x, -oo))
476
+ assert O(1/x**4 + exp(-x), (x, -oo)) == O(exp(-x), (x, -oo))
477
+ assert O(1/x, (x, oo)).subs(x, -x) == O(-1/x, (x, -oo))
478
+
479
+ def test_issue_18606():
480
+ assert unchanged(Order, 0)
481
+
482
+
483
+ def test_issue_22165():
484
+ assert O(log(x)).contains(2)
485
+
486
+
487
+ def test_issue_23231():
488
+ # This test checks Order for expressions having
489
+ # arguments containing variables in exponents/powers.
490
+ assert O(x**x + 2**x, (x, oo)) == O(exp(x*log(x)), (x, oo))
491
+ assert O(x**x + x**2, (x, oo)) == O(exp(x*log(x)), (x, oo))
492
+ assert O(x**x + 1/x**2, (x, oo)) == O(exp(x*log(x)), (x, oo))
493
+ assert O(2**x + 3**x , (x, oo)) == O(exp(x*log(3)), (x, oo))
494
+
495
+
496
+ def test_issue_9917():
497
+ assert O(x*sin(x) + 1, (x, oo)) == O(x, (x, oo))
498
+
499
+
500
+ def test_issue_22836():
501
+ assert O(2**x + factorial(x), (x, oo)) == O(factorial(x), (x, oo))
502
+ assert O(2**x + factorial(x) + x**x, (x, oo)) == O(exp(x*log(x)), (x, oo))
503
+ assert O(x + factorial(x), (x, oo)) == O(factorial(x), (x, oo))
.venv/lib/python3.13/site-packages/sympy/series/tests/test_residues.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.function import Function
2
+ from sympy.core.numbers import (I, Rational, pi)
3
+ from sympy.core.singleton import S
4
+ from sympy.core.symbol import Symbol
5
+ from sympy.functions.combinatorial.factorials import factorial
6
+ from sympy.functions.elementary.exponential import (exp, log)
7
+ from sympy.functions.elementary.hyperbolic import tanh
8
+ from sympy.functions.elementary.miscellaneous import sqrt
9
+ from sympy.functions.elementary.trigonometric import (cot, sin, tan)
10
+ from sympy.series.residues import residue
11
+ from sympy.testing.pytest import XFAIL, raises
12
+ from sympy.abc import x, z, a, s, k
13
+
14
+
15
+ def test_basic1():
16
+ assert residue(1/x, x, 0) == 1
17
+ assert residue(-2/x, x, 0) == -2
18
+ assert residue(81/x, x, 0) == 81
19
+ assert residue(1/x**2, x, 0) == 0
20
+ assert residue(0, x, 0) == 0
21
+ assert residue(5, x, 0) == 0
22
+ assert residue(x, x, 0) == 0
23
+ assert residue(x**2, x, 0) == 0
24
+
25
+
26
+ def test_basic2():
27
+ assert residue(1/x, x, 1) == 0
28
+ assert residue(-2/x, x, 1) == 0
29
+ assert residue(81/x, x, -1) == 0
30
+ assert residue(1/x**2, x, 1) == 0
31
+ assert residue(0, x, 1) == 0
32
+ assert residue(5, x, 1) == 0
33
+ assert residue(x, x, 1) == 0
34
+ assert residue(x**2, x, 5) == 0
35
+
36
+
37
+ def test_f():
38
+ f = Function("f")
39
+ assert residue(f(x)/x**5, x, 0) == f(x).diff(x, 4).subs(x, 0)/24
40
+
41
+
42
+ def test_functions():
43
+ assert residue(1/sin(x), x, 0) == 1
44
+ assert residue(2/sin(x), x, 0) == 2
45
+ assert residue(1/sin(x)**2, x, 0) == 0
46
+ assert residue(1/sin(x)**5, x, 0) == Rational(3, 8)
47
+
48
+
49
+ def test_expressions():
50
+ assert residue(1/(x + 1), x, 0) == 0
51
+ assert residue(1/(x + 1), x, -1) == 1
52
+ assert residue(1/(x**2 + 1), x, -1) == 0
53
+ assert residue(1/(x**2 + 1), x, I) == -I/2
54
+ assert residue(1/(x**2 + 1), x, -I) == I/2
55
+ assert residue(1/(x**4 + 1), x, 0) == 0
56
+ assert residue(1/(x**4 + 1), x, exp(I*pi/4)).equals(-(Rational(1, 4) + I/4)/sqrt(2))
57
+ assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/4/a**3
58
+
59
+
60
+ @XFAIL
61
+ def test_expressions_failing():
62
+ n = Symbol('n', integer=True, positive=True)
63
+ assert residue(exp(z)/(z - pi*I/4*a)**n, z, I*pi*a) == \
64
+ exp(I*pi*a/4)/factorial(n - 1)
65
+
66
+
67
+ def test_NotImplemented():
68
+ raises(NotImplementedError, lambda: residue(exp(1/z), z, 0))
69
+
70
+
71
+ def test_bug():
72
+ assert residue(2**(z)*(s + z)*(1 - s - z)/z**2, z, 0) == \
73
+ 1 + s*log(2) - s**2*log(2) - 2*s
74
+
75
+
76
+ def test_issue_5654():
77
+ assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/(4*a**3)
78
+ assert residue(1/s*1/(z - exp(s)), s, 0) == 1/(z - 1)
79
+ assert residue((1 + k)/s*1/(z - exp(s)), s, 0) == k/(z - 1) + 1/(z - 1)
80
+
81
+
82
+ def test_issue_6499():
83
+ assert residue(1/(exp(z) - 1), z, 0) == 1
84
+
85
+
86
+ def test_issue_14037():
87
+ assert residue(sin(x**50)/x**51, x, 0) == 1
88
+
89
+
90
+ def test_issue_21176():
91
+ f = x**2*cot(pi*x)/(x**4 + 1)
92
+ assert residue(f, x, -sqrt(2)/2 - sqrt(2)*I/2).cancel().together(deep=True)\
93
+ == sqrt(2)*(1 - I)/(8*tan(sqrt(2)*pi*(1 + I)/2))
94
+
95
+
96
+ def test_issue_21177():
97
+ r = -sqrt(3)*tanh(sqrt(3)*pi/2)/3
98
+ a = residue(cot(pi*x)/((x - 1)*(x - 2) + 1), x, S(3)/2 - sqrt(3)*I/2)
99
+ b = residue(cot(pi*x)/(x**2 - 3*x + 3), x, S(3)/2 - sqrt(3)*I/2)
100
+ assert a == r
101
+ assert (b - a).cancel() == 0
.venv/lib/python3.13/site-packages/sympy/series/tests/test_sequences.py ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.containers import Tuple
2
+ from sympy.core.function import Function
3
+ from sympy.core.numbers import oo, Rational
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import symbols, Symbol
6
+ from sympy.functions.combinatorial.numbers import tribonacci, fibonacci
7
+ from sympy.functions.elementary.exponential import exp
8
+ from sympy.functions.elementary.miscellaneous import sqrt
9
+ from sympy.functions.elementary.trigonometric import cos, sin
10
+ from sympy.series import EmptySequence
11
+ from sympy.series.sequences import (SeqMul, SeqAdd, SeqPer, SeqFormula,
12
+ sequence)
13
+ from sympy.sets.sets import Interval
14
+ from sympy.tensor.indexed import Indexed, Idx
15
+ from sympy.series.sequences import SeqExpr, SeqExprOp, RecursiveSeq
16
+ from sympy.testing.pytest import raises, slow
17
+
18
+ x, y, z = symbols('x y z')
19
+ n, m = symbols('n m')
20
+
21
+
22
+ def test_EmptySequence():
23
+ assert S.EmptySequence is EmptySequence
24
+
25
+ assert S.EmptySequence.interval is S.EmptySet
26
+ assert S.EmptySequence.length is S.Zero
27
+
28
+ assert list(S.EmptySequence) == []
29
+
30
+
31
+ def test_SeqExpr():
32
+ #SeqExpr is a baseclass and does not take care of
33
+ #ensuring all arguments are Basics hence the use of
34
+ #Tuple(...) here.
35
+ s = SeqExpr(Tuple(1, n, y), Tuple(x, 0, 10))
36
+
37
+ assert isinstance(s, SeqExpr)
38
+ assert s.gen == (1, n, y)
39
+ assert s.interval == Interval(0, 10)
40
+ assert s.start == 0
41
+ assert s.stop == 10
42
+ assert s.length == 11
43
+ assert s.variables == (x,)
44
+
45
+ assert SeqExpr(Tuple(1, 2, 3), Tuple(x, 0, oo)).length is oo
46
+
47
+
48
+ def test_SeqPer():
49
+ s = SeqPer((1, n, 3), (x, 0, 5))
50
+
51
+ assert isinstance(s, SeqPer)
52
+ assert s.periodical == Tuple(1, n, 3)
53
+ assert s.period == 3
54
+ assert s.coeff(3) == 1
55
+ assert s.free_symbols == {n}
56
+
57
+ assert list(s) == [1, n, 3, 1, n, 3]
58
+ assert s[:] == [1, n, 3, 1, n, 3]
59
+ assert SeqPer((1, n, 3), (x, -oo, 0))[0:6] == [1, n, 3, 1, n, 3]
60
+
61
+ raises(ValueError, lambda: SeqPer((1, 2, 3), (0, 1, 2)))
62
+ raises(ValueError, lambda: SeqPer((1, 2, 3), (x, -oo, oo)))
63
+ raises(ValueError, lambda: SeqPer(n**2, (0, oo)))
64
+
65
+ assert SeqPer((n, n**2, n**3), (m, 0, oo))[:6] == \
66
+ [n, n**2, n**3, n, n**2, n**3]
67
+ assert SeqPer((n, n**2, n**3), (n, 0, oo))[:6] == [0, 1, 8, 3, 16, 125]
68
+ assert SeqPer((n, m), (n, 0, oo))[:6] == [0, m, 2, m, 4, m]
69
+
70
+
71
+ def test_SeqFormula():
72
+ s = SeqFormula(n**2, (n, 0, 5))
73
+
74
+ assert isinstance(s, SeqFormula)
75
+ assert s.formula == n**2
76
+ assert s.coeff(3) == 9
77
+
78
+ assert list(s) == [i**2 for i in range(6)]
79
+ assert s[:] == [i**2 for i in range(6)]
80
+ assert SeqFormula(n**2, (n, -oo, 0))[0:6] == [i**2 for i in range(6)]
81
+
82
+ assert SeqFormula(n**2, (0, oo)) == SeqFormula(n**2, (n, 0, oo))
83
+
84
+ assert SeqFormula(n**2, (0, m)).subs(m, x) == SeqFormula(n**2, (0, x))
85
+ assert SeqFormula(m*n**2, (n, 0, oo)).subs(m, x) == \
86
+ SeqFormula(x*n**2, (n, 0, oo))
87
+
88
+ raises(ValueError, lambda: SeqFormula(n**2, (0, 1, 2)))
89
+ raises(ValueError, lambda: SeqFormula(n**2, (n, -oo, oo)))
90
+ raises(ValueError, lambda: SeqFormula(m*n**2, (0, oo)))
91
+
92
+ seq = SeqFormula(x*(y**2 + z), (z, 1, 100))
93
+ assert seq.expand() == SeqFormula(x*y**2 + x*z, (z, 1, 100))
94
+ seq = SeqFormula(sin(x*(y**2 + z)),(z, 1, 100))
95
+ assert seq.expand(trig=True) == SeqFormula(sin(x*y**2)*cos(x*z) + sin(x*z)*cos(x*y**2), (z, 1, 100))
96
+ assert seq.expand() == SeqFormula(sin(x*y**2 + x*z), (z, 1, 100))
97
+ assert seq.expand(trig=False) == SeqFormula(sin(x*y**2 + x*z), (z, 1, 100))
98
+ seq = SeqFormula(exp(x*(y**2 + z)), (z, 1, 100))
99
+ assert seq.expand() == SeqFormula(exp(x*y**2)*exp(x*z), (z, 1, 100))
100
+ assert seq.expand(power_exp=False) == SeqFormula(exp(x*y**2 + x*z), (z, 1, 100))
101
+ assert seq.expand(mul=False, power_exp=False) == SeqFormula(exp(x*(y**2 + z)), (z, 1, 100))
102
+
103
+ def test_sequence():
104
+ form = SeqFormula(n**2, (n, 0, 5))
105
+ per = SeqPer((1, 2, 3), (n, 0, 5))
106
+ inter = SeqFormula(n**2)
107
+
108
+ assert sequence(n**2, (n, 0, 5)) == form
109
+ assert sequence((1, 2, 3), (n, 0, 5)) == per
110
+ assert sequence(n**2) == inter
111
+
112
+
113
+ def test_SeqExprOp():
114
+ form = SeqFormula(n**2, (n, 0, 10))
115
+ per = SeqPer((1, 2, 3), (m, 5, 10))
116
+
117
+ s = SeqExprOp(form, per)
118
+ assert s.gen == (n**2, (1, 2, 3))
119
+ assert s.interval == Interval(5, 10)
120
+ assert s.start == 5
121
+ assert s.stop == 10
122
+ assert s.length == 6
123
+ assert s.variables == (n, m)
124
+
125
+
126
+ def test_SeqAdd():
127
+ per = SeqPer((1, 2, 3), (n, 0, oo))
128
+ form = SeqFormula(n**2)
129
+
130
+ per_bou = SeqPer((1, 2), (n, 1, 5))
131
+ form_bou = SeqFormula(n**2, (6, 10))
132
+ form_bou2 = SeqFormula(n**2, (1, 5))
133
+
134
+ assert SeqAdd() == S.EmptySequence
135
+ assert SeqAdd(S.EmptySequence) == S.EmptySequence
136
+ assert SeqAdd(per) == per
137
+ assert SeqAdd(per, S.EmptySequence) == per
138
+ assert SeqAdd(per_bou, form_bou) == S.EmptySequence
139
+
140
+ s = SeqAdd(per_bou, form_bou2, evaluate=False)
141
+ assert s.args == (form_bou2, per_bou)
142
+ assert s[:] == [2, 6, 10, 18, 26]
143
+ assert list(s) == [2, 6, 10, 18, 26]
144
+
145
+ assert isinstance(SeqAdd(per, per_bou, evaluate=False), SeqAdd)
146
+
147
+ s1 = SeqAdd(per, per_bou)
148
+ assert isinstance(s1, SeqPer)
149
+ assert s1 == SeqPer((2, 4, 4, 3, 3, 5), (n, 1, 5))
150
+ s2 = SeqAdd(form, form_bou)
151
+ assert isinstance(s2, SeqFormula)
152
+ assert s2 == SeqFormula(2*n**2, (6, 10))
153
+
154
+ assert SeqAdd(form, form_bou, per) == \
155
+ SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
156
+ assert SeqAdd(form, SeqAdd(form_bou, per)) == \
157
+ SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
158
+ assert SeqAdd(per, SeqAdd(form, form_bou), evaluate=False) == \
159
+ SeqAdd(per, SeqFormula(2*n**2, (6, 10)))
160
+
161
+ assert SeqAdd(SeqPer((1, 2), (n, 0, oo)), SeqPer((1, 2), (m, 0, oo))) == \
162
+ SeqPer((2, 4), (n, 0, oo))
163
+
164
+
165
+ def test_SeqMul():
166
+ per = SeqPer((1, 2, 3), (n, 0, oo))
167
+ form = SeqFormula(n**2)
168
+
169
+ per_bou = SeqPer((1, 2), (n, 1, 5))
170
+ form_bou = SeqFormula(n**2, (n, 6, 10))
171
+ form_bou2 = SeqFormula(n**2, (1, 5))
172
+
173
+ assert SeqMul() == S.EmptySequence
174
+ assert SeqMul(S.EmptySequence) == S.EmptySequence
175
+ assert SeqMul(per) == per
176
+ assert SeqMul(per, S.EmptySequence) == S.EmptySequence
177
+ assert SeqMul(per_bou, form_bou) == S.EmptySequence
178
+
179
+ s = SeqMul(per_bou, form_bou2, evaluate=False)
180
+ assert s.args == (form_bou2, per_bou)
181
+ assert s[:] == [1, 8, 9, 32, 25]
182
+ assert list(s) == [1, 8, 9, 32, 25]
183
+
184
+ assert isinstance(SeqMul(per, per_bou, evaluate=False), SeqMul)
185
+
186
+ s1 = SeqMul(per, per_bou)
187
+ assert isinstance(s1, SeqPer)
188
+ assert s1 == SeqPer((1, 4, 3, 2, 2, 6), (n, 1, 5))
189
+ s2 = SeqMul(form, form_bou)
190
+ assert isinstance(s2, SeqFormula)
191
+ assert s2 == SeqFormula(n**4, (6, 10))
192
+
193
+ assert SeqMul(form, form_bou, per) == \
194
+ SeqMul(per, SeqFormula(n**4, (6, 10)))
195
+ assert SeqMul(form, SeqMul(form_bou, per)) == \
196
+ SeqMul(per, SeqFormula(n**4, (6, 10)))
197
+ assert SeqMul(per, SeqMul(form, form_bou2,
198
+ evaluate=False), evaluate=False) == \
199
+ SeqMul(form, per, form_bou2, evaluate=False)
200
+
201
+ assert SeqMul(SeqPer((1, 2), (n, 0, oo)), SeqPer((1, 2), (n, 0, oo))) == \
202
+ SeqPer((1, 4), (n, 0, oo))
203
+
204
+
205
+ def test_add():
206
+ per = SeqPer((1, 2), (n, 0, oo))
207
+ form = SeqFormula(n**2)
208
+
209
+ assert per + (SeqPer((2, 3))) == SeqPer((3, 5), (n, 0, oo))
210
+ assert form + SeqFormula(n**3) == SeqFormula(n**2 + n**3)
211
+
212
+ assert per + form == SeqAdd(per, form)
213
+
214
+ raises(TypeError, lambda: per + n)
215
+ raises(TypeError, lambda: n + per)
216
+
217
+
218
+ def test_sub():
219
+ per = SeqPer((1, 2), (n, 0, oo))
220
+ form = SeqFormula(n**2)
221
+
222
+ assert per - (SeqPer((2, 3))) == SeqPer((-1, -1), (n, 0, oo))
223
+ assert form - (SeqFormula(n**3)) == SeqFormula(n**2 - n**3)
224
+
225
+ assert per - form == SeqAdd(per, -form)
226
+
227
+ raises(TypeError, lambda: per - n)
228
+ raises(TypeError, lambda: n - per)
229
+
230
+
231
+ def test_mul__coeff_mul():
232
+ assert SeqPer((1, 2), (n, 0, oo)).coeff_mul(2) == SeqPer((2, 4), (n, 0, oo))
233
+ assert SeqFormula(n**2).coeff_mul(2) == SeqFormula(2*n**2)
234
+ assert S.EmptySequence.coeff_mul(100) == S.EmptySequence
235
+
236
+ assert SeqPer((1, 2), (n, 0, oo)) * (SeqPer((2, 3))) == \
237
+ SeqPer((2, 6), (n, 0, oo))
238
+ assert SeqFormula(n**2) * SeqFormula(n**3) == SeqFormula(n**5)
239
+
240
+ assert S.EmptySequence * SeqFormula(n**2) == S.EmptySequence
241
+ assert SeqFormula(n**2) * S.EmptySequence == S.EmptySequence
242
+
243
+ raises(TypeError, lambda: sequence(n**2) * n)
244
+ raises(TypeError, lambda: n * sequence(n**2))
245
+
246
+
247
+ def test_neg():
248
+ assert -SeqPer((1, -2), (n, 0, oo)) == SeqPer((-1, 2), (n, 0, oo))
249
+ assert -SeqFormula(n**2) == SeqFormula(-n**2)
250
+
251
+
252
+ def test_operations():
253
+ per = SeqPer((1, 2), (n, 0, oo))
254
+ per2 = SeqPer((2, 4), (n, 0, oo))
255
+ form = SeqFormula(n**2)
256
+ form2 = SeqFormula(n**3)
257
+
258
+ assert per + form + form2 == SeqAdd(per, form, form2)
259
+ assert per + form - form2 == SeqAdd(per, form, -form2)
260
+ assert per + form - S.EmptySequence == SeqAdd(per, form)
261
+ assert per + per2 + form == SeqAdd(SeqPer((3, 6), (n, 0, oo)), form)
262
+ assert S.EmptySequence - per == -per
263
+ assert form + form == SeqFormula(2*n**2)
264
+
265
+ assert per * form * form2 == SeqMul(per, form, form2)
266
+ assert form * form == SeqFormula(n**4)
267
+ assert form * -form == SeqFormula(-n**4)
268
+
269
+ assert form * (per + form2) == SeqMul(form, SeqAdd(per, form2))
270
+ assert form * (per + per) == SeqMul(form, per2)
271
+
272
+ assert form.coeff_mul(m) == SeqFormula(m*n**2, (n, 0, oo))
273
+ assert per.coeff_mul(m) == SeqPer((m, 2*m), (n, 0, oo))
274
+
275
+
276
+ def test_Idx_limits():
277
+ i = symbols('i', cls=Idx)
278
+ r = Indexed('r', i)
279
+
280
+ assert SeqFormula(r, (i, 0, 5))[:] == [r.subs(i, j) for j in range(6)]
281
+ assert SeqPer((1, 2), (i, 0, 5))[:] == [1, 2, 1, 2, 1, 2]
282
+
283
+
284
+ @slow
285
+ def test_find_linear_recurrence():
286
+ assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \
287
+ (n, 0, 10)).find_linear_recurrence(11) == [1, 1]
288
+ assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \
289
+ 1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11]
290
+ assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \
291
+ == [4, -6, 4, -1]
292
+ assert sequence(x**n, (n,0,20)).find_linear_recurrence(21) == [x]
293
+ assert sequence((1,2,3)).find_linear_recurrence(10, 5) == [0, 0, 1]
294
+ assert sequence(((1 + sqrt(5))/2)**n + \
295
+ (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1]
296
+ assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \
297
+ (n,0,oo)).find_linear_recurrence(10) == [1, 1]
298
+ assert sequence((1,2,3,4,6),(n, 0, 4)).find_linear_recurrence(5) == []
299
+ assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
300
+ == ([], None)
301
+ assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
302
+ == ([Rational(19, 2), -20, Rational(27, 2)], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2))
303
+ assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \
304
+ == ([1, 1], -x/(x**2 + x - 1))
305
+ assert sequence(tribonacci(n)).find_linear_recurrence(30,gfvar=x) \
306
+ == ([1, 1, 1], -x/(x**3 + x**2 + x - 1))
307
+
308
+ def test_RecursiveSeq():
309
+ y = Function('y')
310
+ n = Symbol('n')
311
+ fib = RecursiveSeq(y(n - 1) + y(n - 2), y(n), n, [0, 1])
312
+ assert fib.coeff(3) == 2
.venv/lib/python3.13/site-packages/sympy/series/tests/test_series.py ADDED
@@ -0,0 +1,421 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.evalf import N
2
+ from sympy.core.function import (Derivative, Function, PoleError, Subs)
3
+ from sympy.core.numbers import (E, Float, Rational, oo, pi, I)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, symbols)
6
+ from sympy.functions.elementary.exponential import (LambertW, exp, log)
7
+ from sympy.functions.elementary.miscellaneous import sqrt
8
+ from sympy.functions.elementary.trigonometric import (atan, cos, sin)
9
+ from sympy.functions.special.gamma_functions import gamma
10
+ from sympy.integrals.integrals import Integral, integrate
11
+ from sympy.series.order import O
12
+ from sympy.series.series import series
13
+ from sympy.abc import x, y, n, k
14
+ from sympy.testing.pytest import raises
15
+ from sympy.core import EulerGamma
16
+
17
+
18
+ def test_sin():
19
+ e1 = sin(x).series(x, 0)
20
+ e2 = series(sin(x), x, 0)
21
+ assert e1 == e2
22
+
23
+
24
+ def test_cos():
25
+ e1 = cos(x).series(x, 0)
26
+ e2 = series(cos(x), x, 0)
27
+ assert e1 == e2
28
+
29
+
30
+ def test_exp():
31
+ e1 = exp(x).series(x, 0)
32
+ e2 = series(exp(x), x, 0)
33
+ assert e1 == e2
34
+
35
+
36
+ def test_exp2():
37
+ e1 = exp(cos(x)).series(x, 0)
38
+ e2 = series(exp(cos(x)), x, 0)
39
+ assert e1 == e2
40
+
41
+
42
+ def test_issue_5223():
43
+ assert series(1, x) == 1
44
+ assert next(S.Zero.lseries(x)) == 0
45
+ assert cos(x).series() == cos(x).series(x)
46
+ raises(ValueError, lambda: cos(x + y).series())
47
+ raises(ValueError, lambda: x.series(dir=""))
48
+
49
+ assert (cos(x).series(x, 1) -
50
+ cos(x + 1).series(x).subs(x, x - 1)).removeO() == 0
51
+ e = cos(x).series(x, 1, n=None)
52
+ assert [next(e) for i in range(2)] == [cos(1), -((x - 1)*sin(1))]
53
+ e = cos(x).series(x, 1, n=None, dir='-')
54
+ assert [next(e) for i in range(2)] == [cos(1), (1 - x)*sin(1)]
55
+ # the following test is exact so no need for x -> x - 1 replacement
56
+ assert abs(x).series(x, 1, dir='-') == x
57
+ assert exp(x).series(x, 1, dir='-', n=3).removeO() == \
58
+ E - E*(-x + 1) + E*(-x + 1)**2/2
59
+
60
+ D = Derivative
61
+ assert D(x**2 + x**3*y**2, x, 2, y, 1).series(x).doit() == 12*x*y
62
+ assert next(D(cos(x), x).lseries()) == D(1, x)
63
+ assert D(
64
+ exp(x), x).series(n=3) == D(1, x) + D(x, x) + D(x**2/2, x) + D(x**3/6, x) + O(x**3)
65
+
66
+ assert Integral(x, (x, 1, 3), (y, 1, x)).series(x) == -4 + 4*x
67
+
68
+ assert (1 + x + O(x**2)).getn() == 2
69
+ assert (1 + x).getn() is None
70
+
71
+ raises(PoleError, lambda: ((1/sin(x))**oo).series())
72
+ logx = Symbol('logx')
73
+ assert ((sin(x))**y).nseries(x, n=1, logx=logx) == \
74
+ exp(y*logx) + O(x*exp(y*logx), x)
75
+
76
+ assert sin(1/x).series(x, oo, n=5) == 1/x - 1/(6*x**3) + O(x**(-5), (x, oo))
77
+ assert abs(x).series(x, oo, n=5, dir='+') == x
78
+ assert abs(x).series(x, -oo, n=5, dir='-') == -x
79
+ assert abs(-x).series(x, oo, n=5, dir='+') == x
80
+ assert abs(-x).series(x, -oo, n=5, dir='-') == -x
81
+
82
+ assert exp(x*log(x)).series(n=3) == \
83
+ 1 + x*log(x) + x**2*log(x)**2/2 + O(x**3*log(x)**3)
84
+ # XXX is this right? If not, fix "ngot > n" handling in expr.
85
+ p = Symbol('p', positive=True)
86
+ assert exp(sqrt(p)**3*log(p)).series(n=3) == \
87
+ 1 + p**S('3/2')*log(p) + O(p**3*log(p)**3)
88
+
89
+ assert exp(sin(x)*log(x)).series(n=2) == 1 + x*log(x) + O(x**2*log(x)**2)
90
+
91
+
92
+ def test_issue_6350():
93
+ expr = integrate(exp(k*(y**3 - 3*y)), (y, 0, oo), conds='none')
94
+ assert expr.series(k, 0, 3) == -(-1)**(S(2)/3)*sqrt(3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(6*pi*k**(S(1)/3)) - \
95
+ sqrt(3)*k*gamma(-S(2)/3)*gamma(-S(1)/3)/(6*pi) - \
96
+ (-1)**(S(1)/3)*sqrt(3)*k**(S(1)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(6*pi) - \
97
+ (-1)**(S(2)/3)*sqrt(3)*k**(S(5)/3)*gamma(S(1)/3)**2*gamma(S(2)/3)/(4*pi) - \
98
+ (-1)**(S(1)/3)*sqrt(3)*k**(S(7)/3)*gamma(-S(1)/3)*gamma(S(1)/3)*gamma(S(2)/3)/(8*pi) + O(k**3)
99
+
100
+
101
+ def test_issue_11313():
102
+ assert Integral(cos(x), x).series(x) == sin(x).series(x)
103
+ assert Derivative(sin(x), x).series(x, n=3).doit() == cos(x).series(x, n=3)
104
+
105
+ assert Derivative(x**3, x).as_leading_term(x) == 3*x**2
106
+ assert Derivative(x**3, y).as_leading_term(x) == 0
107
+ assert Derivative(sin(x), x).as_leading_term(x) == 1
108
+ assert Derivative(cos(x), x).as_leading_term(x) == -x
109
+
110
+ # This result is equivalent to zero, zero is not return because
111
+ # `Expr.series` doesn't currently detect an `x` in its `free_symbol`s.
112
+ assert Derivative(1, x).as_leading_term(x) == Derivative(1, x)
113
+
114
+ assert Derivative(exp(x), x).series(x).doit() == exp(x).series(x)
115
+ assert 1 + Integral(exp(x), x).series(x) == exp(x).series(x)
116
+
117
+ assert Derivative(log(x), x).series(x).doit() == (1/x).series(x)
118
+ assert Integral(log(x), x).series(x) == Integral(log(x), x).doit().series(x).removeO()
119
+
120
+
121
+ def test_series_of_Subs():
122
+ from sympy.abc import z
123
+
124
+ subs1 = Subs(sin(x), x, y)
125
+ subs2 = Subs(sin(x) * cos(z), x, y)
126
+ subs3 = Subs(sin(x * z), (x, z), (y, x))
127
+
128
+ assert subs1.series(x) == subs1
129
+ subs1_series = (Subs(x, x, y) + Subs(-x**3/6, x, y) +
130
+ Subs(x**5/120, x, y) + O(y**6))
131
+ assert subs1.series() == subs1_series
132
+ assert subs1.series(y) == subs1_series
133
+ assert subs1.series(z) == subs1
134
+ assert subs2.series(z) == (Subs(z**4*sin(x)/24, x, y) +
135
+ Subs(-z**2*sin(x)/2, x, y) + Subs(sin(x), x, y) + O(z**6))
136
+ assert subs3.series(x).doit() == subs3.doit().series(x)
137
+ assert subs3.series(z).doit() == sin(x*y)
138
+
139
+ raises(ValueError, lambda: Subs(x + 2*y, y, z).series())
140
+ assert Subs(x + y, y, z).series(x).doit() == x + z
141
+
142
+
143
+ def test_issue_3978():
144
+ f = Function('f')
145
+ assert f(x).series(x, 0, 3, dir='-') == \
146
+ f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
147
+ x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
148
+ assert f(x).series(x, 0, 3) == \
149
+ f(0) + x*Subs(Derivative(f(x), x), x, 0) + \
150
+ x**2*Subs(Derivative(f(x), x, x), x, 0)/2 + O(x**3)
151
+ assert f(x**2).series(x, 0, 3) == \
152
+ f(0) + x**2*Subs(Derivative(f(x), x), x, 0) + O(x**3)
153
+ assert f(x**2+1).series(x, 0, 3) == \
154
+ f(1) + x**2*Subs(Derivative(f(x), x), x, 1) + O(x**3)
155
+
156
+ class TestF(Function):
157
+ pass
158
+
159
+ assert TestF(x).series(x, 0, 3) == TestF(0) + \
160
+ x*Subs(Derivative(TestF(x), x), x, 0) + \
161
+ x**2*Subs(Derivative(TestF(x), x, x), x, 0)/2 + O(x**3)
162
+
163
+ from sympy.series.acceleration import richardson, shanks
164
+ from sympy.concrete.summations import Sum
165
+ from sympy.core.numbers import Integer
166
+
167
+
168
+ def test_acceleration():
169
+ e = (1 + 1/n)**n
170
+ assert round(richardson(e, n, 10, 20).evalf(), 10) == round(E.evalf(), 10)
171
+
172
+ A = Sum(Integer(-1)**(k + 1) / k, (k, 1, n))
173
+ assert round(shanks(A, n, 25).evalf(), 4) == round(log(2).evalf(), 4)
174
+ assert round(shanks(A, n, 25, 5).evalf(), 10) == round(log(2).evalf(), 10)
175
+
176
+
177
+ def test_issue_5852():
178
+ assert series(1/cos(x/log(x)), x, 0) == 1 + x**2/(2*log(x)**2) + \
179
+ 5*x**4/(24*log(x)**4) + O(x**6)
180
+
181
+
182
+ def test_issue_4583():
183
+ assert cos(1 + x + x**2).series(x, 0, 5) == cos(1) - x*sin(1) + \
184
+ x**2*(-sin(1) - cos(1)/2) + x**3*(-cos(1) + sin(1)/6) + \
185
+ x**4*(-11*cos(1)/24 + sin(1)/2) + O(x**5)
186
+
187
+
188
+ def test_issue_6318():
189
+ eq = (1/x)**Rational(2, 3)
190
+ assert (eq + 1).as_leading_term(x) == eq
191
+
192
+
193
+ def test_x_is_base_detection():
194
+ eq = (x**2)**Rational(2, 3)
195
+ assert eq.series() == x**Rational(4, 3)
196
+
197
+
198
+ def test_issue_7203():
199
+ assert series(cos(x), x, pi, 3) == \
200
+ -1 + (x - pi)**2/2 + O((x - pi)**3, (x, pi))
201
+
202
+
203
+ def test_exp_product_positive_factors():
204
+ a, b = symbols('a, b', positive=True)
205
+ x = a * b
206
+ assert series(exp(x), x, n=8) == 1 + a*b + a**2*b**2/2 + \
207
+ a**3*b**3/6 + a**4*b**4/24 + a**5*b**5/120 + a**6*b**6/720 + \
208
+ a**7*b**7/5040 + O(a**8*b**8, a, b)
209
+
210
+
211
+ def test_issue_8805():
212
+ assert series(1, n=8) == 1
213
+
214
+
215
+ def test_issue_9173():
216
+ p0,p1,p2,p3,b0,b1,b2=symbols('p0 p1 p2 p3 b0 b1 b2')
217
+ Q=(p0+(p1+(p2+p3/y)/y)/y)/(1+((p3/(b0*y)+(b0*p2-b1*p3)/b0**2)/y+\
218
+ (b0**2*p1-b0*b1*p2-p3*(b0*b2-b1**2))/b0**3)/y)
219
+
220
+ series = Q.series(y,n=3)
221
+
222
+ assert series == y*(b0*p2/p3+b0*(-p2/p3+b1/b0))+y**2*(b0*p1/p3+b0*p2*\
223
+ (-p2/p3+b1/b0)/p3+b0*(-p1/p3+(p2/p3-b1/b0)**2+b1*p2/(b0*p3)+\
224
+ b2/b0-b1**2/b0**2))+b0+O(y**3)
225
+ assert series.simplify() == b2*y**2 + b1*y + b0 + O(y**3)
226
+
227
+
228
+ def test_issue_9549():
229
+ y = (x**2 + x + 1) / (x**3 + x**2)
230
+ assert series(y, x, oo) == x**(-5) - 1/x**4 + x**(-3) + 1/x + O(x**(-6), (x, oo))
231
+
232
+
233
+ def test_issue_10761():
234
+ assert series(1/(x**-2 + x**-3), x, 0) == x**3 - x**4 + x**5 + O(x**6)
235
+
236
+
237
+ def test_issue_12578():
238
+ y = (1 - 1/(x/2 - 1/(2*x))**4)**(S(1)/8)
239
+ assert y.series(x, 0, n=17) == 1 - 2*x**4 - 8*x**6 - 34*x**8 - 152*x**10 - 714*x**12 - \
240
+ 3472*x**14 - 17318*x**16 + O(x**17)
241
+
242
+
243
+ def test_issue_12791():
244
+ beta = symbols('beta', positive=True)
245
+ theta, varphi = symbols('theta varphi', real=True)
246
+
247
+ expr = (-beta**2*varphi*sin(theta) + beta**2*cos(theta) + \
248
+ beta*varphi*sin(theta) - beta*cos(theta) - beta + 1)/(beta*cos(theta) - 1)**2
249
+
250
+ sol = (0.5/(0.5*cos(theta) - 1.0)**2 - 0.25*cos(theta)/(0.5*cos(theta) - 1.0)**2
251
+ + (beta - 0.5)*(-0.25*varphi*sin(2*theta) - 1.5*cos(theta)
252
+ + 0.25*cos(2*theta) + 1.25)/((0.5*cos(theta) - 1.0)**2*(0.5*cos(theta) - 1.0))
253
+ + 0.25*varphi*sin(theta)/(0.5*cos(theta) - 1.0)**2
254
+ + O((beta - S.Half)**2, (beta, S.Half)))
255
+
256
+ assert expr.series(beta, 0.5, 2).trigsimp() == sol
257
+
258
+
259
+ def test_issue_14384():
260
+ x, a = symbols('x a')
261
+ assert series(x**a, x) == x**a
262
+ assert series(x**(-2*a), x) == x**(-2*a)
263
+ assert series(exp(a*log(x)), x) == exp(a*log(x))
264
+ raises(PoleError, lambda: series(x**I, x))
265
+ raises(PoleError, lambda: series(x**(I + 1), x))
266
+ raises(PoleError, lambda: series(exp(I*log(x)), x))
267
+
268
+
269
+ def test_issue_14885():
270
+ assert series(x**Rational(-3, 2)*exp(x), x, 0) == (x**Rational(-3, 2) + 1/sqrt(x) +
271
+ sqrt(x)/2 + x**Rational(3, 2)/6 + x**Rational(5, 2)/24 + x**Rational(7, 2)/120 +
272
+ x**Rational(9, 2)/720 + x**Rational(11, 2)/5040 + O(x**6))
273
+
274
+
275
+ def test_issue_15539():
276
+ assert series(atan(x), x, -oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x - pi/2
277
+ + O(x**(-6), (x, -oo)))
278
+ assert series(atan(x), x, oo) == (-1/(5*x**5) + 1/(3*x**3) - 1/x + pi/2
279
+ + O(x**(-6), (x, oo)))
280
+
281
+
282
+ def test_issue_7259():
283
+ assert series(LambertW(x), x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + 125*x**5/24 + O(x**6)
284
+ assert series(LambertW(x**2), x, n=8) == x**2 - x**4 + 3*x**6/2 + O(x**8)
285
+ assert series(LambertW(sin(x)), x, n=4) == x - x**2 + 4*x**3/3 + O(x**4)
286
+
287
+ def test_issue_11884():
288
+ assert cos(x).series(x, 1, n=1) == cos(1) + O(x - 1, (x, 1))
289
+
290
+
291
+ def test_issue_18008():
292
+ y = x*(1 + x*(1 - x))/((1 + x*(1 - x)) - (1 - x)*(1 - x))
293
+ assert y.series(x, oo, n=4) == -9/(32*x**3) - 3/(16*x**2) - 1/(8*x) + S(1)/4 + x/2 + \
294
+ O(x**(-4), (x, oo))
295
+
296
+
297
+ def test_issue_18842():
298
+ f = log(x/(1 - x))
299
+ assert f.series(x, 0.491, n=1).removeO().nsimplify() == \
300
+ -S(180019443780011)/5000000000000000
301
+
302
+
303
+ def test_issue_19534():
304
+ dt = symbols('dt', real=True)
305
+ expr = 16*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0)/45 + \
306
+ 49*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
307
+ 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
308
+ 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
309
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
310
+ 0.96296296296296296296*dt + 1.0) + 0.051640768506639183825*dt + \
311
+ dt*(1/2 - sqrt(21)/14) + 1.0)/180 + 49*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
312
+ 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
313
+ 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
314
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
315
+ 0.96296296296296296296*dt + 1.0) + \
316
+ 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
317
+ 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
318
+ 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
319
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
320
+ 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
321
+ 1.1854881643947648988*dt + dt*(sqrt(21)/14 + 1/2) + 1.0)/180 + \
322
+ dt*(0.66666666666666666667*dt*(2.0*dt + 1.0) + \
323
+ 6.0173399699313066769*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
324
+ 4.1117044797036320069*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
325
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
326
+ 0.96296296296296296296*dt + 1.0) - \
327
+ 7.0189140975801991157*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
328
+ 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
329
+ 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
330
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
331
+ 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) + \
332
+ 0.94010945196161777522*dt*(-0.23637909581542530626*dt*(2.0*dt + 1.0) - \
333
+ 0.74817562366625959291*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
334
+ 0.88085458023927036857*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
335
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
336
+ 0.96296296296296296296*dt + 1.0) + \
337
+ 2.1165151389911680013*dt*(-0.049335189898860408029*dt*(2.0*dt + 1.0) + \
338
+ 0.29601113939316244817*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) - \
339
+ 0.12564355335492979587*dt*(0.074074074074074074074*dt*(2.0*dt + 1.0) + \
340
+ 0.2962962962962962963*dt*(0.125*dt*(2.0*dt + 1.0) + 0.875*dt + 1.0) + \
341
+ 0.96296296296296296296*dt + 1.0) + 0.22431393315265061193*dt + 1.0) - \
342
+ 0.35816132904077632692*dt + 1.0) + 5.5065024887242400038*dt + 1.0)/20 + dt/20 + 1
343
+
344
+ assert N(expr.series(dt, 0, 8), 20) == (
345
+ - Float('0.00092592592592592596126289', precision=70) * dt**7
346
+ + Float('0.0027777777777777783174695', precision=70) * dt**6
347
+ + Float('0.016666666666666656027029', precision=70) * dt**5
348
+ + Float('0.083333333333333300951828', precision=70) * dt**4
349
+ + Float('0.33333333333333337034077', precision=70) * dt**3
350
+ + Float('1.0', precision=70) * dt**2
351
+ + Float('1.0', precision=70) * dt
352
+ + Float('1.0', precision=70)
353
+ )
354
+
355
+
356
+ def test_issue_11407():
357
+ a, b, c, x = symbols('a b c x')
358
+ assert series(sqrt(a + b + c*x), x, 0, 1) == sqrt(a + b) + O(x)
359
+ assert series(sqrt(a + b + c + c*x), x, 0, 1) == sqrt(a + b + c) + O(x)
360
+
361
+
362
+ def test_issue_14037():
363
+ assert (sin(x**50)/x**51).series(x, n=0) == 1/x + O(1, x)
364
+
365
+
366
+ def test_issue_20551():
367
+ expr = (exp(x)/x).series(x, n=None)
368
+ terms = [ next(expr) for i in range(3) ]
369
+ assert terms == [1/x, 1, x/2]
370
+
371
+
372
+ def test_issue_20697():
373
+ p_0, p_1, p_2, p_3, b_0, b_1, b_2 = symbols('p_0 p_1 p_2 p_3 b_0 b_1 b_2')
374
+ Q = (p_0 + (p_1 + (p_2 + p_3/y)/y)/y)/(1 + ((p_3/(b_0*y) + (b_0*p_2\
375
+ - b_1*p_3)/b_0**2)/y + (b_0**2*p_1 - b_0*b_1*p_2 - p_3*(b_0*b_2\
376
+ - b_1**2))/b_0**3)/y)
377
+ assert Q.series(y, n=3).ratsimp() == b_2*y**2 + b_1*y + b_0 + O(y**3)
378
+
379
+
380
+ def test_issue_21245():
381
+ fi = (1 + sqrt(5))/2
382
+ assert (1/(1 - x - x**2)).series(x, 1/fi, 1).factor() == \
383
+ (-37*sqrt(5) - 83 + 13*sqrt(5)*x + 29*x + O((x - 2/(1 + sqrt(5)))**2, (x\
384
+ , 2/(1 + sqrt(5)))))/((2*sqrt(5) + 5)**2*(x + sqrt(5)*x - 2))
385
+
386
+
387
+
388
+ def test_issue_21938():
389
+ expr = sin(1/x + exp(-x)) - sin(1/x)
390
+ assert expr.series(x, oo) == (1/(24*x**4) - 1/(2*x**2) + 1 + O(x**(-6), (x, oo)))*exp(-x)
391
+
392
+
393
+ def test_issue_23432():
394
+ expr = 1/sqrt(1 - x**2)
395
+ result = expr.series(x, 0.5)
396
+ assert result.is_Add and len(result.args) == 7
397
+
398
+
399
+ def test_issue_23727():
400
+ res = series(sqrt(1 - x**2), x, 0.1)
401
+ assert res.is_Add == True
402
+
403
+
404
+ def test_issue_24266():
405
+ #type1: exp(f(x))
406
+ assert (exp(-I*pi*(2*x+1))).series(x, 0, 3) == -1 + 2*I*pi*x + 2*pi**2*x**2 + O(x**3)
407
+ assert (exp(-I*pi*(2*x+1))*gamma(1+x)).series(x, 0, 3) == -1 + x*(EulerGamma + 2*I*pi) + \
408
+ x**2*(-EulerGamma**2/2 + 23*pi**2/12 - 2*EulerGamma*I*pi) + O(x**3)
409
+
410
+ #type2: c**f(x)
411
+ assert ((2*I)**(-I*pi*(2*x+1))).series(x, 0, 2) == exp(pi**2/2 - I*pi*log(2)) + \
412
+ x*(pi**2*exp(pi**2/2 - I*pi*log(2)) - 2*I*pi*exp(pi**2/2 - I*pi*log(2))*log(2)) + O(x**2)
413
+ assert ((2)**(-I*pi*(2*x+1))).series(x, 0, 2) == exp(-I*pi*log(2)) - 2*I*pi*x*exp(-I*pi*log(2))*log(2) + O(x**2)
414
+
415
+ #type3: f(y)**g(x)
416
+ assert ((y)**(I*pi*(2*x+1))).series(x, 0, 2) == exp(I*pi*log(y)) + 2*I*pi*x*exp(I*pi*log(y))*log(y) + O(x**2)
417
+ assert ((I*y)**(I*pi*(2*x+1))).series(x, 0, 2) == exp(I*pi*log(I*y)) + 2*I*pi*x*exp(I*pi*log(I*y))*log(I*y) + O(x**2)
418
+
419
+
420
+ def test_issue_26856():
421
+ raises(ValueError, lambda: (2**x).series(x, oo, -1))