def test__parallel_dict_from_expr_no_gens(): assert parallel_dict_from_expr([x*y, Integer(3)]) == \ ([{(1, 1): 1}, {(0, 0): 3}], (x, y)) assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \ ([{(1, 1, 0): 1}, {(0, 0, 1): 2}, {(0, 0, 0): 3}], (x, y, z)) assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),)) == \ ([{(3,): 1}], (x,))
def test__parallel_dict_from_expr_no_gens(): assert parallel_dict_from_expr([x*y, Integer(3)]) == \ ([{(1, 1): 1}, {(0, 0): 3}], (x, y)) assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \ ([{(1, 1, 0): 1}, {(0, 0, 1): 2}, {(0, 0, 0): 3}], (x, y, z)) assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),)) == \ ([{(3,): 1}], (x,))
def test__parallel_dict_from_expr_if_gens(): assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \ ([{(1,): 1, (0,): 2*y + 3*z}, {(0,): 7}], (x,)) assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),), gens=(x,)) == \ ([{(3,): 1}], (x,)) pytest.raises(PolynomialError, lambda: parallel_dict_from_expr((A*x,), gens=(x,)))
def test__parallel_dict_from_expr_if_gens(): assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \ ([{(1,): 1, (0,): 2*y + 3*z}, {(0,): 7}], (x,)) assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),), gens=(x,)) == \ ([{(3,): 1}], (x,)) pytest.raises(PolynomialError, lambda: parallel_dict_from_expr((A*x,), gens=(x,)))
def test_parallel_dict_from_expr(): assert parallel_dict_from_expr([x - 1, x**2 - 2]) == ([{ (0, ): -1, (1, ): 1 }, { (0, ): -2, (2, ): 1 }], (x, )) pytest.raises(PolynomialError, lambda: parallel_dict_from_expr([A * B - B * A]))
def test_parallel_dict_from_expr(): assert parallel_dict_from_expr([Eq(x, 1), Eq(x**2, 2)]) == ([{ (0, ): -Integer(1), (1, ): Integer(1) }, { (0, ): -Integer(2), (2, ): Integer(1) }], (x, )) pytest.raises(PolynomialError, lambda: parallel_dict_from_expr([A * B - B * A]))
def sdm_from_vector(vec, O, K, **opts): """ Create an sdm from an iterable of expressions. Coefficients are created in the ground field ``K``, and terms are ordered according to monomial order ``O``. Named arguments are passed on to the polys conversion code and can be used to specify for example generators. Examples ======== >>> from diofant.abc import x, y, z >>> from diofant.polys import QQ, lex >>> sdm_from_vector([x**2+y**2, 2*z], lex, QQ) [((1, 0, 0, 1), 2), ((0, 2, 0, 0), 1), ((0, 0, 2, 0), 1)] """ dics, gens = parallel_dict_from_expr(sympify(vec), **opts) dic = {} for i, d in enumerate(dics): for k, v in d.items(): dic[(i,) + k] = K.convert(v) return sdm_from_dict(dic, O)
def test__dict_from_expr_no_gens(): pytest.raises(GeneratorsNeeded, lambda: parallel_dict_from_expr([Integer(17)])) assert parallel_dict_from_expr([x]) == ([{(1, ): 1}], (x, )) assert parallel_dict_from_expr([y]) == ([{(1, ): 1}], (y, )) assert parallel_dict_from_expr([x * y]) == ([{(1, 1): 1}], (x, y)) assert parallel_dict_from_expr([x + y]) == ([{ (1, 0): 1, (0, 1): 1 }], (x, y)) assert parallel_dict_from_expr([sqrt(2)]) == ([{(1, ): 1}], (sqrt(2), )) pytest.raises(GeneratorsNeeded, lambda: parallel_dict_from_expr([sqrt(2)], greedy=False)) assert parallel_dict_from_expr([x * y], domain=ZZ.inject(x)) == ([{ (1, ): x }], (y, )) assert parallel_dict_from_expr([x * y], domain=ZZ.inject(y)) == ([{ (1, ): y }], (x, )) assert parallel_dict_from_expr([3 * sqrt(2) * pi * x * y], extension=None) == ([{ (1, 1, 1, 1): 3 }], (x, y, pi, sqrt(2))) assert parallel_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 parallel_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)))
def test__dict_from_expr_if_gens(): assert parallel_dict_from_expr([Integer(17)], gens=(x, )) == ([{ (0, ): 17 }], (x, )) assert parallel_dict_from_expr([Integer(17)], gens=(x, y)) == ([{ (0, 0): 17 }], (x, y)) assert parallel_dict_from_expr([Integer(17)], gens=(x, y, z)) == ([{ (0, 0, 0): 17 }], (x, y, z)) assert parallel_dict_from_expr([Integer(-17)], gens=(x, )) == ([{ (0, ): -17 }], (x, )) assert parallel_dict_from_expr([Integer(-17)], gens=(x, y)) == ([{ (0, 0): -17 }], (x, y)) assert parallel_dict_from_expr([Integer(-17)], gens=(x, y, z)) == ([{ (0, 0, 0): -17 }], (x, y, z)) assert parallel_dict_from_expr([17 * x], gens=(x, )) == ([{ (1, ): 17 }], (x, )) assert parallel_dict_from_expr([17 * x], gens=(x, y)) == ([{ (1, 0): 17 }], (x, y)) assert parallel_dict_from_expr([17 * x], gens=(x, y, z)) == ([{ (1, 0, 0): 17 }], (x, y, z)) assert parallel_dict_from_expr([17 * x**7], gens=(x, )) == ([{ (7, ): 17 }], (x, )) assert parallel_dict_from_expr([17 * x**7 * y], gens=(x, y)) == ([{ (7, 1): 17 }], (x, y)) assert parallel_dict_from_expr([17 * x**7 * y * z**12], gens=(x, y, z)) == ([{ (7, 1, 12): 17 }], (x, y, z)) assert parallel_dict_from_expr([x + 2 * y + 3 * z], gens=(x, )) == ([{ (1, ): 1, (0, ): 2 * y + 3 * z }], (x, )) assert parallel_dict_from_expr([x + 2 * y + 3 * z], gens=(x, y)) == ([{ (1, 0): 1, (0, 1): 2, (0, 0): 3 * z }], (x, y)) assert parallel_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 parallel_dict_from_expr([x * y + 2 * x * z + 3 * y * z], gens=(x, )) == ([{ (1, ): y + 2 * z, (0, ): 3 * y * z }], (x, )) assert parallel_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 parallel_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 parallel_dict_from_expr([2**y * x], gens=(x, )) == ([{ (1, ): 2**y }], (x, )) assert parallel_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: parallel_dict_from_expr([2**y * x], gens=(x, y)))
def test_parallel_dict_from_expr(): assert parallel_dict_from_expr([Eq(x, 1), Eq( x**2, 2)]) == ([{(0,): -1, (1,): 1}, {(0,): -2, (2,): 1}], (x,)) pytest.raises(PolynomialError, lambda: parallel_dict_from_expr([A*B - B*A]))
def test__parallel_dict_from_expr_if_gens(): assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \ ([{(1,): Integer(1), (0,): 2*y + 3*z}, {(0,): Integer(7)}], (x,)) assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),), gens=(x,)) == \ ([{(3,): 1}], (x,))