Exemplo n.º 1
0
def test_minpoly_fraction_field():
    assert minimal_polynomial(1/x)(y) == x*y - 1
    assert minimal_polynomial(1/(x + 1))(y) == x*y + y - 1

    assert minimal_polynomial(sqrt(x))(y) == y**2 - x
    assert minimal_polynomial(sqrt(x), method='groebner')(y) == y**2 - x

    assert minimal_polynomial(sqrt(x + 1))(y) == y**2 - x - 1
    assert minimal_polynomial(sqrt(x)/x)(y) == x*y**2 - 1
    assert minimal_polynomial(sqrt(2)*sqrt(x))(y) == y**2 - 2 * x

    assert minimal_polynomial(sqrt(2) + sqrt(x))(y) == \
        y**4 - 2*x*y**2 - 4*y**2 + x**2 - 4*x + 4
    assert minimal_polynomial(sqrt(2) + sqrt(x), method='groebner')(y) == \
        y**4 - 2*x*y**2 - 4*y**2 + x**2 - 4*x + 4

    assert minimal_polynomial(cbrt(x))(y) == y**3 - x
    assert minimal_polynomial(cbrt(x) + sqrt(x))(y) == \
        y**6 - 3*x*y**4 - 2*x*y**3 + 3*x**2*y**2 - 6*x**2*y - x**3 + x**2

    assert minimal_polynomial(sqrt(x)/z)(y) == z**2*y**2 - x
    assert minimal_polynomial(sqrt(x)/(z + 1))(y) == z**2*y**2 + 2*z*y**2 + y**2 - x

    assert minimal_polynomial(1/x) == PurePoly(x*y - 1, y)
    assert minimal_polynomial(1/(x + 1)) == PurePoly((x + 1)*y - 1, y)
    assert minimal_polynomial(sqrt(x)) == PurePoly(y**2 - x, y)
    assert minimal_polynomial(sqrt(x) / z) == PurePoly(z**2*y**2 - x, y)

    # this is (sqrt(1 + x**3)/x).integrate(x).diff(x) - sqrt(1 + x**3)/x
    a = sqrt(x)/sqrt(1 + x**(-3)) - sqrt(x**3 + 1)/x + 1/(x**Rational(5, 2) *
                                                          (1 + x**(-3))**Rational(3, 2)) + 1/(x**Rational(11, 2)*(1 + x**(-3))**Rational(3, 2))

    assert minimal_polynomial(a)(y) == y

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(exp(x)))
Exemplo n.º 2
0
def test_reduce_inequalities_general():
    assert reduce_inequalities(sqrt(2) * x >= 1) == (sqrt(2) / 2 <= x)
    assert reduce_inequalities(PurePoly(x + 1, x) > 0) == (Integer(-1) < x)

    # issue sympy/sympy#10196
    assert reduce_inequalities(x**2 >= 0)
    assert reduce_inequalities(x**2 < 0) is false
Exemplo n.º 3
0
def test_sympyissue_18874():
    e = [sqrt(2) + sqrt(5), sqrt(2)]

    assert primitive_element(e) == (PurePoly(x**4 - 46 * x**2 + 169),
                                    [1, 2], [[0, QQ(-20, 39), 0,
                                              QQ(1, 39)],
                                             [0, QQ(59, 78), 0,
                                              QQ(-1, 78)]])
Exemplo n.º 4
0
def test_primitive_element():
    assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]])

    assert (primitive_element([sqrt(2), sqrt(3)]) ==
            (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0],
                                                    [QQ(-1, 2), 0, QQ(11, 2), 0]]))
    A = QQ.algebraic_field(sqrt(2))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))
    assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) ==
            (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1],
             [[A.unit], [1, -A.unit]]))

    A = QQ.algebraic_field(sqrt(2) + sqrt(3))
    assert (primitive_element([sqrt(2), sqrt(3)], domain=A) ==
            (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1],
             [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]]))

    pytest.raises(ValueError, lambda: primitive_element([]))

    # issue sympy/sympy#13849
    assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) ==
            (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0],
                                                    [QQ(-1, 24), 0, QQ(43, 12), 0]]))

    # issue sympy/sympy#14117
    assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) ==
            (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0],
                                                        [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
Exemplo n.º 5
0
def test_solve_poly_inequality():
    assert psolve(Poly(0, x), '==') == [S.Reals]
    assert psolve(Poly(1, x), '==') == [S.EmptySet]
    assert psolve(PurePoly(x + 1, x), '>') == [Interval(-1, oo, True, True)]
    pytest.raises(ValueError, lambda: psolve(x, '=='))
    pytest.raises(ValueError, lambda: psolve(Poly(x, x), '??'))

    assert (solve_poly_inequalities(
        ((Poly(x**2 - 3), '>'),
         (Poly(-x**2 + 1), '>'))) == Union(Interval(-oo, -sqrt(3), True, True),
                                           Interval(-1, 1, True, True),
                                           Interval(sqrt(3), oo, True, True)))
Exemplo n.º 6
0
def test_solve_poly_inequality():
    assert psolve(Integer(0).as_poly(x), '==') == [S.ExtendedReals]
    assert psolve(Integer(1).as_poly(x), '==') == [S.EmptySet]
    assert psolve(PurePoly(x + 1, x), '>') == [Interval(-1, oo, True, False)]
    pytest.raises(ValueError, lambda: psolve(x, '=='))
    pytest.raises(ValueError, lambda: psolve(x.as_poly(), '??'))

    assert (solve_poly_inequalities(
        (((x**2 - 3).as_poly(), '>'),
         ((-x**2 + 1).as_poly(),
          '>'))) == Union(Interval(-oo, -sqrt(3), False, True),
                          Interval(-1, 1, True, True),
                          Interval(sqrt(3), oo, True, False)))
Exemplo n.º 7
0
def test_minpoly_domain():
    F = QQ.algebraic_field(sqrt(2))

    assert minimal_polynomial(sqrt(2), domain=F) == PurePoly(x - sqrt(2), x, domain=F)
    assert minimal_polynomial(sqrt(8), domain=F)(x) == x - 2*sqrt(2)
    assert minimal_polynomial(sqrt(Rational(3, 2)), domain=F)(x) == 2*x**2 - 3

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(y, domain=QQ))

    # issue sympy/sympy#14494

    F = QQ.algebraic_field(I)
    assert minimal_polynomial(I, domain=F)(x) == x - I

    F = QQ.algebraic_field(sqrt(3)*I)
    assert minimal_polynomial(exp(I*pi/3), domain=F)(x) == 2*x - sqrt(3)*I - 1
