Esempio n. 1
0
def test_reduce_rational_inequalities_real_relational():
    def OpenInterval(a, b):
        return Interval(a, b, True, True)
    def LeftOpenInterval(a, b):
        return Interval(a, b, True, False)
    def RightOpenInterval(a, b):
        return Interval(a, b, False, True)

    with assuming(Q.real(x)):
        assert reduce_rational_inequalities([[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \
            Union(OpenInterval(-oo, -4), Interval(-2, -1), OpenInterval(4, oo))

        assert reduce_rational_inequalities([[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x, relational=False) == \
            Union(OpenInterval(-5, 2), OpenInterval(2, 3))

        assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x, assume=Q.real(x), relational=False) == \
            RightOpenInterval(-1, 5)

        assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-3, -1), OpenInterval(1, oo))

        assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-4, 1), OpenInterval(1, 4))

        assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x, assume=Q.real(x), relational=False) == \
            Union(OpenInterval(-oo, -4), RightOpenInterval(S(3)/2, oo))

        assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x, assume=Q.real(x), relational=False) == \
            Union(LeftOpenInterval(-oo, -2), LeftOpenInterval(0, 4))
Esempio n. 2
0
def test_reduce_poly_inequalities_real_relational():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_poly_inequalities([[Eq(x ** 2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_poly_inequalities([[Le(x ** 2, 0)]], x, relational=True) == Eq(x, 0)
        assert reduce_poly_inequalities([[Lt(x ** 2, 0)]], x, relational=True) is False
        assert reduce_poly_inequalities([[Ge(x ** 2, 0)]], x, relational=True) is True
        assert reduce_poly_inequalities([[Gt(x ** 2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))
        assert reduce_poly_inequalities([[Ne(x ** 2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))

        assert reduce_poly_inequalities([[Eq(x ** 2, 1)]], x, relational=True) == Or(Eq(x, -1), Eq(x, 1))
        assert reduce_poly_inequalities([[Le(x ** 2, 1)]], x, relational=True) == And(Le(-1, x), Le(x, 1))
        assert reduce_poly_inequalities([[Lt(x ** 2, 1)]], x, relational=True) == And(Lt(-1, x), Lt(x, 1))
        assert reduce_poly_inequalities([[Ge(x ** 2, 1)]], x, relational=True) == Or(Le(x, -1), Le(1, x))
        assert reduce_poly_inequalities([[Gt(x ** 2, 1)]], x, relational=True) == Or(Lt(x, -1), Lt(1, x))
        assert reduce_poly_inequalities([[Ne(x ** 2, 1)]], x, relational=True) == Or(
            Lt(x, -1), And(Lt(-1, x), Lt(x, 1)), Lt(1, x)
        )

        assert reduce_poly_inequalities([[Le(x ** 2, 1.0)]], x, relational=True) == And(Le(-1.0, x), Le(x, 1.0))
        assert reduce_poly_inequalities([[Lt(x ** 2, 1.0)]], x, relational=True) == And(Lt(-1.0, x), Lt(x, 1.0))
        assert reduce_poly_inequalities([[Ge(x ** 2, 1.0)]], x, relational=True) == Or(Le(x, -1.0), Le(1.0, x))
        assert reduce_poly_inequalities([[Gt(x ** 2, 1.0)]], x, relational=True) == Or(Lt(x, -1.0), Lt(1.0, x))
        assert reduce_poly_inequalities([[Ne(x ** 2, 1.0)]], x, relational=True) == Or(
            Lt(x, -1.0), And(Lt(-1.0, x), Lt(x, 1.0)), Lt(1.0, x)
        )
Esempio n. 3
0
def test_zero_pow():
    assert satask(Q.zero(x**y), Q.zero(x) & Q.positive(y)) is True
    assert satask(Q.zero(x**y), Q.nonzero(x) & Q.zero(y)) is False

    assert satask(Q.zero(x), Q.zero(x**y)) is True

    assert satask(Q.zero(x**y), Q.zero(x)) is None
Esempio n. 4
0
def test_reduce_poly_inequalities_real_relational():
    global_assumptions.add(Q.real(x))
    global_assumptions.add(Q.real(y))

    assert reduce_poly_inequalities([[Eq(x ** 2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Le(x ** 2, 0)]], x, relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Lt(x ** 2, 0)]], x, relational=True) == False
    assert reduce_poly_inequalities([[Ge(x ** 2, 0)]], x, relational=True) == True
    assert reduce_poly_inequalities([[Gt(x ** 2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))
    assert reduce_poly_inequalities([[Ne(x ** 2, 0)]], x, relational=True) == Or(Lt(x, 0), Lt(0, x))

    assert reduce_poly_inequalities([[Eq(x ** 2, 1)]], x, relational=True) == Or(Eq(x, -1), Eq(x, 1))
    assert reduce_poly_inequalities([[Le(x ** 2, 1)]], x, relational=True) == And(Le(-1, x), Le(x, 1))
    assert reduce_poly_inequalities([[Lt(x ** 2, 1)]], x, relational=True) == And(Lt(-1, x), Lt(x, 1))
    assert reduce_poly_inequalities([[Ge(x ** 2, 1)]], x, relational=True) == Or(Le(x, -1), Le(1, x))
    assert reduce_poly_inequalities([[Gt(x ** 2, 1)]], x, relational=True) == Or(Lt(x, -1), Lt(1, x))
    assert reduce_poly_inequalities([[Ne(x ** 2, 1)]], x, relational=True) == Or(
        Lt(x, -1), And(Lt(-1, x), Lt(x, 1)), Lt(1, x)
    )

    assert (
        reduce_poly_inequalities([[Eq(x ** 2, 1.0)]], x, relational=True).evalf() == Or(Eq(x, -1.0), Eq(x, 1.0)).evalf()
    )
    assert reduce_poly_inequalities([[Le(x ** 2, 1.0)]], x, relational=True) == And(Le(-1.0, x), Le(x, 1.0))
    assert reduce_poly_inequalities([[Lt(x ** 2, 1.0)]], x, relational=True) == And(Lt(-1.0, x), Lt(x, 1.0))
    assert reduce_poly_inequalities([[Ge(x ** 2, 1.0)]], x, relational=True) == Or(Le(x, -1.0), Le(1.0, x))
    assert reduce_poly_inequalities([[Gt(x ** 2, 1.0)]], x, relational=True) == Or(Lt(x, -1.0), Lt(1.0, x))
    assert reduce_poly_inequalities([[Ne(x ** 2, 1.0)]], x, relational=True) == Or(
        Lt(x, -1.0), And(Lt(-1.0, x), Lt(x, 1.0)), Lt(1.0, x)
    )

    global_assumptions.remove(Q.real(x))
    global_assumptions.remove(Q.real(y))
Esempio n. 5
0
def test_assumptions():
    n = Symbol('n')
    A = MatrixSymbol('A', 1, n)
    P = PermutationMatrix(A)
    assert ask(Q.integer_elements(P))
    assert ask(Q.real_elements(P))
    assert ask(Q.complex_elements(P))
Esempio n. 6
0
def test_POSV():
    X = MatrixSymbol('X', n, n)
    Y = MatrixSymbol('Y', n, m)
    posv = POSV(X, Y)
    assert posv.outputs[0] == X.I*Y
    assert not POSV.valid(posv.inputs, True)
    assert POSV.valid(posv.inputs, Q.symmetric(X) & Q.positive_definite(X))
Esempio n. 7
0
def test_pow2():
    # powers of (-1)
    assert refine((-1)**(x + y), Q.even(x)) == (-1)**y
    assert refine((-1)**(x + y + z), Q.odd(x) & Q.odd(z)) == (-1)**y
    assert refine((-1)**(x + y + 1), Q.odd(x)) == (-1)**y
    assert refine((-1)**(x + y + 2), Q.odd(x)) == (-1)**(y + 1)
    assert refine((-1)**(x + 3)) == (-1)**(x + 1)
Esempio n. 8
0
def test_triangular():
    assert ask(Q.upper_triangular(X + Z.T + Identity(2)), Q.upper_triangular(X) &
            Q.lower_triangular(Z)) is True
    assert ask(Q.upper_triangular(X*Z.T), Q.upper_triangular(X) &
            Q.lower_triangular(Z)) is True
    assert ask(Q.lower_triangular(Identity(3))) is True
    assert ask(Q.lower_triangular(ZeroMatrix(3, 3))) is True
    assert ask(Q.triangular(X), Q.unit_triangular(X))
Esempio n. 9
0
def test_valid():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, n)
    C = MatrixSymbol('C', n, n)
    assert GEMM.valid((1, A, B, 2, C), True)
    assert not SYMM.valid((1, A, B, 2, C), True)
    assert SYMM.valid((1, A, B, 2, C), Q.symmetric(A))
    assert SYMM.valid((1, A, B, 2, C), Q.symmetric(B))
Esempio n. 10
0
def test_Abs():
    assert refine(Abs(x), Q.positive(x)) == x
    assert refine(1 + Abs(x), Q.positive(x)) == 1 + x
    assert refine(Abs(x), Q.negative(x)) == -x
    assert refine(1 + Abs(x), Q.negative(x)) == 1 - x

    assert refine(Abs(x ** 2)) != x ** 2
    assert refine(Abs(x ** 2), Q.real(x)) == x ** 2
Esempio n. 11
0
def test_ElemProd_code():
    from computations.matrices.fortran.core import generate
    ic = inplace_compile(c)
    with assuming(Q.real_elements(x), Q.real_elements(y)):
        s = generate(ic, [x,y], [HadamardProduct(x,y)])
    with open('tmp/elem_test.f90','w') as f:
        f.write(s)
    assert "= X * Y" in s
Esempio n. 12
0
def test_refine_issue_12724():
    expr1 = refine(Abs(x * y), Q.positive(x))
    expr2 = refine(Abs(x * y * z), Q.positive(x))
    assert expr1 == x * Abs(y)
    assert expr2 == x * Abs(y * z)
    y1 = Symbol('y1', real = True)
    expr3 = refine(Abs(x * y1**2 * z), Q.positive(x))
    assert expr3 == x * y1**2 * Abs(z)
Esempio n. 13
0
def test_pow4():
    assert refine((-1)**((-1)**x/2 - 7*S.Half), Q.integer(x)) == (-1)**(x + 1)
    assert refine((-1)**((-1)**x/2 - 9*S.Half), Q.integer(x)) == (-1)**x

    # powers of Abs
    assert refine(Abs(x)**2, Q.real(x)) == x**2
    assert refine(Abs(x)**3, Q.real(x)) == Abs(x)**3
    assert refine(Abs(x)**2) == Abs(x)**2
Esempio n. 14
0
def test_POSV_codemap():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, m)
    assumptions = Q.positive_definite(A) & Q.symmetric(A)
    codemap = POSV(A, B).codemap('A B INFO'.split(), assumptions)
    call = POSV.fortran_template % codemap
    assert "('U', n, m, A, n, B, n, INFO)" in call
    assert 'dposv' in call.lower()
Esempio n. 15
0
def test_numerics():
    import numpy as np
    with assuming(Q.real_elements(X), Q.real_elements(y)):
        f = build(ic, inputs, outputs, filename='numerics.f90',
                modname='numerics')
    nX, ny = np.ones((5, 5)), np.ones(5)
    result = np.matrix(nX) * np.matrix(ny).T
    assert np.allclose(f(nX, ny), result)
Esempio n. 16
0
def test_dtype_of():
    X = MatrixSymbol('X', n, n)
    assert 'integer' in dtype_of(X, Q.integer_elements(X))
    assert 'real' in dtype_of(X, Q.real_elements(X))
    assert 'complex' in dtype_of(X, Q.complex_elements(X))

    alpha = Symbol('alpha', integer=True)
    assert 'integer' in dtype_of(alpha)
Esempio n. 17
0
 def _eval_determinant(self):
     if self.blockshape == (2, 2):
         [[A, B],
          [C, D]] = self.blocks.tolist()
         if ask(Q.invertible(A)):
             return det(A)*det(D - C*A.I*B)
         elif ask(Q.invertible(D)):
             return det(D)*det(A - B*D.I*C)
     return Determinant(self)
Esempio n. 18
0
def test_execution():
    with assuming(Q.real_elements(X), Q.real_elements(Y)):
        f = build(pgemm, [X, Y], [pgemm.duration], filename='tmp/profile.f90',
                modname='profile')
    assert callable(f)
    nX, nY = np.random.rand(500, 500), np.random.rand(500, 500)
    result = f(nX, nY)
    assert isinstance(result, float)
    assert result > 0
Esempio n. 19
0
def test_es_toy():
    K = MatrixSymbol('K',n,1)
    phi = MatrixSymbol('phi', n, 1)
    V = MatrixSymbol('V',n,1)

    c = AXPY(1.0, HP(K,phi), DFT(n).T*HP(V,DFT(n)*phi)) + ElemProd(K,phi) + IFFTW(HP(V, DFT(n) * phi)) + FFTW(phi) + ElemProd(V, DFT(n) * phi)
#    show(c)
    with assuming(Q.complex_elements(phi), Q.real_elements(K), Q.real_elements(V)):
        f = build(c, [K,V,phi], [HP(K,phi) + DFT(n).T * HP(V,DFT(n) * phi)],
                modname='es', filename='tmp/es.f90')
Esempio n. 20
0
def test_GEMM():
    with assuming(Q.real_elements(A), Q.real_elements(X)):
        f = build(GEMM(1.0, A, X, 0.0, ZeroMatrix(A.rows, X.cols)),
                [A, X], [A*X], modname='gemmtest', filename='tmp/gemmtest.f90')

    nA = np.asarray([[1, 2], [3, 4]], dtype=np.float64, order='F')
    nX = np.asarray([[1, 1], [1, 1]], dtype=np.float64, order='F')
    expected = np.asarray([[3., 3.], [7., 7.]])
    result = f(nA, nX)
    assert np.allclose(expected, result)
def test_is_literal():
    assert is_literal(True) is True
    assert is_literal(False) is True
    assert is_literal(A) is True
    assert is_literal(~A) is True
    assert is_literal(Or(A, B)) is False
    assert is_literal(Q.zero(A)) is True
    assert is_literal(Not(Q.zero(A))) is True
    assert is_literal(Or(A, B)) is False
    assert is_literal(And(Q.zero(A), Q.zero(B))) is False
Esempio n. 22
0
def test_Join_build():
    c = Join(B)
    with assuming(Q.real_elements(X), Q.real_elements(Y)):
        f = build(c, [X, Y], [B], filename='tmp/join.f90', modname='join')
    nX = np.ones((2, 3), dtype=np.float64)
    nY = np.ones((2, 3), dtype=np.float64)
    result   = f(nX, nY)
    expected = np.ones((2, 6), dtype=np.float64)

    assert np.allclose(result, expected)
Esempio n. 23
0
def test_prime():
    assert satask(Q.prime(5)) is True
    assert satask(Q.prime(6)) is False
    assert satask(Q.prime(-5)) is False

    assert satask(Q.prime(x*y), Q.integer(x) & Q.integer(y)) is None
    assert satask(Q.prime(x*y), Q.prime(x) & Q.prime(y)) is False
Esempio n. 24
0
def test_POSV():
    c = POSV(A, y)
    with assuming(Q.real_elements(A), Q.real_elements(y)):
        f = build(c, [A, y], [A.I*y], modname='posv', filename='tmp/posv.f90')

    nA, ny = np.asarray([[2, 1], [1, 2]], dtype='float64').reshape((2, 2)), np.ones(2)
    mA = np.matrix(nA)
    my = np.matrix(ny).T
    expected = np.linalg.solve(mA, my)
    f(nA, ny)
    assert np.allclose(expected, ny)
Esempio n. 25
0
def test_solve_inequalities():
    system = [Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]

    assert solve(system) == \
        And(Or(And(Lt(-sqrt(2), re(x)), Lt(re(x), -1)),
               And(Lt(1, re(x)), Lt(re(x), sqrt(2)))), Eq(im(x), 0))
    assert solve(system, assume=Q.real(x)) == \
        Or(And(Lt(-sqrt(2), x), Lt(x, -1)), And(Lt(1, x), Lt(x, sqrt(2))))

    # issue 3528, 3448
    assert solve((x - 3)/(x - 2) < 0, x, assume=Q.real(x)) == And(Lt(2, x), Lt(x, 3))
    assert solve(x/(x + 1) > 1, x, assume=Q.real(x)) == Lt(x, -1)
def test_reduce_poly_inequalities_real_interval():
    global_assumptions.add(Q.real(x))
    global_assumptions.add(Q.real(y))

    assert reduce_poly_inequalities(
        [[Eq(x**2, 0)]], x, relational=False) == FiniteSet(0)
    assert reduce_poly_inequalities(
        [[Le(x**2, 0)]], x, relational=False) == FiniteSet(0)
    assert reduce_poly_inequalities(
        [[Lt(x**2, 0)]], x, relational=False) == S.EmptySet
    assert reduce_poly_inequalities(
        [[Ge(x**2, 0)]], x, relational=False) == Interval(-oo, oo)
    assert reduce_poly_inequalities(
        [[Gt(x**2, 0)]], x, relational=False) == FiniteSet(0).complement
    assert reduce_poly_inequalities(
        [[Ne(x**2, 0)]], x, relational=False) == FiniteSet(0).complement

    assert reduce_poly_inequalities(
        [[Eq(x**2, 1)]], x, relational=False) == FiniteSet(-1, 1)
    assert reduce_poly_inequalities(
        [[Le(x**2, 1)]], x, relational=False) == Interval(-1, 1)
    assert reduce_poly_inequalities(
        [[Lt(x**2, 1)]], x, relational=False) == Interval(-1, 1, True, True)
    assert reduce_poly_inequalities([[Ge(x**2, 1)]], x, relational=False) == Union(Interval(-oo, -1), Interval(1, oo))
    assert reduce_poly_inequalities(
        [[Gt(x**2, 1)]], x, relational=False) == Interval(-1, 1).complement
    assert reduce_poly_inequalities(
        [[Ne(x**2, 1)]], x, relational=False) == FiniteSet(-1, 1).complement
    assert reduce_poly_inequalities([[Eq(
        x**2, 1.0)]], x, relational=False) == FiniteSet(-1.0, 1.0).evalf()
    assert reduce_poly_inequalities(
        [[Le(x**2, 1.0)]], x, relational=False) == Interval(-1.0, 1.0)
    assert reduce_poly_inequalities([[Lt(
        x**2, 1.0)]], x, relational=False) == Interval(-1.0, 1.0, True, True)
    assert reduce_poly_inequalities([[Ge(x**2, 1.0)]], x, relational=False) == Union(Interval(-inf, -1.0), Interval(1.0, inf))
    assert reduce_poly_inequalities([[Gt(x**2, 1.0)]], x, relational=False) == Union(Interval(-inf, -1.0, right_open=True), Interval(1.0, inf, left_open=True))
    assert reduce_poly_inequalities([[Ne(
        x**2, 1.0)]], x, relational=False) == FiniteSet(-1.0, 1.0).complement

    s = sqrt(2)

    assert reduce_poly_inequalities([[Lt(
        x**2 - 1, 0), Gt(x**2 - 1, 0)]], x, relational=False) == S.EmptySet
    assert reduce_poly_inequalities([[Le(x**2 - 1, 0), Ge(
        x**2 - 1, 0)]], x, relational=False) == FiniteSet(-1, 1)
    assert reduce_poly_inequalities([[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x, relational=False) == Union(Interval(-s, -1, False, False), Interval(1, s, False, False))
    assert reduce_poly_inequalities([[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x, relational=False) == Union(Interval(-s, -1, False, True), Interval(1, s, True, False))
    assert reduce_poly_inequalities([[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x, relational=False) == Union(Interval(-s, -1, True, False), Interval(1, s, False, True))
    assert reduce_poly_inequalities([[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x, relational=False) == Union(Interval(-s, -1, True, True), Interval(1, s, True, True))
    assert reduce_poly_inequalities([[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x, relational=False) == Union(Interval(-s, -1, True, True), Interval(-1, 1, True, True), Interval(1, s, True, True))

    global_assumptions.remove(Q.real(x))
    global_assumptions.remove(Q.real(y))
Esempio n. 27
0
def test_matrix_element_sets():
    X = MatrixSymbol('X', 4, 4)
    assert ask(Q.real(X[1, 2]), Q.real_elements(X))
    assert ask(Q.integer(X[1, 2]), Q.integer_elements(X))
    assert ask(Q.complex(X[1, 2]), Q.complex_elements(X))
    assert ask(Q.integer_elements(Identity(3)))
    assert ask(Q.integer_elements(ZeroMatrix(3, 3)))
    from sympy.matrices.expressions.fourier import DFT
    assert ask(Q.complex_elements(DFT(3)))
Esempio n. 28
0
def _test_orthogonal_unitary(predicate):
    assert ask(predicate(X), predicate(X))
    assert ask(predicate(X.T), predicate(X)) is True
    assert ask(predicate(X.I), predicate(X)) is True
    assert ask(predicate(X**2), predicate(X))
    assert ask(predicate(Y)) is False
    assert ask(predicate(X)) is None
    assert ask(predicate(X), ~Q.invertible(X)) is False
    assert ask(predicate(X*Z*X), predicate(X) & predicate(Z)) is True
    assert ask(predicate(Identity(3))) is True
    assert ask(predicate(ZeroMatrix(3, 3))) is False
    assert ask(Q.invertible(X), predicate(X))
    assert not ask(predicate(X + Z), predicate(X) & predicate(Z))
Esempio n. 29
0
def test_fftw_inverse():
    c = FFTW(y)
    with assuming(Q.complex(y)):
        f = build(c, [y], [DFT(y)], modname='fftw2', filename='tmp/fftw2.f90')

    c = IFFTW(y)
    with assuming(Q.complex(y)):
        fi = build(c, [y], [DFT(y).T], modname='ifftw', filename='tmp/ifftw.f90')

    x = np.random.random_sample((8,)) + 1j * np.random.random_sample((8,))
    expected = x
    f(x)
    fi(x)
    assert np.allclose(expected, x)
Esempio n. 30
0
def test_POTRS():
    from sympy.matrices.expressions.factorizations import UofCholesky
    from computations.matrices.lapack import POTRS
    c = POTRS(UofCholesky(A), X)
    with assuming(Q.real_elements(A), Q.real_elements(X),
            Q.positive_definite(A)):
        f = build(c, [UofCholesky(A), X], [A.I*X], modname='potrs',
                                                   filename='tmp/potrs.f90')

    nA = np.asarray([[1, 0], [0, 1]], dtype=np.float64, order='F')
    nX = np.asarray([[1, 2], [3, 4]], dtype=np.float64, order='F')
    expected = np.asarray([[1., 2.], [3., 4.]])
    f(nA, nX); result = nX
    assert np.allclose(expected, result)
Esempio n. 31
0
def test_reduce_poly_inequalities_real_interval():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities([[Eq(x**2, 0)]],
                                            x,
                                            relational=False) == FiniteSet(0)
        assert reduce_rational_inequalities([[Le(x**2, 0)]],
                                            x,
                                            relational=False) == FiniteSet(0)
        assert reduce_rational_inequalities([[Lt(x**2, 0)]],
                                            x,
                                            relational=False) == S.EmptySet
        assert reduce_rational_inequalities([[Ge(x**2, 0)]],
                                            x,
                                            relational=False) == Interval(
                                                -oo, oo)
        assert reduce_rational_inequalities(
            [[Gt(x**2, 0)]], x, relational=False) == FiniteSet(0).complement
        assert reduce_rational_inequalities(
            [[Ne(x**2, 0)]], x, relational=False) == FiniteSet(0).complement

        assert reduce_rational_inequalities([[Eq(x**2, 1)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1, 1)
        assert reduce_rational_inequalities([[Le(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1)
        assert reduce_rational_inequalities([[Lt(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1, True, True)
        assert reduce_rational_inequalities([[Ge(x**2, 1)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-oo, -1),
                                                Interval(1, oo))
        assert reduce_rational_inequalities([[Gt(x**2, 1)]],
                                            x,
                                            relational=False) == Interval(
                                                -1, 1).complement
        assert reduce_rational_inequalities([[Ne(x**2, 1)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1, 1).complement
        assert reduce_rational_inequalities([[Eq(x**2, 1.0)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1.0, 1.0).evalf()
        assert reduce_rational_inequalities([[Le(x**2, 1.0)]],
                                            x,
                                            relational=False) == Interval(
                                                -1.0, 1.0)
        assert reduce_rational_inequalities([[Lt(x**2, 1.0)]],
                                            x,
                                            relational=False) == Interval(
                                                -1.0, 1.0, True, True)
        assert reduce_rational_inequalities([[Ge(x**2, 1.0)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-inf, -1.0),
                                                Interval(1.0, inf))
        assert reduce_rational_inequalities([[Gt(x**2, 1.0)]],
                                            x,
                                            relational=False) == Union(
                                                Interval(-inf,
                                                         -1.0,
                                                         right_open=True),
                                                Interval(1.0,
                                                         inf,
                                                         left_open=True))
        assert reduce_rational_inequalities([[Ne(x**2, 1.0)]],
                                            x,
                                            relational=False) == FiniteSet(
                                                -1.0, 1.0).complement

        s = sqrt(2)

        assert reduce_rational_inequalities(
            [[Lt(x**2 - 1, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == S.EmptySet
        assert reduce_rational_inequalities(
            [[Le(x**2 - 1, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == FiniteSet(-1, 1)
        assert reduce_rational_inequalities(
            [[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, False, False),
                                       Interval(1, s, False, False))
        assert reduce_rational_inequalities(
            [[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, False, True),
                                       Interval(1, s, True, False))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, False),
                                       Interval(1, s, False, True))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, True),
                                       Interval(1, s, True, True))
        assert reduce_rational_inequalities(
            [[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x,
            relational=False) == Union(Interval(-s, -1, True, True),
                                       Interval(-1, 1, True, True),
                                       Interval(1, s, True, True))
Esempio n. 32
0
def test_AppliedPredicate():
    sT(Q.even(Symbol('z')), "AppliedPredicate(Q.even, Symbol('z'))")
Esempio n. 33
0
def test_refine():
    assert refine(C * C.T * D, Q.orthogonal(C)).doit() == D

    kC = k * C
    assert refine(kC * C.T, Q.orthogonal(C)).doit() == k * Identity(n)
    assert refine(kC * kC.T, Q.orthogonal(C)).doit() == (k**2) * Identity(n)
def test_laguerre_poly():
    raises(ValueError, lambda: laguerre_poly(-1, x))

    assert laguerre_poly(1, x, polys=True) == Poly(-x + 1)

    assert laguerre_poly(0, x) == 1
    assert laguerre_poly(1, x) == -x + 1
    assert laguerre_poly(2, x) == Q(1, 2) * x**2 - Q(4, 2) * x + 1
    assert laguerre_poly(
        3, x) == -Q(1, 6) * x**3 + Q(9, 6) * x**2 - Q(18, 6) * x + 1
    assert laguerre_poly(4, x) == Q(1, 24) * x**4 - Q(16, 24) * x**3 + Q(
        72, 24) * x**2 - Q(96, 24) * x + 1
    assert laguerre_poly(5, x) == -Q(1, 120) * x**5 + Q(25, 120) * x**4 - Q(
        200, 120) * x**3 + Q(600, 120) * x**2 - Q(600, 120) * x + 1
    assert laguerre_poly(
        6, x) == Q(1, 720) * x**6 - Q(36, 720) * x**5 + Q(450, 720) * x**4 - Q(
            2400, 720) * x**3 + Q(5400, 720) * x**2 - Q(4320, 720) * x + 1

    assert laguerre_poly(0, x, a) == 1
    assert laguerre_poly(1, x, a) == -x + a + 1
    assert laguerre_poly(
        2, x, a) == x**2 / 2 + (-a - 2) * x + a**2 / 2 + 3 * a / 2 + 1
    assert laguerre_poly(3, x, a) == -x**3 / 6 + (a / 2 + Q(3) / 2) * x**2 + (
        -a**2 / 2 - 5 * a / 2 - 3) * x + a**3 / 6 + a**2 + 11 * a / 6 + 1

    assert laguerre_poly(1).dummy_eq(-x + 1)
    assert laguerre_poly(1, polys=True) == Poly(-x + 1)
Esempio n. 35
0
def test_diagonal():
    assert ask(Q.diagonal(X + Z.T + Identity(2)),
               Q.diagonal(X) & Q.diagonal(Z)) is True
    assert ask(Q.diagonal(ZeroMatrix(3, 3)))
    assert ask(Q.lower_triangular(X) & Q.upper_triangular(X), Q.diagonal(X))
    assert ask(Q.diagonal(X), Q.lower_triangular(X) & Q.upper_triangular(X))
    assert ask(Q.symmetric(X), Q.diagonal(X))
    assert ask(Q.triangular(X), Q.diagonal(X))
Esempio n. 36
0
def test_MatrixSlice():
    X = MatrixSymbol('X', 4, 4)
    B = MatrixSlice(X, (1, 3), (1, 3))
    C = MatrixSlice(X, (0, 3), (1, 3))
    assert ask(Q.symmetric(B), Q.symmetric(X))
    assert ask(Q.invertible(B), Q.invertible(X))
    assert ask(Q.diagonal(B), Q.diagonal(X))
    assert ask(Q.orthogonal(B), Q.orthogonal(X))
    assert ask(Q.upper_triangular(B), Q.upper_triangular(X))

    assert not ask(Q.symmetric(C), Q.symmetric(X))
    assert not ask(Q.invertible(C), Q.invertible(X))
    assert not ask(Q.diagonal(C), Q.diagonal(X))
    assert not ask(Q.orthogonal(C), Q.orthogonal(X))
    assert not ask(Q.upper_triangular(C), Q.upper_triangular(X))
Esempio n. 37
0
def test_field_assumptions():
    X = MatrixSymbol('X', 4, 4)
    Y = MatrixSymbol('Y', 4, 4)
    assert ask(Q.real_elements(X), Q.real_elements(X))
    assert not ask(Q.integer_elements(X), Q.real_elements(X))
    assert ask(Q.complex_elements(X), Q.real_elements(X))
    assert ask(Q.real_elements(X + Y), Q.real_elements(X)) is None
    assert ask(Q.real_elements(X + Y), Q.real_elements(X) & Q.real_elements(Y))
    from sympy.matrices.expressions.hadamard import HadamardProduct
    assert ask(Q.real_elements(HadamardProduct(X, Y)),
               Q.real_elements(X) & Q.real_elements(Y))
    assert ask(Q.complex_elements(X + Y),
               Q.real_elements(X) & Q.complex_elements(Y))

    assert ask(Q.real_elements(X.T), Q.real_elements(X))
    assert ask(Q.real_elements(X.I), Q.real_elements(X) & Q.invertible(X))
    assert ask(Q.real_elements(Trace(X)), Q.real_elements(X))
    assert ask(Q.integer_elements(Determinant(X)), Q.integer_elements(X))
    assert not ask(Q.integer_elements(X.I), Q.integer_elements(X))
Esempio n. 38
0
from sympy import ask, Q

print("ask(Q.even(2)):", ask(Q.even(2)))
print("ask(Q.odd(2)):", ask(Q.odd(2)))
print("ask(Q.even(3)):", ask(Q.even(3)))
print("ask(Q.odd(3)):", ask(Q.odd(3)))
print("ask(Q.even(4)):", ask(Q.even(4)))
print("ask(Q.odd(4)):", ask(Q.odd(4)))
print("-----------------------")
print("ask(Q.prime(2)):", ask(Q.prime(2)))
print("ask(Q.prime(3)):", ask(Q.prime(3)))
print("ask(Q.prime(4)):", ask(Q.prime(4)))
"""
ask(Q.even(2)): True
ask(Q.odd(2)): False
ask(Q.even(3)): False
ask(Q.odd(3)): True
ask(Q.even(4)): True
ask(Q.odd(4)): False
-----------------------
ask(Q.prime(2)): True
ask(Q.prime(3)): True
ask(Q.prime(4)): False
"""
Esempio n. 39
0
def test_positive_definite():
    assert ask(Q.positive_definite(X), Q.positive_definite(X))
    assert ask(Q.positive_definite(X.T), Q.positive_definite(X)) is True
    assert ask(Q.positive_definite(X.I), Q.positive_definite(X)) is True
    assert ask(Q.positive_definite(Y)) is False
    assert ask(Q.positive_definite(X)) is None
    assert ask(Q.positive_definite(X * Z * X),
               Q.positive_definite(X) & Q.positive_definite(Z)) is True
    assert ask(Q.positive_definite(X), Q.orthogonal(X))
    assert ask(Q.positive_definite(Y.T * X * Y),
               Q.positive_definite(X) & Q.fullrank(Y)) is True
    assert not ask(Q.positive_definite(Y.T * X * Y), Q.positive_definite(X))
    assert ask(Q.positive_definite(Identity(3))) is True
    assert ask(Q.positive_definite(ZeroMatrix(3, 3))) is False
    assert ask(Q.positive_definite(X + Z),
               Q.positive_definite(X) & Q.positive_definite(Z)) is True
    assert not ask(Q.positive_definite(-X), Q.positive_definite(X))
    assert ask(Q.positive(X[1, 1]), Q.positive_definite(X))
Esempio n. 40
0
def test_reduce_poly_inequalities_real_relational():
    with assuming(Q.real(x), Q.real(y)):
        assert reduce_rational_inequalities([[Eq(x**2, 0)]],
                                            x,
                                            relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities([[Le(x**2, 0)]],
                                            x,
                                            relational=True) == Eq(x, 0)
        assert reduce_rational_inequalities(
            [[Lt(x**2, 0)]], x, relational=True) is False
        assert reduce_rational_inequalities(
            [[Ge(x**2, 0)]], x, relational=True) is True
        assert reduce_rational_inequalities([[Gt(x**2, 0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, 0), Gt(x, 0))
        assert reduce_rational_inequalities([[Ne(x**2, 0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, 0), Gt(x, 0))

        assert reduce_rational_inequalities([[Eq(x**2, 1)]],
                                            x,
                                            relational=True) == Or(
                                                Eq(x, -1), Eq(x, 1))
        assert reduce_rational_inequalities([[Le(x**2, 1)]],
                                            x,
                                            relational=True) == And(
                                                Le(-1, x), Le(x, 1))
        assert reduce_rational_inequalities([[Lt(x**2, 1)]],
                                            x,
                                            relational=True) == And(
                                                Lt(-1, x), Lt(x, 1))
        assert reduce_rational_inequalities([[Ge(x**2, 1)]],
                                            x,
                                            relational=True) == Or(
                                                Le(x, -1), Ge(x, 1))
        assert reduce_rational_inequalities([[Gt(x**2, 1)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, -1), Gt(x, 1))
        assert reduce_rational_inequalities([[Ne(x**2, 1)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, -1),
                                                And(Lt(-1, x), Lt(x, 1)),
                                                Gt(x, 1))

        assert reduce_rational_inequalities([[Le(x**2, 1.0)]],
                                            x,
                                            relational=True) == And(
                                                Le(-1.0, x), Le(x, 1.0))
        assert reduce_rational_inequalities([[Lt(x**2, 1.0)]],
                                            x,
                                            relational=True) == And(
                                                Lt(-1.0, x), Lt(x, 1.0))
        assert reduce_rational_inequalities([[Ge(x**2, 1.0)]],
                                            x,
                                            relational=True) == Or(
                                                Le(x, -1.0), Ge(x, 1.0))
        assert reduce_rational_inequalities([[Gt(x**2, 1.0)]],
                                            x,
                                            relational=True) == Or(
                                                Lt(x, -1.0), Gt(x, 1.0))
        assert reduce_rational_inequalities([[Ne(x**2, 1.0)]], x, relational=True) == \
            Or(Lt(x, -1.0), And(Lt(-1.0, x), Lt(x, 1.0)), Gt(x, 1.0))
Esempio n. 41
0
 def _contains(self, other):
     if ask(Q.integer(other)):
         return True
     return False
Esempio n. 42
0
def test_satisfiable_non_symbols():
    x, y = symbols("x y")
    assumptions = Q.zero(x * y)
    facts = Implies(Q.zero(x * y), Q.zero(x) | Q.zero(y))
    query = ~Q.zero(x) & ~Q.zero(y)
    refutations = [
        {
            Q.zero(x): True,
            Q.zero(x * y): True
        },
        {
            Q.zero(y): True,
            Q.zero(x * y): True
        },
        {
            Q.zero(x): True,
            Q.zero(y): True,
            Q.zero(x * y): True
        },
        {
            Q.zero(x): True,
            Q.zero(y): False,
            Q.zero(x * y): True
        },
        {
            Q.zero(x): False,
            Q.zero(y): True,
            Q.zero(x * y): True
        },
    ]
    assert not satisfiable(And(assumptions, facts, query), algorithm="dpll")
    assert satisfiable(And(assumptions, facts, ~query),
                       algorithm="dpll") in refutations
    assert not satisfiable(And(assumptions, facts, query), algorithm="dpll2")
    assert (satisfiable(And(assumptions, facts, ~query), algorithm="dpll2")
            in refutations)
Esempio n. 43
0
def test_refine():
    assert refine(det(A), Q.orthogonal(A)) == 1
    assert refine(det(A), Q.singular(A)) == 0
Esempio n. 44
0
def test_reduce_inequalities_assume():
    assert reduce_inequalities(
        [Le(x**2, 1), Q.real(x)]) == And(Le(-1, x), Le(x, 1))
    assert reduce_inequalities(
        [Le(x**2, 1)], Q.real(x)) == And(Le(-1, x), Le(x, 1))
Esempio n. 45
0
def test_invertible():
    assert ask(Q.invertible(X), Q.invertible(X))
    assert ask(Q.invertible(Y)) is False
    assert ask(Q.invertible(X * Y), Q.invertible(X)) is False
    assert ask(Q.invertible(X * Z), Q.invertible(X)) is None
    assert ask(Q.invertible(X * Z), Q.invertible(X) & Q.invertible(Z)) is True
    assert ask(Q.invertible(X.T)) is None
    assert ask(Q.invertible(X.T), Q.invertible(X)) is True
    assert ask(Q.invertible(X.I)) is True
    assert ask(Q.invertible(Identity(3))) is True
    assert ask(Q.invertible(ZeroMatrix(3, 3))) is False
    assert ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
Esempio n. 46
0
def test_issue_6343():
    eq = -3*x**2/2 - 45*x/4 + S(33)/2 > 0
    assert reduce_inequalities(eq, Q.real(x)) == \
        And(x < -S(15)/4 + sqrt(401)/4, -sqrt(401)/4 - S(15)/4 < x)
Esempio n. 47
0
def test_det_trace_positive():
    X = MatrixSymbol('X', 4, 4)
    assert ask(Q.positive(Trace(X)), Q.positive_definite(X))
    assert ask(Q.positive(Determinant(X)), Q.positive_definite(X))
Esempio n. 48
0
def test_unitary():
    _test_orthogonal_unitary(Q.unitary)
    assert ask(Q.unitary(X), Q.orthogonal(X))
Esempio n. 49
0
def test_non_atoms():
    assert ask(Q.real(Trace(X)), Q.positive(Trace(X)))
Esempio n. 50
0
def test_symmetric():
    assert ask(Q.symmetric(X), Q.symmetric(X))
    assert ask(Q.symmetric(X * Z), Q.symmetric(X)) is None
    assert ask(Q.symmetric(X * Z), Q.symmetric(X) & Q.symmetric(Z)) is True
    assert ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z)) is True
    assert ask(Q.symmetric(Y)) is False
    assert ask(Q.symmetric(Y * Y.T)) is True
    assert ask(Q.symmetric(Y.T * X * Y)) is None
    assert ask(Q.symmetric(Y.T * X * Y), Q.symmetric(X)) is True
    assert ask(Q.symmetric(X * X * X * X * X * X * X * X * X * X),
               Q.symmetric(X)) is True
Esempio n. 51
0
def test_square():
    assert ask(Q.square(X))
    assert not ask(Q.square(Y))
    assert ask(Q.square(Y * Y.T))
Esempio n. 52
0
def test_invertible_fullrank():
    assert ask(Q.invertible(X), Q.fullrank(X))
def test_legendre_poly():
    raises(ValueError, lambda: legendre_poly(-1, x))

    assert legendre_poly(1, x, polys=True) == Poly(x)

    assert legendre_poly(0, x) == 1
    assert legendre_poly(1, x) == x
    assert legendre_poly(2, x) == Q(3, 2) * x**2 - Q(1, 2)
    assert legendre_poly(3, x) == Q(5, 2) * x**3 - Q(3, 2) * x
    assert legendre_poly(4, x) == Q(35, 8) * x**4 - Q(30, 8) * x**2 + Q(3, 8)
    assert legendre_poly(5,
                         x) == Q(63, 8) * x**5 - Q(70, 8) * x**3 + Q(15, 8) * x
    assert legendre_poly(
        6, x) == Q(231, 16) * x**6 - Q(315, 16) * x**4 + Q(105, 16) * x**2 - Q(
            5, 16)

    assert legendre_poly(1).dummy_eq(x)
    assert legendre_poly(1, polys=True) == Poly(x)
Esempio n. 54
0
def test_singular():
    assert ask(Q.singular(X)) is None
    assert ask(Q.singular(X), Q.invertible(X)) is False
    assert ask(Q.singular(X), ~Q.invertible(X)) is True
Esempio n. 55
0
def test_laguerre_poly():
    raises(ValueError, "laguerre_poly(-1, x)")

    assert laguerre_poly(1, x, polys=True) == Poly(-x + 1)

    assert laguerre_poly(0, x) == 1
    assert laguerre_poly(1, x) == -x + 1
    assert laguerre_poly(2, x) == Q(1, 2) * x**2 - Q(4, 2) * x + 1
    assert laguerre_poly(
        3, x) == -Q(1, 6) * x**3 + Q(9, 6) * x**2 - Q(18, 6) * x + 1
    assert laguerre_poly(4, x) == Q(1, 24) * x**4 - Q(16, 24) * x**3 + Q(
        72, 24) * x**2 - Q(96, 24) * x + 1
    assert laguerre_poly(5, x) == -Q(1, 120) * x**5 + Q(25, 120) * x**4 - Q(
        200, 120) * x**3 + Q(600, 120) * x**2 - Q(600, 120) * x + 1
    assert laguerre_poly(
        6, x) == Q(1, 720) * x**6 - Q(36, 720) * x**5 + Q(450, 720) * x**4 - Q(
            2400, 720) * x**3 + Q(5400, 720) * x**2 - Q(4320, 720) * x + 1
Esempio n. 56
0
def test_matrix_element_sets_determinant_trace():
    assert ask(Q.integer(Determinant(X)), Q.integer_elements(X))
    assert ask(Q.integer(Trace(X)), Q.integer_elements(X))
Esempio n. 57
0
def test_orthogonal():
    assert ask(Q.orthogonal(X), Q.orthogonal(X))
    assert ask(Q.orthogonal(X.T), Q.orthogonal(X)) is True
    assert ask(Q.orthogonal(X.I), Q.orthogonal(X)) is True
    assert ask(Q.orthogonal(Y)) is False
    assert ask(Q.orthogonal(X)) is None
    assert ask(Q.orthogonal(X*Z*X), Q.orthogonal(X) & Q.orthogonal(Z)) is True
    assert ask(Q.orthogonal(Identity(3))) is True
    assert ask(Q.orthogonal(ZeroMatrix(3, 3))) is False
    assert ask(Q.invertible(X), Q.orthogonal(X))
    assert not ask(Q.orthogonal(X + Z), Q.orthogonal(X) & Q.orthogonal(Z))
Esempio n. 58
0
def test_matrix_element_sets_slices_blocks():
    from sympy.matrices.expressions import BlockMatrix
    X = MatrixSymbol('X', 4, 4)
    assert ask(Q.integer_elements(X[:, 3]), Q.integer_elements(X))
    assert ask(Q.integer_elements(BlockMatrix([[X], [X]])),
               Q.integer_elements(X))
Esempio n. 59
0
 def _contains(self, other):
     return (other >= self.inf and other <= self.sup
             and ask(Q.integer((self.start - other) / self.step)))
Esempio n. 60
0
def test_fullrank():
    assert ask(Q.fullrank(X), Q.fullrank(X))
    assert ask(Q.fullrank(X.T), Q.fullrank(X)) is True
    assert ask(Q.fullrank(X)) is None
    assert ask(Q.fullrank(Y)) is None
    assert ask(Q.fullrank(X * Z), Q.fullrank(X) & Q.fullrank(Z)) is True
    assert ask(Q.fullrank(Identity(3))) is True
    assert ask(Q.fullrank(ZeroMatrix(3, 3))) is False
    assert ask(Q.invertible(X), ~Q.fullrank(X)) == False