Exemple #1
0
def test_euler_high_order():
    # an example from hep-th/0309038
    m = Symbol('m')
    k = Symbol('k')
    x = Function('x')
    y = Function('y')
    t = Symbol('t')
    L = (m * diff(x(t), t)**2 / 2 + m * diff(y(t), t)**2 / 2 -
         k * diff(x(t), t) * diff(y(t), t, t) +
         k * diff(y(t), t) * diff(x(t), t, t))
    assert euler(L, [x(t), y(t)]) == [
        Eq(2 * k * diff(y(t), t, t, t) - m * diff(x(t), t, t)),
        Eq(-2 * k * diff(x(t), t, t, t) - m * diff(y(t), t, t))
    ]

    w = Symbol('w')
    L = diff(x(t, w), t, w)**2 / 2
    assert euler(L) == [Eq(diff(x(t, w), t, t, w, w))]
Exemple #2
0
def test_ccode_Indexed_without_looking_for_contraction():
    len_y = 5
    y = IndexedBase('y', shape=(len_y, ))
    x = IndexedBase('x', shape=(len_y, ))
    Dy = IndexedBase('Dy', shape=(len_y - 1, ))
    i = Idx('i', len_y - 1)
    e = Eq(Dy[i], (y[i + 1] - y[i]) / (x[i + 1] - x[i]))
    code0 = ccode(e.rhs, assign_to=e.lhs, contract=False)
    assert code0 == 'Dy[i] = (y[%s] - y[i])/(x[%s] - x[i]);' % (i + 1, i + 1)
Exemple #3
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    p3 = piecewise_fold(p2)
    assert (p2.subs(x, -pi / 2) == 0.0)
    assert (p2.subs(x, 1) == 0.0)
    assert (p2.subs(x, -pi / 4) == 1.0)
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert (piecewise_fold(p4) == Piecewise(
        (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True)))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert (piecewise_fold(r1) == Not(x < 1))

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert (Piecewise((1, And(Not(x < 1), x < 0)), (0, True)))
Exemple #4
0
def test_univariate_relational_as_set():
    assert (x > 0).as_set() == Interval(0, oo, True)
    assert (x >= 0).as_set() == Interval(0, oo)
    assert (x < 0).as_set() == Interval(-oo, 0, False, True)
    assert (x <= 0).as_set() == Interval(-oo, 0)
    assert Eq(x, 0).as_set() == FiniteSet(0)
    assert Ne(x, 0).as_set() == Interval(-oo, 0, False, True) + Interval(0, oo, True)

    assert (x**2 >= 4).as_set() == Interval(-oo, -2) + Interval(2, oo)
Exemple #5
0
def runtest_autowrap_matrix_matrix(language, backend):
    expr = Eq(C[i, j], A[i, k] * B[k, j])
    matmat = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M1 = numpy.random.rand(10, 20)
    M2 = numpy.random.rand(20, 15)
    M3 = numpy.dot(M1, M2)
    assert numpy.sum(numpy.abs(M3 - matmat(M1, M2))) < 1e-13
Exemple #6
0
def test_simplify():
    assert simplify(x * (y + 1) - x * y - x + 1 < x) == (x > 1)
    assert simplify(Integer(1) < -x) == (x < -1)

    # issue sympy/sympy#10304
    d = -(3 * 2**pi)**(1 / pi) + 2 * 3**(1 / pi)
    assert d.is_real
    assert simplify(Eq(1 + I * d, 0)) is False
    assert simplify(Ne(1 + I * d, 0)) is True
Exemple #7
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / abs(p1), True))
    assert p2.subs({x: -pi / 2}) == 0.0
    assert p2.subs({x: 1}) == 0.0
    assert p2.subs({x: -pi / 4}) == 1.0
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert (piecewise_fold(p4) == Piecewise(
        (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True)))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert (piecewise_fold(r1) == Not(x < 1))

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert p7
    assert Piecewise((1, And(Not(x < 1), x < 0)), (0, True))
