Beispiel #1
0
    def __init__(self, monom, gens=None):
        if not iterable(monom):
            rep, gens = dict_from_expr(sympify(monom), gens=gens)
            if len(rep) == 1 and list(rep.values())[0] == 1:
                monom = list(rep.keys())[0]
            else:
                raise ValueError("Expected a monomial got %s" % monom)

        self.exponents = tuple(map(int, monom))
        self.gens = gens
Beispiel #2
0
def test_dict_from_expr():
    assert dict_from_expr(Eq(x, 1)) == ({(0,): -1, (1,): 1}, (x,))
    pytest.raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
Beispiel #3
0
def test__dict_from_expr_no_gens():
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(Integer(17)))

    assert dict_from_expr(x) == ({(1,): 1}, (x,))
    assert dict_from_expr(y) == ({(1,): 1}, (y,))

    assert dict_from_expr(x*y) == ({(1, 1): 1}, (x, y))
    assert dict_from_expr(x + y) == ({(1, 0): 1, (0, 1): 1}, (x, y))

    assert dict_from_expr(sqrt(2)) == ({(1,): 1}, (sqrt(2),))
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(sqrt(2), greedy=False))

    assert dict_from_expr(x*y, domain=ZZ.poly_ring(x)) == ({(1,): x}, (y,))
    assert dict_from_expr(x*y, domain=ZZ.poly_ring(y)) == ({(1,): y}, (x,))

    assert dict_from_expr(3*sqrt(
        2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2)))
    assert dict_from_expr(3*sqrt(
        2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))

    f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y)

    assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1,
                                  (1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
Beispiel #4
0
def test__dict_from_expr_if_gens():
    assert dict_from_expr(Integer(17), gens=(x,)) == ({(0,): 17}, (x,))
    assert dict_from_expr(Integer(17), gens=(x, y)) == ({(0, 0): 17}, (x, y))
    assert dict_from_expr(Integer(17), gens=(x, y, z)) == ({(0, 0, 0): 17},
                                                           (x, y, z))

    assert dict_from_expr(Integer(-17), gens=(x,)) == ({(0,): -17}, (x,))
    assert dict_from_expr(Integer(-17), gens=(x, y)) == ({(0, 0): -17}, (x, y))
    assert dict_from_expr(Integer(-17), gens=(x, y, z)) == ({(0, 0, 0): -17},
                                                            (x, y, z))

    assert dict_from_expr(17*x, gens=(x,)) == ({(1,): 17}, (x,))
    assert dict_from_expr(17*x, gens=(x, y)) == ({(1, 0): 17}, (x, y))
    assert dict_from_expr(17*x, gens=(x, y, z)) == ({(1, 0, 0): 17}, (x, y, z))

    assert dict_from_expr(17*x**7, gens=(x,)) == ({(7,): 17}, (x,))
    assert dict_from_expr(17*x**7*y, gens=(x, y)) == ({(7, 1): 17}, (x, y))
    assert dict_from_expr(17*x**7*y*z**12, gens=(x, y, z)) == ({(7, 1, 12): 17},
                                                               (x, y, z))

    assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == ({(1,): 1,
                                                         (0,): 2*y + 3*z}, (x,))
    assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == ({(1, 0): 1, (0, 1): 2,
                                                           (0, 0): 3*z}, (x, y))
    assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == ({(1, 0, 0): 1,
                                                              (0, 1, 0): 2,
                                                              (0, 0, 1): 3},
                                                             (x, y, z))

    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == ({(1,): y + 2*z,
                                                               (0,): 3*y*z},
                                                              (x,))
    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == ({(1, 1): 1,
                                                                 (1, 0): 2*z,
                                                                 (0, 1): 3*z},
                                                                (x, y))
    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == ({(1, 1, 0): 1,
                                                                    (1, 0, 1): 2,
                                                                    (0, 1, 1): 3},
                                                                   (x, y, z))

    assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,))
    assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == ({(0, 1): 1, (1, 0): 1},
                                                          (x, Integral(x, (x, 1, 2))))
    pytest.raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
Beispiel #5
0
def test_dict_from_expr():
    assert dict_from_expr(Eq(x, 1)) == ({(0,): -1, (1,): 1}, (x,))
    pytest.raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
