Пример #1
0
def test_sympyissue_11538():
    assert construct_domain(E)[0] == ZZ[E]

    assert (construct_domain(x**2 + 2 * x +
                             E) == (ZZ[x, E], ZZ[x,
                                                 E].convert(x**2 + 2 * x + E)))

    assert (construct_domain(x + y + GoldenRatio) == (EX,
                                                      EX.convert(x + y +
                                                                 GoldenRatio)))
Пример #2
0
def test_composite_option():
    assert construct_domain({(1,): sin(y)}, composite=False) == \
        (EX, {(1,): EX(sin(y))})

    assert construct_domain({(1,): y}, composite=False) == \
        (EX, {(1,): EX(y)})

    assert construct_domain({(1, 1): 1}, composite=False) == \
        (ZZ, {(1, 1): 1})

    assert construct_domain({(1, 0): y}, composite=False) == \
        (EX, {(1, 0): EX(y)})
Пример #3
0
def test_precision():
    f1 = Float("1.01")
    f2 = Float("1.0000000000000000000001")
    for x in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300,
              f1, f2]:
        result = construct_domain([x])
        y = float(result[1][0])
        assert abs(x - y)/x < 1e-14  # Test relative accuracy

    result = construct_domain([f1])
    assert result[1][0] - 1 > 1e-50

    result = construct_domain([f2])
    assert result[1][0] - 1 > 1e-50
Пример #4
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
Пример #5
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
Пример #6
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
Пример #7
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([Integer(1), Integer(2),
                             Integer(3)]) == (ZZ, [ZZ(1), ZZ(2),
                                                   ZZ(3)])
    assert construct_domain(
        [Integer(1), Integer(2), Integer(3)],
        field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([Rational(1, 2),
                             Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    result = construct_domain([3.14, 1, Rational(1, 2)])
    assert isinstance(result[0], RealField)
    assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]

    assert construct_domain([3.14, sqrt(2)],
                            extension=None) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)],
                            extension=True) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([sqrt(2), 3.14],
                            extension=True) == (EX, [EX(sqrt(2)),
                                                     EX(3.14)])

    assert construct_domain([1, sqrt(2)],
                            extension=None) == (EX, [EX(1), EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert construct_domain([7, Rational(1, 2), sqrt(2)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(Rational(1, 2)), alg.convert(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])

    dom = ZZ[x]

    assert construct_domain([2*x, 3]) == \
        (dom, [dom.convert(2*x), dom.convert(3)])

    dom = ZZ[x, y]

    assert construct_domain([2*x, 3*y]) == \
        (dom, [dom.convert(2*x), dom.convert(3*y)])

    dom = QQ[x]

    assert construct_domain([x/2, 3]) == \
        (dom, [dom.convert(x/2), dom.convert(3)])

    dom = QQ[x, y]

    assert construct_domain([x/2, 3*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3*y)])

    dom = RR[x]

    assert construct_domain([x/2, 3.5]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5)])

    dom = RR[x, y]

    assert construct_domain([x/2, 3.5*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5*y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2/x, 3]) == \
        (dom, [dom.convert(2/x), dom.convert(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2/x, 3*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3*y)])

    dom = RR.frac_field(x)

    assert construct_domain([2/x, 3.5]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2/x, 3.5*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5*y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})
Пример #8
0
def test_sympyissue_11538():
    assert construct_domain(E)[0] == ZZ.poly_ring(E)
    assert (construct_domain(x**2 + 2 * x + E) == (ZZ.poly_ring(
        x, E), ZZ.poly_ring(x, E)(x**2 + 2 * x + E)))
    assert (construct_domain(x + y + GoldenRatio) == (EX,
                                                      EX(x + y + GoldenRatio)))