Exemple #8
0
def test_python_relational():
    assert python(Eq(x, y)) == 'e = Eq(x, y)'
    assert python(Ge(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x >= y"
    assert python(Le(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x <= y"
    assert python(Gt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x > y"
    assert python(Lt(x, y)) == "x = Symbol('x')\ny = Symbol('y')\ne = x < y"
    assert python(
        Ne(x / (y + 1),
           y**2)) in ['e = Ne(x/(1 + y), y**2)', 'e = Ne(x/(y + 1), y**2)']
Exemple #9
0
def test_fcode_Indexed_without_looking_for_contraction():
    len_y = 5
    y = IndexedBase('y', shape=(len_y,))
    x = IndexedBase('x', shape=(len_y,))
    Dy = IndexedBase('Dy', shape=(len_y - 1,))
    i = Idx('i', len_y - 1)
    e = Eq(Dy[i], (y[i + 1] - y[i])/(x[i + 1] - x[i]))
    code0 = fcode(e.rhs, assign_to=e.lhs, contract=False)
    assert code0.endswith('Dy(i) = (y(i + 1) - y(i))/(x(i + 1) - x(i))')
Exemple #10
0
def test_simplify_other():
    assert simplify(sin(x)**2 + cos(x)**2) == 1
    assert simplify(gamma(x + 1)/gamma(x)) == x
    assert simplify(sin(x)**2 + cos(x)**2 + factorial(x)/gamma(x)) == 1 + x
    assert simplify(
        Eq(sin(x)**2 + cos(x)**2, factorial(x)/gamma(x))) == Eq(x, 1)
    nc = symbols('nc', commutative=False)
    assert simplify(x + x*nc) == x*(1 + nc)
    # issue sympy/sympy#6123
    # f = exp(-I*(k*sqrt(t) + x/(2*sqrt(t)))**2)
    # ans = integrate(f, (k, -oo, oo), conds='none')
    ans = (I*(-pi*x*exp(-3*I*pi/4 + I*x**2/(4*t))*erf(x*exp(-3*I*pi/4) /
                                                      (2*sqrt(t)))/(2*sqrt(t)) + pi*x*exp(-3*I*pi/4 + I*x**2/(4*t)) /
              (2*sqrt(t)))*exp(-I*x**2/(4*t))/(sqrt(pi)*x) - I*sqrt(pi) *
           (-erf(x*exp(I*pi/4)/(2*sqrt(t))) + 1)*exp(I*pi/4)/(2*sqrt(t)))
    assert simplify(ans) == -(-1)**Rational(3, 4)*sqrt(pi)/sqrt(t)
    # issue sympy/sympy#6370
    assert simplify(2**(2 + x)/4) == 2**x
Exemple #11
0
def test_Eq():
    assert Eq(x**2, 0) == Eq(x**2, 0)
    assert Eq(x**2, 0) != Eq(x**2, 1)

    assert Eq(x) == Eq(x, 0)

    assert Eq(x, x)  # issue sympy/sympy#5719

    # issue sympy/sympy#6116
    p = Symbol('p', positive=True)
    assert Eq(p, 0) is false
Exemple #12
0
def test_autowrap_args():
    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y), backend='dummy', args=(x,)))
    f = autowrap(Eq(z, x + y), backend='dummy', args=(y, x))
    assert f() == str(x + y)
    assert f.args == 'y, x'
    assert f.returns == 'z'

    pytest.raises(CodeGenArgumentListError,
                  lambda: autowrap(Eq(z, x + y + z), backend='dummy', args=(x, y)))
    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == 'y, x, z'
    assert f.returns == 'z'

    f = autowrap(Eq(z, x + y + z), backend='dummy', args=(y, x, z))
    assert f() == str(x + y + z)
    assert f.args == 'y, x, z'
    assert f.returns == 'z'
Exemple #13
0
def test_deltasummation_mul_add_x_y_add_y_kd():
    assert ds((x + y) * (y + Kd(i, j)), (j, 1, 3)) == Piecewise(
        (3 * (x + y) * y + x + y, And(Integer(1) <= i, i <= 3)),
        (3 * (x + y) * y, True))
    assert ds((x + y)*(y + Kd(i, j)), (j, 1, 1)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 1)), ((x + y)*y, True))
    assert ds((x + y)*(y + Kd(i, j)), (j, 2, 2)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 2)), ((x + y)*y, True))
    assert ds((x + y)*(y + Kd(i, j)), (j, 3, 3)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 3)), ((x + y)*y, True))
    assert ds((x + y) * (y + Kd(i, j)), (j, 1, k)) == Piecewise(
        (k * (x + y) * y + x + y, And(Integer(1) <= i, i <= k)),
        (k * (x + y) * y, True))
    assert ds((x + y) * (y + Kd(i, j)), (j, k, 3)) == Piecewise(
        ((4 - k) * (x + y) * y + x + y, And(k <= i, i <= 3)),
        ((4 - k) * (x + y) * y, True))
    assert ds((x + y) * (y + Kd(i, j)), (j, k, l)) == Piecewise(
        ((l - k + 1) * (x + y) * y + x + y, And(k <= i, i <= l)),
        ((l - k + 1) * (x + y) * y, True))
