Пример #1
0
def swinnerton_dyer_poly(n, x=None, **args):
    """Generates n-th Swinnerton-Dyer polynomial in `x`.  """
    from .numberfields import minimal_polynomial
    if n <= 0:
        raise ValueError(
            "can't generate Swinnerton-Dyer polynomial of order %s" % n)

    if x is not None:
        sympify(x)
    else:
        x = Dummy('x')

    if n > 3:
        p = 2
        a = [sqrt(2)]
        for i in range(2, n + 1):
            p = nextprime(p)
            a.append(sqrt(p))
        return minimal_polynomial(Add(*a), x, polys=args.get('polys', False))

    if n == 1:
        ex = x**2 - 2
    elif n == 2:
        ex = x**4 - 10 * x**2 + 1
    elif n == 3:
        ex = x**8 - 40 * x**6 + 352 * x**4 - 960 * x**2 + 576
    if not args.get('polys', False):
        return ex
    else:
        return PurePoly(ex, x)
Пример #2
0
    def _new(cls, poly, index):
        """Construct new ``RootOf`` object from raw data. """
        obj = Expr.__new__(cls)

        obj.poly = PurePoly(poly)
        obj.index = index

        try:
            _reals_cache[obj.poly] = _reals_cache[poly]
            _complexes_cache[obj.poly] = _complexes_cache[poly]
        except KeyError:
            pass

        return obj
Пример #3
0
def legendre_poly(n, x=None, **args):
    """Generates Legendre polynomial of degree `n` in `x`. """
    if n < 0:
        raise ValueError("can't generate Legendre polynomial of degree %s" % n)

    poly = DMP(dup_legendre(int(n), QQ), QQ)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #4
0
def chebyshevt_poly(n, x=None, **args):
    """Generates Chebyshev polynomial of the first kind of degree `n` in `x`. """
    if n < 0:
        raise ValueError(
            "can't generate 1st kind Chebyshev polynomial of degree %s" % n)

    poly = DMP(dup_chebyshevt(int(n), ZZ), ZZ)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #5
0
def jacobi_poly(n, a, b, x=None, **args):
    """Generates Jacobi polynomial of degree `n` in `x`. """
    if n < 0:
        raise ValueError("can't generate Jacobi polynomial of degree %s" % n)

    K, v = construct_domain([a, b], field=True)
    poly = DMP(dup_jacobi(int(n), v[0], v[1], K), K)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #6
0
def cyclotomic_poly(n, x=None, **args):
    """Generates cyclotomic polynomial of order `n` in `x`. """
    if n <= 0:
        raise ValueError("can't generate cyclotomic polynomial of order %s" %
                         n)

    poly = DMP(dup_zz_cyclotomic_poly(int(n), ZZ), ZZ)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #7
0
def gegenbauer_poly(n, a, x=None, **args):
    """Generates Gegenbauer polynomial of degree `n` in `x`. """
    if n < 0:
        raise ValueError("can't generate Gegenbauer polynomial of degree %s" %
                         n)

    K, a = construct_domain(a, field=True)
    poly = DMP(dup_gegenbauer(int(n), a, K), K)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #8
0
def spherical_bessel_fn(n, x=None, **args):
    """
    Coefficients for the spherical Bessel functions.

    Those are only needed in the jn() function.

    The coefficients are calculated from:

    fn(0, z) = 1/z
    fn(1, z) = 1/z**2
    fn(n-1, z) + fn(n+1, z) == (2*n+1)/z * fn(n, z)

    Examples
    ========

    >>> from diofant import Symbol

    >>> z = Symbol("z")
    >>> spherical_bessel_fn(1, z)
    z**(-2)
    >>> spherical_bessel_fn(2, z)
    -1/z + 3/z**3
    >>> spherical_bessel_fn(3, z)
    -6/z**2 + 15/z**4
    >>> spherical_bessel_fn(4, z)
    1/z - 45/z**3 + 105/z**5
    """

    if n < 0:
        dup = dup_spherical_bessel_fn_minus(-int(n), ZZ)
    else:
        dup = dup_spherical_bessel_fn(int(n), ZZ)

    poly = DMP(dup, ZZ)

    if x is not None:
        poly = Poly.new(poly, 1 / x)
    else:
        poly = PurePoly.new(poly, 1 / Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #9
0
    def __new__(cls, f, x, index=None, radicals=True, expand=True):
        """Construct a new ``RootOf`` object for ``k``-th root of ``f``. """
        x = sympify(x)

        if index is None and x.is_Integer:
            x, index = None, x
        else:
            index = sympify(index)

        if index is not None and index.is_Integer:
            index = int(index)
        else:
            raise ValueError("expected an integer root index, got %s" % index)

        poly = PurePoly(f, x, greedy=False, expand=expand)

        if not poly.is_univariate:
            raise PolynomialError("only univariate polynomials are allowed")

        degree = poly.degree()

        if degree <= 0:
            raise PolynomialError("can't construct RootOf object for %s" % f)

        if index < -degree or index >= degree:
            raise IndexError("root index out of [%d, %d] range, got %d" %
                             (-degree, degree - 1, index))
        elif index < 0:
            index += degree

        dom = poly.get_domain()

        if not dom.is_Exact:
            poly = poly.to_exact()

        roots = cls._roots_trivial(poly, radicals)

        if roots is not None:
            return roots[index]

        coeff, poly = preprocess_roots(poly)
        dom = poly.get_domain()

        if not dom.is_ZZ:
            raise NotImplementedError("RootOf is not supported over %s" % dom)

        root = cls._indexed_root(poly, index)
        return coeff * cls._postprocess_root(root, radicals)
Пример #10
0
def laguerre_poly(n, x=None, alpha=None, **args):
    """Generates Laguerre polynomial of degree `n` in `x`. """
    if n < 0:
        raise ValueError("can't generate Laguerre polynomial of degree %s" % n)

    if alpha is not None:
        K, alpha = construct_domain(alpha,
                                    field=True)  # XXX: ground_field=True
    else:
        K, alpha = QQ, QQ(0)

    poly = DMP(dup_laguerre(int(n), alpha, K), K)

    if x is not None:
        poly = Poly.new(poly, x)
    else:
        poly = PurePoly.new(poly, Dummy('x'))

    if not args.get('polys', False):
        return poly.as_expr()
    else:
        return poly
Пример #11
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(Poly(0, x), 0))
    pytest.raises(PolynomialError, lambda: RootOf(Poly(1, 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: Poly(
            Symbol('a', nonzero=False) * x**5 + 2 * x - 1, x).all_roots())

    assert RootOf(Poly(x - y, x), 0) == y

    assert RootOf(Poly(x**2 - y, x), 0) == -sqrt(y)
    assert RootOf(Poly(x**2 - y, 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)
Пример #12
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)
Пример #13
0
def test_pickling_polys_polytools():
    for c in (Poly, Poly(x, x), PurePoly, PurePoly(x)):
        check(c)
Пример #14
0
 def _transform(cls, expr, x):
     """Transform an expression to a polynomial. """
     poly = PurePoly(expr, x, greedy=False)
     return preprocess_roots(poly)