Пример #1
0
def test_sympyissue_8413():
    x = Symbol('x', extended_real=True)
    # we can't evaluate in general because non-reals are not
    # comparable: Min(floor(3.2 + I), 3.2 + I) -> ValueError
    assert Min(floor(x), x) == floor(x)
    assert Min(ceiling(x), x) == x
    assert Max(floor(x), x) == x
    assert Max(ceiling(x), x) == ceiling(x)
Пример #2
0
def test_piecewise_integrate_inequality_conditions():
    x, y = symbols("x y", real=True)
    c1, c2 = symbols("c1 c2", positive=True, real=True)
    g = Piecewise((0, c1*x > 1), (1, c1*x > 0), (0, True))
    assert integrate(g, (x, -oo, 0)) == 0
    assert integrate(g, (x, -5, 0)) == 0
    assert integrate(g, (x, 0, 5)) == Min(5, 1/c1)
    assert integrate(g, (x, 0, oo)) == 1/c1

    g = Piecewise((0, c1*x + c2*y > 1), (1, c1*x + c2*y > 0), (0, True))
    assert integrate(g, (x, -oo, 0)).subs(y, 0) == 0
    assert integrate(g, (x, -5, 0)).subs(y, 0) == 0
    assert integrate(g, (x, 0, 5)).subs(y, 0) == Min(5, 1/c1)
    assert integrate(g, (x, 0, oo)).subs(y, 0) == 1/c1
Пример #3
0
def test_supinf():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', extended_real=True)

    assert (Interval(0, 1) + FiniteSet(2)).sup == 2
    assert (Interval(0, 1) + FiniteSet(2)).inf == 0
    assert (Interval(0, 1) + FiniteSet(x)).sup == Max(1, x)
    assert (Interval(0, 1) + FiniteSet(x)).inf == Min(0, x)
    assert FiniteSet(5, 1, x).sup == Max(5, x)
    assert FiniteSet(5, 1, x).inf == Min(1, x)
    assert FiniteSet(5, 1, x, y).sup == Max(5, x, y)
    assert FiniteSet(5, 1, x, y).inf == Min(1, x, y)
    assert FiniteSet(5, 1, x, y, oo, -oo).sup == +oo
    assert FiniteSet(5, 1, x, y, oo, -oo).inf == -oo
    assert FiniteSet('Ham', 'Eggs').sup == Max('Ham', 'Eggs')
Пример #4
0
def test_rewrite_MaxMin_as_Heaviside():
    assert Max(0, x).rewrite(Heaviside) == x * Heaviside(x)
    assert Max(3, x).rewrite(Heaviside) == x*Heaviside(x - 3) + \
        3*Heaviside(-x + 3)
    assert Max(0, x+2, 2*x).rewrite(Heaviside) == \
        2*x*Heaviside(2*x)*Heaviside(x - 2) + \
        (x + 2)*Heaviside(-x + 2)*Heaviside(x + 2)

    assert Min(0, x).rewrite(Heaviside) == x * Heaviside(-x)
    assert Min(3, x).rewrite(Heaviside) == x*Heaviside(-x + 3) + \
        3*Heaviside(x - 3)
    assert Min(x, -x, -2).rewrite(Heaviside) == \
        x*Heaviside(-2*x)*Heaviside(-x - 2) - \
        x*Heaviside(2*x)*Heaviside(x - 2) \
        - 2*Heaviside(-x + 2)*Heaviside(x + 2)
Пример #5
0
def test_finite_basic():
    A = FiniteSet(1, 2, 3)
    B = FiniteSet(3, 4, 5)
    AorB = Union(A, B)
    AandB = A.intersect(B)
    assert A.is_subset(AorB) and B.is_subset(AorB)
    assert AandB.is_subset(A)
    assert AandB == FiniteSet(3)

    assert A.inf == 1 and A.sup == 3
    assert AorB.inf == 1 and AorB.sup == 5
    assert FiniteSet(x, 1, 5).sup == Max(x, 5)
    assert FiniteSet(x, 1, 5).inf == Min(x, 1)

    # issue 7335
    assert FiniteSet(S.EmptySet) != S.EmptySet
    assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3)
    assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3)

    # Ensure a variety of types can exist in a FiniteSet
    assert FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval)

    assert (A > B) is False
    assert (A >= B) is False
    assert (A < B) is False
    assert (A <= B) is False
    assert AorB > A and AorB > B
    assert AorB >= A and AorB >= B
    assert A >= A and A <= A
    assert A >= AandB and B >= AandB
    assert A > AandB and B > AandB