Exemplo n.º 8
0
def test_solve_poly_inequality():
    assert psolve(Poly(0, x), '==') == [S.Reals]
    assert psolve(Poly(1, x), '==') == [S.EmptySet]
    assert psolve(PurePoly(x + 1, x), ">") == [Interval(-1, oo, True, True)]
    pytest.raises(ValueError, lambda: psolve(x, '=='))
    pytest.raises(ValueError, lambda: psolve(Poly(x, x), '??'))
Exemplo n.º 9
0
def test_reduce_inequalities_general():
    assert reduce_inequalities(Ge(sqrt(2)*x, 1)) == And(sqrt(2)/2 <= x, x < oo)
    assert reduce_inequalities(PurePoly(x + 1, x) > 0) == And(Integer(-1) < x, x < oo)
Exemplo n.º 10
0
def test_minimal_polynomial():
    assert minimal_polynomial(-7)(x) == x + 7
    assert minimal_polynomial(-1)(x) == x + 1
    assert minimal_polynomial(+0)(x) == x
    assert minimal_polynomial(+1)(x) == x - 1
    assert minimal_polynomial(+7)(x) == x - 7

    assert minimal_polynomial(Rational(1, 3),
                              method='groebner')(x) == 3 * x - 1

    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(pi, method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(sin(sqrt(2)), method='groebner'))
    pytest.raises(NotAlgebraic,
                  lambda: minimal_polynomial(2**pi, method='groebner'))

    pytest.raises(ValueError, lambda: minimal_polynomial(1, method='spam'))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2
    assert minimal_polynomial(sqrt(5))(x) == x**2 - 5
    assert minimal_polynomial(sqrt(6))(x) == x**2 - 6

    assert minimal_polynomial(2 * sqrt(2))(x) == x**2 - 8
    assert minimal_polynomial(3 * sqrt(5))(x) == x**2 - 45
    assert minimal_polynomial(4 * sqrt(6))(x) == x**2 - 96

    assert minimal_polynomial(2 * sqrt(2) + 3)(x) == x**2 - 6 * x + 1
    assert minimal_polynomial(3 * sqrt(5) + 6)(x) == x**2 - 12 * x - 9
    assert minimal_polynomial(4 * sqrt(6) + 7)(x) == x**2 - 14 * x - 47

    assert minimal_polynomial(2 * sqrt(2) - 3)(x) == x**2 + 6 * x + 1
    assert minimal_polynomial(3 * sqrt(5) - 6)(x) == x**2 + 12 * x - 9
    assert minimal_polynomial(4 * sqrt(6) - 7)(x) == x**2 + 14 * x - 47

    assert minimal_polynomial(sqrt(1 + sqrt(6)))(x) == x**4 - 2 * x**2 - 5
    assert minimal_polynomial(sqrt(I + sqrt(6)))(x) == x**8 - 10 * x**4 + 49

    assert minimal_polynomial(2 * I +
                              sqrt(2 + I))(x) == x**4 + 4 * x**2 + 8 * x + 37

    assert minimal_polynomial(sqrt(2) + sqrt(3))(x) == x**4 - 10 * x**2 + 1
    assert minimal_polynomial(sqrt(2) + sqrt(3) +
                              sqrt(6))(x) == x**4 - 22 * x**2 - 48 * x - 23

    e = 1 / sqrt(sqrt(1 + sqrt(3)) - 4)
    assert minimal_polynomial(e) == minimal_polynomial(e, method='groebner')
    assert minimal_polynomial(e)(x) == (222 * x**8 + 240 * x**6 + 94 * x**4 +
                                        16 * x**2 + 1)

    a = 1 - 9 * sqrt(2) + 7 * sqrt(3)

    assert minimal_polynomial(
        1 / a)(x) == 392 * x**4 - 1232 * x**3 + 612 * x**2 + 4 * x - 1
    assert minimal_polynomial(
        1 / sqrt(a))(x) == 392 * x**8 - 1232 * x**6 + 612 * x**4 + 4 * x**2 - 1

    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(oo))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(2**y))
    pytest.raises(NotAlgebraic, lambda: minimal_polynomial(sin(1)))

    assert minimal_polynomial(sqrt(2))(x) == x**2 - 2

    assert minimal_polynomial(sqrt(2)) == PurePoly(x**2 - 2)
    assert minimal_polynomial(sqrt(2), method='groebner') == PurePoly(x**2 - 2)

    a = sqrt(2)
    b = sqrt(3)

    assert minimal_polynomial(b)(x) == x**2 - 3

    assert minimal_polynomial(a) == PurePoly(x**2 - 2)
    assert minimal_polynomial(b) == PurePoly(x**2 - 3)

    assert minimal_polynomial(sqrt(a)) == PurePoly(x**4 - 2)
    assert minimal_polynomial(a + 1) == PurePoly(x**2 - 2 * x - 1)
    assert minimal_polynomial(sqrt(a / 2 +
                                   17))(x) == 2 * x**4 - 68 * x**2 + 577
    assert minimal_polynomial(sqrt(b / 2 +
                                   17))(x) == 4 * x**4 - 136 * x**2 + 1153

    # issue diofant/diofant#431
    K = QQ.algebraic_field(sqrt(2))
    theta = K.to_expr(K([17, Rational(1, 2)]))
    assert minimal_polynomial(theta)(x) == 2 * x**2 - 68 * x + 577

    K = QQ.algebraic_field(RootOf(x**7 + x - 1, 3))
    theta = K.to_expr(K([1, 0, 0, 2, 1]))
    ans = minimal_polynomial(theta)(x)
    assert ans == (x**7 - 7 * x**6 + 19 * x**5 - 27 * x**4 + 63 * x**3 -
                   115 * x**2 + 82 * x - 147)
    assert minimal_polynomial(theta, method='groebner')(x) == ans
    K = QQ.algebraic_field(RootOf(x**5 + 5 * x - 1, 2))
    theta = K.to_expr(K([1, -1, 1]))
    ans = (x**30 - 15 * x**28 - 10 * x**27 + 135 * x**26 + 330 * x**25 -
           705 * x**24 - 150 * x**23 + 3165 * x**22 - 6850 * x**21 +
           7182 * x**20 + 3900 * x**19 + 4435 * x**18 + 11970 * x**17 -
           259725 * x**16 - 18002 * x**15 + 808215 * x**14 - 200310 * x**13 -
           647115 * x**12 + 299280 * x**11 - 1999332 * x**10 + 910120 * x**9 +
           2273040 * x**8 - 5560320 * x**7 + 5302000 * x**6 - 2405376 * x**5 +
           1016640 * x**4 - 804480 * x**3 + 257280 * x**2 - 53760 * x + 1280)
    assert minimal_polynomial(sqrt(theta) + root(theta, 3),
                              method='groebner')(x) == ans
    K1 = QQ.algebraic_field(RootOf(x**3 + 4 * x - 15, 1))
    K2 = QQ.algebraic_field(RootOf(x**3 - x + 1, 0))
    theta = sqrt(1 + 1 / (K1.to_expr(K1([1, 0, 1])) + 1 /
                          (sqrt(3) + K2.to_expr(K2([-1, 2, 1])))))
    ans = (2262264837876687263 * x**36 - 38939909597855051866 * x**34 +
           315720420314462950715 * x**32 - 1601958657418182606114 * x**30 +
           5699493671077371036494 * x**28 - 15096777696140985506150 * x**26 +
           30847690820556462893974 * x**24 - 49706549068200640994022 * x**22 +
           64013601241426223813103 * x**20 - 66358713088213594372990 * x**18 +
           55482571280904904971976 * x**16 - 37309340229165533529076 * x**14 +
           20016999328983554519040 * x**12 - 8446273798231518826782 * x**10 +
           2738866994867366499481 * x**8 - 657825125060873756424 * x**6 +
           110036313740049140508 * x**4 - 11416087328869938298 * x**2 +
           551322649782053543)
    assert minimal_polynomial(theta)(x) == ans

    a = sqrt(2) / 3 + 7
    f = 81*x**8 - 2268*x**6 - 4536*x**5 + 22644*x**4 + 63216*x**3 - \
        31608*x**2 - 189648*x + 141358

    assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)))(x) == f

    assert minimal_polynomial(a**Rational(
        3, 2))(x) == 729 * x**4 - 506898 * x**2 + 84604519

    K = QQ.algebraic_field(RootOf(x**3 + x - 1, 0))
    a = K.to_expr(K([0, 1]))
    assert minimal_polynomial(1 / a**2)(x) == x**3 - x**2 - 2 * x - 1

    # issue sympy/sympy#5994
    eq = (-1 / (800 * sqrt(
        Rational(-1, 240) + 1 /
        (18000 * cbrt(Rational(-1, 17280000) + sqrt(15) * I / 28800000)) +
        2 * cbrt(Rational(-1, 17280000) + sqrt(15) * I / 28800000))))
    assert minimal_polynomial(eq)(x) == 8000 * x**2 - 1

    ex = 1 + sqrt(2) + sqrt(3)
    mp = minimal_polynomial(ex)(x)
    assert mp == x**4 - 4 * x**3 - 4 * x**2 + 16 * x - 8

    ex = 1 / (1 + sqrt(2) + sqrt(3))
    mp = minimal_polynomial(ex)(x)
    assert mp == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1

    p = cbrt(expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3))
    mp = minimal_polynomial(p)(x)
    assert mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008
    p = expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3)
    mp = minimal_polynomial(p)(x)
    assert mp == x**8 - 512 * x**7 - 118208 * x**6 + 31131136 * x**5 + 647362560 * x**4 - 56026611712 * x**3 + 116994310144 * x**2 + 404854931456 * x - 27216576512

    assert minimal_polynomial(-sqrt(5) / 2 - Rational(1, 2) +
                              (-sqrt(5) / 2 - Rational(1, 2))**2)(x) == x - 1
    a = 1 + sqrt(2)
    assert minimal_polynomial((a * sqrt(2) + a)**3)(x) == x**2 - 198 * x + 1

    p = 1 / (1 + sqrt(2) + sqrt(3))
    assert minimal_polynomial(
        p, method='groebner')(x) == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1

    p = 2 / (1 + sqrt(2) + sqrt(3))
    assert minimal_polynomial(
        p, method='groebner')(x) == x**4 - 4 * x**3 + 2 * x**2 + 4 * x - 2

    assert minimal_polynomial(1 + sqrt(2) * I,
                              method='groebner')(x) == x**2 - 2 * x + 3
    assert minimal_polynomial(1 / (1 + sqrt(2)) + 1,
                              method='groebner')(x) == x**2 - 2
    assert minimal_polynomial(sqrt(2) * I + I * (1 + sqrt(2)),
                              method='groebner')(x) == x**4 + 18 * x**2 + 49

    assert minimal_polynomial(exp_polar(0))(x) == x - 1
