Example #1
0
def test_chebyshevt_poly():
    pytest.raises(ValueError, lambda: chebyshevt_poly(-1, x))

    assert chebyshevt_poly(1, x, polys=True) == x.as_poly()

    assert chebyshevt_poly(0, x) == 1
    assert chebyshevt_poly(1, x) == x
    assert chebyshevt_poly(2, x) == 2 * x**2 - 1
    assert chebyshevt_poly(3, x) == 4 * x**3 - 3 * x
    assert chebyshevt_poly(4, x) == 8 * x**4 - 8 * x**2 + 1
    assert chebyshevt_poly(5, x) == 16 * x**5 - 20 * x**3 + 5 * x
    assert chebyshevt_poly(6, x) == 32 * x**6 - 48 * x**4 + 18 * x**2 - 1

    assert chebyshevt_poly(1, polys=True) == x.as_poly()
Example #2
0
def test_root_factors():
    assert root_factors(Integer(1).as_poly(x)) == [Integer(1).as_poly(x)]
    assert root_factors(x.as_poly()) == [x.as_poly()]

    assert root_factors(x**2 - 1, x) == [x + 1, x - 1]
    assert root_factors(x**2 - y, x) == [x - sqrt(y), x + sqrt(y)]

    assert root_factors((x**4 - 1)**2) == \
        [x + 1, x + 1, x - 1, x - 1, x - I, x - I, x + I, x + I]

    assert root_factors((x**4 - 1).as_poly(), filter='Z') == \
        [(x + 1).as_poly(), (x - 1).as_poly(), (x**2 + 1).as_poly()]
    assert root_factors(8*x**2 + 12*x**4 + 6*x**6 + x**8, x, filter='Q') == \
        [x, x, x**6 + 6*x**4 + 12*x**2 + 8]

    pytest.raises(ValueError, lambda: root_factors((x * y).as_poly()))
Example #3
0
def test_legendre_poly():
    pytest.raises(ValueError, lambda: legendre_poly(-1, x))

    assert legendre_poly(1, x, polys=True) == x.as_poly()

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

    assert legendre_poly(1, polys=True) == x.as_poly()
Example #4
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)))
Example #5
0
def test_printing():
    f, g = [[]] * 2
    e = PolynomialDivisionFailed(f, g, EX)
    assert str(e)[:57] == ("couldn't reduce degree in a polynomial "
                           'division algorithm')
    assert str(e)[-140:][:57] == ('You may want to use a different '
                                  'simplification algorithm.')

    f, g = [[]] * 2
    e = PolynomialDivisionFailed(f, g, RR)
    assert str(e)[-139:][:74] == ('Your working precision or tolerance of '
                                  'computations may be set improperly.')

    f, g = [[]] * 2
    e = PolynomialDivisionFailed(f, g, ZZ)
    assert str(e)[-168:][:80] == (
        'Zero detection is guaranteed in this '
        'coefficient domain. This may indicate a bug')

    e = OperationNotSupported(x.as_poly(), 'spam')
    assert str(e).find('spam') >= 0
    assert str(e).find('operation not supported') >= 0

    exc = PolificationFailed(1, x, x**2)
    assert str(exc).find("can't construct a polynomial from x") >= 0
    exc = PolificationFailed(1, [x], [x**2], True)
    assert str(exc).find("can't construct polynomials from x") >= 0

    e = ComputationFailed('LT', 1, exc)
    assert str(e).find('failed without generators') >= 0
    assert str(e).find('x**2') >= 0

    e = ExactQuotientFailed(x.as_poly(), (x**2).as_poly())
    assert str(e).find('does not divide') >= 0
    assert str(e).find('x**2') >= 0
    assert str(e).find('in ZZ') < 0
    e = ExactQuotientFailed(x.as_poly(), (x**2).as_poly(), ZZ)
    assert str(e).find('in ZZ') >= 0
Example #6
0
def test_sympyissue_13545():
    assert (x + 1).as_poly(x, modulus=2) + 1 == x.as_poly(x, modulus=2)
    pytest.raises(NotImplementedError,
                  lambda: x.as_poly(modulus=2) + x.as_poly(modulus=3))