Пример #6
0
def test_image_Intersection():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', extended_real=True)
    assert (imageset(x, x**2,
                     Interval(-2, 0).intersection(Interval(x, y))) == Interval(
                         0, 4).intersection(
                             Interval(Min(x**2, y**2), Max(x**2, y**2))))
Пример #7
0
def test_AssocOp_Function():
    e = Min(-sqrt(3)*cos(pi/18)/6 +
            re(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                       sqrt(3)*I/18)))/3 + sin(pi/18)/2 + 2 +
            I*(-cos(pi/18)/2 - sqrt(3)*sin(pi/18)/6 +
               im(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3),
            re(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 -
            sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
            I*(im(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3 -
               sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))
    # the following should not raise a recursion error; it
    # should raise a value error because the first arg computes
    # a non-comparable (prec=1) imaginary part
    pytest.raises(ValueError, lambda: e.evalf(2, strict=False))
Пример #8
0
def test_interval_symbolic_end_points():
    a = Symbol('a', extended_real=True)

    assert Union(Interval(0, a), Interval(0, 3)).sup == Max(a, 3)
    assert Union(Interval(a, 0), Interval(-3, 0)).inf == Min(-3, a)

    assert Interval(0, a).contains(1) == LessThan(1, a)
Пример #9
0
def test_AssocOp_Function():
    e = Min(-sqrt(3)*cos(pi/18)/6 +
            re(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                       sqrt(3)*I/18)))/3 + sin(pi/18)/2 + 2 +
            I*(-cos(pi/18)/2 - sqrt(3)*sin(pi/18)/6 +
               im(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3),
            re(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 -
            sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
            I*(im(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3 -
               sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))
    # the following should not raise a recursion error; it
    # should raise a value error because the first arg computes
    # a non-comparable (prec=1) imaginary part
    pytest.raises(ValueError, lambda: e.evalf(2, strict=False))
Пример #10
0
def test_supinf():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', extended_real=True)

    assert (Interval(0, 1) + FiniteSet(2)).sup == 2
    assert (Interval(0, 1) + FiniteSet(2)).inf == 0
    assert (Interval(0, 1) + FiniteSet(x)).sup == Max(1, x)
    assert (Interval(0, 1) + FiniteSet(x)).inf == Min(0, x)
    assert FiniteSet(5, 1, x).sup == Max(5, x)
    assert FiniteSet(5, 1, x).inf == Min(1, x)
    assert FiniteSet(5, 1, x, y).sup == Max(5, x, y)
    assert FiniteSet(5, 1, x, y).inf == Min(1, x, y)
    assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).sup == \
        S.Infinity
    assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).inf == \
        S.NegativeInfinity
    assert FiniteSet('Ham', 'Eggs').sup == Max('Ham', 'Eggs')
Пример #11
0
def test_solve_linear_inequalities():
    eqs = [x >= 0, 2 * x + 4 * y <= 14, x - 2 * y <= 1]
    ans = ((x >= Integer(0)) & (x <= Integer(4)) &
           (y >= x / 2 - Rational(1, 2)) & (y <= -x / 2 + Rational(7, 2)))

    assert reduce_inequalities(eqs) == ans

    eqs = [x + y >= 4, x <= 1, y <= 1]

    assert reduce_inequalities(eqs) == false

    eqs = [x + 2 * y <= 3, 2 * x + y <= 5]

    assert reduce_inequalities(eqs) == (y <= Min(-2 * x + 5,
                                                 -x / 2 + Rational(3, 2)))

    eqs = [x + 2 * y < 3, 2 * x + y < 5]

    assert reduce_inequalities(eqs) == (y < Min(-2 * x + 5,
                                                -x / 2 + Rational(3, 2)))

    eqs = [x + 2 * y <= 3, 2 * x + y < 5]
    ans = (((y <= -x / 2 + Rational(3, 2)) &
            (-x / 2 + Rational(3, 2) < -2 * x + 5)) |
           ((y < -2 * x + 5) & (-2 * x + 5 <= -x / 2 + Rational(3, 2))))

    assert reduce_inequalities(eqs) == ans

    eqs = [x + 2 * y >= 3, 2 * x + y >= 5]

    assert reduce_inequalities(eqs) == (y >= Max(-2 * x + 5,
                                                 -x / 2 + Rational(3, 2)))

    eqs = [x + 2 * y > 3, 2 * x + y > 5]

    assert reduce_inequalities(eqs) == (y > Max(-2 * x + 5,
                                                -x / 2 + Rational(3, 2)))

    eqs = [x + 2 * y >= 3, 2 * x + y > 5]
    ans = (((y >= -x / 2 + Rational(3, 2)) &
            (-x / 2 + Rational(3, 2) > -2 * x + 5)) |
           ((y > -2 * x + 5) & (-2 * x + 5 >= -x / 2 + Rational(3, 2))))

    assert reduce_inequalities(eqs) == ans
Пример #12
0
def test_definite_integrals_abs():
    # issue sympy/sympy#8430
    assert integrate(abs(x), (x, 0, 1)) == Rational(1, 2)
    # issue sympy/sympy#7165
    r = Symbol('r', real=True)
    assert (integrate(abs(x - r**2),
                      (x, 0, 2)) == r**2 * Max(0, Min(2, r**2)) +
            r**2 * Min(2, r**2) - 2 * r**2 - Max(0, Min(2, r**2))**2 / 2 -
            Min(2, r**2)**2 / 2 + 2)
    # issue sympy/sympy#8733
    assert integrate(abs(x + 1), (x, 0, 1)) == Rational(3, 2)

    e = x * abs(x**2 - 9)
    assert integrate(e, (x, -2, 2)) == 0
    assert integrate(e, (x, -1, 2)) == Rational(39, 4)
    assert integrate(e, (x, -2, 7)) == Rational(1625, 4)
    assert integrate(e, (x, -3, 11)) == 3136
    assert integrate(e, (x, -17, -2)) == Rational(-78425, 4)
    assert integrate(e, (x, -17, 20)) == Rational(74481, 4)
Пример #13
0
def test_gruntz_eval_special_slow():
    assert limit(gamma(x + 1)/sqrt(2*pi) - exp(-x)*(x**(x + Rational(1, 2)) +
                 x**(x - Rational(1, 2))/12), x, oo) == oo
    assert limit(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x, oo) == 0
    assert limit(exp(gamma(x - exp(-x))*exp(1/x)) - exp(gamma(x)), x, oo) == oo
    assert limit((Ei(x - exp(-exp(x))) - Ei(x)) *
                 exp(-x)*exp(exp(x))*x, x, oo) == -1
    assert limit(exp((log(2) + 1)*x)*(zeta(x + exp(-x)) - zeta(x)),
                 x, oo) == -log(2)
    # TODO 8.36 (bessel)
    assert limit(Max(x, exp(x))/log(Min(exp(-x), exp(-exp(x)))), x, oo) == -1
Пример #14
0
def test_Max():
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    p = Symbol('p', positive=True)
    r = Symbol('r', extended_real=True)

    assert Max(5, 4) == 5

    # lists

    pytest.raises(ValueError, Max)
    assert Max(x, y) == Max(y, x)
    assert Max(x, y, z) == Max(z, y, x)
    assert Max(x, Max(y, z)) == Max(z, y, x)
    assert Max(x, Min(y, oo)) == Max(x, y)
    assert Max(n, -oo, n_, p, 2) == Max(p, 2)
    assert Max(n, -oo, n_, p) == p
    assert Max(2, x, p, n, -oo, -oo, n_, p, 2) == Max(2, x, p)
    assert Max(0, x, 1, y) == Max(1, x, y)
    assert Max(r, r + 1, r - 1) == 1 + r
    assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000)
    assert Max(cos(x), sin(x)) == Max(sin(x), cos(x))
    assert Max(cos(x), sin(x)).subs({x: 1}) == sin(1)
    assert Max(cos(x), sin(x)).subs({x: Rational(1, 2)}) == cos(Rational(1, 2))
    pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs({x: I}))
    pytest.raises(ValueError, lambda: Max(I))
    pytest.raises(ValueError, lambda: Max(I, x))
    pytest.raises(ValueError, lambda: Max(zoo, 1))
    # interesting:
    # Max(n, -oo, n_,  p, 2) == Max(p, 2)
    # True
    # Max(n, -oo, n_,  p, 1000) == Max(p, 1000)
    # False

    assert Max(1, x).diff(x) == Heaviside(x - 1)
    assert Max(x, 1).diff(x) == Heaviside(x - 1)
    assert Max(x**2, 1 + x, 1).diff(x) == \
        2*x*Heaviside(x**2 - Max(1, x + 1)) \
        + Heaviside(x - Max(1, x**2) + 1)

    pytest.raises(ArgumentIndexError, lambda: Max(1, x).fdiff(3))

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Max(a, b) should be real
    assert Max(a, b).is_extended_real

    # issue sympy/sympy#7233
    e = Max(0, x)
    assert e.evalf(strict=False).args == (0, x)

    assert Max(2 * a * b * sqrt(2), a * b * 6).simplify() == 6 * a * b