Exemplo n.º 11
0
def test_sparse_matrix():
    def sparse_eye(n):
        return SparseMatrix.eye(n)

    def sparse_zeros(n):
        return SparseMatrix.zeros(n)

    # creation args
    pytest.raises(TypeError, lambda: SparseMatrix(1, 2))
    pytest.raises(ValueError, lambda: SparseMatrix(2, 2, (1, 3, 4, 5, 6)))

    a = SparseMatrix(((1, 0), (0, 1)))
    assert SparseMatrix(a) == a

    a = MutableSparseMatrix([])
    b = MutableDenseMatrix([1, 2])
    assert a.row_join(b) == b
    assert a.col_join(b) == b
    assert type(a.row_join(b)) == type(a)
    assert type(a.col_join(b)) == type(a)

    # test element assignment
    a = SparseMatrix(((1, 0), (0, 1)))

    a[3] = 4
    assert a[1, 1] == 4
    a[3] = 1

    a[0, 0] = 2
    assert a == SparseMatrix(((2, 0), (0, 1)))
    a[1, 0] = 5
    assert a == SparseMatrix(((2, 0), (5, 1)))
    a[1, 1] = 0
    assert a == SparseMatrix(((2, 0), (5, 0)))
    assert a._smat == {(0, 0): 2, (1, 0): 5}

    # test_multiplication
    a = SparseMatrix((
        (1, 2),
        (3, 1),
        (0, 6),
    ))

    b = SparseMatrix((
        (1, 2),
        (3, 0),
    ))

    c = a * b
    assert c[0, 0] == 7
    assert c[0, 1] == 2
    assert c[1, 0] == 6
    assert c[1, 1] == 6
    assert c[2, 0] == 18
    assert c[2, 1] == 0

    c = b * x
    assert isinstance(c, SparseMatrix)
    assert c[0, 0] == x
    assert c[0, 1] == 2 * x
    assert c[1, 0] == 3 * x
    assert c[1, 1] == 0

    c = 5 * b
    assert isinstance(c, SparseMatrix)
    assert c[0, 0] == 5
    assert c[0, 1] == 2 * 5
    assert c[1, 0] == 3 * 5
    assert c[1, 1] == 0

    # test_power
    A = SparseMatrix([[2, 3], [4, 5]])
    assert (A**5)[:] == [6140, 8097, 10796, 14237]
    A = SparseMatrix([[2, 1, 3], [4, 2, 4], [6, 12, 1]])
    assert (A**3)[:] == [290, 262, 251, 448, 440, 368, 702, 954, 433]

    # test_creation
    a = SparseMatrix([[x, 0], [0, 0]])
    m = a
    assert m.cols == m.rows
    assert m.cols == 2
    assert m[:] == [x, 0, 0, 0]
    b = SparseMatrix(2, 2, [x, 0, 0, 0])
    m = b
    assert m.cols == m.rows
    assert m.cols == 2
    assert m[:] == [x, 0, 0, 0]

    assert a == b
    S = sparse_eye(3)
    del S[1, :]
    assert S == SparseMatrix([[1, 0, 0], [0, 0, 1]])
    S = sparse_eye(3)
    del S[:, 1]
    assert S == SparseMatrix([[1, 0], [0, 0], [0, 1]])
    S = SparseMatrix.eye(3)
    S[2, 1] = 2
    S.col_swap(1, 0)
    assert S == SparseMatrix([[0, 1, 0], [1, 0, 0], [2, 0, 1]])
    S.row_swap(0, 1)
    assert S == SparseMatrix([[1, 0, 0], [0, 1, 0], [2, 0, 1]])
    S.col_swap(0, 1)
    assert S == SparseMatrix([[0, 1, 0], [1, 0, 0], [0, 2, 1]])
    S.row_swap(0, 2)
    assert S == SparseMatrix([[0, 2, 1], [1, 0, 0], [0, 1, 0]])

    S.col_swap(0, 2)
    assert S == SparseMatrix([[1, 2, 0], [0, 0, 1], [0, 1, 0]])

    a = SparseMatrix(1, 2, [1, 2])
    b = a.copy()
    c = a.copy()
    assert a[0] == 1
    del a[0, :]
    assert a == SparseMatrix(0, 2, [])
    del b[:, 1]
    assert b == SparseMatrix(1, 1, [1])

    # test_determinant
    assert SparseMatrix(1, 1, [0]).det() == 0
    assert SparseMatrix([[1]]).det() == 1
    assert SparseMatrix(((-3, 2), (8, -5))).det() == -1
    assert SparseMatrix(((x, 1), (y, 2 * y))).det() == 2 * x * y - y
    assert SparseMatrix(((1, 1, 1), (1, 2, 3), (1, 3, 6))).det() == 1
    assert SparseMatrix(((3, -2, 0, 5), (-2, 1, -2, 2), (0, -2, 5, 0),
                         (5, 0, 3, 4))).det() == -289
    assert SparseMatrix(((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12),
                         (13, 14, 15, 16))).det() == 0
    assert SparseMatrix(((3, 2, 0, 0, 0), (0, 3, 2, 0, 0), (0, 0, 3, 2, 0),
                         (0, 0, 0, 3, 2), (2, 0, 0, 0, 3))).det() == 275
    assert SparseMatrix(((1, 0, 1, 2, 12), (2, 0, 1, 1, 4), (2, 1, 1, -1, 3),
                         (3, 2, -1, 1, 8), (1, 1, 1, 0, 6))).det() == -55
    assert SparseMatrix(((-5, 2, 3, 4, 5), (1, -4, 3, 4, 5), (1, 2, -3, 4, 5),
                         (1, 2, 3, -2, 5), (1, 2, 3, 4, -1))).det() == 11664
    assert SparseMatrix(((2, 7, -1, 3, 2), (0, 0, 1, 0, 1), (-2, 0, 7, 0, 2),
                         (-3, -2, 4, 5, 3), (1, 0, 0, 0, 1))).det() == 123

    # test_slicing
    m0 = sparse_eye(4)
    assert m0[:3, :3] == sparse_eye(3)
    assert m0[2:4, 0:2] == sparse_zeros(2)

    m1 = SparseMatrix(3, 3, lambda i, j: i + j)
    assert m1[0, :] == SparseMatrix(1, 3, (0, 1, 2))
    assert m1[1:3, 1] == SparseMatrix(2, 1, (2, 3))

    m2 = SparseMatrix([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11],
                       [12, 13, 14, 15]])
    assert m2[:, -1] == SparseMatrix(4, 1, [3, 7, 11, 15])
    assert m2[-2:, :] == SparseMatrix([[8, 9, 10, 11], [12, 13, 14, 15]])

    assert SparseMatrix([[1, 2], [3, 4]])[[1], [1]] == Matrix([[4]])

    # test_submatrix_assignment
    m = sparse_zeros(4)
    m[2:4, 2:4] = sparse_eye(2)
    assert m == SparseMatrix([(0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 1, 0),
                              (0, 0, 0, 1)])
    assert len(m._smat) == 2
    m[:2, :2] = sparse_eye(2)
    assert m == sparse_eye(4)
    m[:, 0] = SparseMatrix(4, 1, (1, 2, 3, 4))
    assert m == SparseMatrix([(1, 0, 0, 0), (2, 1, 0, 0), (3, 0, 1, 0),
                              (4, 0, 0, 1)])
    m[:, :] = sparse_zeros(4)
    assert m == sparse_zeros(4)
    m[:, :] = ((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16))
    assert m == SparseMatrix(
        ((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)))
    m[:2, 0] = [0, 0]
    assert m == SparseMatrix(
        ((0, 2, 3, 4), (0, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)))

    # test_reshape
    m0 = sparse_eye(3)
    assert m0.reshape(1, 9) == SparseMatrix(1, 9, (1, 0, 0, 0, 1, 0, 0, 0, 1))
    m1 = SparseMatrix(3, 4, lambda i, j: i + j)
    assert m1.reshape(4, 3) == \
        SparseMatrix([(0, 1, 2), (3, 1, 2), (3, 4, 2), (3, 4, 5)])
    assert m1.reshape(2, 6) == \
        SparseMatrix([(0, 1, 2, 3, 1, 2), (3, 4, 2, 3, 4, 5)])

    # test_applyfunc
    m0 = sparse_eye(3)
    assert m0.applyfunc(lambda x: 2 * x) == sparse_eye(3) * 2
    assert m0.applyfunc(lambda x: 0) == sparse_zeros(3)

    # test_LUdecomp
    testmat = SparseMatrix([[0, 2, 5, 3], [3, 3, 7, 4], [8, 4, 0, 2],
                            [-2, 6, 3, 4]])
    L, U, p = testmat.LUdecomposition()
    assert L.is_lower
    assert U.is_upper
    assert (L * U).permuteBkwd(p) - testmat == sparse_zeros(4)

    testmat = SparseMatrix([[6, -2, 7, 4], [0, 3, 6, 7], [1, -2, 7, 4],
                            [-9, 2, 6, 3]])
    L, U, p = testmat.LUdecomposition()
    assert L.is_lower
    assert U.is_upper
    assert (L * U).permuteBkwd(p) - testmat == sparse_zeros(4)

    M = Matrix(((1, x, 1), (2, y, 0), (y, 0, z)))
    L, U, p = M.LUdecomposition()
    assert L.is_lower
    assert U.is_upper
    assert (L * U).permuteBkwd(p) - M == sparse_zeros(3)

    # test_LUsolve
    A = SparseMatrix([[2, 3, 5], [3, 6, 2], [8, 3, 6]])
    B = SparseMatrix(3, 1, [3, 7, 5])
    b = A * B
    soln = A.LUsolve(b)
    assert soln == B
    A = SparseMatrix([[0, -1, 2], [5, 10, 7], [8, 3, 4]])
    B = SparseMatrix(3, 1, [-1, 2, 5])
    b = A * B
    soln = A.LUsolve(b)
    assert soln == B

    # test_inverse
    A = sparse_eye(4)
    assert A.inv() == sparse_eye(4)
    assert A.inv(method='CH') == sparse_eye(4)
    assert A.inv(method='LDL') == sparse_eye(4)

    A = SparseMatrix([[2, 3, 5], [3, 6, 2], [7, 2, 6]])
    Ainv = SparseMatrix(Matrix(A).inv())
    assert A * Ainv == sparse_eye(3)
    assert A.inv(method='CH') == Ainv
    assert A.inv(method='LDL') == Ainv

    A = SparseMatrix([[2, 3, 5], [3, 6, 2], [5, 2, 6]])
    Ainv = SparseMatrix(Matrix(A).inv())
    assert A * Ainv == sparse_eye(3)
    assert A.inv(method='CH') == Ainv
    assert A.inv(method='LDL') == Ainv

    # test_cross
    v1 = Matrix(1, 3, [1, 2, 3])
    v2 = Matrix(1, 3, [3, 4, 5])
    assert v1.cross(v2) == Matrix(1, 3, [-2, 4, -2])
    assert v1.norm(2)**2 == 14

    # conjugate
    a = SparseMatrix(((1, 2 + I), (3, 4)))
    assert a.C == SparseMatrix([[1, 2 - I], [3, 4]])

    # mul
    assert a * Matrix(2, 2, [1, 0, 0, 1]) == a
    assert a + Matrix(2, 2, [1, 1, 1, 1]) == SparseMatrix([[2, 3 + I], [4, 5]])
    assert a * 0 == Matrix([[0, 0], [0, 0]])

    # col join
    assert a.col_join(sparse_eye(2)) == SparseMatrix([[1, 2 + I], [3, 4],
                                                      [1, 0], [0, 1]])
    A = SparseMatrix(ones(3))
    B = eye(3)
    assert A.col_join(B) == Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 0, 0],
                                    [0, 1, 0], [0, 0, 1]])

    # row join
    A = SparseMatrix(((1, 0, 1), (0, 1, 0), (1, 1, 0)))
    B = Matrix(((1, 0, 0), (0, 1, 0), (0, 0, 1)))
    assert A.row_join(B) == Matrix([[1, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0],
                                    [1, 1, 0, 0, 0, 1]])

    # symmetric
    assert not a.is_symmetric(simplify=False)
    assert sparse_eye(3).is_symmetric(simplify=False)

    # test_cofactor
    assert sparse_eye(3) == sparse_eye(3).cofactorMatrix()
    test = SparseMatrix([[1, 3, 2], [2, 6, 3], [2, 3, 6]])
    assert test.cofactorMatrix() == \
        SparseMatrix([[27, -6, -6], [-12, 2, 3], [-3, 1, 0]])
    test = SparseMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert test.cofactorMatrix() == \
        SparseMatrix([[-3, 6, -3], [6, -12, 6], [-3, 6, -3]])

    # test_jacobian
    L = SparseMatrix(1, 2, [x**2 * y, 2 * y**2 + x * y])
    syms = [x, y]
    assert L.jacobian(syms) == Matrix([[2 * x * y, x**2], [y, 4 * y + x]])

    L = SparseMatrix(1, 2, [x, x**2 * y**3])
    assert L.jacobian(syms) == SparseMatrix([[1, 0],
                                             [2 * x * y**3, x**2 * 3 * y**2]])

    # test_QR
    A = Matrix([[1, 2], [2, 3]])
    Q, S = A.QRdecomposition()
    R = Rational
    assert Q == Matrix([[5**R(-1, 2), (R(2) / 5) * (R(1) / 5)**R(-1, 2)],
                        [2 * 5**R(-1, 2), (-R(1) / 5) * (R(1) / 5)**R(-1, 2)]])
    assert S == Matrix([[5**R(1, 2), 8 * 5**R(-1, 2)],
                        [0, (R(1) / 5)**R(1, 2)]])
    assert Q * S == A
    assert Q.T * Q == sparse_eye(2)

    R = Rational
    # test nullspace
    # first test reduced row-ech form

    M = SparseMatrix([[5, 7, 2, 1], [1, 6, 2, -1]])
    out, tmp = M.rref()
    assert out == Matrix([[1, 0, -R(2) / 23, R(13) / 23],
                          [0, 1, R(8) / 23, R(-6) / 23]])

    M = SparseMatrix([[1, 3, 0, 2, 6, 3, 1], [-2, -6, 0, -2, -8, 3, 1],
                      [3, 9, 0, 0, 6, 6, 2], [-1, -3, 0, 1, 0, 9, 3]])

    out, tmp = M.rref()
    assert out == Matrix([[1, 3, 0, 0, 2, 0, 0], [0, 0, 0, 1, 2, 0, 0],
                          [0, 0, 0, 0, 0, 1, R(1) / 3], [0, 0, 0, 0, 0, 0, 0]])
    # now check the vectors
    basis = M.nullspace()
    assert basis[0] == Matrix([-3, 1, 0, 0, 0, 0, 0])
    assert basis[1] == Matrix([0, 0, 1, 0, 0, 0, 0])
    assert basis[2] == Matrix([-2, 0, 0, -2, 1, 0, 0])
    assert basis[3] == Matrix([0, 0, 0, 0, 0, R(-1) / 3, 1])

    # test eigen
    sparse_eye3 = sparse_eye(3)
    assert sparse_eye3.charpoly(x) == PurePoly(((x - 1)**3))
    assert sparse_eye3.charpoly(y) == PurePoly(((y - 1)**3))

    # test values
    M = Matrix([(0, 1, -1), (1, 1, 0), (-1, 0, 1)])
    vals = M.eigenvals()
    assert sorted(vals) == [-1, 1, 2]

    R = Rational
    M = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    assert M.eigenvects() == [
        (1, 3, [Matrix([1, 0, 0]),
                Matrix([0, 1, 0]),
                Matrix([0, 0, 1])])
    ]
    M = Matrix([[5, 0, 2], [3, 2, 0], [0, 0, 1]])
    assert M.eigenvects() == [(1, 1, [Matrix([R(-1) / 2,
                                              R(3) / 2, 1])]),
                              (2, 1, [Matrix([0, 1, 0])]),
                              (5, 1, [Matrix([1, 1, 0])])]

    assert M.zeros(3, 5) == SparseMatrix(3, 5, {})
    A = SparseMatrix(
        10, 10, {
            (0, 0): 18,
            (0, 9): 12,
            (1, 4): 18,
            (2, 7): 16,
            (3, 9): 12,
            (4, 2): 19,
            (5, 7): 16,
            (6, 2): 12,
            (9, 7): 18
        })
    assert A.row_list() == [(0, 0, 18), (0, 9, 12), (1, 4, 18), (2, 7, 16),
                            (3, 9, 12), (4, 2, 19), (5, 7, 16), (6, 2, 12),
                            (9, 7, 18)]
    assert A.col_list() == [(0, 0, 18), (4, 2, 19), (6, 2, 12), (1, 4, 18),
                            (2, 7, 16), (5, 7, 16), (9, 7, 18), (0, 9, 12),
                            (3, 9, 12)]
    assert SparseMatrix.eye(2).nnz() == 2

    M = SparseMatrix.eye(3) * 2
    M[1, 0] = -1
    M.col_op(1, lambda v, i: v + 2 * M[i, 0])
    assert M == Matrix([[2, 4, 0], [-1, 0, 0], [0, 0, 2]])

    M = SparseMatrix.zeros(3)
    M.fill(1)
    assert M == ones(3)

    assert SparseMatrix(ones(0, 3)).tolist() == []