Пример #9
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([Integer(1), Integer(2),
                             Integer(3)]) == (ZZ, [ZZ(1), ZZ(2),
                                                   ZZ(3)])
    assert construct_domain(
        [Integer(1), Integer(2), Integer(3)],
        field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])

    assert construct_domain([Rational(1, 2),
                             Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain([3.14, 1, Rational(1, 2)
                             ]) == (RR, [RR(3.14), RR(1.0),
                                         RR(0.5)])

    assert construct_domain([3.14, sqrt(2)],
                            extension=None) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)],
                            extension=True) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([sqrt(2), 3.14],
                            extension=True) == (EX, [EX(sqrt(2)),
                                                     EX(3.14)])

    assert construct_domain([1, sqrt(2)],
                            extension=None) == (EX, [EX(1), EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert (construct_domain(
        [7, Rational(1, 2), sqrt(2)],
        extension=True) == (alg, [alg(7),
                                  alg(Rational(1, 2)),
                                  alg(sqrt(2))]))

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert (construct_domain([7, sqrt(2), sqrt(3)], extension=True) == (alg, [
        alg(7), alg(sqrt(2)), alg(sqrt(3))
    ]))

    dom = ZZ.poly_ring(x)

    assert construct_domain([2 * x, 3]) == (dom, [dom(2 * x), dom(3)])

    dom = ZZ.poly_ring(x, y)

    assert construct_domain([2 * x, 3 * y]) == (dom, [dom(2 * x), dom(3 * y)])

    dom = QQ.poly_ring(x)

    assert construct_domain([x / 2, 3]) == (dom, [dom(x / 2), dom(3)])

    dom = QQ.poly_ring(x, y)

    assert construct_domain([x / 2, 3 * y]) == (dom, [dom(x / 2), dom(3 * y)])

    dom = RR.poly_ring(x)

    assert construct_domain([x / 2, 3.5]) == (dom, [dom(x / 2), dom(3.5)])

    dom = RR.poly_ring(x, y)

    assert construct_domain([x / 2,
                             3.5 * y]) == (dom, [dom(x / 2),
                                                 dom(3.5 * y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2 / x, 3]) == (dom, [dom(2 / x), dom(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2 / x, 3 * y]) == (dom, [dom(2 / x), dom(3 * y)])

    dom = RR.frac_field(x)

    assert construct_domain([2 / x, 3.5]) == (dom, [dom(2 / x), dom(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2 / x,
                             3.5 * y]) == (dom, [dom(2 / x),
                                                 dom(3.5 * y)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})
Пример #10
0
    def _integrate(field=None):
        irreducibles = set()

        for poly in reducibles:
            for z in poly.free_symbols:
                if z in V:
                    break  # should this be: `irreducibles |= \
            else:  # set(root_factors(poly, z, filter=field))`
                continue  # and the line below deleted?
                #                          |
                #                          V
            irreducibles |= set(root_factors(poly, z, filter=field))

        log_coeffs, log_part = [], []
        B = _symbols('B', len(irreducibles))

        # Note: the ordering matters here
        for poly, b in reversed(list(ordered(zip(irreducibles, B)))):
            if poly.has(*V):
                poly_coeffs.append(b)
                log_part.append(b * log(poly))

        # TODO: Currently it's better to use symbolic expressions here instead
        # of rational functions, because it's simpler and FracElement doesn't
        # give big speed improvement yet. This is because cancelation is slow
        # due to slow polynomial GCD algorithms. If this gets improved then
        # revise this code.
        candidate = poly_part / poly_denom + Add(*log_part)
        h = F - _derivation(candidate) / denom
        raw_numer = h.as_numer_denom()[0]

        # Rewrite raw_numer as a polynomial in K[coeffs][V] where K is a field
        # that we have to determine. We can't use simply atoms() because log(3),
        # sqrt(y) and similar expressions can appear, leading to non-trivial
        # domains.
        syms = set(poly_coeffs) | set(V)
        non_syms = set()

        def find_non_syms(expr):
            if expr.is_Integer or expr.is_Rational:
                pass  # ignore trivial numbers
            elif expr in syms:
                pass  # ignore variables
            elif not expr.has(*syms):
                non_syms.add(expr)
            elif expr.is_Add or expr.is_Mul or expr.is_Pow:
                list(map(find_non_syms, expr.args))
            else:
                # TODO: Non-polynomial expression. This should have been
                # filtered out at an earlier stage.
                raise PolynomialError

        try:
            find_non_syms(raw_numer)
        except PolynomialError:
            return
        else:
            ground, _ = construct_domain(non_syms, field=True)

        coeff_ring = PolyRing(poly_coeffs, ground)
        ring = PolyRing(V, coeff_ring)

        numer = ring.from_expr(raw_numer)

        solution = solve_lin_sys(numer.coeffs(), coeff_ring)

        if solution is None:
            return
        else:
            solution = [(coeff_ring.symbols[coeff_ring.index(k)], v.as_expr())
                        for k, v in solution.items()]
            return candidate.subs(solution).subs(
                list(zip(poly_coeffs, [S.Zero] * len(poly_coeffs))))