Пример #15
0
def get_initial_supports_for_variable_powers(
        var_powers: Iterable[Tuple[Symbol, Number]], program: Program):
    """
    Returns lower and upper bounds for the initial support of variable powers
    """
    supports = {}
    for v, p in var_powers:
        if program.initial_values[v].is_random_var:
            supports[v**p] = program.initial_values[v].random_var.get_support(
                p)
        else:
            values = [b[0]**p for b in program.initial_values[v].branches]
            supports[v**p] = Min(*values), Max(*values)

    return supports
Пример #16
0
def test_inverse_mellin_transform():
    IMT = inverse_mellin_transform

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)
Пример #17
0
def test_finite_basic():
    assert isinstance(FiniteSet(evaluate=False), FiniteSet)

    A = FiniteSet(1, 2, 3)
    B = FiniteSet(3, 4, 5)
    AorB = Union(A, B)
    AandB = A.intersection(B)
    assert A.is_subset(AorB) and B.is_subset(AorB)
    assert AandB.is_subset(A)
    assert AandB == FiniteSet(3)

    assert A.inf == 1 and A.sup == 3
    assert AorB.inf == 1 and AorB.sup == 5
    assert FiniteSet(x, 1, 5).sup == Max(x, 5)
    assert FiniteSet(x, 1, 5).inf == Min(x, 1)

    # issue sympy/sympy#7335
    assert FiniteSet(S.EmptySet) != S.EmptySet
    assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3)
    assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3)

    # Ensure a variety of types can exist in a FiniteSet
    assert FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval)

    assert (A > B) is False
    assert (A >= B) is False
    assert (A < B) is False
    assert (A <= B) is False
    assert AorB > A and AorB > B
    assert AorB >= A and AorB >= B
    assert A >= A and A <= A  # pylint: disable=comparison-with-itself
    assert A >= AandB and B >= AandB
    assert A > AandB and B > AandB

    assert (FiniteSet(pi, E).evalf() == FiniteSet(
        Float('2.7182818284590451', dps=15), Float('3.1415926535897931',
                                                   dps=15)))

    # issue sympy/sympy#10337
    assert (FiniteSet(2) == 3) is False
    assert (FiniteSet(2) != 3) is True

    pytest.raises(TypeError, lambda: FiniteSet(2) < 3)
    pytest.raises(TypeError, lambda: FiniteSet(2) <= 3)
    pytest.raises(TypeError, lambda: FiniteSet(2) > 3)
    pytest.raises(TypeError, lambda: FiniteSet(2) >= 3)