Exemplo n.º 12
0
def test_RootOf___new__():
    assert RootOf(x, 0) == 0
    assert RootOf(x, -1) == 0

    assert RootOf(x - 1, 0) == 1
    assert RootOf(x - 1, -1) == 1

    assert RootOf(x + 1, 0) == -1
    assert RootOf(x + 1, -1) == -1

    assert RootOf(x**2 + 2 * x + 3, 0) == -1 - I * sqrt(2)
    assert RootOf(x**2 + 2 * x + 3, 1) == -1 + I * sqrt(2)
    assert RootOf(x**2 + 2 * x + 3, -1) == -1 + I * sqrt(2)
    assert RootOf(x**2 + 2 * x + 3, -2) == -1 - I * sqrt(2)

    r = RootOf(x**2 + 2 * x + 3, 0, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2 * x + 3, 1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2 * x + 3, -1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2 * x + 3, -2, radicals=False)
    assert isinstance(r, RootOf) is True

    assert RootOf((x - 1) * (x + 1), 0, radicals=False) == -1
    assert RootOf((x - 1) * (x + 1), 1, radicals=False) == 1
    assert RootOf((x - 1) * (x + 1), -1, radicals=False) == 1
    assert RootOf((x - 1) * (x + 1), -2, radicals=False) == -1

    assert RootOf((x - 1) * (x + 1), 0, radicals=True) == -1
    assert RootOf((x - 1) * (x + 1), 1, radicals=True) == 1
    assert RootOf((x - 1) * (x + 1), -1, radicals=True) == 1
    assert RootOf((x - 1) * (x + 1), -2, radicals=True) == -1

    assert RootOf((x - 1) * (x**3 + x + 3), 0) == RootOf(x**3 + x + 3, 0)
    assert RootOf((x - 1) * (x**3 + x + 3), 1) == 1
    assert RootOf((x - 1) * (x**3 + x + 3), 2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1) * (x**3 + x + 3), 3) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1) * (x**3 + x + 3), -1) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1) * (x**3 + x + 3), -2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1) * (x**3 + x + 3), -3) == 1
    assert RootOf((x - 1) * (x**3 + x + 3), -4) == RootOf(x**3 + x + 3, 0)

    assert RootOf(x**4 + 3 * x**3, 0) == -3
    assert RootOf(x**4 + 3 * x**3, 1) == 0
    assert RootOf(x**4 + 3 * x**3, 2) == 0
    assert RootOf(x**4 + 3 * x**3, 3) == 0

    pytest.raises(GeneratorsNeeded, lambda: RootOf(0, 0))
    pytest.raises(GeneratorsNeeded, lambda: RootOf(1, 0))

    pytest.raises(PolynomialError, lambda: RootOf(Integer(0).as_poly(x), 0))
    pytest.raises(PolynomialError, lambda: RootOf(Integer(1).as_poly(x), 0))

    pytest.raises(PolynomialError, lambda: RootOf(x - y, 0))

    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -4))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -3))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 2))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 3))
    pytest.raises(ValueError, lambda: RootOf(x**2 - 1, x))
    pytest.raises(
        NotImplementedError,
        lambda: RootOf(Symbol('a', nonzero=False) * x**5 + 2 * x - 1, x, 0))
    pytest.raises(
        NotImplementedError, lambda:
        (Symbol('a', nonzero=False) * x**5 + 2 * x - 1).as_poly(x).all_roots())

    assert RootOf((x - y).as_poly(x), 0) == y

    assert RootOf((x**2 - y).as_poly(x), 0) == -sqrt(y)
    assert RootOf((x**2 - y).as_poly(x), 1) == sqrt(y)

    assert isinstance(RootOf(x**3 - y, x, 0), RootOf)
    p = Symbol('p', positive=True)
    assert RootOf(x**3 - p, x, 0) == root(p, 3) * RootOf(x**3 - 1, 0)

    assert RootOf(y * x**3 + y * x + 2 * y, x, 0) == -1

    assert RootOf(x**3 + x + 1, 0).is_commutative is True

    e = RootOf(x**2 - 4, x, 1, evaluate=False)
    assert isinstance(e, RootOf)
    assert e.doit() == 2
    assert e.args == (x**2 - 4, x, 1)
    assert e.poly == PurePoly(x**2 - 4, x)
    assert e.index == 1

    assert RootOf(x**7 - 0.1 * x + 1, 0) == RootOf(10 * x**7 - x + 10, 0)

    e = x**7 - x
    p = (x**7 - x).as_poly(modulus=7)
    F7 = p.domain
    assert (RootOf(p, 1) == RootOf(e, 1, modulus=7) == RootOf(
        e, x, 1, modulus=7) == RootOf(p, 1, evaluate=False) == RootOf(
            e, 1, domain=F7, evaluate=False))
Exemplo n.º 13
0
def test_pickling_polys_polytools():
    for c in (Poly, Poly(x, x), PurePoly, PurePoly(x)):
        check(c)

    for c in (GroebnerBasis, GroebnerBasis([x**2 - 1], x)):
        check(c)
Exemplo n.º 14
0
def test_solve_poly_inequality():
    assert psolve(Poly(0, x), '==') == [S.Reals]
    assert psolve(Poly(1, x), '==') == [S.EmptySet]
    assert psolve(PurePoly(x + 1, x), ">") == [Interval(-1, oo, True, True)]