Beispiel #6
0
def test__dict_from_expr_no_gens():
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(Integer(17)))

    assert dict_from_expr(x) == ({(1,): 1}, (x,))
    assert dict_from_expr(y) == ({(1,): 1}, (y,))

    assert dict_from_expr(x*y) == ({(1, 1): 1}, (x, y))
    assert dict_from_expr(x + y) == ({(1, 0): 1, (0, 1): 1}, (x, y))

    assert dict_from_expr(sqrt(2)) == ({(1,): 1}, (sqrt(2),))
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(sqrt(2), greedy=False))

    assert dict_from_expr(x*y, domain=ZZ.poly_ring(x)) == ({(1,): x}, (y,))
    assert dict_from_expr(x*y, domain=ZZ.poly_ring(y)) == ({(1,): y}, (x,))

    assert dict_from_expr(3*sqrt(
        2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2)))
    assert dict_from_expr(3*sqrt(
        2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))

    f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y)

    assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1,
                                  (1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
Beispiel #7
0
def test__dict_from_expr_if_gens():
    assert dict_from_expr(Integer(17), gens=(x,)) == ({(0,): 17}, (x,))
    assert dict_from_expr(Integer(17), gens=(x, y)) == ({(0, 0): 17}, (x, y))
    assert dict_from_expr(Integer(17), gens=(x, y, z)) == ({(0, 0, 0): 17},
                                                           (x, y, z))

    assert dict_from_expr(Integer(-17), gens=(x,)) == ({(0,): -17}, (x,))
    assert dict_from_expr(Integer(-17), gens=(x, y)) == ({(0, 0): -17}, (x, y))
    assert dict_from_expr(Integer(-17), gens=(x, y, z)) == ({(0, 0, 0): -17},
                                                            (x, y, z))

    assert dict_from_expr(17*x, gens=(x,)) == ({(1,): 17}, (x,))
    assert dict_from_expr(17*x, gens=(x, y)) == ({(1, 0): 17}, (x, y))
    assert dict_from_expr(17*x, gens=(x, y, z)) == ({(1, 0, 0): 17}, (x, y, z))

    assert dict_from_expr(17*x**7, gens=(x,)) == ({(7,): 17}, (x,))
    assert dict_from_expr(17*x**7*y, gens=(x, y)) == ({(7, 1): 17}, (x, y))
    assert dict_from_expr(17*x**7*y*z**12, gens=(x, y, z)) == ({(7, 1, 12): 17},
                                                               (x, y, z))

    assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == ({(1,): 1,
                                                         (0,): 2*y + 3*z}, (x,))
    assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == ({(1, 0): 1, (0, 1): 2,
                                                           (0, 0): 3*z}, (x, y))
    assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == ({(1, 0, 0): 1,
                                                              (0, 1, 0): 2,
                                                              (0, 0, 1): 3},
                                                             (x, y, z))

    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == ({(1,): y + 2*z,
                                                               (0,): 3*y*z},
                                                              (x,))
    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == ({(1, 1): 1,
                                                                 (1, 0): 2*z,
                                                                 (0, 1): 3*z},
                                                                (x, y))
    assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == ({(1, 1, 0): 1,
                                                                    (1, 0, 1): 2,
                                                                    (0, 1, 1): 3},
                                                                   (x, y, z))

    assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,))
    assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == ({(0, 1): 1, (1, 0): 1},
                                                          (x, Integral(x, (x, 1, 2))))
    pytest.raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
Beispiel #8
0
def test_dict_from_expr():
    assert dict_from_expr(Eq(x, 1)) == \
        ({(0,): -Integer(1), (1,): Integer(1)}, (x,))
    pytest.raises(PolynomialError, lambda: dict_from_expr(A * B - B * A))
Beispiel #9
0
def _minpoly_op_algebraic_element(op, ex1, ex2, x, dom, mp1=None, mp2=None):
    """
    return the minimal polynomial for ``op(ex1, ex2)``

    Parameters
    ==========

    op : operation ``Add`` or ``Mul``
    ex1, ex2 : expressions for the algebraic elements
    x : indeterminate of the polynomials
    dom: ground domain
    mp1, mp2 : minimal polynomials for ``ex1`` and ``ex2`` or None

    Examples
    ========

    >>> from diofant import sqrt, Add, Mul, QQ
    >>> from diofant.abc import x, y

    >>> p1 = sqrt(sqrt(2) + 1)
    >>> p2 = sqrt(sqrt(2) - 1)
    >>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ)
    x - 1
    >>> q1 = sqrt(y)
    >>> q2 = 1 / y
    >>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y))
    x**2*y**2 - 2*x*y - y**3 + 1

    References
    ==========

    .. [1] http://en.wikipedia.org/wiki/Resultant
    .. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638
           "Degrees of sums in a separable field extension".
    """
    y = Dummy(str(x))
    if mp1 is None:
        mp1 = _minpoly_compose(ex1, x, dom)
    if mp2 is None:
        mp2 = _minpoly_compose(ex2, y, dom)
    else:
        mp2 = mp2.subs({x: y})

    if op is Add:
        # mp1a = mp1.subs({x: x - y})
        if dom == QQ:
            R, X = ring('X', QQ)
            p1 = R(dict_from_expr(mp1)[0])
            p2 = R(dict_from_expr(mp2)[0])
        else:
            (p1, p2), _ = parallel_poly_from_expr((mp1, x - y), x, y)
            r = p1.compose(p2)
            mp1a = r.as_expr()

    elif op is Mul:
        mp1a = _muly(mp1, x, y)
    else:
        raise NotImplementedError('option not available')

    if op is Mul or dom != QQ:
        r = resultant(mp1a, mp2, gens=[y, x])
    else:
        r = rs_compose_add(p1, p2)
        r = expr_from_dict(r.as_expr_dict(), x)

    deg1 = degree(mp1, x)
    deg2 = degree(mp2, y)
    if op is Mul and deg1 == 1 or deg2 == 1:
        # if deg1 = 1, then mp1 = x - a; mp1a = x - y - a;
        # r = mp2(x - a), so that `r` is irreducible
        return r

    r = Poly(r, x, domain=dom)
    _, factors = r.factor_list()
    res = _choose_factor(factors, x, op(ex1, ex2), dom)
    return res.as_expr()