Пример #18
0
    def get_support(self, k=1):
        if self.distribution == 'bernoulli':
            return sympify(0), sympify(1)

        if self.distribution == 'geometric':
            return sympify(1), oo

        if self.distribution == 'exponential':
            return sympify(0), oo

        if self.distribution == 'beta':
            return sympify(0), sympify(1)

        if self.distribution == 'uniform':
            l, u = self.parameters
            return interval_to_power(l, u, k)

        if self.distribution == 'chi-squared':
            return sympify(0), oo

        if self.distribution == 'rayleigh':
            return sympify(0), oo

        if self.distribution == 'symbolic-support':
            l, u = self.parameters
            return interval_to_power(l, u, k)

        if self.distribution == 'gauss':
            return interval_to_power(-oo, oo, k)

        if self.distribution == 'laplace':
            return interval_to_power(-oo, oo, k)

        if self.distribution == 'binomial':
            n, _ = self.parameters
            return interval_to_power(0, n, k)

        if self.distribution == 'hypergeometric':
            N, K, n = self.parameters
            return interval_to_power(Max(0, n + K - N), Min(n, K), k)
Пример #19
0
def test_finite_basic():
    assert isinstance(FiniteSet(evaluate=False), FiniteSet)

    A = FiniteSet(1, 2, 3)
    B = FiniteSet(3, 4, 5)
    AorB = Union(A, B)
    AandB = A.intersection(B)
    assert A.is_subset(AorB) and B.is_subset(AorB)
    assert AandB.is_subset(A)
    assert AandB == FiniteSet(3)

    assert A.inf == 1 and A.sup == 3
    assert AorB.inf == 1 and AorB.sup == 5
    assert FiniteSet(x, 1, 5).sup == Max(x, 5)
    assert FiniteSet(x, 1, 5).inf == Min(x, 1)

    # issue sympy/sympy#7335
    assert FiniteSet(S.EmptySet) != S.EmptySet
    assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3)
    assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3)

    # Ensure a variety of types can exist in a FiniteSet
    assert FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval)

    assert (A > B) is False
    assert (A >= B) is False
    assert (A < B) is False
    assert (A <= B) is False
    assert AorB > A and AorB > B
    assert AorB >= A and AorB >= B
    assert A >= A and A <= A
    assert A >= AandB and B >= AandB
    assert A > AandB and B > AandB

    assert (FiniteSet(pi, E).evalf() ==
            FiniteSet(Float('2.7182818284590451', prec=15),
                      Float('3.1415926535897931', prec=15)))
