def test_combine_inverse(): assert Mul._combine_inverse(x*I*y, x*I) == y assert Mul._combine_inverse(x*I*y, y*I) == x assert Mul._combine_inverse(oo*I*y, y*I) == oo assert Mul._combine_inverse(oo*I*y, oo*I) == y assert Add._combine_inverse(oo, oo) == Integer(0) assert Add._combine_inverse(oo*I, oo*I) == Integer(0)
def test_visual_io(): sm = smoothness_p fi = factorint # with smoothness_p n = 124 d = fi(n) m = fi(d, visual=True) t = sm(n) s = sm(t) for th in [d, s, t, n, m]: assert sm(th, visual=True) == s assert sm(th, visual=1) == s for th in [d, s, t, n, m]: assert sm(th, visual=False) == t assert [sm(th, visual=None) for th in [d, s, t, n, m]] == [s, d, s, t, t] assert [sm(th, visual=2) for th in [d, s, t, n, m]] == [s, d, s, t, t] # with factorint for th in [d, m, n]: assert fi(th, visual=True) == m assert fi(th, visual=1) == m for th in [d, m, n]: assert fi(th, visual=False) == d assert [fi(th, visual=None) for th in [d, m, n]] == [m, d, d] assert [fi(th, visual=0) for th in [d, m, n]] == [m, d, d] # test reevaluation no = {'evaluate': False} assert sm({4: 2}, visual=False) == sm(16) assert sm(Mul(*[Pow(k, v, **no) for k, v in {4: 2, 2: 6}.items()], **no), visual=False) == sm(2**10) assert fi({4: 2}, visual=False) == fi(16) assert fi(Mul(*[Pow(k, v, **no) for k, v in {4: 2, 2: 6}.items()], **no), visual=False) == fi(2**10)
def test_Mul_is_infinite(): x = Symbol('x') f = Symbol('f', finite=True) i = Symbol('i', infinite=True) z = Dummy(zero=True) nzf = Dummy(finite=True, zero=False) from diofant import Mul assert (x * f).is_finite is None assert (x * i).is_finite is None assert (f * i).is_finite is False assert (x * f * i).is_finite is None assert (z * i).is_finite is False assert (nzf * i).is_finite is False assert (z * f).is_finite is True assert Mul(0, f, evaluate=False).is_finite is True assert Mul(0, i, evaluate=False).is_finite is False assert (x * f).is_infinite is None assert (x * i).is_infinite is None assert (f * i).is_infinite is None assert (x * f * i).is_infinite is None assert (z * i).is_infinite is S.NaN.is_infinite assert (nzf * i).is_infinite is True assert (z * f).is_infinite is False assert Mul(0, f, evaluate=False).is_infinite is False assert Mul(0, i, evaluate=False).is_infinite is S.NaN.is_infinite
def test_sympyissue_6367(): z = -5*sqrt(2)/(2*sqrt(2*sqrt(29) + 29)) + sqrt(-sqrt(29)/29 + Rational(1, 2)) assert Mul(*[powsimp(a) for a in Mul.make_args(z.normal())]) == 0 assert powsimp(z.normal()) == 0 assert simplify(z) == 0 assert powsimp(sqrt(2 + sqrt(3))*sqrt(2 - sqrt(3)) + 1) == 2 assert powsimp(z) != 0
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] assert roots_cubic(Poly(x**3 + 1, x)) == \ [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ] res = roots_cubic(Poly(x**3 + 2 * a / 27, x)) assert res == [ -root(a + sqrt(a**2), 3) / 3, Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False), Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3) * I / 2, root(a + sqrt(a**2), 3), evaluate=False) ]
def test_combine_inverse(): assert Mul._combine_inverse(x * I * y, x * I) == y assert Mul._combine_inverse(x * I * y, y * I) == x assert Mul._combine_inverse(oo * I * y, y * I) == oo assert Mul._combine_inverse(oo * I * y, oo * I) == y assert Add._combine_inverse(oo, oo) == Integer(0) assert Add._combine_inverse(oo * I, oo * I) == Integer(0)
def test_2arg_hack(): N = Symbol('N', commutative=False) ans = Mul(2, y + 1, evaluate=False) assert (2 * x * (y + 1)).subs(x, 1, hack2=True) == ans assert (2 * (y + 1 + N)).subs(N, 0, hack2=True) == ans assert Mul(1, x, evaluate=False).subs(x, 2, hack2=True) == 2 assert Mul(1, x, evaluate=False).subs(x, y, hack2=True) == y
def test_sympyissue_10773(): with evaluate(False): ans = Mul(Integer(-10), Pow(Integer(5), Integer(-1))) assert sympify('-10/5', evaluate=False) == ans with evaluate(False): ans = Mul(Integer(-10), Pow(Integer(-5), Integer(-1))) assert sympify('-10/-5', evaluate=False) == ans
def test_TRmorrie(): assert TRmorrie(7*Mul(*[cos(i) for i in range(10)])) == \ 7*sin(12)*sin(16)*cos(5)*cos(7)*cos(9)/(64*sin(1)*sin(3)) assert TRmorrie(x) == x assert TRmorrie(2 * x) == 2 * x e = cos(pi / 7) * cos(2 * pi / 7) * cos(4 * pi / 7) assert TR8(TRmorrie(e)) == -Rational(1, 8) e = Mul(*[cos(2**i * pi / 17) for i in range(1, 17)]) assert TR8(TR3(TRmorrie(e))) == Rational(1, 65536)
def test_factor_terms(): A = Symbol('A', commutative=False) assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \ 9*x*y + 9*x + _keep_coeff(Integer(3), x + 1)**_keep_coeff(Integer(2), x + 1) + 9 assert factor_terms(9*(x + x*y + 1) + (3)**(2 + 2*x)) == \ _keep_coeff(Integer(9), 3**(2*x) + x*y + x + 1) assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \ 9*3**(2*x)*(a + 1) assert factor_terms(x + x*A) == \ x*(1 + A) assert factor_terms(sin(x + x*A)) == \ sin(x*(1 + A)) assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \ _keep_coeff(Integer(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1) assert factor_terms(x + (x*y + x)**(3*x + 3)) == \ x + (x*(y + 1))**_keep_coeff(Integer(3), x + 1) assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \ x*(a + 2*b)*(y + 1) i = Integral(x, (x, 0, oo)) assert factor_terms(i) == i # check radical extraction eq = sqrt(2) + sqrt(10) assert factor_terms(eq) == eq assert factor_terms(eq, radical=True) == sqrt(2) * (1 + sqrt(5)) eq = root(-6, 3) + root(6, 3) assert factor_terms( eq, radical=True) == 6**Rational(1, 3) * (1 + (-1)**Rational(1, 3)) eq = [x + x * y] ans = [x * (y + 1)] for c in [list, tuple, set]: assert factor_terms(c(eq)) == c(ans) assert factor_terms(Tuple(x + x * y)) == Tuple(x * (y + 1)) assert factor_terms(Interval(0, 1)) == Interval(0, 1) e = 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=False) == 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=True) == sqrt(2) / sqrt(a + 2) eq = x / (x + 1 / x) + 1 / (x**2 + 1) assert factor_terms(eq, fraction=False) == eq assert factor_terms(eq, fraction=True) == 1 assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \ y*(2 + 1/(x + 1))/x**2 # if not True, then processesing for this in factor_terms is not necessary assert gcd_terms(-x - y) == -x - y assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False) # if not True, then "special" processesing in factor_terms is not necessary assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1) e = exp(-x - 2) + x assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x assert factor_terms(e, sign=False) == e assert factor_terms(exp(-4 * x - 2) - x) == -x + exp(Mul(-2, 2 * x + 1, evaluate=False))
def t(a, b, arg, n): m1 = meijerg(a, b, arg) m2 = Mul(*_inflate_g(m1, n)) # NOTE: (the random number)**9 must still be on the principal sheet. # Thus make b&d small to create random numbers of small imaginary part. return verify_numerically(m1.subs(subs), m2.subs(subs), x, b=0.1, d=-0.1)
def test_TR10i(): assert TR10i(cos(1) * cos(3) + sin(1) * sin(3)) == cos(2) assert TR10i(cos(1) * cos(3) - sin(1) * sin(3)) == cos(4) assert TR10i(cos(1) * sin(3) - sin(1) * cos(3)) == sin(2) assert TR10i(cos(1) * sin(3) + sin(1) * cos(3)) == sin(4) assert TR10i(cos(1) * sin(3) + sin(1) * cos(3) + 7) == sin(4) + 7 assert TR10i(cos(1) * sin(3) + sin(1) * cos(3) + cos(3)) == cos(3) + sin(4) assert TR10i(2*cos(1)*sin(3) + 2*sin(1)*cos(3) + cos(3)) == \ 2*sin(4) + cos(3) assert TR10i(cos(2)*cos(3) + sin(2)*(cos(1)*sin(2) + cos(2)*sin(1))) == \ cos(1) eq = (cos(2) * cos(3) + sin(2) * (cos(1) * sin(2) + cos(2) * sin(1))) * cos(5) + sin(1) * sin(5) assert TR10i(eq) == TR10i(eq.expand()) == cos(4) assert TR10i(sqrt(2)*cos(x)*x + sqrt(6)*sin(x)*x) == \ 2*sqrt(2)*x*sin(x + pi/6) assert TR10i( cos(x) / sqrt(6) + sin(x) / sqrt(2) + cos(x) / sqrt(6) / 3 + sin(x) / sqrt(2) / 3) == 4 * sqrt(6) * sin(x + pi / 6) / 9 assert TR10i(cos(x)/sqrt(6) + sin(x)/sqrt(2) + cos(y)/sqrt(6)/3 + sin(y)/sqrt(2)/3) == \ sqrt(6)*sin(x + pi/6)/3 + sqrt(6)*sin(y + pi/6)/9 assert TR10i(cos(x) + sqrt(3) * sin(x) + 2 * sqrt(3) * cos(x + pi / 6)) == 4 * cos(x) assert TR10i( cos(x) + sqrt(3) * sin(x) + 2 * sqrt(3) * cos(x + pi / 6) + 4 * sin(x)) == 4 * sqrt(2) * sin(x + pi / 4) assert TR10i(cos(2)*sin(3) + sin(2)*cos(4)) == \ sin(2)*cos(4) + sin(3)*cos(2) A = Symbol('A', commutative=False) assert TR10i(sqrt(2)*cos(x)*A + sqrt(6)*sin(x)*A) == \ 2*sqrt(2)*sin(x + pi/6)*A c = cos(x) s = sin(x) h = sin(y) r = cos(y) for si in ((1, 1), (1, -1), (-1, 1), (-1, -1)): for a in ((c * r, s * h), (c * h, s * r)): # explicit 2-args args = zip(si, a) ex = Add(*[Mul(*ai) for ai in args]) t = TR10i(ex) assert not (ex - t.expand(trig=True) or t.is_Add) c = cos(x) s = sin(x) h = sin(pi / 6) r = cos(pi / 6) for si in ((1, 1), (1, -1), (-1, 1), (-1, -1)): for a in ((c * r, s * h), (c * h, s * r)): # induced args = zip(si, a) ex = Add(*[Mul(*ai) for ai in args]) t = TR10i(ex) assert not (ex - t.expand(trig=True) or t.is_Add)
def test_diofant_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3 * I, 'exp(x)': exp(x), '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, 'Float(Integer(3).evalf(3))': 3.00, 'factorint(12, visual=True)': Mul(Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir=1)': Limit(sin(x), x, 0, dir=1), } for text, result in inputs.items(): assert parse_expr(text) == result
def _print_Mul(self, expr): if _coeff_isneg(expr): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('minus')) x.appendChild(self._print_Mul(-expr)) return x from diofant.simplify import fraction numer, denom = fraction(expr) if denom is not S.One: x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('divide')) x.appendChild(self._print(numer)) x.appendChild(self._print(denom)) return x coeff, terms = expr.as_coeff_mul() if coeff is S.One and len(terms) == 1: # XXX since the negative coefficient has been handled, I don't # thing a coeff of 1 can remain return self._print(terms[0]) terms = Mul._from_args(terms).as_ordered_factors() x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('times')) if (coeff != 1): x.appendChild(self._print(coeff)) for term in terms: x.appendChild(self._print(term)) return x
def test_Mul(): assert str(x / y) == 'x/y' assert str(y / x) == 'y/x' assert str(x / y / z) == 'x/(y*z)' assert str((x + 1) / (y + 2)) == '(x + 1)/(y + 2)' assert str(2 * x / 3) == '2*x/3' assert str(-2 * x / 3) == '-2*x/3' assert str(-1.0 * x) == '-1.0*x' assert str(1.0 * x) == '1.0*x' class CustomClass1(Expr): is_commutative = True class CustomClass2(Expr): is_commutative = True cc1 = CustomClass1() cc2 = CustomClass2() assert str(2 * cc1) == '2*CustomClass1()' assert str(cc1 * 2) == '2*CustomClass1()' assert str(cc1 * Float('1.5')) == '1.5*CustomClass1()' assert str(cc2 * 2) == '2*CustomClass2()' assert str(cc2 * 2 * cc1) == '2*CustomClass1()*CustomClass2()' assert str(cc1 * 2 * cc2) == '2*CustomClass1()*CustomClass2()' assert str(Mul(1, 1, evaluate=False)) == '1*1'
def test_issue_5728(): b = x * sqrt(y) a = sqrt(b) c = sqrt(sqrt(x) * y) assert powsimp(a * b) == sqrt(b)**3 assert powsimp(a * b**2 * sqrt(y)) == sqrt(y) * a**5 assert powsimp(a * x**2 * c**3 * y) == c**3 * a**5 assert powsimp(a * x * c**3 * y**2) == c**7 * a assert powsimp(x * c**3 * y**2) == c**7 assert powsimp(x * c**3 * y) == x * y * c**3 assert powsimp(sqrt(x) * c**3 * y) == c**5 assert powsimp(sqrt(x) * a**3 * sqrt(y)) == sqrt(x) * sqrt(y) * a**3 assert powsimp(Mul(sqrt(x)*c**3*sqrt(y), y, evaluate=False)) == \ sqrt(x)*sqrt(y)**3*c**3 assert powsimp(a**2 * a * x**2 * y) == a**7 # symbolic powers work, too b = x**y * y a = b * sqrt(b) assert a.is_Mul is True assert powsimp(a) == sqrt(b)**3 # as does exp a = x * exp(2 * y / 3) assert powsimp(a * sqrt(a)) == sqrt(a)**3 assert powsimp(a**2 * sqrt(a)) == sqrt(a)**5 assert powsimp(a**2 * sqrt(sqrt(a))) == sqrt(sqrt(a))**9
def test_as_content_primitive(): # although the _as_content_primitive methods do not alter the underlying structure, # the as_content_primitive function will touch up the expression and join # bases that would otherwise have not been joined. assert ((x*(2 + 2*x)*(3*x + 3)**2)).as_content_primitive() == \ (18, x*(x + 1)**3) assert (2 + 2*x + 2*y*(3 + 3*y)).as_content_primitive() == \ (2, x + 3*y*(y + 1) + 1) assert ((2 + 6*x)**2).as_content_primitive() == (4, (3*x + 1)**2) assert ((2 + 6*x)**(2*y)).as_content_primitive() == \ (1, (_keep_coeff(Integer(2), (3*x + 1)))**(2*y)) assert (5 + 10*x + 2*y*(3 + 3*y)).as_content_primitive() == \ (1, 10*x + 6*y*(y + 1) + 5) assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive() == \ (11, x*(y + 1)) assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive() == \ (121, x**2*(y + 1)**2) assert (y**2).as_content_primitive() == (1, y**2) assert oo.as_content_primitive() == (1, oo) eq = x**(2 + y) assert (eq).as_content_primitive() == (1, eq) assert (Rational(1, 2)**(2 + x)).as_content_primitive() == (Rational(1, 4), 2**-x) assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \ (Rational(1, 4), Rational(-1, 2)**x) assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \ (Rational(1, 4), Rational(-1, 2)**x) assert (4**((1 + y)/2)).as_content_primitive() == (2, 4**(y/2)) assert (3**((1 + y)/2)).as_content_primitive() == \ (1, 3**(Mul(Rational(1, 2), 1 + y, evaluate=False))) assert (5**Rational(3, 4)).as_content_primitive() == (1, 5**Rational(3, 4)) assert (5**Rational(7, 4)).as_content_primitive() == (5, 5**Rational(3, 4)) assert Add(5*z/7, 0.5*x, 3*y/2, evaluate=False).as_content_primitive() == \ (Rational(1, 14), 7.0*x + 21*y + 10*z) assert (2**Rational(3, 4) + root(2, 4)*sqrt(3)).as_content_primitive(radical=True) == \ (1, root(2, 4)*(sqrt(2) + sqrt(3)))
def test_Mul(): assert str(x / y) == "x/y" assert str(y / x) == "y/x" assert str(x / y / z) == "x/(y*z)" assert str((x + 1) / (y + 2)) == "(x + 1)/(y + 2)" assert str(2 * x / 3) == '2*x/3' assert str(-2 * x / 3) == '-2*x/3' assert str(-1.0 * x) == '-1.0*x' assert str(1.0 * x) == '1.0*x' class CustomClass1(Expr): is_commutative = True class CustomClass2(Expr): is_commutative = True cc1 = CustomClass1() cc2 = CustomClass2() assert str(Rational(2) * cc1) == '2*CustomClass1()' assert str(cc1 * Rational(2)) == '2*CustomClass1()' assert str(cc1 * Float("1.5")) == '1.5*CustomClass1()' assert str(cc2 * Rational(2)) == '2*CustomClass2()' assert str(cc2 * Rational(2) * cc1) == '2*CustomClass1()*CustomClass2()' assert str(cc1 * Rational(2) * cc2) == '2*CustomClass1()*CustomClass2()' assert str(Mul(1, 1, evaluate=False)) == '1*1'
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_TR9(): a = Rational(1, 2) b = 3 * a assert TR9(a) == a assert TR9(cos(1) + cos(2)) == 2 * cos(a) * cos(b) assert TR9(cos(1) - cos(2)) == 2 * sin(a) * sin(b) assert TR9(sin(1) - sin(2)) == -2 * sin(a) * cos(b) assert TR9(sin(1) + sin(2)) == 2 * sin(b) * cos(a) assert TR9(cos(1) + 2 * sin(1) + 2 * sin(2)) == cos(1) + 4 * sin(b) * cos(a) assert TR9(cos(4) + cos(2) + 2 * cos(1) * cos(3)) == 4 * cos(1) * cos(3) assert TR9((cos(4) + cos(2)) / cos(3) / 2 + cos(3)) == 2 * cos(1) * cos(2) assert TR9(cos(3) + cos(4) + cos(5) + cos(6)) == \ 4*cos(Rational(1, 2))*cos(1)*cos(Rational(9, 2)) assert TR9(cos(3) + cos(3) * cos(2)) == cos(3) + cos(2) * cos(3) assert TR9(-cos(y) + cos(x * y)) == -2 * sin(x * y / 2 - y / 2) * sin(x * y / 2 + y / 2) assert TR9(-sin(y) + sin(x * y)) == 2 * sin(x * y / 2 - y / 2) * cos(x * y / 2 + y / 2) c = cos(x) s = sin(x) for si in ((1, 1), (1, -1), (-1, 1), (-1, -1)): for a in ((c, s), (s, c), (cos(x), cos(x * y)), (sin(x), sin(x * y))): args = zip(si, a) ex = Add(*[Mul(*ai) for ai in args]) t = TR9(ex) assert not (a[0].func == a[1].func and (not verify_numerically(ex, t.expand(trig=True)) or t.is_Add) or a[1].func != a[0].func and ex != t)
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_fu(): assert fu(sin(50)**2 + cos(50)**2 + sin(pi / 6)) == Rational(3, 2) assert fu(sqrt(6) * cos(x) + sqrt(2) * sin(x)) == 2 * sqrt(2) * sin(x + pi / 3) eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2 * cos(x)**2 assert fu(eq) == cos(x)**4 - 2 * cos(y)**2 + 2 assert fu(Rational(1, 2) - cos(2 * x) / 2) == sin(x)**2 assert fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b))) == \ sqrt(2)*sin(a + b + pi/4) assert fu(sqrt(3) * cos(x) / 2 + sin(x) / 2) == sin(x + pi / 3) assert fu(1 - sin(2*x)**2/4 - sin(y)**2 - cos(x)**4) == \ -cos(x)**2 + cos(y)**2 assert fu(cos(4 * pi / 9)) == sin(pi / 18) assert fu( cos(pi / 9) * cos(2 * pi / 9) * cos(3 * pi / 9) * cos(4 * pi / 9)) == Rational(1, 16) assert fu( tan(7*pi/18) + tan(5*pi/18) - sqrt(3)*tan(5*pi/18)*tan(7*pi/18)) == \ -sqrt(3) assert fu(tan(1) * tan(2)) == tan(1) * tan(2) expr = Mul(*[cos(2**i) for i in range(10)]) assert fu(expr) == sin(1024) / (1024 * sin(1))
def test_together(): assert together(0) == 0 assert together(1) == 1 assert together(x * y * z) == x * y * z assert together(x + y) == x + y assert together(1 / x) == 1 / x assert together(1 / x + 1) == (x + 1) / x assert together(1 / x + 3) == (3 * x + 1) / x assert together(1 / x + x) == (x**2 + 1) / x assert together((1 / x + 1, 1 / x + 3)) == ((x + 1) / x, (3 * x + 1) / x) assert together(1 / x + Rational(1, 2)) == (x + 2) / (2 * x) assert together(Rational(1, 2) + x / 2) == Mul(Rational(1, 2), x + 1, evaluate=False) assert together(1 / x + 2 / y) == (2 * x + y) / (y * x) assert together(1 / (1 + 1 / x)) == x / (1 + x) assert together(x / (1 + 1 / x)) == x**2 / (1 + x) assert together(1 / x + 1 / y + 1 / z) == (x * y + x * z + y * z) / (x * y * z) assert together( 1 / (1 + x + 1 / y + 1 / z)) == y * z / (y + z + y * z + x * y * z) assert together(1 / (x * y) + 1 / (x * y)**2) == y**(-2) * x**(-2) * (1 + x * y) assert together(1 / (x * y) + 1 / (x * y)**4) == y**(-4) * x**(-4) * (1 + x**3 * y**3) assert together(1 / (x**7 * y) + 1 / (x * y)**4) == y**(-4) * x**(-7) * (x**3 + y**3) assert together(5/(2 + 6/(3 + 7/(4 + 8/(5 + 9/x))))) == \ Rational(5, 2)*((171 + 119*x)/(279 + 203*x)) assert together(1 + 1 / (x + 1)**2) == (1 + (x + 1)**2) / (x + 1)**2 assert together(1 + 1 / (x * (1 + x))) == (1 + x * (1 + x)) / (x * (1 + x)) assert together(1 / (x * (x + 1)) + 1 / (x * (x + 2))) == (3 + 2 * x) / (x * (1 + x) * (2 + x)) assert together(1 + 1 / (2 * x + 2)**2) == (4 * (x + 1)**2 + 1) / (4 * (x + 1)**2) assert together(sin(1 / x + 1 / y)) == sin(1 / x + 1 / y) assert together(sin(1 / x + 1 / y), deep=True) == sin((x + y) / (x * y)) assert together(1 / exp(x) + 1 / (x * exp(x))) == (1 + x) / (x * exp(x)) assert together(1 / exp(2 * x) + 1 / (x * exp(3 * x))) == (1 + exp(x) * x) / (x * exp(3 * x)) assert together(Integral(1 / x + 1 / y, x)) == Integral((x + y) / (x * y), x) assert together(Eq(1 / x + 1 / y, 1 + 1 / z)) == Eq((x + y) / (x * y), (z + 1) / z) assert together((A * B)**-1 + (B * A)**-1) == (A * B)**-1 + (B * A)**-1
def test_factor_nc(): x, y = symbols('x,y') k = symbols('k', integer=True) n, m, o = symbols('n,m,o', commutative=False) # mul and multinomial expansion is needed e = x * (1 + y)**2 assert _mexpand(e) == x + x * 2 * y + x * y**2 def factor_nc_test(e): ex = _mexpand(e) assert ex.is_Add f = factor_nc(ex) assert not f.is_Add and _mexpand(f) == ex factor_nc_test(x * (1 + y)) factor_nc_test(n * (x + 1)) factor_nc_test(n * (x + m)) factor_nc_test((x + m) * n) factor_nc_test(n * m * (x * o + n * o * m) * n) s = Sum(x, (x, 1, 2)) factor_nc_test(x * (1 + s)) factor_nc_test(x * (1 + s) * s) factor_nc_test(x * (1 + sin(s))) factor_nc_test((1 + n)**2) factor_nc_test((x + n) * (x + m) * (x + y)) factor_nc_test(x * (n * m + 1)) factor_nc_test(x * (n * m + x)) factor_nc_test(x * (x * n * m + 1)) factor_nc_test(x * n * (x * m + 1)) factor_nc_test(x * (m * n + x * n * m)) factor_nc_test(n * (1 - m) * n**2) factor_nc_test((n + m)**2) factor_nc_test((n - m) * (n + m)**2) factor_nc_test((n + m)**2 * (n - m)) factor_nc_test((m - n) * (n + m)**2 * (n - m)) assert factor_nc(n * (n + n * m)) == n**2 * (1 + m) assert factor_nc(m * (m * n + n * m * n**2)) == m * (m + n * m * n) * n eq = m * sin(n) - sin(n) * m assert factor_nc(eq) == eq eq = (sin(n) + x) * (cos(n) + x) assert factor_nc(eq.expand()) == eq # issue sympy/sympy#6534 assert (2 * n + 2 * m).factor() == 2 * (n + m) # issue sympy/sympy#6701 assert factor_nc(n**k + n**(k + 1)) == n**k * (1 + n) assert factor_nc((m * n)**k + (m * n)**(k + 1)) == (1 + m * n) * (m * n)**k # issue sympy/sympy#6918 assert factor_nc(-n * (2 * x**2 + 2 * x)) == -2 * n * x * (x + 1) assert factor_nc(1 + Mul(Expr(), Expr(), evaluate=False)) == 1 + Expr()**2
def test_collect_const(): # coverage not provided by above tests assert collect_const(2*sqrt(3) + 4*a*sqrt(5)) == \ 2*(2*sqrt(5)*a + sqrt(3)) # let the primitive reabsorb assert collect_const(2*sqrt(3) + 4*a*sqrt(5), sqrt(3)) == \ 2*sqrt(3) + 4*a*sqrt(5) assert collect_const(sqrt(2)*(1 + sqrt(2)) + sqrt(3) + x*sqrt(2)) == \ sqrt(2)*(x + 1 + sqrt(2)) + sqrt(3) # issue sympy/sympy#5290 assert collect_const(2*x + 2*y + 1, 2) == \ collect_const(2*x + 2*y + 1) == \ Add(Integer(1), Mul(2, x + y, evaluate=False), evaluate=False) assert collect_const(-y - z) == Mul(-1, y + z, evaluate=False) assert collect_const(2*x - 2*y - 2*z, 2) == \ Mul(2, x - y - z, evaluate=False) assert collect_const(2*x - 2*y - 2*z, -2) == \ Add(2*x, Mul(-2, y + z, evaluate=False), evaluate=False)
def test_sympyissue_21176(): e = x**2 * cot(pi * x) / (x**4 + 1) pt = -sqrt(2) / 2 - sqrt(2) * I / 2 assert residue( e, x, pt) == sqrt(2) * I / Mul(2, -2 + 2 * I, tan(sqrt(2) * pi / 2 + sqrt(2) * I * pi / 2), evaluate=False)
def _density(self): proditer = product( *[iter(space._density.items()) for space in self.spaces]) d = {} for items in proditer: elems, probs = list(zip(*items)) elem = sumsets(elems) prob = Mul(*probs) d[elem] = d.get(elem, 0) + prob return Dict(d)
def test_visual_factorint(): assert factorint(1, visual=1) == 1 forty2 = factorint(42, visual=True) assert type(forty2) == Mul assert str(forty2) == '2**1*3**1*7**1' assert factorint(1, visual=True) is Integer(1) no = {'evaluate': False} assert factorint(42**2, visual=True) == Mul(Pow(2, 2, **no), Pow(3, 2, **no), Pow(7, 2, **no), **no) assert -1 in factorint(-42, visual=True).args
def test_expand_non_commutative(): A = Symbol('A', commutative=False) B = Symbol('B', commutative=False) C = Symbol('C', commutative=False) a = Symbol('a') b = Symbol('b') i = Symbol('i', integer=True) n = Symbol('n', negative=True) m = Symbol('m', negative=True) p = Symbol('p', polar=True) np = Symbol('p', polar=False) assert (C * (A + B)).expand() == C * A + C * B assert (C * (A + B)).expand() != A * C + B * C assert ((A + B)**2).expand() == A**2 + A * B + B * A + B**2 assert ((A + B)**3).expand() == (A**2 * B + B**2 * A + A * B**2 + B * A**2 + A**3 + B**3 + A * B * A + B * A * B) # issue 6219 assert ((a * A * B * A**-1)**2).expand() == a**2 * A * B**2 / A # Note that (a*A*B*A**-1)**2 is automatically converted to a**2*(A*B*A**-1)**2 assert ((a * A * B * A**-1)**2).expand(deep=False) == a**2 * (A * B * A**-1)**2 assert ((a * A * B * A**-1)**2).expand() == a**2 * (A * B**2 * A**-1) assert ((a * A * B * A**-1)**2).expand(force=True) == a**2 * A * B**2 * A**(-1) assert ((a * A * B)**2).expand() == a**2 * A * B * A * B assert ((a * A)**2).expand() == a**2 * A**2 assert ((a * A * B)**i).expand() == a**i * (A * B)**i assert ((a * A * (B * (A * B / A)**2))**i).expand() == a**i * (A * B * A * B**2 / A)**i # issue 6558 assert (A * B * (A * B)**-1).expand() == A * B * (A * B)**-1 assert ((a * A)**i).expand() == a**i * A**i assert ((a * A * B * A**-1)**3).expand() == a**3 * A * B**3 / A assert ((a*A*B*A*B/A)**3).expand() == \ a**3*A*B*(A*B**2)*(A*B**2)*A*B*A**(-1) assert ((a*A*B*A*B/A)**-3).expand() == \ a**-3*(A*B*(A*B**2)*(A*B**2)*A*B*A**(-1))**-1 assert ((a * b * A * B * A**-1)**i).expand() == a**i * b**i * (A * B / A)**i assert ((a * (a * b)**i)**i).expand() == a**i * a**(i**2) * b**(i**2) e = Pow(Mul(a, 1 / a, A, B, evaluate=False), Integer(2), evaluate=False) assert e.expand() == A * B * A * B assert sqrt(a * (A * b)**i).expand() == sqrt(a * b**i * A**i) assert (sqrt(-a)**a).expand() == sqrt(-a)**a assert expand((-2 * n)**(i / 3)) == 2**(i / 3) * (-n)**(i / 3) assert expand( (-2 * n * m)**(i / a)) == (-2)**(i / a) * (-n)**(i / a) * (-m)**(i / a) assert expand((-2 * a * p)**b) == 2**b * p**b * (-a)**b assert expand((-2 * a * np)**b) == 2**b * (-a * np)**b assert expand(sqrt(A * B)) == sqrt(A * B) assert expand(sqrt(-2 * a * b)) == sqrt(2) * sqrt(-a * b)
def test_visual_factorint(): assert factorint(1, visual=1) == 1 forty2 = factorint(42, visual=True) assert type(forty2) == Mul assert str(forty2) == '2**1*3**1*7**1' assert factorint(1, visual=True) is S.One no = dict(evaluate=False) assert factorint(42**2, visual=True) == Mul(Pow(2, 2, **no), Pow(3, 2, **no), Pow(7, 2, **no), **no) assert -1 in factorint(-42, visual=True).args
def test_gcd_terms(): f = 2*(x + 1)*(x + 4)/(5*x**2 + 5) + (2*x + 2)*(x + 5)/(x**2 + 1)/5 + \ (2*x + 2)*(x + 6)/(5*x**2 + 5) assert _gcd_terms(f) == (Rational(6, 5) * ((1 + x) / (1 + x**2)), 5 + x, 1) assert _gcd_terms(Add.make_args(f)) == \ (Rational(6, 5)*((1 + x)/(1 + x**2)), 5 + x, 1) newf = Rational(6, 5) * ((1 + x) * (5 + x) / (1 + x**2)) assert gcd_terms(f) == newf args = Add.make_args(f) # non-Basic sequences of terms treated as terms of Add assert gcd_terms(list(args)) == newf assert gcd_terms(tuple(args)) == newf assert gcd_terms(set(args)) == newf # but a Basic sequence is treated as a container assert gcd_terms(Tuple(*args)) != newf assert gcd_terms(Basic(Tuple(1, 3*y + 3*x*y), Tuple(1, 3))) == \ Basic((1, 3*y*(x + 1)), (1, 3)) # but we shouldn't change keys of a dictionary or some may be lost assert gcd_terms(Dict((x*(1 + y), 2), (x + x*y, y + x*y))) == \ Dict({x*(y + 1): 2, x + x*y: y*(1 + x)}) assert gcd_terms((2 * x + 2)**3 + (2 * x + 2)**2) == 4 * (x + 1)**2 * (2 * x + 3) assert gcd_terms(0) == 0 assert gcd_terms(1) == 1 assert gcd_terms(x) == x assert gcd_terms(2 + 2 * x) == Mul(2, 1 + x, evaluate=False) arg = x * (2 * x + 4 * y) garg = 2 * x * (x + 2 * y) assert gcd_terms(arg) == garg assert gcd_terms(sin(arg)) == sin(garg) # issue sympy/sympy#6139-like alpha, alpha1, alpha2, alpha3 = symbols('alpha:4') a = alpha**2 - alpha * x**2 + alpha + x**3 - x * (alpha + 1) rep = { alpha: (1 + sqrt(5)) / 2 + alpha1 * x + alpha2 * x**2 + alpha3 * x**3 } s = (a / (x - alpha)).subs(rep).series(x, 0, 1) assert simplify(collect(s, x)) == -sqrt(5) / 2 - Rational(3, 2) + O(x) # issue sympy/sympy#5917 assert _gcd_terms([Integer(0), Integer(0)]) == (0, 0, 1) assert _gcd_terms([2 * x + 4]) == (2, x + 2, 1) eq = x / (x + 1 / x) assert gcd_terms(eq, fraction=False) == eq
def check_solutions(eq): """ Determines whether solutions returned by diophantine() satisfy the original equation. Hope to generalize this so we can remove functions like check_ternay_quadratic, check_solutions_normal, check_solutions() """ s = diophantine(eq) factors = Mul.make_args(eq) var = list(eq.free_symbols) var.sort(key=default_sort_key) while s: solution = s.pop() for f in factors: if diop_simplify(f.subs(zip(var, solution))) == 0: break else: return False return True
def test_xreplace(): e = Mul(2, 1 + x, evaluate=False) assert e.xreplace({}) == e assert e.xreplace({y: x}) == e