Exemple #14
0
def test_matexpr():
    assert (x*A).shape == A.shape
    assert (x*A).__class__ == MatMul
    assert 2*A - A - A == ZeroMatrix(*A.shape)
    assert (A*B).shape == (n, l)
    assert A.equals(ZeroMatrix(3, 3)) is None

    # issue diofant/diofant#469
    expr = Eq(C, D)
    assert simplify(expr) == expr
Exemple #15
0
def runtest_ufuncify(language, backend):
    a, b, c = symbols('a b c')

    if backend == 'numpy':
        pytest.raises(ValueError, lambda: ufuncify((a, b), Eq(b, a + b), backend=backend))
        pytest.raises(ValueError, lambda: ufuncify((a, b, c), [Eq(b, a), Eq(c, a**2)], backend=backend))

    helpers = [('helper', a*b, (a, b)), ('spam', sin(a), (a,))]

    fabc = ufuncify((a, b, c), a*b + c, backend=backend)
    fabc_2 = ufuncify((a, b, c), a*b + c, backend=backend, helpers=helpers)
    facb = ufuncify((a, c, b), a*b + c, backend=backend)
    grid = numpy.linspace(-2, 2, 50)
    b = numpy.linspace(-5, 4, 50)
    c = numpy.linspace(-1, 1, 50)
    expected = grid*b + c
    numpy.testing.assert_allclose(fabc(grid, b, c), expected)
    numpy.testing.assert_allclose(fabc_2(grid, b, c), expected)
    numpy.testing.assert_allclose(facb(grid, c, b), expected)
Exemple #16
0
def test_deltasummation_mul_add_x_kd_add_y_kd():
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, 3)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, And(Integer(1) <= i, i <= 3)), (0, True)) +
        3 * (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, 1)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, Eq(i, 1)), (0, True)) + (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 2, 2)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, Eq(i, 2)), (0, True)) + (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 3, 3)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, Eq(i, 3)), (0, True)) + (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, 1, k)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, And(Integer(1) <= i, i <= k)), (0, True)) +
        k * (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, k, 3)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, And(k <= i, i <= 3)), (0, True)) + (4 - k) *
        (Kd(i, k) + x) * y)
    assert ds((x + Kd(i, k)) * (y + Kd(i, j)), (j, k, l)) == piecewise_fold(
        Piecewise((Kd(i, k) + x, And(k <= i, i <= l)), (0, True)) +
        (l - k + 1) * (Kd(i, k) + x) * y)
Exemple #17
0
def runtest_autowrap_matrix_vector(language, backend):
    x, y = symbols('x y', cls=IndexedBase)
    expr = Eq(y[i], A[i, j] * x[j])
    mv = autowrap(expr, language, backend)

    # compare with numpy's dot product
    M = numpy.random.rand(10, 20)
    x = numpy.random.rand(20)
    y = numpy.dot(M, x)
    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-13