Пример #20
0
def test_inverse_mellin_transform():
    IMT = inverse_mellin_transform

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    # issue sympy/sympy#8882

    # This is the original test.
    # r = Symbol('r')
    # psi = 1/r*sin(r)*exp(-(a0*r))
    # h = -1/2*diff(psi, r, r) - 1/r*psi
    # f = 4*pi*psi*h*r**2
    # assert integrate(f, (r, -oo, 3), meijerg=True).has(Integral) == True

    # To save time, only the critical part is included.
    F = -a**(-s + 1)*(4 + 1/a**2)**(-s/2)*sqrt(1/a**2)*exp(-s*I*pi) * \
        sin(s*atan(sqrt(1/a**2)/2))*gamma(s)
    pytest.raises(
        IntegralTransformError, lambda: inverse_mellin_transform(
            F, s, x, (-1, oo), **{
                'as_meijerg': True,
                'needeval': True
            }))
Пример #21
0
def test_aresame_ordering():
    e = Min(3, z)
    s = (Min(z, 3), 3)
    assert e.subs(*s) == 3
Пример #22
0
def test_Min():
    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 isinstance(Min(n, n_), Min)
    assert isinstance(Min(nn, nn_), Min)
    assert isinstance(Min(np, np_), Min)
    assert isinstance(Min(p, p_), 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(zoo, 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)

    pytest.raises(ArgumentIndexError, lambda: Min(1, x).fdiff(3))

    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 sympy/sympy#7619
    f = Function('f')
    assert Min(1, 2 * Min(f(1), 2))  # doesn't fail

    # issue sympy/sympy#7233
    e = Min(0, x)
    assert e.evalf == e.n
    assert e.evalf(strict=False).args == (0, x)
Пример #23
0
def test_mellin_transform():
    from diofant import Max, Min
    MT = mellin_transform

    bpos = symbols('b', positive=True)

    # 8.4.2
    assert MT(x**nu*Heaviside(x - 1), x, s) == \
        (-1/(nu + s), (-oo, -re(nu)), True)
    assert MT(x**nu*Heaviside(1 - x), x, s) == \
        (1/(nu + s), (-re(nu), oo), True)

    assert MT((1 - x)**(beta - 1)*Heaviside(1 - x), x, s) == \
        (gamma(beta)*gamma(s)/gamma(beta + s), (0, oo), re(-beta) < 0)
    assert MT((x - 1)**(beta - 1)*Heaviside(x - 1), x, s) == \
        (gamma(beta)*gamma(1 - beta - s)/gamma(1 - s),
            (-oo, -re(beta) + 1), re(-beta) < 0)

    assert MT((1 + x)**(-rho), x, s) == \
        (gamma(s)*gamma(rho - s)/gamma(rho), (0, re(rho)), True)

    # TODO also the conditions should be simplified
    assert MT(abs(1 - x)**(-rho), x,
              s) == (cos(pi * (rho / 2 - s)) * gamma(s) * gamma(rho - s) /
                     (cos(pi * rho / 2) * gamma(rho)), (0, re(rho)),
                     And(re(rho) - 1 < 0,
                         re(rho) < 1))
    mt = MT((1 - x)**(beta - 1) * Heaviside(1 - x) + a *
            (x - 1)**(beta - 1) * Heaviside(x - 1), x, s)
    assert mt[1], mt[2] == ((0, -re(beta) + 1), True)

    assert MT((x**a - b**a)/(x - b), x, s)[0] == \
        pi*b**(a + s - 1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s)))
    assert MT((x**a - bpos**a)/(x - bpos), x, s) == \
        (pi*bpos**(a + s - 1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s))),
            (Max(-re(a), 0), Min(1 - re(a), 1)), True)

    expr = (sqrt(x + b**2) + b)**a
    assert MT(expr.subs(b, bpos), x, s) == \
        (-a*(2*bpos)**(a + 2*s)*gamma(s)*gamma(-a - 2*s)/gamma(-a - s + 1),
         (0, -re(a)/2), True)
    expr = (sqrt(x + b**2) + b)**a / sqrt(x + b**2)
    assert MT(expr.subs(b, bpos), x, s) == \
        (2**(a + 2*s)*bpos**(a + 2*s - 1)*gamma(s)
                                         * gamma(1 - a - 2*s)/gamma(1 - a - s),
            (0, -re(a)/2 + Rational(1, 2)), True)

    # 8.4.2
    assert MT(exp(-x), x, s) == (gamma(s), (0, oo), True)
    assert MT(exp(-1 / x), x, s) == (gamma(-s), (-oo, 0), True)

    # 8.4.5
    assert MT(log(x)**4 * Heaviside(1 - x), x, s) == (24 / s**5, (0, oo), True)
    assert MT(log(x)**3 * Heaviside(x - 1), x, s) == (6 / s**4, (-oo, 0), True)
    assert MT(log(x + 1), x, s) == (pi / (s * sin(pi * s)), (-1, 0), True)
    assert MT(log(1 / x + 1), x, s) == (pi / (s * sin(pi * s)), (0, 1), True)
    assert MT(log(abs(1 - x)), x, s) == (pi / (s * tan(pi * s)), (-1, 0), True)
    assert MT(log(abs(1 - 1 / x)), x,
              s) == (pi / (s * tan(pi * s)), (0, 1), True)

    # TODO we cannot currently do these (needs summation of 3F2(-1))
    #      this also implies that they cannot be written as a single g-function
    #      (although this is possible)
    mt = MT(log(x) / (x + 1), x, s)
    assert mt[1:] == ((0, 1), True)
    assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg)
    mt = MT(log(x)**2 / (x + 1), x, s)
    assert mt[1:] == ((0, 1), True)
    assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg)
    mt = MT(log(x) / (x + 1)**2, x, s)
    assert mt[1:] == ((0, 2), True)
    assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg)

    # 8.4.14
    assert MT(erf(sqrt(x)), x, s) == \
        (-gamma(s + Rational(1, 2))/(sqrt(pi)*s), (-Rational(1, 2), 0), True)
