-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_limits.py
926 lines (637 loc) · 27.6 KB
/
test_limits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
"""Limit computation tests."""
import itertools
import pytest
from diofant import (E, Float, Function, I, Integral, Limit, O, Piecewise,
PoleError, Rational, Sum, Symbol, acos, acosh, asin, atan,
besselk, binomial, cbrt, ceiling, cos, cosh, cot, diff,
digamma, erf, erfi, exp, factorial, floor, gamma,
integrate, limit, log, nan, oo, pi, polygamma, root, sign,
simplify, sin, sinh, sqrt, subfactorial, symbols, tan)
from diofant.abc import a, b, c, n, x, y, z
from diofant.series.limits import heuristics
__all__ = ()
def test_basic1():
assert limit(x, x, -oo) == -oo
assert limit(x**2, x, -oo) == oo
assert limit(x*log(x), x, 0) == 0
assert limit(x - x**2, x, oo) == -oo
assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
assert limit((1 + x)**oo, x, 0) == oo
assert limit((1 + x)**oo, x, 0, dir='-') == 0
assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo
assert limit(y/x/log(x), x, 0) == -oo*sign(y)
assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
limit(Sum(1/x, (x, 1, y)) - log(y), y, oo)
limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo)
assert limit(nan, x, -oo) == nan
assert limit(O(2)*x, x, nan) == nan
assert limit(sin(O(x)), x, 0) == 0
assert limit(1/(x - 1), x, 1) == oo
assert limit(1/(x - 1), x, 1, dir='-') == -oo
assert limit(1/(5 - x)**3, x, 5) == -oo
assert limit(1/(5 - x)**3, x, 5, dir='-') == oo
assert limit(1/sin(x), x, pi) == -oo
assert limit(1/sin(x), x, pi, dir='-') == oo
assert limit(1/cos(x), x, pi/2) == -oo
assert limit(1/cos(x), x, pi/2, dir='-') == oo
assert limit(1/tan(x**3), x, cbrt(2*pi)) == oo
assert limit(1/tan(x**3), x, cbrt(2*pi), dir='-') == -oo
assert limit(1/cot(x)**3, x, 3*pi/2) == -oo
assert limit(1/cot(x)**3, x, 3*pi/2, dir='-') == oo
# approaching 0
# from dir='+'
assert limit(1 + 1/x, x, 0) == oo
# from dir='-'
# Add
assert limit(1 + 1/x, x, 0, dir='-') == -oo
# Pow
assert limit(x**(-2), x, 0, dir='-') == oo
assert limit(x**(-3), x, 0, dir='-') == -oo
assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
assert limit(x**2, x, 0, dir='-') == 0
assert limit(sqrt(x), x, 0, dir='-') == 0
assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi))
assert limit((1 + cos(x))**oo, x, 0) == oo
assert limit(x**2, x, 0, dir='real') == 0
assert limit(exp(x), x, 0, dir='real') == 1
pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
# issue diofant/diofant#74
assert limit(sign(log(1 - 1/x)), x, oo) == -1
# issue sympy/sympy#8166
f = Function('f')
assert limit(f(x), x, 4) == Limit(f(x), x, 4)
def test_basic2():
assert limit(x**x, x, 0) == 1
assert limit((exp(x) - 1)/x, x, 0) == 1
def test_basic3():
assert limit(1/x, x, 0) == oo
assert limit(1/x, x, 0, dir='-') == -oo
def test_basic4():
assert limit(2*x + y*x, x, 0) == 0
assert limit(2*x + y*x, x, 1) == 2 + y
assert limit(2*x**8 + y*x**(-3), x, -2) == 512 - y/8
assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0
assert integrate(1/(x**3 + 1), (x, 0, oo)) == 2*pi*sqrt(3)/9
# coverage test
l = Limit(Piecewise((x, x > 1), (0, True)), x, -1)
assert l.doit() == l
# issue sympy/sympy#16714
e = ((n**(n + 1) + (n + 1)**n)/n**(n + 1))**n
assert limit(e, n, oo) == E**E
# issue sympy/sympy#18492
e1 = 2*sqrt(x)*Piecewise(((4*x - 2)/abs(sqrt(4 - 4*(2*x - 1)**2)),
4*x - 2 >= 0),
((2 - 4*x)/abs(sqrt(4 - 4*(2*x - 1)**2)), True))
e2 = Piecewise((x**2/2, x <= Rational(1, 2)), (x/2 - Rational(1, 8), True))
e3 = Piecewise(((x - 9)/5, x < -1), ((x - 9)/5, x > 4),
(sqrt(abs(x - 3)), True))
assert limit(e1, x, 0) == 1
assert limit(e2, x, 0) == 0
assert limit(e2, x, oo) == oo
assert limit(e3, x, -1) == 2
assert limit(e3, x, oo) == oo
def test_basic5():
class MyFunction(Function):
@classmethod
def eval(cls, arg):
if arg is oo:
return nan
assert limit(MyFunction(x), x, oo) == Limit(MyFunction(x), x, oo)
assert limit(4/x > 8, x, 0) # relational test
assert limit(MyFunction(x) > 0, x, oo) == Limit(MyFunction(x) > 0, x, oo)
# issue sympy/sympy#11833
a = Symbol('a', positive=True)
f = exp(x*(-a - 1)) * sinh(x)
assert limit(f, x, oo) == 0
assert limit(O(x), x, x**2) == Limit(O(x), x, x**2)
def test_basic6():
# pull sympy/sympy#22491
assert limit(1/asin(x), x, 0, dir='+') == oo
assert limit(1/asin(x), x, 0, dir='-') == -oo
assert limit(1/sinh(x), x, 0, dir='+') == oo
assert limit(1/sinh(x), x, 0, dir='-') == -oo
assert limit(log(1/x) + 1/sin(x), x, 0) == oo
assert limit(log(1/x) + 1/x, x, 0) == oo
def test_sympyissue_3885():
assert limit(x*y + x*z, z, 2) == x*y + 2*x
def test_Limit():
assert Limit(sin(x)/x, x, 0) != 1
assert Limit(sin(x)/x, x, 0).doit() == 1
def test_floor():
assert limit(floor(x), x, -2, '+') == -2
assert limit(floor(x), x, -2, '-') == -3
assert limit(floor(x), x, -1, '+') == -1
assert limit(floor(x), x, -1, '-') == -2
assert limit(floor(x), x, 0, '+') == 0
assert limit(floor(x), x, 0, '-') == -1
assert limit(floor(x), x, 1, '+') == 1
assert limit(floor(x), x, 1, '-') == 0
assert limit(floor(x), x, 2, '+') == 2
assert limit(floor(x), x, 2, '-') == 1
assert limit(floor(x), x, 248, '+') == 248
assert limit(floor(x), x, 248, '-') == 247
def test_floor_requires_robust_assumptions():
assert limit(floor(sin(x)), x, 0, '+') == 0
assert limit(floor(sin(x)), x, 0, '-') == -1
assert limit(floor(cos(x)), x, 0, '+') == 0
assert limit(floor(cos(x)), x, 0, '-') == 0
assert limit(floor(5 + sin(x)), x, 0, '+') == 5
assert limit(floor(5 + sin(x)), x, 0, '-') == 4
assert limit(floor(5 + cos(x)), x, 0, '+') == 5
assert limit(floor(5 + cos(x)), x, 0, '-') == 5
def test_ceiling():
assert limit(ceiling(x), x, -2, '+') == -1
assert limit(ceiling(x), x, -2, '-') == -2
assert limit(ceiling(x), x, -1, '+') == 0
assert limit(ceiling(x), x, -1, '-') == -1
assert limit(ceiling(x), x, 0, '+') == 1
assert limit(ceiling(x), x, 0, '-') == 0
assert limit(ceiling(x), x, 1, '+') == 2
assert limit(ceiling(x), x, 1, '-') == 1
assert limit(ceiling(x), x, 2, '+') == 3
assert limit(ceiling(x), x, 2, '-') == 2
assert limit(ceiling(x), x, 248, '+') == 249
assert limit(ceiling(x), x, 248, '-') == 248
def test_ceiling_requires_robust_assumptions():
assert limit(ceiling(sin(x)), x, 0, '+') == 1
assert limit(ceiling(sin(x)), x, 0, '-') == 0
assert limit(ceiling(cos(x)), x, 0, '+') == 1
assert limit(ceiling(cos(x)), x, 0, '-') == 1
assert limit(ceiling(5 + sin(x)), x, 0, '+') == 6
assert limit(ceiling(5 + sin(x)), x, 0, '-') == 5
assert limit(ceiling(5 + cos(x)), x, 0, '+') == 6
assert limit(ceiling(5 + cos(x)), x, 0, '-') == 6
def test_atan():
assert limit(atan(x)*sin(1/x), x, 0) == 0
assert limit(atan(x) + sqrt(x + 1) - sqrt(x), x, oo) == pi/2
def test_abs():
assert limit(abs(x), x, 0) == 0
assert limit(abs(sin(x)), x, 0) == 0
assert limit(abs(cos(x)), x, 0) == 1
assert limit(abs(sin(x + 1)), x, 0) == sin(1)
# sympy/sympy#12398
assert limit(abs(log(n)/n**3), n, oo) == 0
expr = abs(log(n)/n**3)
expr2 = expr.subs({n: n + 1})
assert limit(n*(expr/expr2 - 1), n, oo) == 3
def test_heuristic():
x = Symbol('x', extended_real=True)
assert heuristics(sin(1/x) + atan(x), x, 0, '+') == sin(oo)
assert heuristics(log(2 + sqrt(atan(x))*sin(1/x)), x, 0, '+') == log(2)
assert heuristics(tan(tan(1/x)), x, 0, '+') is None
def test_sympyissue_3871():
z = Symbol('z', positive=True)
f = -1/z*exp(-z*x)
assert limit(f, x, oo) == 0
assert f.limit(x, oo) == 0
def test_exponential():
x = Symbol('x', extended_real=True)
assert limit((1 + x/n)**n, n, oo) == exp(x)
assert limit((1 + x/(2*n))**n, n, oo) == exp(x/2)
assert limit((1 + x/(2*n + 1))**n, n, oo) == exp(x/2)
assert limit(((x - 1)/(x + 1))**x, x, oo) == exp(-2)
assert limit(1 + (1 + 1/x)**x, x, oo) == 1 + E
@pytest.mark.xfail
def test_exponential2():
assert limit((1 + x/(n + sin(n)))**n, n, oo) == exp(x)
def test_doit():
f = Integral(2 * x, x)
l = Limit(f, x, oo)
assert l.doit() == oo
def test_doit2():
f = Integral(2 * x, x)
l = Limit(f, x, oo)
# limit() breaks on the contained Integral.
assert l.doit(deep=False) == l
def test_sympyissue_3792():
assert limit((1 - cos(x))/x**2, x, Rational(1, 2)) == 4 - 4*cos(Rational(1, 2))
assert limit(sin(sin(x + 1) + 1), x, 0) == sin(1 + sin(1))
assert limit(abs(sin(x + 1) + 1), x, 0) == 1 + sin(1)
def test_sympyissue_4090():
assert limit(1/(x + 3), x, 2) == Rational(1, 5)
assert limit(1/(x + pi), x, 2) == 1/(2 + pi)
assert limit(log(x)/(x**2 + 3), x, 2) == log(2)/7
assert limit(log(x)/(x**2 + pi), x, 2) == log(2)/(4 + pi)
def test_sympyissue_4547():
assert limit(cot(x), x, 0) == oo
assert limit(cot(x), x, pi/2) == 0
def test_sympyissue_5164():
assert limit(x**0.5, x, oo) == oo**0.5 == oo
assert limit(x**0.5, x, 16) == 4.0
assert limit(x**0.5, x, 0) == 0
assert limit(x**(-0.5), x, oo) == 0
assert limit(x**(-0.5), x, 4) == 0.5
def test_sympyissue_5183():
# using list(...) so pytest can recalculate values
tests = list(itertools.product([x, -x],
[-1, 1],
[2, 3, Rational(1, 2), Rational(2, 3)],
['-', '+']))
results = (oo, oo, -oo, oo, -oo*I, oo, -oo*cbrt(-1), oo,
0, 0, 0, 0, 0, 0, 0, 0,
oo, oo, oo, -oo, oo, -oo*I, oo, -oo*cbrt(-1),
0, 0, 0, 0, 0, 0, 0, 0)
assert len(tests) == len(results)
for args, res in zip(tests, results):
y, s, e, d = args
eq = y**(s*e)
assert limit(eq, x, 0, dir=d) == res
def test_sympyissue_5184():
assert limit(sin(x)/x, x, oo) == 0
assert limit(atan(x), x, oo) == pi/2
assert limit(gamma(x), x, oo) == oo
assert limit(cos(x)/x, x, oo) == 0
assert limit(gamma(x), x, Rational(1, 2)) == sqrt(pi)
r = Symbol('r', real=True)
assert limit(r*sin(1/r), r, 0) == 0
def test_sympyissue_5229():
assert limit((1 + y)**(1/y) - E, y, 0) == 0
def test_sympyissue_4546():
# using list(...) so pytest can recalculate values
tests = list(itertools.product([cot, tan],
[-pi/2, 0, pi/2, pi, 3*pi/2],
['-', '+']))
results = (0, 0, -oo, oo, 0, 0, -oo, oo, 0, 0,
oo, -oo, 0, 0, oo, -oo, 0, 0, oo, -oo)
assert len(tests) == len(results)
for args, res in zip(tests, results):
f, l, d = args
eq = f(x)
assert limit(eq, x, l, dir=d) == res
def test_sympyissue_3934():
assert limit((1 + x**log(3))**(1/x), x, 0) == 1
assert limit((5**(1/x) + 3**(1/x))**x, x, 0) == 5
def test_compute_leading_term():
assert limit(x**Rational(77, 3)/(1 + x**Rational(77, 3)), x, oo) == 1
assert limit(x**Rational('101.1')/(1 + x**Rational('101.1')), x, oo) == 1
def test_sympyissue_5955():
assert limit((x**16)/(1 + x**16), x, oo) == 1
assert limit((x**100)/(1 + x**100), x, oo) == 1
assert limit((x**1885)/(1 + x**1885), x, oo) == 1
assert limit((x**100/((x + 1)**100 + exp(-x))), x, oo) == 1
def test_newissue():
assert limit(exp(1/sin(x))/exp(cot(x)), x, 0) == 1
def test_extended_real_line():
assert limit(x - oo, x, oo) == -oo
assert limit(oo - x, x, -oo) == oo
assert limit(x**2/(x - 5) - oo, x, oo) == -oo
assert limit(1/(x + sin(x)) - oo, x, 0) == -oo
assert limit(oo/x, x, oo) == oo
assert limit(x - oo + 1/x, x, oo) == -oo
assert limit(x - oo + 1/x, x, 0) == -oo
@pytest.mark.xfail
def test_order_oo():
x = Symbol('x', positive=True, finite=True)
assert O(x)*oo != O(1, x)
assert limit(oo/(x**2 - 4), x, oo) == oo
def test_sympyissue_5436():
# also issue sympy/sympy#13312 (but see diofant/diofant#425!)
assert limit(exp(x*y), x, oo) == exp(oo*sign(y))
assert limit(exp(-x*y), x, oo) == exp(-oo*sign(y))
def test_Limit_dir():
pytest.raises(TypeError, lambda: Limit(x, x, 0, dir=0))
pytest.raises(ValueError, lambda: Limit(x, x, 0, dir='0'))
def test_polynomial():
assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, oo) == 1
assert limit((x + 1)**1000/((x + 1)**1000 + 1), x, -oo) == 1
def test_rational():
assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, oo) == (z - 1)/(y*z)
assert limit(1/y - (1/(y + x) + x/(y + x)/y)/z, x, -oo) == (z - 1)/(y*z)
def test_sympyissue_5740():
assert limit(log(x)*z - log(2*x)*y, x, 0) == oo*sign(y - z)
def test_sympyissue_6366():
n = Symbol('n', integer=True, positive=True)
r = (n + 1)*x**(n + 1)/(x**(n + 1) - 1) - x/(x - 1)
assert limit(r, x, 1).simplify() == n/2
def test_factorial():
f = factorial(x)
assert limit(f, x, oo) == oo
assert limit(x/f, x, oo) == 0
# see Stirling's approximation:
# https://en.wikipedia.org/wiki/Stirling's_approximation
assert limit(f/(sqrt(2*pi*x)*(x/E)**x), x, oo) == 1
assert limit(f, x, -oo) == factorial(-oo)
assert (limit(f, x, x**2) - factorial(x**2)).simplify() == 0
assert (limit(f, x, -x**2) - factorial(-x**2)).simplify() == 0
def test_sympyissue_6560():
e = 5*x**3/4 - 3*x/4 + (y*(3*x**2/2 - Rational(1, 2)) +
35*x**4/8 - 15*x**2/4 + Rational(3, 8))/(2*(y + 1))
assert limit(e, y, oo) == (5*x**3 + 3*x**2 - 3*x - 1)/4
def test_sympyissue_5172():
r = Symbol('r', positive=True)
p = Symbol('p', positive=True)
m = Symbol('m', negative=True)
expr = ((2*n*(n - r + 1)/(n + r*(n - r + 1)))**c +
(r - 1)*(n*(n - r + 2)/(n + r*(n - r + 1)))**c - n)/(n**c - n)
expr = expr.subs({c: c + 1})
assert limit(expr.subs({c: m}), n, oo) == 1
assert limit(expr.subs({c: p}), n, oo) == (2**(p + 1) + r -
1)/(r + 1)**(p + 1)
def test_sympyissue_7088():
assert limit(sqrt(x/(x + a)), x, oo) == 1
def test_sympyissue_6364():
e = z/(1 - sqrt(1 + z)*sin(a)**2 - sqrt(1 - z)*cos(a)**2)
assert (limit(e, z, 0) - 2/cos(2*a)).simplify() == 0
def test_sympyissue_4099():
assert limit(a/x, x, 0) == oo*sign(a)
assert limit(-a/x, x, 0) == -oo*sign(a)
assert limit(-a*x, x, oo) == -oo*sign(a)
assert limit(a*x, x, oo) == oo*sign(a)
def test_sympyissue_4503():
dx = Symbol('dx')
assert limit((sqrt(1 + exp(x + dx)) - sqrt(1 + exp(x)))/dx, dx, 0) == \
exp(x)/(2*sqrt(exp(x) + 1))
def test_sympyissue_8730():
assert limit(subfactorial(x), x, oo) == oo
def test_issue_55():
assert limit((x + exp(x))/(x - 1), x, -oo) == 1
assert limit((x*exp(x))/(exp(x) - 1), x, -oo) == 0 # issue sympy/sympy#2929
# issue sympy/sympy#22982
assert limit((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x, oo) == oo
assert limit((log(E + 1/x))**(1 - sqrt(E + 1/x)), x, oo) == 1
assert limit((log(E + 1/x) - 1)**-sqrt(E + 1/x), x, oo) == oo
def test_sympyissue_8061():
assert limit(4**(acos(1/(1 + x**2))**2)/log(1 + x, 4), x, 0) == oo
def test_sympyissue_8229():
assert limit((root(x, 4) - 2)/(sqrt(x) - 4)**Rational(2, 3),
x, 16) == 0
def test_sympyissue_9205():
assert Limit(x, x, a).free_symbols == {a}
assert Limit(x, x, a, '-').free_symbols == {a}
assert Limit(x + y, x + y, a).free_symbols == {a}
assert Limit(-x**2 + y, x**2, a).free_symbols == {y, a}
def test_sympyissue_10610():
assert limit(3**x*3**(-x - 1)*(x + 1)**2/x**2, x, oo) == Rational(1, 3)
assert limit(2**x*2**(-x - 1)*(x + 1)*(y - 1)**(-x) *
(y - 1)**(x + 1)/(x + 2), x, oo) == y/2 - Rational(1, 2)
def test_sympyissue_9075():
assert limit((6**(x + 1) + x + 1)/(6**x + x), x, oo) == 6
def test_sympyissue_8634():
p = Symbol('p', positive=True)
assert limit(x**p, x, -oo) == oo*sign((-1)**p)
def test_sympyissue_9558():
assert limit(sin(x)**15, x, 0, '-') == 0 # should be fast
def test_issue_296():
e = log(exp(1/x)/Float(2) + exp(-1/x)/2)*x**2
assert e.limit(x, oo) == 0.5
def test_sympyissue_5383():
e = (1.0 + 1.0*x)**(1.0/x)
assert e.limit(x, 0) == E.evalf()
def test_sympyissue_6171():
e = Piecewise((0, x < 0), (1, True))
assert e.limit(x, 0) == 1
assert e.limit(x, 0, '-') == 0
def test_sympyissue_11526():
df = diff(1/(a*log((x - b)/(x - c))), x)
res = -1/(-a*c + a*b)
assert limit(df, x, oo) == res
assert (limit(simplify(df), x, oo) - res).simplify() == 0
e = log((1/x - b)/(1/x - c))
assert e.as_leading_term(x) == x*(c - b)
def test_sympyissue_11672():
assert limit(Rational(-1, 2)**x, x, oo) == 0
assert limit(1/(-2)**x, x, oo) == 0
def test_sympyissue_8635():
n = Symbol('n', integer=True, positive=True)
k = 0
assert limit(x**n - x**(n - k), x, oo) == 0
k = 1
assert limit(x**n - x**(n - k), x, oo) == oo
k = 2
assert limit(x**n - x**(n - k), x, oo) == oo
k = 3
assert limit(x**n - x**(n - k), x, oo) == oo
def test_sympyissue_8157():
n = Symbol('n', integer=True)
limit(cos(pi*n), n, oo) # not raises
def test_sympyissue_5415():
assert limit(polygamma(2 + 1/x, 3 + exp(-x)), x, oo) == polygamma(2, 3)
def test_sympyissue_2865():
l1 = limit(O(1/x, (x, oo)), x, 0)
assert l1 != 0 and isinstance(l1, Limit)
l2 = limit(O(x, (x, oo)), x, 0)
assert l2 != 0 and isinstance(l2, Limit)
def test_sympyissue_11879():
assert limit(((x + y)**n - x**n)/y, y, 0).powsimp() == n*x**(n-1)
def test_sympyissue_12555():
assert limit((3**x + 2*x**10)/(x**10 + E**x), x, -oo) == 2
def test_sympyissue_12769():
r = Symbol('r', real=True)
a, b, s0, K, F0 = symbols('a b s0 K F0', positive=True, real=True)
fx = (F0**b*K**b*r*s0 -
sqrt((F0**2*K**(2*b)*a**2*(b - 1) +
F0**(2*b)*K**2*a**2*(b - 1) +
F0**(2*b)*K**(2*b)*s0**2*(b - 1)*(b**2 - 2*b + 1) -
2*F0**(2*b)*K**(b + 1)*a*r*s0*(b**2 - 2*b + 1) +
2*F0**(b + 1)*K**(2*b)*a*r*s0*(b**2 - 2*b + 1) -
2*F0**(b + 1)*K**(b + 1)*a**2*(b - 1))/((b - 1)*(b**2 - 2*b + 1))))*(b*r - b - r + 1)
assert limit(fx, K, F0) == (F0**(2*b)*b*r**2*s0 - 2*F0**(2*b)*b*r*s0 +
F0**(2*b)*b*s0 - F0**(2*b)*r**2*s0 +
2*F0**(2*b)*r*s0 - F0**(2*b)*s0)
def test_sympyissue_13332():
assert limit(sqrt(30)*5**(-5*n - 1)*(46656*n)**n *
(5*n + 2)**(5*n + Rational(5, 2)) *
(6*n + 2)**(-6*n - Rational(5, 2)), n, oo) == Rational(25, 36)
def test_sympyissue_13382():
assert limit(n*(((n + 1)**2 + 1)/(n**2 + 1) - 1), n, oo) == 2
def test_sympyissue_13403():
assert limit(n*(-1 + (n + log(n + 1) + 1)/(n + log(n))), n, oo) == 1
def test_sympyissue_13416():
assert limit((-n**3*log(n)**3 +
(n - 1)*(n + 1)**2*log(n + 1)**3)/(n**2*log(n)**3),
n, oo) == 1
def test_sympyissue_13462():
assert limit(n**2*(2*n*(-(1 - 1/(2*n))**x + 1) -
x - (-x**2/4 + x/4)/n), n, oo) == x/12 - x**2/8 + x**3/24
def test_sympyissue_13575():
assert limit(acos(erfi(x)), x, 1) == pi/2 + I*log(sqrt(erf(I)**2 + 1) +
erf(I))
def test_issue_558():
n = Symbol('n')
r = Symbol('r', positive=True)
c = Symbol('c')
expr = ((2*n*(n - r + 1)/(n + r*(n - r + 1)))**c +
(r - 1)*(n*(n - r + 2)/(n + r*(n - r + 1)))**c - n)/(n**c - n)
expr = expr.subs({c: c + 1})
assert limit(expr, n, oo) == Limit(expr, n, oo)
def test_sympyissue_14393():
assert limit((x**b - y**b)/(x**a - y**a), x, y) == b*y**b/y**a/a
def test_sympyissue_14590():
assert limit((n**3*((n + 1)/n)**n)/((n + 1)*(n + 2)*(n + 3)), n, oo) == E
def test_sympyissue_14793():
e = ((x + Rational(1, 2))*log(x) - x +
log(2*pi)/2 - log(factorial(x)) + 1/(12*x))*x**3
assert limit(e, x, oo) == Rational(1, 360)
def test_sympyissue_14811():
assert limit(((1 + Rational(2, 3)**(x + 1))**2**x)/(2**Rational(4, 3)**(x - 1)), x, oo) == oo
def test_sympyissue_15055():
assert limit(n**3*((-n - 1)*sin(1/n) + (n + 2)*sin(1/(n + 1)))/(-n + 1), n, oo) == 1
def test_sympyissue_15146():
assert limit((n/2)*(-2*n**3 - 2*(n**3 - 1)*n**2*digamma(n**3 + 1) +
2*(n**3 - 1)*n**2*digamma(n**3 + n + 1) +
n + 3), n, oo) == Rational(1, 3)
def test_sympyissue_15323():
assert limit(((1 - 1/x)**x).diff(x), x, 1) == 1
def test_sympyissue_15984():
assert limit((-x + log(exp(x) + 1))/x, x, oo, dir='-') == 0
def test_sympyissue_16222():
assert limit(exp(x), x, 10000000) == exp(10000000)
assert limit(exp(x), x, 100000000) == exp(100000000)
assert limit(exp(x), x, 1000000000) == exp(1000000000)
@pytest.mark.timeout(20)
def test_sympyissue_15282():
assert limit((x**2000 - (x + 1)**2000)/x**1999, x, oo) == -2000
def test_sympyissue_16722():
n, z = symbols('n z')
assert isinstance(limit(binomial(n + z, n)*n**-z, n, oo), Limit)
z = symbols('z', positive=True)
assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
n = symbols('n', positive=True, integer=True)
z = symbols('z', positive=True)
assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
n, z = symbols('n z', positive=True, integer=True)
assert limit(binomial(n + z, n)*n**-z, n, oo) == 1/gamma(z + 1)
def test_sympyissue_15673():
p = symbols('p')
alpha = symbols('α', positive=True)
e = Limit(4*pi*p**(-alpha)*(p**3 - p**alpha)/(alpha - 3), p, 0)
assert isinstance(e.doit(), Limit) # but see diofant/diofant#425
def test_sympyissue_17380():
assert limit(x*(((x + 1)**2 + 1)/(x**2 + 1) - 1), x, oo) == 2
def test_sympyissue_17431():
assert limit(((n + 1) + 1)/(((n + 1) + 2)*factorial(n + 1)) *
(n + 2)*factorial(n)/(n + 1), n, oo) == 0
assert limit((n + 2)**2*factorial(n)/((n + 1)*(n + 3)*factorial(n + 1)),
n, oo) == 0
# test from sympy/sympy#17434 (see also diofant/diofant#425):
y = symbols('y', integer=True, positive=True)
assert isinstance(limit(x*factorial(x)/factorial(x + y), x, oo), Limit)
def test_sympyissue_17792():
n = Symbol('n', positive=True, integer=True)
assert limit(factorial(n)/sqrt(n)*(E/n)**n, n, oo) == sqrt(2*pi)
def test_sympyissue_18118():
assert limit(sign(x), x, 0, '+') == +1
assert limit(sign(x), x, 0, '-') == -1
assert limit(sign(sin(x)), x, 0, '+') == +1
assert limit(sign(sin(x)), x, 0, '-') == -1
def test_sympyissue_6599():
assert limit((x + cos(x))/x, x, oo) == 1
def test_sympyissue_18176():
x = Symbol('x', real=True, positive=True)
n = Symbol('n', integer=True, positive=True)
k = Symbol('k')
e = x**n - x**(n - k)
assert limit(e.subs({k: 0}), x, oo) == 0
assert limit(e.subs({k: 1}), x, oo) == oo
def test_sympyissue_18306():
assert limit(sin(sqrt(x))/sqrt(sin(x)), x, 0) == 1
def test_sympyissue_18378():
assert limit(log(exp(3*x) + x)/log(exp(x) + x**100), x, oo) == 3
def test_sympyissue_18399():
assert limit((1 - x/2)**(3*x), x, oo) == oo
assert limit((-x)**x, x, oo) == oo
def test_sympyissue_18452():
assert limit(abs(log(x))**x, x, 0, '+') == 1
assert limit(abs(log(x))**x, x, 0, '-') == 1
def test_sympyissue_18482():
assert limit(sqrt(x**2 + 6*x) + (x**3 + x**2)/(x**2 + 1), x, -oo) == -2
assert limit((x**3 + x**2 + sqrt(x*(x + 6))*(x**2 + 1))/(x**2 + 1),
x, -oo) == -2
assert limit((2*exp(3*x)/(exp(2*x) + 1))**(1/x), x, oo) == E
def test_sympyissue_18501():
assert limit(abs(log(x - 1)**3 - 1), x, 1, '+') == oo
def test_sympyissue_18508():
assert limit(sin(x)/sqrt(1 - cos(x)), x, 0) == sqrt(2)
def test_sympyissue_18707():
p = Symbol('p', positive=True, real=True)
assert limit(1/p**n, n, oo) == p**-oo
def test_sympyissue_18997():
assert limit(abs(log(x)), x, 0) == oo
assert limit(abs(log(abs(x))), x, 0) == oo
def test_sympyissue_18992():
assert limit(x/(factorial(x)**(1/x)), x, oo) == E
n = Symbol('n', positive=True, integer=True)
assert limit(n/(factorial(n)**(1/n)), n, oo) == E
def test_sympyissue_19026():
assert limit(abs(log(x) + 1)/log(x), x, oo) == 1
def test_sympyissue_19770():
m = Symbol('m', extended_real=True)
assert limit(cos(x*m)/x, x, oo) == 0
def test_sympyissue_19766():
assert limit(2**(-x)*sqrt(4**(x + 1) + 1), x, oo) == 2
def test_sympyissue_14874():
assert limit(besselk(0, x), x, oo) == 0
def test_sympyissue_20365():
assert limit(((x + 1)**(1/x) - E)/x, x, 0) == -E/2
def test_sympyissue_20704():
assert limit(x*(abs(1/x + y) - abs(y - 1/x))/2, x, 0) == 0
def test_sympyissue_21031():
assert limit(((1 + x)**(1/x) -
(1 + 2*x)**(1/(2*x)))/asin(x), x, 0) == E/2
def test_sympyissue_21038():
assert limit(sin(pi*x)/(3*x - 12), x, 4) == pi/3
def test_sympyissue_21029():
e = (sinh(x) + cosh(x) - 1)/x/4
ans0 = Rational(1, 4)
ansz = (exp(z) - 1)/z/4
assert limit(e, x, 0) == ans0
assert limit(e, x, z) == ansz
assert limit(ansz, z, 0) == ans0
def test_sympyissue_20578():
e = abs(x)*sin(1/x)
assert all(_ == 0 for _ in [limit(e, x, 0, '+'),
limit(e, x, 0, '-'),
limit(e, x, 0, 'real')])
def test_sympyissue_19453():
beta = Symbol('beta', real=True, positive=True)
h = Symbol('h', real=True, positive=True)
m = Symbol('m', real=True, positive=True)
w = Symbol('omega', real=True, positive=True)
g = Symbol('g', real=True, positive=True)
q = 3*h**2*beta*g*exp(h*beta*w/2)
p = m**2*w**2
s = exp(h*beta*w) - 1
z = (-q/(4*p*s) - q/(2*p*s**2) -
q*(exp(h*beta*w) + 1)/(2*p*s**3) + exp(h*beta*w/2)/s)
e = -diff(log(z), beta)
assert limit(e - h*w/2, beta, oo) == 0
assert limit(e.simplify() - h*w/2, beta, oo) == 0
def test_sympyissue_19442():
pytest.raises(PoleError, lambda: limit(1/x, x, 0, 'real'))
def test_sympyissue_21530():
assert limit(sinh(n + 1)/sinh(n), n, oo) == E
def test_sympyissue_21550():
r = (sqrt(5) - 1)/2
assert limit((x - r)/(x**2 + x - 1), x, r).simplify() == sqrt(5)/5
def test_sympyissue_21606():
assert limit(cos(x)/sign(x), x, pi, '+') == -1
assert limit(cos(x)/sign(x), x, pi, '-') == -1
def test_sympyissue_21756():
e = (1 - exp(-2*I*pi*z))/(1 - exp(-2*I*pi*z/5))
assert limit(e, z, 0) == 5
def test_sympyissue_21785():
assert sqrt((-a**2 + x**2)/(1 - x**2)).limit(a, 1, '-') == I
def test_sympyissue_22220():
e1 = sqrt(30)*atan(sqrt(30)*tan(x/2)/6)/30
e2 = sqrt(30)*I*(-log(sqrt(2)*tan(x/2) - 2*sqrt(15)*I/5) +
+log(sqrt(2)*tan(x/2) + 2*sqrt(15)*I/5))/60
assert limit(e1, x, -pi) == -sqrt(30)*pi/60
assert limit(e2, x, -pi) == -sqrt(30)*pi/30
assert limit(e1, x, -pi, '-') == sqrt(30)*pi/60
assert limit(e2, x, -pi, '-') == 0
def test_sympyissue_22334():
k = Symbol('k', positive=True)
assert limit((n + 1)**k/((n + 1)**(k + 1) - n**(k + 1)), n, oo) == 1/(k + 1)
def test_sympyissue_22893():
a, b = symbols('a b', positive=True)
e = (a*exp(-a*x) + b*exp(-b*x))*exp(b*x)
assert isinstance(limit(e, x, oo), Limit)
def test_sympyissue_22986():
assert limit(acosh(1 + 1/x)*sqrt(x), x, oo) == sqrt(2)
def test_sympyissue_23231():
assert limit((2**x - 2**-x)/(2**x + 2**-x), x, -oo) == -1
def test_issue_1164():
# also sympy/sympy#14502
assert limit(factorial(x) - x**x, x, oo) == -oo
l = Limit(factorial(x) - x**oo, x, oo)
assert l.doit() == l