Exemple #18
0
def test_pde_1st_linear_constant_coeff():
    f, F = map(Function, ['f', 'F'])
    u = f(x, y)
    eq = -2 * u.diff(x) + 4 * u.diff(y) + 5 * u - exp(x + 3 * y)
    sol = pdsolve(eq)
    assert sol == Eq(f(
        x, y), (F(4 * x + 2 * y) + exp(x / Integer(2) + 4 * y) / Integer(15)) *
                     exp(x / Integer(2) - y))
    assert classify_pde(eq) == ('1st_linear_constant_coeff',
                                '1st_linear_constant_coeff_Integral')
    assert checkpdesol(eq, sol)[0]

    eq = (u.diff(x) / u) + (u.diff(y) / u) + 1 - (exp(x + y) / u)
    sol = pdsolve(eq)
    assert sol == Eq(f(x, y),
                     F(x - y) * exp(-x / 2 - y / 2) + exp(x + y) / Integer(3))
    assert classify_pde(eq) == ('1st_linear_constant_coeff',
                                '1st_linear_constant_coeff_Integral')
    assert checkpdesol(eq, sol)[0]

    eq = 2 * u + -u.diff(x) + 3 * u.diff(y) + sin(x)
    sol = pdsolve(eq)
    assert sol == Eq(
        f(x, y),
        F(3 * x + y) * exp(x / Integer(5) - 3 * y / Integer(5)) -
        2 * sin(x) / Integer(5) - cos(x) / Integer(5))
    assert classify_pde(eq) == ('1st_linear_constant_coeff',
                                '1st_linear_constant_coeff_Integral')
    assert checkpdesol(eq, sol)[0]

    eq = u + u.diff(x) + u.diff(y) + x * y
    sol = pdsolve(eq)
    assert sol == Eq(
        f(x, y),
        -x * y + x + y + F(x - y) * exp(-x / Integer(2) - y / Integer(2)) - 2)
    assert classify_pde(eq) == ('1st_linear_constant_coeff',
                                '1st_linear_constant_coeff_Integral')
    assert checkpdesol(eq, sol)[0]

    eq = u + u.diff(x) + u.diff(y) + log(x)
    assert classify_pde(eq) == ('1st_linear_constant_coeff',
                                '1st_linear_constant_coeff_Integral')
Exemple #19
0
def test_geometric_sums():
    assert summation(pi**n, (n, 0, b)) == (1 - pi**(b + 1)) / (1 - pi)
    assert summation(2 * 3**n, (n, 0, b)) == 3**(b + 1) - 1
    assert summation(Rational(1, 2)**n, (n, 1, oo)) == 1
    assert summation(2**n, (n, 0, b)) == 2**(b + 1) - 1
    assert summation(2**n, (n, 1, oo)) == oo
    assert summation(2**(-n), (n, 1, oo)) == 1
    assert summation(3**(-n), (n, 4, oo)) == Rational(1, 54)
    assert summation(2**(-4 * n + 3), (n, 1, oo)) == Rational(8, 15)
    assert summation(2**(n + 1), (n, 1, b)) == 2 * 2**(b + 1) - 4

    # issue sympy/sympy#7097
    assert sum(x**n / n
               for n in range(1, 401)) == summation(x**n / n, (n, 1, 400))

    # issue sympy/sympy#6664:
    assert summation(x**n, (n, 0, oo)) == Piecewise(
        (1 / (-x + 1), abs(x) < 1), (Sum(x**n, (n, 0, oo)), True))

    assert summation(-2**n, (n, 0, oo)) == -oo
    assert summation(I**n, (n, 0, oo)) == Sum(I**n, (n, 0, oo))

    # issue sympy/sympy#6802:
    assert summation((-1)**(2 * x + 2), (x, 0, n)) == n + 1
    assert summation((-2)**(2 * x + 2),
                     (x, 0, n)) == 4 * 4**(n + 1) / 3 - Rational(4, 3)
    assert summation((-1)**x, (x, 0, n)) == -(-1)**(n + 1) / 2 + Rational(1, 2)
    assert summation(y**x, (x, a, b)) == Piecewise(
        (-a + b + 1, Eq(y, 1)), ((y**a - y**(b + 1)) / (-y + 1), True))
    assert summation((-2)**(y * x + 2), (x, 0, n)) == 4 * Piecewise(
        (n + 1, Eq(
            (-2)**y, 1)), ((-(-2)**(y * (n + 1)) + 1) / (-(-2)**y + 1), True))

    # issue sympy/sympy#8251:
    assert summation((1 / (n + 1)**2) * n**2, (n, 0, oo)) == oo

    # issue sympy/sympy#9908:
    assert Sum(1 / (n**3 - 1),
               (n, -oo, -2)) == summation(1 / (n**3 - 1), (n, -oo, -2))

    # issue sympy/sympy#11642:
    assert summation(0.5**n, (n, 1, oo)) == Float('1.0', dps=15)