Пример #24
0
def test_inverse_mellin_transform():
    from diofant import (sin, simplify, Max, Min, expand, powsimp, exp_polar,
                         cos, cot)
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x)
    assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \
        (x**2 + 1)*Heaviside(1 - x)/(4*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x

    # test factorisation of polys
    r = symbols('r', extended_real=True)
    assert IMT(1/(s**2 + 1), s, exp(-x), (None, oo)
              ).subs(x, r).rewrite(sin).simplify() \
        == sin(r)*Heaviside(1 - exp(-r))

    # test multiplicative substitution
    _a, _b = symbols('a b', positive=True)
    assert IMT(_b**(-s / _a) * factorial(s / _a) / s, s, x,
               (0, oo)) == exp(-_b * x**_a)
    assert IMT(factorial(_a / _b + s / _b) / (_a + s), s, x,
               (-_a, oo)) == x**_a * exp(-x**_b)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    nu = symbols('nu', extended_real=True, finite=True)
    assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1)
    assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
        == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s),
                         s, x, (-oo, None))) \
        == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \
        == (1/(x + 1))**rho
    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s)
                        * gamma(-c/2 - s)/gamma(1 - c - s),
                        s, x, (0, -re(c)/2))) == \
        (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s)
                        / gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
        b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) +
        b**2 + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s)
                        / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
        b**c*(sqrt(1 + x/b**2) + 1)**c

    # Section 8.4.5
    assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
        log(x)**3*Heaviside(x - 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi / (s * sin(2 * pi * s)), s, x,
               (-Rational(1, 2), 0)) == log(sqrt(x) + 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x)

    # TODO
    def mysimp(expr):
        from diofant import expand, logcombine, powsimp
        return expand(powsimp(logcombine(expr, force=True),
                              force=True,
                              deep=True),
                      force=True).replace(exp_polar, exp)

    assert mysimp(mysimp(IMT(pi / (s * tan(pi * s)), s, x, (-1, 0)))) in [
        log(1 - x) * Heaviside(1 - x) + log(x - 1) * Heaviside(x - 1),
        log(x) * Heaviside(x - 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1)
    ]
    # test passing cot
    assert mysimp(IMT(pi * cot(pi * s) / s, s, x, (0, 1))) in [
        log(1 / x - 1) * Heaviside(1 - x) + log(1 - 1 / x) * Heaviside(x - 1),
        -log(x) * Heaviside(-x + 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1),
    ]

    # 8.4.14
    assert IMT(-gamma(s + Rational(1, 2))/(sqrt(pi)*s), s, x, (-Rational(1, 2), 0)) == \
        erf(sqrt(x))

    # 8.4.19
    assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, Rational(3, 4)))) \
        == besselj(a, 2*sqrt(x))
    assert simplify(IMT(2**a*gamma(Rational(1, 2) - 2*s)*gamma(s + (a + 1)/2)
                      / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-(re(a) + 1)/2, Rational(1, 4)))) == \
        sin(sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(Rational(1, 2) - 2*s)
                      / (gamma(Rational(1, 2) - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-re(a)/2, Rational(1, 4)))) == \
        cos(sqrt(x))*besselj(a, sqrt(x))
    # TODO this comes out as an amazing mess, but simplifies nicely
    assert simplify(IMT(gamma(a + s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                      s, x, (-re(a), Rational(1, 2)))) == \
        besselj(a, sqrt(x))**2
    assert simplify(IMT(gamma(s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                      s, x, (0, Rational(1, 2)))) == \
        besselj(-a, sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
                      / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
                         * gamma(a/2 + b/2 - s + 1)),
                      s, x, (-(re(a) + re(b))/2, Rational(1, 2)))) == \
        besselj(a, sqrt(x))*besselj(b, sqrt(x))

    # Section 8.4.20
    # TODO this can be further simplified!
    assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) *
                    gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) /
                    (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
                    s, x,
                    (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), Rational(1, 2)))) == \
                    besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) -
                    besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b)
    # TODO more

    # for coverage

    assert IMT(pi / cos(pi * s), s, x,
               (0, Rational(1, 2))) == sqrt(x) / (x + 1)
