def test_evalf_integer_parts(): a = floor(log(8)/log(2) - exp(-1000), evaluate=False) b = floor(log(8)/log(2), evaluate=False) assert a.evalf() == 3 assert b.evalf() == 3 # equals, as a fallback, can still fail but it might succeed as here assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10 assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336800) assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \ int(11188719610782480504630258070757734324011354208865721592720336801) assert int(floor((GoldenRatio**999 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(999) assert int(floor((GoldenRatio**1000 / sqrt(5) + Rational(1, 2))) .evalf(1000)) == fibonacci(1000) assert ceiling(x).evalf(subs={x: 3}) == 3 assert ceiling(x).evalf(subs={x: 3*I}) == 3*I assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2 + 3*I # issue sympy/sympy#10323 l = 1206577996382235787095214 y = ceiling(sqrt(l)) assert y == 1098443442506 assert y**2 >= l def check(x): c, f = ceiling(sqrt(x)), floor(sqrt(x)) assert (c - 1)**2 < x and c**2 >= x assert (f + 1)**2 > x and f**2 <= x check(2**30 + 1) check(2**100 + 1) check(2**112 + 2)
def test_limit(): assert gruntz(x, x) == oo assert gruntz(-x, x) == -oo assert gruntz(-x, x) == -oo assert gruntz((-x)**2, x) == oo assert gruntz(-x**2, x) == -oo assert gruntz((1/x)*log(1/x), x) == 0 # Gruntz: p15, 2.11 assert gruntz(1/x, x) == 0 assert gruntz(exp(x), x) == oo assert gruntz(-exp(x), x) == -oo assert gruntz(exp(x)/x, x) == oo assert gruntz(1/x - exp(-x), x) == 0 assert gruntz(x + 1/x, x) == oo assert gruntz((1/x)**(1/x), x) == 1 # Gruntz: p15, 2.11 assert gruntz((exp(1/x) - 1)*x, x) == 1 assert gruntz(1 + 1/x, x) == 1 assert gruntz(-exp(1/x), x) == -1 assert gruntz(x + exp(-x), x) == oo assert gruntz(x + exp(-x**2), x) == oo assert gruntz(x + exp(-exp(x)), x) == oo assert gruntz(13 + 1/x - exp(-x), x) == 13 a = Symbol('a') assert gruntz(x - log(1 + exp(x)), x) == 0 assert gruntz(x - log(a + exp(x)), x) == 0 assert gruntz(exp(x)/(1 + exp(x)), x) == 1 assert gruntz(exp(x)/(a + exp(x)), x) == 1 assert gruntz((3**x + 5**x)**(1/x), x) == 5 # issue sympy/sympy#3463 assert gruntz(Ei(x + exp(-x))*exp(-x)*x, x) == 1 assert gruntz(1/li(x), x) == 0 assert gruntz(1/Li(x), x) == 0 # issue diofant/diofant#56 assert gruntz((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x) == oo # issue sympy/sympy#9471 assert gruntz((((27**(log(x, 3))))/x**3), x) == 1 assert gruntz((((27**(log(x, 3) + 1)))/x**3), x) == 27 # issue sympy/sympy#9449 y = Symbol('y') assert gruntz(x*(abs(1/x + y) - abs(y - 1/x))/2, x) == sign(y) # issue sympy/sympy#8481 assert gruntz(m**x * exp(-m) / factorial(x), x) == 0 # issue sympy/sympy#4187 assert gruntz(exp(1/x)*log(1/x) - Ei(1/x), x) == -EulerGamma assert gruntz(exp(x)*log(x) - Ei(x), x) == oo # issue sympy/sympy#10382 assert gruntz(fibonacci(x + 1)/fibonacci(x), x) == GoldenRatio assert gruntz(zeta(x), x) == 1 assert gruntz(zeta(m)*zeta(x), x) == zeta(m)
def test_limit(): assert gruntz(x, x) == oo assert gruntz(-x, x) == -oo assert gruntz(-x, x) == -oo assert gruntz((-x)**2, x) == oo assert gruntz(-x**2, x) == -oo assert gruntz((1 / x) * log(1 / x), x) == 0 # Gruntz: p15, 2.11 assert gruntz(1 / x, x) == 0 assert gruntz(exp(x), x) == oo assert gruntz(-exp(x), x) == -oo assert gruntz(exp(x) / x, x) == oo assert gruntz(1 / x - exp(-x), x) == 0 assert gruntz(x + 1 / x, x) == oo assert gruntz((1 / x)**(1 / x), x) == 1 # Gruntz: p15, 2.11 assert gruntz((exp(1 / x) - 1) * x, x) == 1 assert gruntz(1 + 1 / x, x) == 1 assert gruntz(-exp(1 / x), x) == -1 assert gruntz(x + exp(-x), x) == oo assert gruntz(x + exp(-x**2), x) == oo assert gruntz(x + exp(-exp(x)), x) == oo assert gruntz(13 + 1 / x - exp(-x), x) == 13 a = Symbol('a') assert gruntz(x - log(1 + exp(x)), x) == 0 assert gruntz(x - log(a + exp(x)), x) == 0 assert gruntz(exp(x) / (1 + exp(x)), x) == 1 assert gruntz(exp(x) / (a + exp(x)), x) == 1 assert gruntz((3**x + 5**x)**(1 / x), x) == 5 # issue sympy/sympy#3463 assert gruntz(Ei(x + exp(-x)) * exp(-x) * x, x) == 1 assert gruntz(1 / li(x), x) == 0 assert gruntz(1 / Li(x), x) == 0 # issue diofant/diofant#56 assert gruntz((log(E + 1 / x) - 1)**(1 - sqrt(E + 1 / x)), x) == oo # issue sympy/sympy#9471 assert gruntz((((27**(log(x, 3)))) / x**3), x) == 1 assert gruntz((((27**(log(x, 3) + 1))) / x**3), x) == 27 # issue sympy/sympy#9449 y = Symbol('y') assert gruntz(x * (abs(1 / x + y) - abs(y - 1 / x)) / 2, x) == sign(y) # issue sympy/sympy#8481 assert gruntz(m**x * exp(-m) / factorial(x), x) == 0 # issue sympy/sympy#4187 assert gruntz(exp(1 / x) * log(1 / x) - Ei(1 / x), x) == -EulerGamma assert gruntz(exp(x) * log(x) - Ei(x), x) == oo # issue sympy/sympy#10382 assert gruntz(fibonacci(x + 1) / fibonacci(x), x) == GoldenRatio assert gruntz(zeta(x), x) == 1 assert gruntz(zeta(m) * zeta(x), x) == zeta(m)
def test_evalf_near_integers(): # Binet's formula def f(n): return ((1 + sqrt(5))**n)/(2**n * sqrt(5)) assert NS(f(5000) - fibonacci(5000), 10, maxn=1500) == '5.156009964e-1046' # Some near-integer identities from # http://mathworld.wolfram.com/AlmostInteger.html assert NS('sin(2017*2**(1/5))', 15) == '-1.00000000000000' assert NS('sin(2017*2**(1/5))', 20) == '-0.99999999999999997857' assert NS('1+sin(2017*2**(1/5))', 15) == '2.14322287389390e-17' assert NS('45 - 613*E/37 + 35/991', 15) == '6.03764498766326e-11'
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert lucas(x) == lucas(x, evaluate=False) assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2 * x assert fibonacci(x).rewrite( sqrt) == (GoldenRatio**x - cos(pi * x) / GoldenRatio**x) / sqrt(5) assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt) pytest.raises(ValueError, lambda: fibonacci(-2, x)) n = Symbol('n', integer=True) assert isinstance(fibonacci(n, x).rewrite(sqrt), fibonacci)
def test_Function(): assert mathematica_code(f(x, y, z)) == 'f[x, y, z]' assert mathematica_code(sin(x)**cos(x)) == 'Sin[x]^Cos[x]' assert mathematica_code(sign(x)) == 'Sign[x]' assert mathematica_code(atanh(x), user_functions={'atanh': 'ArcTanh'}) == 'ArcTanh[x]' assert (mathematica_code(meijerg( ((1, 1), (3, 4)), ((1, ), ()), x)) == 'MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]') assert (mathematica_code(hyper( (1, 2, 3), (3, 4), x)) == 'HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]') assert mathematica_code(Min(x, y)) == 'Min[x, y]' assert mathematica_code(Max(x, y)) == 'Max[x, y]' assert mathematica_code(Max(x, 2)) == 'Max[2, x]' # issue sympy/sympy#15344 assert mathematica_code(binomial(x, y)) == 'Binomial[x, y]' assert mathematica_code(log(x)) == 'Log[x]' assert mathematica_code(tan(x)) == 'Tan[x]' assert mathematica_code(cot(x)) == 'Cot[x]' assert mathematica_code(asin(x)) == 'ArcSin[x]' assert mathematica_code(acos(x)) == 'ArcCos[x]' assert mathematica_code(atan(x)) == 'ArcTan[x]' assert mathematica_code(acot(x)) == 'ArcCot[x]' assert mathematica_code(sinh(x)) == 'Sinh[x]' assert mathematica_code(cosh(x)) == 'Cosh[x]' assert mathematica_code(tanh(x)) == 'Tanh[x]' assert mathematica_code(coth(x)) == 'Coth[x]' assert mathematica_code(asinh(x)) == 'ArcSinh[x]' assert mathematica_code(acosh(x)) == 'ArcCosh[x]' assert mathematica_code(atanh(x)) == 'ArcTanh[x]' assert mathematica_code(acoth(x)) == 'ArcCoth[x]' assert mathematica_code(sech(x)) == 'Sech[x]' assert mathematica_code(csch(x)) == 'Csch[x]' assert mathematica_code(erf(x)) == 'Erf[x]' assert mathematica_code(erfi(x)) == 'Erfi[x]' assert mathematica_code(erfc(x)) == 'Erfc[x]' assert mathematica_code(conjugate(x)) == 'Conjugate[x]' assert mathematica_code(re(x)) == 'Re[x]' assert mathematica_code(im(x)) == 'Im[x]' assert mathematica_code(polygamma(x, y)) == 'PolyGamma[x, y]' assert mathematica_code(factorial(x)) == 'Factorial[x]' assert mathematica_code(factorial2(x)) == 'Factorial2[x]' assert mathematica_code(rf(x, y)) == 'Pochhammer[x, y]' assert mathematica_code(gamma(x)) == 'Gamma[x]' assert mathematica_code(zeta(x)) == 'Zeta[x]' assert mathematica_code(Heaviside(x)) == 'UnitStep[x]' assert mathematica_code(fibonacci(x)) == 'Fibonacci[x]' assert mathematica_code(polylog(x, y)) == 'PolyLog[x, y]' assert mathematica_code(loggamma(x)) == 'LogGamma[x]' assert mathematica_code(uppergamma(x, y)) == 'Gamma[x, y]' class MyFunc1(Function): @classmethod def eval(cls, x): pass class MyFunc2(Function): @classmethod def eval(cls, x, y): pass pytest.raises( ValueError, lambda: mathematica_code(MyFunc1(x), user_functions={'MyFunc1': ['Myfunc1']})) assert mathematica_code(MyFunc1(x), user_functions={'MyFunc1': 'Myfunc1'}) == 'Myfunc1[x]' assert mathematica_code( MyFunc2(x, y), user_functions={'MyFunc2': [(lambda *x: False, 'Myfunc2')]}) == 'MyFunc2[x, y]'