def test_multiple_normal():
    X, Y = Normal('x', 0, 1), Normal('y', 0, 1)

    assert E(X + Y) == 0
    assert variance(X + Y) == 2
    assert variance(X + X) == 4
    assert covariance(X, Y) == 0
    assert covariance(2*X + Y, -X) == -2*variance(X)
    assert skewness(X) == 0
    assert skewness(X + Y) == 0
    assert correlation(X, Y) == 0
    assert correlation(X, X + Y) == correlation(X, X - Y)
    assert moment(X, 2) == 1
    assert cmoment(X, 3) == 0
    assert moment(X + Y, 4) == 12
    assert cmoment(X, 2) == variance(X)
    assert smoment(X*X, 2) == 1
    assert smoment(X + Y, 3) == skewness(X + Y)
    assert E(X, Eq(X + Y, 0)) == 0
    assert variance(X, Eq(X + Y, 0)) == Rational(1, 2)
def test_triangular():
    a = Symbol('a')
    b = Symbol('b')
    c = Symbol('c')

    X = Triangular('x', a, b, c)
    assert density(X)(x) == Piecewise(
        ((2*x - 2*a)/((-a + b)*(-a + c)), And(a <= x, x < c)),
        (2/(-a + b), Eq(x, c)),
        ((-2*x + 2*b)/((-a + b)*(b - c)), And(x <= b, c < x)),
        (0, True))
Exemple #22
0
def test_sympyissue_4527():
    k, m = symbols('k m', integer=True)
    assert integrate(sin(k * x) * sin(m * x), (x, 0, pi)) == Piecewise(
        (0, And(Eq(k, 0), Eq(m, 0))), (-pi / 2, Eq(k, -m)), (pi / 2, Eq(k, m)),
        (0, True))
    assert integrate(sin(k * x) * sin(m * x), (x, )) == Piecewise(
        (0, And(Eq(k, 0), Eq(m, 0))),
        (-x * sin(m * x)**2 / 2 - x * cos(m * x)**2 / 2 +
         sin(m * x) * cos(m * x) / (2 * m), Eq(k, -m)),
        (x * sin(m * x)**2 / 2 + x * cos(m * x)**2 / 2 -
         sin(m * x) * cos(m * x) / (2 * m), Eq(k, m)),
        (m * sin(k * x) * cos(m * x) /
         (k**2 - m**2) - k * sin(m * x) * cos(k * x) / (k**2 - m**2), True))
Exemple #23
0
def test_Eq():
    assert Eq(Interval(0, 1), Interval(0, 1))
    assert Eq(Interval(0, 1), Union(Interval(2, 3),
                                    Interval(4, 5))).is_Equality
    assert Eq(Interval(0, 1), Interval(0, 2)) is S.false

    s1 = FiniteSet(0, 1)
    s2 = FiniteSet(1, 2)

    assert Eq(s1, s1)
    assert Eq(s1, s2) is S.false
    assert Eq(FiniteSet(1, 2), FiniteSet(3, 4, 5)) is S.false

    assert Eq(s1*s2, s1*s2)
    assert Eq(s1*s2, s2*s1) is S.false

    p1 = ProductSet(FiniteSet(1), FiniteSet(2))
    assert Eq(p1, s1).is_Equality
    p2 = ProductSet(FiniteSet(1), FiniteSet(2), FiniteSet(3))
    assert Eq(p1, p2) is S.false
