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 _eval_sum_hyper(f, i, a): """ Returns (res, cond). Sums from a to oo. """ from diofant.functions import hyper from diofant.simplify import hyperexpand, hypersimp, fraction, simplify from diofant.polys.polytools import Poly, factor if a != 0: return _eval_sum_hyper(f.subs(i, i + a), i, 0) if f.subs(i, 0) == 0: if simplify(f.subs(i, Dummy('i', integer=True, positive=True))) == 0: return Integer(0), True return _eval_sum_hyper(f.subs(i, i + 1), i, 0) hs = hypersimp(f, i) if hs is None: return numer, denom = fraction(factor(hs)) top, topl = numer.as_coeff_mul(i) bot, botl = denom.as_coeff_mul(i) ab = [top, bot] factors = [topl, botl] params = [[], []] for k in range(2): for fac in factors[k]: mul = 1 if fac.is_Pow: mul = fac.exp fac = fac.base if not mul.is_Integer: return p = Poly(fac, i) if p.degree() != 1: return m, n = p.all_coeffs() ab[k] *= m**mul params[k] += [n / m] * mul # Add "1" to numerator parameters, to account for implicit n! in # hypergeometric series. ap = params[0] + [1] bq = params[1] x = ab[0] / ab[1] h = hyper(ap, bq, x) e = h try: e = hyperexpand(h) except PolynomialError: pass if e is S.NaN and h.convergence_statement: e = h return f.subs(i, 0) * e, h.convergence_statement
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_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]"
def test_call(): a, b, x = symbols('a, b, x', cls=Dummy) f = Hyper_Function([2, a], [b]) assert f(x) == hyper([2, a], [b], 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]"