Пример #25
0
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]'
Пример #26
0
def test_piecewise_integrate():
    x, y = symbols('x y', real=True)

    # XXX Use '<=' here! '>=' is not yet implemented ..
    f = Piecewise(((x - 2)**2, 0 <= x), (1, True))
    assert integrate(f, (x, -2, 2)) == Rational(14, 3)

    g = Piecewise(((x - 5)**5, 4 <= x), (f, True))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == Rational(43, 6)

    g = Piecewise(((x - 5)**5, 4 <= x), (f, x < 4))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == Rational(43, 6)

    g = Piecewise(((x - 5)**5, 2 <= x), (f, x < 2))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(701, 6)

    g = Piecewise(((x - 5)**5, 2 <= x), (f, True))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(701, 6)

    g = Piecewise(((x - 5)**5, 2 <= x), (2 * f, True))
    assert integrate(g, (x, -2, 2)) == 2 * Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(673, 6)

    g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
    assert integrate(g, (x, -1, 1)) == 0

    g = Piecewise((1, x - y < 0), (0, True))
    assert integrate(g, (y, -oo, 0)) == -Min(0, x)
    assert integrate(g, (y, 0, oo)) == oo - Max(0, x)
    assert integrate(g, (y, -oo, oo)) == oo - x

    g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
    assert integrate(g, (x, -5, 1)) == Rational(1, 2)
    assert integrate(g, (x, -5, y)).subs(y, 1) == Rational(1, 2)
    assert integrate(g, (x, y, 1)).subs(y, -5) == Rational(1, 2)
    assert integrate(g, (x, 1, -5)) == -Rational(1, 2)
    assert integrate(g, (x, 1, y)).subs(y, -5) == -Rational(1, 2)
    assert integrate(g, (x, y, -5)).subs(y, 1) == -Rational(1, 2)
    assert integrate(g, (x, -5, y)) == Piecewise(
        (0, y < 0), (y**2 / 2, y <= 1), (y - 0.5, True))
    assert integrate(g, (x, y, 1)) == Piecewise(
        (0.5, y < 0), (0.5 - y**2 / 2, y <= 1), (1 - y, True))

    g = Piecewise((1 - x, Interval(0, 1).contains(x)),
                  (1 + x, Interval(-1, 0).contains(x)), (0, True))
    assert integrate(g, (x, -5, 1)) == 1
    assert integrate(g, (x, -5, y)).subs(y, 1) == 1
    assert integrate(g, (x, y, 1)).subs(y, -5) == 1
    assert integrate(g, (x, 1, -5)) == -1
    assert integrate(g, (x, 1, y)).subs(y, -5) == -1
    assert integrate(g, (x, y, -5)).subs(y, 1) == -1
    assert integrate(g, (x, -5, y)) == Piecewise(
        (-y**2 / 2 + y + 0.5, Interval(0, 1).contains(y)),
        (y**2 / 2 + y + 0.5, Interval(-1, 0).contains(y)), (0, y <= -1),
        (1, True))
    assert integrate(g, (x, y, 1)) == Piecewise(
        (y**2 / 2 - y + 0.5, Interval(0, 1).contains(y)),
        (-y**2 / 2 - y + 0.5, Interval(-1, 0).contains(y)), (1, y <= -1),
        (0, True))

    g = Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0), (1 + x, True))
    assert integrate(g, (x, -5, 1)) == 1
    assert integrate(g, (x, -5, y)).subs(y, 1) == 1
    assert integrate(g, (x, y, 1)).subs(y, -5) == 1
    assert integrate(g, (x, 1, -5)) == -1
    assert integrate(g, (x, 1, y)).subs(y, -5) == -1
    assert integrate(g, (x, y, -5)).subs(y, 1) == -1
    assert integrate(g, (x, -5, y)) == Piecewise((0, y <= -1), (1, y >= 1),
                                                 (-y**2 / 2 + y + 0.5, y > 0),
                                                 (y**2 / 2 + y + 0.5, True))
    assert integrate(g, (x, y, 1)) == Piecewise((1, y <= -1), (0, y >= 1),
                                                (y**2 / 2 - y + 0.5, y > 0),
                                                (-y**2 / 2 - y + 0.5, True))