def plot_and_save(name):
    # implicit plot tests
    plot_implicit(Eq(y, cos(x)), (x, -5, 5), (y, -2, 2)).save(tmp_file(name))
    plot_implicit(Eq(y**2, x**3 - x), (x, -5, 5), (y, -4, 4),
                  show=False).save(tmp_file(name))
    plot_implicit(y > 1 / x, (x, -5, 5), (y, -2, 2),
                  show=False).save(tmp_file(name))
    plot_implicit(y < 1 / tan(x), (x, -5, 5), (y, -2, 2),
                  show=False).save(tmp_file(name))
    plot_implicit(y >= 2 * sin(x) * cos(x), (x, -5, 5), (y, -2, 2),
                  show=False).save(tmp_file(name))
    plot_implicit(y <= x**2, (x, -3, 3), (y, -1, 5),
                  show=False).save(tmp_file(name))
    plot_implicit(Or(And(x < y, x > y**2),
                     sin(y) > x), show=False).save(tmp_file(name))
    plot_implicit(Or(And(x < y, x > y**2), sin(y)),
                  show=False).save(tmp_file(name))
    plot_implicit(Or(x < y, sin(x) >= y), show=False).save(tmp_file(name))

    # Test all input args for plot_implicit
    plot_implicit(Eq(y**2, x**3 - x), show=False).save(tmp_file(name))
    plot_implicit(Eq(y**2, x**3 - x), adaptive=False,
                  show=False).save(tmp_file(name))
    plot_implicit(Eq(y**2, x**3 - x), adaptive=False, points=500,
                  show=False).save(tmp_file(name))
    plot_implicit(y > x, (x, -5, 5), show=False).save(tmp_file(name))
    plot_implicit(And(y > exp(x), y > x + 2), show=False).save(tmp_file(name))
    plot_implicit(Or(y > x, y > -x), show=False).save(tmp_file(name))
    plot_implicit(x**2 - 1, (x, -5, 5), show=False).save(tmp_file(name))
    plot_implicit(x**2 - 1, show=False).save(tmp_file(name))
    plot_implicit(y > x, depth=-5, show=False).save(tmp_file(name))
    plot_implicit(y > x, depth=5, show=False).save(tmp_file(name))
    plot_implicit(y > cos(x), adaptive=False, show=False).save(tmp_file(name))
    plot_implicit(y < cos(x), adaptive=False, show=False).save(tmp_file(name))
    plot_implicit(y - cos(pi / x), show=False).save(tmp_file(name))

    pytest.raises(ValueError, lambda: plot_implicit(y > x, (x, -1, 1, 2)))

    # issue sympy/sympy#17719
    plot_implicit(((x - 1)**2 + y**2 < 2) ^ ((x + 1)**2 + y**2 < 2),
                  show=False).save(tmp_file(name))
Exemple #25
0
def test_dice():
    # TODO: Make iid method!
    X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)
    a, b = symbols('a b')

    assert E(X) == 3 + S.Half
    assert variance(X) == Rational(35, 12)
    assert E(X + Y) == 7
    assert E(X + X) == 7
    assert E(a * X + b) == a * E(X) + b
    assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2)
    assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2)
    assert cmoment(X, 0) == 1
    assert cmoment(4 * X, 3) == 64 * cmoment(X, 3)
    assert covariance(X, Y) == S.Zero
    assert covariance(X, X + Y) == variance(X)
    assert density(Eq(cos(X * S.Pi), 1))[True] == S.Half
    assert correlation(X, Y) == 0
    assert correlation(X, Y) == correlation(Y, X)
    assert smoment(X + Y, 3) == skewness(X + Y)
    assert smoment(X, 0) == 1
    assert P(X > 3) == S.Half
    assert P(2 * X > 6) == S.Half
    assert P(X > Y) == Rational(5, 12)
    assert P(Eq(X, Y)) == P(Eq(X, 1))

    assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3)
    assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3)
    assert E(X + Y, Eq(X, Y)) == E(2 * X)
    assert moment(X, 0) == 1
    assert moment(5 * X, 2) == 25 * moment(X, 2)

    assert P(X > 3, X > 3) == S.One
    assert P(X > Y, Eq(Y, 6)) == S.Zero
    assert P(Eq(X + Y, 12)) == S.One / 36
    assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One / 6

    assert density(X + Y) == density(Y + Z) != density(X + X)
    d = density(2 * X + Y**Z)
    assert d[Integer(22)] == S.One / 108 and d[Integer(
        4100)] == S.One / 216 and Integer(3130) not in d

    assert pspace(X).domain.as_boolean() == Or(
        *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]])

    assert where(X > 3).set == FiniteSet(4, 5, 6)
