def test_catalan(): n = Symbol('n', integer=True) m = Symbol('n', integer=True, positive=True) catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786] for i, c in enumerate(catalans): assert catalan(i) == c assert catalan(n).rewrite(factorial).subs(n, i) == c assert catalan(n).rewrite(Product).subs(n, i).doit() == c assert catalan(x) == catalan(x) assert catalan(2 * x).rewrite(binomial) == binomial(4 * x, 2 * x) / (2 * x + 1) assert catalan(Rational(1, 2)).rewrite(gamma) == 8 / (3 * pi) assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\ 8 / (3 * pi) assert catalan(3 * x).rewrite(gamma) == 4**( 3 * x) * gamma(3 * x + Rational(1, 2)) / (sqrt(pi) * gamma(3 * x + 2)) assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2, ), 1) assert catalan(n).rewrite(factorial) == factorial( 2 * n) / (factorial(n + 1) * factorial(n)) assert isinstance(catalan(n).rewrite(Product), catalan) assert isinstance(catalan(m).rewrite(Product), Product) assert diff(catalan(x), x) == (polygamma(0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4)) * catalan(x) assert catalan(x).evalf() == catalan(x) c = catalan(S.Half).evalf() assert str(c) == '0.848826363156775' c = catalan(I).evalf(3) assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
def test_catalan(): n = Symbol('n', integer=True) m = Symbol('n', integer=True, positive=True) catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786] for i, c in enumerate(catalans): assert catalan(i) == c assert catalan(n).rewrite(factorial).subs({n: i}) == c assert catalan(n).rewrite(Product).subs({n: i}).doit() == c assert catalan(x) == catalan(x) assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1) assert catalan(Rational(1, 2)).rewrite(gamma) == 8/(3*pi) assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\ 8 / (3 * pi) assert catalan(3*x).rewrite(gamma) == 4**( 3*x)*gamma(3*x + Rational(1, 2))/(sqrt(pi)*gamma(3*x + 2)) assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1) assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1) * factorial(n)) assert isinstance(catalan(n).rewrite(Product), catalan) assert isinstance(catalan(m).rewrite(Product), Product) assert diff(catalan(x), x) == (polygamma( 0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4))*catalan(x) assert catalan(x).evalf() == catalan(x) c = catalan(Rational(1, 2)).evalf() assert str(c) == '0.848826363156775' c = catalan(I).evalf(3) assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
def test_specfun(): for f in [besselj, bessely, besseli, besselk]: assert mcode(f(n, x)) == f.__name__ + '(n, x)' assert mcode(hankel1(n, x)) == 'besselh(n, 1, x)' assert mcode(hankel2(n, x)) == 'besselh(n, 2, x)' assert mcode(airyai(x)) == 'airy(0, x)' assert mcode(airyaiprime(x)) == 'airy(1, x)' assert mcode(airybi(x)) == 'airy(2, x)' assert mcode(airybiprime(x)) == 'airy(3, x)' assert mcode(uppergamma(n, x)) == 'gammainc(x, n, \'upper\')' assert mcode(lowergamma(n, x)) == 'gammainc(x, n, \'lower\')' assert mcode(jn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2' assert mcode(yn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2' assert mcode(Chi(x)) == 'coshint(x)' assert mcode(Ci(x)) == 'cosint(x)' assert mcode(laguerre(n, x)) == 'laguerreL(n, x)' assert mcode(li(x)) == 'logint(x)' assert mcode(loggamma(x)) == 'gammaln(x)' assert mcode(polygamma(n, x)) == 'psi(n, x)' assert mcode(Shi(x)) == 'sinhint(x)' assert mcode(Si(x)) == 'sinint(x)' assert mcode(LambertW(x)) == 'lambertw(x)' assert mcode(LambertW(x, n)) == 'lambertw(n, x)' assert mcode(zeta(x)) == 'zeta(x)' assert mcode(zeta(x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
def test_harmonic_limit(): n = Symbol("n") m = Symbol("m", positive=True) assert limit(harmonic(n, m + 1), n, oo) == zeta(m + 1) assert limit(harmonic(n, 2), n, oo) == pi**2/6 assert limit(harmonic(n, 3), n, oo) == -polygamma(2, 1)/2
def test_harmonic_limit(): n = Symbol("n") m = Symbol("m", positive=True) assert limit(harmonic(n, m + 1), n, oo) == zeta(m + 1) assert limit(harmonic(n, 2), n, oo) == pi**2 / 6 assert limit(harmonic(n, 3), n, oo) == -polygamma(2, 1) / 2
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic( n, 3).rewrite(polygamma) == polygamma(2, n + 1) / 2 - polygamma(2, 1) / 2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m * ( polygamma(m - 1, 1) - polygamma(m - 1, n + 1)) / factorial(m - 1) assert expand_func( harmonic(n + 4) ) == harmonic(n) + 1 / (n + 4) + 1 / (n + 3) + 1 / (n + 2) + 1 / (n + 1) assert expand_func(harmonic( n - 4)) == harmonic(n) - 1 / (n - 1) - 1 / (n - 2) - 1 / (n - 3) - 1 / n assert harmonic(n, m).rewrite("tractable") == harmonic( n, m).rewrite(polygamma).rewrite(gamma).rewrite("tractable") assert expand_func(harmonic(n, 2)).func is harmonic
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]" assert mcode(sign(x)) == "Sign[x]" assert mcode(atanh(x), user_functions={"atanh": "ArcTanh"}) == "ArcTanh[x]" assert (mcode(meijerg(((1, 1), (3, 4)), ((1, ), ()), x)) == "MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]") assert (mcode(hyper((1, 2, 3), (3, 4), x)) == "HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]") assert mcode(Min(x, y)) == "Min[x, y]" assert mcode(Max(x, y)) == "Max[x, y]" assert mcode(Max(x, 2)) == "Max[2, x]" # issue sympy/sympy#15344 assert mcode(binomial(x, y)) == "Binomial[x, y]" assert mcode(log(x)) == "Log[x]" assert mcode(tan(x)) == "Tan[x]" assert mcode(cot(x)) == "Cot[x]" assert mcode(asin(x)) == "ArcSin[x]" assert mcode(acos(x)) == "ArcCos[x]" assert mcode(atan(x)) == "ArcTan[x]" assert mcode(sinh(x)) == "Sinh[x]" assert mcode(cosh(x)) == "Cosh[x]" assert mcode(tanh(x)) == "Tanh[x]" assert mcode(coth(x)) == "Coth[x]" assert mcode(sech(x)) == "Sech[x]" assert mcode(csch(x)) == "Csch[x]" assert mcode(erfc(x)) == "Erfc[x]" assert mcode(conjugate(x)) == "Conjugate[x]" assert mcode(re(x)) == "Re[x]" assert mcode(im(x)) == "Im[x]" assert mcode(polygamma(x, y)) == "PolyGamma[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: mcode(myfunc1(x), user_functions={"myfunc1": ["Myfunc1"]})) assert mcode(myfunc1(x), user_functions={"myfunc1": "Myfunc1"}) == "Myfunc1[x]" assert mcode(myfunc2(x, y), user_functions={"myfunc2": [(lambda *x: False, "Myfunc2")] }) == "myfunc2[x, y]"
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n, 3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma).rewrite(gamma).rewrite("tractable") assert isinstance(expand_func(harmonic(n, 2)), harmonic) assert expand_func(harmonic(n + Rational(1, 2))) == expand_func(harmonic(n + Rational(1, 2))) assert expand_func(harmonic(Rational(-1, 2))) == harmonic(Rational(-1, 2)) assert expand_func(harmonic(x)) == harmonic(x)
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x) ** cos(x)) == "Sin[x]^Cos[x]" assert mcode(sign(x)) == "Sign[x]" assert mcode(atanh(x), user_functions={"atanh": "ArcTanh"}) == "ArcTanh[x]" assert (mcode(meijerg(((1, 1), (3, 4)), ((1,), ()), x)) == "MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]") assert (mcode(hyper((1, 2, 3), (3, 4), x)) == "HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]") assert mcode(Min(x, y)) == "Min[x, y]" assert mcode(Max(x, y)) == "Max[x, y]" assert mcode(Max(x, 2)) == "Max[2, x]" # issue sympy/sympy#15344 assert mcode(binomial(x, y)) == "Binomial[x, y]" assert mcode(log(x)) == "Log[x]" assert mcode(tan(x)) == "Tan[x]" assert mcode(cot(x)) == "Cot[x]" assert mcode(asin(x)) == "ArcSin[x]" assert mcode(acos(x)) == "ArcCos[x]" assert mcode(atan(x)) == "ArcTan[x]" assert mcode(sinh(x)) == "Sinh[x]" assert mcode(cosh(x)) == "Cosh[x]" assert mcode(tanh(x)) == "Tanh[x]" assert mcode(coth(x)) == "Coth[x]" assert mcode(sech(x)) == "Sech[x]" assert mcode(csch(x)) == "Csch[x]" assert mcode(erfc(x)) == "Erfc[x]" assert mcode(conjugate(x)) == "Conjugate[x]" assert mcode(re(x)) == "Re[x]" assert mcode(im(x)) == "Im[x]" assert mcode(polygamma(x, y)) == "PolyGamma[x, y]" assert mcode(factorial(x)) == "Factorial[x]" assert mcode(factorial2(x)) == "Factorial2[x]" assert mcode(rf(x, y)) == "Pochhammer[x, y]" assert mcode(gamma(x)) == "Gamma[x]" assert mcode(zeta(x)) == "Zeta[x]" assert mcode(asinh(x)) == "ArcSinh[x]" assert mcode(Heaviside(x)) == "UnitStep[x]" assert mcode(fibonacci(x)) == "Fibonacci[x]" assert mcode(polylog(x, y)) == "PolyLog[x, y]" assert mcode(atanh(x)) == "ArcTanh[x]" class myfunc1(Function): @classmethod def eval(cls, x): pass class myfunc2(Function): @classmethod def eval(cls, x, y): pass pytest.raises(ValueError, lambda: mcode(myfunc1(x), user_functions={"myfunc1": ["Myfunc1"]})) assert mcode(myfunc1(x), user_functions={"myfunc1": "Myfunc1"}) == "Myfunc1[x]" assert mcode(myfunc2(x, y), user_functions={"myfunc2": [(lambda *x: False, "Myfunc2")]}) == "myfunc2[x, y]"