def test_literal_evalf_is_number_is_zero_is_comparable(): from diofant.integrals.integrals import Integral from diofant.core.symbol import symbols from diofant.core.function import Function x = symbols('x') f = Function('f') # the following should not be changed without a lot of dicussion # `foo.is_number` should be equivalent to `not foo.free_symbols` # it should not attempt anything fancy; see is_zero, is_constant # and equals for more rigorous tests. assert f(1).is_number is True i = Integral(0, (x, x, x)) # expressions that are symbolically 0 can be difficult to prove # so in case there is some easy way to know if something is 0 # it should appear in the is_zero property for that object; # if is_zero is true evalf should always be able to compute that # zero assert i.n() == 0 assert i.is_zero assert i.is_number is False assert i.evalf(2, strict=False) == 0 # issue sympy/sympy#10272 n = sin(1)**2 + cos(1)**2 - 1 assert n.is_comparable is not True assert n.n(2).is_comparable is not True
def __new__(cls, *args): if len(args) == 5: args = [(args[0], args[1]), (args[2], args[3]), args[4]] if len(args) != 3: raise TypeError("args must be either as, as', bs, bs', z or " "as, bs, z") def tr(p): if len(p) != 2: raise TypeError("wrong argument") return TupleArg(_prep_tuple(p[0]), _prep_tuple(p[1])) arg0, arg1 = tr(args[0]), tr(args[1]) if Tuple(arg0, arg1).has(S.Infinity, S.ComplexInfinity, S.NegativeInfinity): raise ValueError("G-function parameters must be finite") if any((a - b).is_integer and (a - b).is_positive for a in arg0[0] for b in arg1[0]): raise ValueError("no parameter a1, ..., an may differ from " "any b1, ..., bm by a positive integer") # TODO should we check convergence conditions? return Function.__new__(cls, arg0, arg1, args[2])
def test_core_dynamicfunctions(): f = Function("f") check(f)
def __new__(cls, ap, bq, z): # TODO should we check convergence conditions? return Function.__new__(cls, _prep_tuple(ap), _prep_tuple(bq), z)
def test_core_dynamicfunctions(): # This fails because f is assumed to be a class at diofant.basic.function.f f = Function("f") check(f)
def findrecur(self, F=Function('F'), n=None): """Find a recurrence formula for the summand of the sum. Given a sum `f(n) = \sum_k F(n, k)`, where `F(n, k)` is doubly hypergeometric (that's, both `F(n + 1, k)/F(n, k)` and `F(n, k + 1)/F(n, k)` are rational functions of `n` and `k`), we find a recurrence for the summand `F(n, k)` of the form .. math:: \sum_{i=0}^I\sum_{j=0}^J a_{i,j}F(n - j, k - i) = 0 Examples ======== >>> from diofant import symbols, factorial, oo >>> n, k = symbols('n, k', integer=True) >>> s = Sum(factorial(n)/(factorial(k)*factorial(n - k)), (k, 0, oo)) >>> s.findrecur() -F(n, k) + F(n - 1, k) + F(n - 1, k - 1) Notes ===== We use Sister Celine's algorithm, see [1]_. References ========== .. [1] M. Petkovšek, H. S. Wilf, D. Zeilberger, A = B, 1996, Ch. 4. """ from diofant import expand_func, gamma, factor, Mul from diofant.polys import together from diofant.simplify import collect if len(self.variables) > 1: raise ValueError else: if self.limits[0][1:] != (S.Zero, S.Infinity): raise ValueError k = self.variables[0] if not n: try: n = (self.function.free_symbols - {k}).pop() except KeyError: raise ValueError a = Function('a') def f(i, j): return self.function.subs([(n, i), (k, j)]) I, J, step = 0, 1, 1 y, x, sols = S.Zero, [], {} while not any(v for a, v in sols.items()): if step % 2 != 0: dy = sum(a(I, j) * f(n - j, k - I) / f(n, k) for j in range(J)) dx = [a(I, j) for j in range(J)] I += 1 else: dy = sum(a(i, J) * f(n - J, k - i) / f(n, k) for i in range(I)) dx = [a(i, J) for i in range(I)] J += 1 step += 1 y += expand_func(dy.rewrite(gamma)) x += dx t = together(y) numer = t.as_numer_denom()[0] numer = Mul( *[t for t in factor(numer).as_coeff_mul()[1] if t.has(a)]) if not numer.is_rational_function(n, k): raise ValueError() z = collect(numer, k) eq = z.as_poly(k).all_coeffs() sols = dict(solve(eq, *x)) y = sum(a(i, j) * F(n - j, k - i) for i in range(I) for j in range(J)) y = y.subs(sols).subs(map(lambda a: (a, 1), x)) return y if y else None
def test_Min(): from diofant.abc import x, y, z n = Symbol('n', negative=True) n_ = Symbol('n_', negative=True) nn = Symbol('nn', nonnegative=True) nn_ = Symbol('nn_', nonnegative=True) p = Symbol('p', positive=True) p_ = Symbol('p_', positive=True) np = Symbol('np', nonpositive=True) np_ = Symbol('np_', nonpositive=True) assert Min(5, 4) == 4 assert Min(-oo, -oo) == -oo assert Min(-oo, n) == -oo assert Min(n, -oo) == -oo assert Min(-oo, np) == -oo assert Min(np, -oo) == -oo assert Min(-oo, 0) == -oo assert Min(0, -oo) == -oo assert Min(-oo, nn) == -oo assert Min(nn, -oo) == -oo assert Min(-oo, p) == -oo assert Min(p, -oo) == -oo assert Min(-oo, oo) == -oo assert Min(oo, -oo) == -oo assert Min(n, n) == n assert Min(n, np) == Min(n, np) assert Min(np, n) == Min(np, n) assert Min(n, 0) == n assert Min(0, n) == n assert Min(n, nn) == n assert Min(nn, n) == n assert Min(n, p) == n assert Min(p, n) == n assert Min(n, oo) == n assert Min(oo, n) == n assert Min(np, np) == np assert Min(np, 0) == np assert Min(0, np) == np assert Min(np, nn) == np assert Min(nn, np) == np assert Min(np, p) == np assert Min(p, np) == np assert Min(np, oo) == np assert Min(oo, np) == np assert Min(0, 0) == 0 assert Min(0, nn) == 0 assert Min(nn, 0) == 0 assert Min(0, p) == 0 assert Min(p, 0) == 0 assert Min(0, oo) == 0 assert Min(oo, 0) == 0 assert Min(nn, nn) == nn assert Min(nn, p) == Min(nn, p) assert Min(p, nn) == Min(p, nn) assert Min(nn, oo) == nn assert Min(oo, nn) == nn assert Min(p, p) == p assert Min(p, oo) == p assert Min(oo, p) == p assert Min(oo, oo) == oo assert Min(n, n_).func is Min assert Min(nn, nn_).func is Min assert Min(np, np_).func is Min assert Min(p, p_).func is Min # lists pytest.raises(ValueError, lambda: Min()) assert Min(x, y) == Min(y, x) assert Min(x, y, z) == Min(z, y, x) assert Min(x, Min(y, z)) == Min(z, y, x) assert Min(x, Max(y, -oo)) == Min(x, y) assert Min(p, oo, n, p, p, p_) == n assert Min(p_, n_, p) == n_ assert Min(n, oo, -7, p, p, 2) == Min(n, -7) assert Min(2, x, p, n, oo, n_, p, 2, -2, -2) == Min(-2, x, n, n_) assert Min(0, x, 1, y) == Min(0, x, y) assert Min(1000, 100, -100, x, p, n) == Min(n, x, -100) assert Min(cos(x), sin(x)) == Min(cos(x), sin(x)) assert Min(cos(x), sin(x)).subs(x, 1) == cos(1) assert Min(cos(x), sin(x)).subs(x, Rational(1, 2)) == sin(Rational(1, 2)) pytest.raises(ValueError, lambda: Min(cos(x), sin(x)).subs(x, I)) pytest.raises(ValueError, lambda: Min(I)) pytest.raises(ValueError, lambda: Min(I, x)) pytest.raises(ValueError, lambda: Min(S.ComplexInfinity, x)) assert Min(1, x).diff(x) == Heaviside(1 - x) assert Min(x, 1).diff(x) == Heaviside(1 - x) assert Min(0, -x, 1 - 2*x).diff(x) == -Heaviside(x + Min(0, -2*x + 1)) \ - 2*Heaviside(2*x + Min(0, -x) - 1) a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True) # a and b are both real, Min(a, b) should be real assert Min(a, b).is_extended_real # issue 7619 f = Function('f') assert Min(1, 2 * Min(f(1), 2)) # doesn't fail # issue 7233 e = Min(0, x) assert e.evalf == e.n assert e.n().args == (0, x)