Пример #27
0
def test_piecewise_integrate_symbolic_conditions():
    a = Symbol('a', real=True)
    b = Symbol('b', real=True)
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    p0 = Piecewise((0, Or(x < a, x > b)), (1, True))
    p1 = Piecewise((0, x < a), (0, x > b), (1, True))
    p2 = Piecewise((0, x > b), (0, x < a), (1, True))
    p3 = Piecewise((0, x < a), (1, x < b), (0, True))
    p4 = Piecewise((0, x > b), (1, x > a), (0, True))
    p5 = Piecewise((1, And(a < x, x < b)), (0, True))
    assert integrate(p0, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p1, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p2, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p3, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p4, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p5, (x, -oo, y)) == Min(b, y) - Min(a, b, y)
    assert integrate(p0, (x, y, oo)) == Max(a, b, y) - Max(a, y)
    assert integrate(p1, (x, y, oo)) == Max(a, b, y) - Max(a, y)
    assert integrate(p2, (x, y, oo)) == Max(a, b, y) - Max(a, y)
    assert integrate(p3, (x, y, oo)) == Max(a, b, y) - Max(a, y)
    assert integrate(p4, (x, y, oo)) == Max(a, b, y) - Max(a, y)
    assert integrate(p5, (x, y, oo)) == Max(a, b, y) - Max(a, y)

    assert integrate(p0, x) == Piecewise((0, Or(x < a, x > b)), (x, True))
    assert integrate(p1, x) == Piecewise((0, Or(x < a, x > b)), (x, True))
    assert integrate(p2, x) == Piecewise((0, Or(x < a, x > b)), (x, True))

    p1 = Piecewise((0, x < a), (0.5, x > b), (1, True))
    p2 = Piecewise((0.5, x > b), (0, x < a), (1, True))
    p3 = Piecewise((0, x < a), (1, x < b), (0.5, True))
    p4 = Piecewise((0.5, x > b), (1, x > a), (0, True))
    p5 = Piecewise((1, And(a < x, x < b)), (0.5, x > b), (0, True))
    assert integrate(p1,
                     (x, -oo, y)) == 0.5 * y + 0.5 * Min(b, y) - Min(a, b, y)
    assert integrate(p2,
                     (x, -oo, y)) == 0.5 * y + 0.5 * Min(b, y) - Min(a, b, y)
    assert integrate(p3,
                     (x, -oo, y)) == 0.5 * y + 0.5 * Min(b, y) - Min(a, b, y)
    assert integrate(p4,
                     (x, -oo, y)) == 0.5 * y + 0.5 * Min(b, y) - Min(a, b, y)
    assert integrate(p5,
                     (x, -oo, y)) == 0.5 * y + 0.5 * Min(b, y) - Min(a, b, y)
Пример #28
0
def test_Min_Max():
    # see sympy/sympy#10375
    assert lambdify((x, y, z), Min(x, y, z))(1, 2, 3) == 1
    assert lambdify((x, y, z), Max(x, y, z))(1, 2, 3) == 3
Пример #29
0
def test_sympyissue_11099():
    e = Min(x, y)
    s = {x: -2, y: 3}
    assert e.evalf(subs=s) == e.subs(s).evalf()
Пример #30
0
def test_aresame_ordering():
    e = Min(3, z)
    s = {Min(z, 3): 3}
    assert e.subs(s) == 3