Exemple #26
0
def test_dependence():
    X, Y = Die('X'), Die('Y')
    assert independent(X, 2*Y)
    assert not dependent(X, 2*Y)

    X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
    assert independent(X, Y)
    assert dependent(X, 2*X)

    # Create a dependency
    XX, YY = given(Tuple(X, Y), Eq(X + Y, 3))
    assert dependent(XX, YY)
Exemple #27
0
def test_pdsolve_all():
    f, F = map(Function, ['f', 'F'])
    u = f(x, y)
    eq = u + u.diff(x) + u.diff(y) + x**2*y
    sol = pdsolve(eq, hint='all')
    keys = ['1st_linear_constant_coeff',
            '1st_linear_constant_coeff_Integral', 'default', 'order']
    assert sorted(sol) == keys
    assert sol['order'] == 1
    assert sol['default'] == '1st_linear_constant_coeff'
    assert sol['1st_linear_constant_coeff'] == Eq(f(x, y),
                                                  -x**2*y + x**2 + 2*x*y - 4*x - 2*y + F(x - y)*exp(-x/Integer(2) - y/Integer(2)) + 6)
Exemple #28
0
def test_heurisch_wrapper():
    f = 1 / (y + x)
    assert heurisch_wrapper(f, x) == log(x + y)
    f = 1 / (y - x)
    assert heurisch_wrapper(f, x) == -log(x - y)
    f = 1 / ((y - x) * (y + x))
    assert heurisch_wrapper(f, x) == \
        Piecewise((1/x, Eq(y, 0)), (log(x + y)/2/y - log(x - y)/2/y, True))
    # issue sympy/sympy#6926
    f = sqrt(x**2 / ((y - x) * (y + x)))
    assert heurisch_wrapper(f, x) == x*sqrt(x**2)*sqrt(1/(-x**2 + y**2)) \
        - y**2*sqrt(x**2)*sqrt(1/(-x**2 + y**2))/x
Exemple #29
0
def test_cython_wrapper_inoutarg():
    code_gen = CythonCodeWrapper(CCodeGen())
    routine = make_routine('test', Eq(z, x + y + z))
    source = get_string(code_gen.dump_pyx, [routine])
    expected = ("cdef extern from 'file.h':\n"
                '    void test(double x, double y, double *z)\n'
                '\n'
                'def test_c(double x, double y, double z):\n'
                '\n'
                '    test(x, y, &z)\n'
                '    return z')
    assert source == expected
Exemple #30
0
def test_heurisch_symbolic_coeffs_1130():
    y = Symbol('y')
    assert heurisch_wrapper(1 / (x**2 + y), x) == Piecewise(
        (-1 / x, Eq(y, 0)),
        (-I * log(x - I * sqrt(y)) / (2 * sqrt(y)) + I * log(x + I * sqrt(y)) /
         (2 * sqrt(y)), True))
    y = Symbol('y', positive=True)
    assert heurisch_wrapper(1 / (x**2 + y), x) in [
        I / sqrt(y) * log(x + sqrt(-y)) / 2 -
        I / sqrt(y) * log(x - sqrt(-y)) / 2, I * log(x + I * sqrt(y)) /
        (2 * sqrt(y)) - I * log(x - I * sqrt(y)) / (2 * sqrt(y))
    ]
Exemple #31
0
def test_equality_subs2():
    f = Function('f')
    x = abc.x
    eq = Eq(f(x)**2, 16)
    assert bool(eq.subs({f(x): 3})) is False
    assert bool(eq.subs({f(x): 4})) is True
Exemple #32
0
def test_equality_subs1():
    f = Function('f')
    x = abc.x
    eq = Eq(f(x)**2, x)
    res = Eq(Integer(16), x)
    assert eq.subs({f(x): 4}) == res