def test_collect_5(): """Collect with respect to a tuple.""" assert collect(x**2*y**4 + z*(x*y**2)**2 + z + a*z, [x*y**2, z]) in [ z*(1 + a + x**2*y**4) + x**2*y**4, z*(1 + a) + x**2*y**4*(1 + z)] assert collect((1 + (x + y) + (x + y)**2).expand(), [x, y]) == 1 + y + x*(1 + 2*y) + x**2 + y**2
def test_collect_5(): """Collect with respect to a tuple""" assert collect(x**2*y**4 + z*(x*y**2)**2 + z + a*z, [x*y**2, z]) in [ z*(1 + a + x**2*y**4) + x**2*y**4, z*(1 + a) + x**2*y**4*(1 + z) ] assert collect((1 + (x + y) + (x + y)**2).expand(), [x, y]) == 1 + y + x*(1 + 2*y) + x**2 + y**2
def test_collect_4(): """Collect with respect to a power""" assert collect(a * x**c + b * x**c, x**c) == x**c * (a + b) # issue sympy/sympy#6096: 2 stays with c (unless c is integer or x is positive0 assert collect(a * x**(2 * c) + b * x**(2 * c), x**c) == x**(2 * c) * (a + b)
def test_collect_func(): f = ((x + a + 1)**3).expand() assert collect(f, x) == a**3 + 3*a**2 + 3*a + x**3 + x**2*(3*a + 3) + \ x*(3*a**2 + 6*a + 3) + 1 assert collect(f, x, factor) == x**3 + 3*x**2*(a + 1) + 3*x*(a + 1)**2 + \ (a + 1)**3 assert collect(f, x, evaluate=False) == { 1: a**3 + 3 * a**2 + 3 * a + 1, x: 3 * a**2 + 6 * a + 3, x**2: 3 * a + 3, x**3: 1 }
def test_collect_D_0(): D = Derivative f = Function('f') fxx = D(f(x), x, x) # collect does not distinguish nested derivatives, so it returns # -- (a + b)*D(D(f, x), x) assert collect(a*fxx + b*fxx, fxx) == (a + b)*fxx
def test_collect_func_xfail(): # XXX: this test will pass when automatic constant distribution is removed (issue sympy/sympy#4596) assert collect(f, x, factor, evaluate=False) == { 1: (a + 1)**3, x: 3 * (a + 1)**2, x**2: 3 * (a + 1), x**3: 1 }
def test_collect_D_0(): D = Derivative f = Function('f') fxx = D(f(x), x, x) # collect does not distinguish nested derivatives, so it returns # -- (a + b)*D(D(f, x), x) assert collect(a * fxx + b * fxx, fxx) == (a + b) * fxx
def test_collect_func(): f = ((x + a + 1)**3).expand() assert collect(f, x) == a**3 + 3*a**2 + 3*a + x**3 + x**2*(3*a + 3) + \ x*(3*a**2 + 6*a + 3) + 1 assert collect(f, x, factor) == x**3 + 3*x**2*(a + 1) + 3*x*(a + 1)**2 + \ (a + 1)**3 assert collect(f, x, evaluate=False) == { 1: a**3 + 3*a**2 + 3*a + 1, x: 3*a**2 + 6*a + 3, x**2: 3*a + 3, x**3: 1 } assert collect(f, x, factor, evaluate=False) == {1: (a + 1)**3, x: 3*(a + 1)**2, x**2: Mul(3, a + 1, evaluate=False), x**3: 1}
def test_collect_Wild(): """Collect with respect to functions with Wild argument""" f = Function('f') w1 = Wild('.1') w2 = Wild('.2') assert collect(f(x) + a*f(x), f(w1)) == (1 + a)*f(x) assert collect(f(x, y) + a*f(x, y), f(w1)) == f(x, y) + a*f(x, y) assert collect(f(x, y) + a*f(x, y), f(w1, w2)) == (1 + a)*f(x, y) assert collect(f(x, y) + a*f(x, y), f(w1, w1)) == f(x, y) + a*f(x, y) assert collect(f(x, x) + a*f(x, x), f(w1, w1)) == (1 + a)*f(x, x) assert collect(a*(x + 1)**y + (x + 1)**y, w1**y) == (1 + a)*(x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, w1**b) == \ a*(x + 1)**y + (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, (x + 1)**w2) == \ (1 + a)*(x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, w1**w2) == (1 + a)*(x + 1)**y
def test_collect_Wild(): """Collect with respect to functions with Wild argument""" f = Function('f') w1 = Wild('.1') w2 = Wild('.2') assert collect(f(x) + a * f(x), f(w1)) == (1 + a) * f(x) assert collect(f(x, y) + a * f(x, y), f(w1)) == f(x, y) + a * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w2)) == (1 + a) * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w1)) == f(x, y) + a * f(x, y) assert collect(f(x, x) + a * f(x, x), f(w1, w1)) == (1 + a) * f(x, x) assert collect(a * (x + 1)**y + (x + 1)**y, w1**y) == (1 + a) * (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, w1**b) == \ a*(x + 1)**y + (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, (x + 1)**w2) == \ (1 + a)*(x + 1)**y assert collect(a * (x + 1)**y + (x + 1)**y, w1**w2) == (1 + a) * (x + 1)**y
def test_collect_1(): """Collect with respect to a Symbol""" assert collect( x + y*x, x ) == x * (1 + y) assert collect( x + x**2, x ) == x + x**2 assert collect( x**2 + y*x**2, x ) == (x**2)*(1 + y) assert collect( x**2 + y*x, x ) == x*y + x**2 assert collect( 2*x**2 + y*x**2 + 3*x*y, [x] ) == x**2*(2 + y) + 3*x*y assert collect( 2*x**2 + y*x**2 + 3*x*y, [y] ) == 2*x**2 + y*(x**2 + 3*x) assert collect( ((1 + y + x)**4).expand(), x) == ((1 + y)**4).expand() + \ x*(4*(1 + y)**3).expand() + x**2*(6*(1 + y)**2).expand() + \ x**3*(4*(1 + y)).expand() + x**4 # symbols can be given as any iterable expr = x + y assert collect(expr, expr.free_symbols) == expr
def test_collect_1(): # Collect with respect to a Symbol. assert collect(x + y*x, x) == x * (1 + y) assert collect(x + x**2, x) == x + x**2 assert collect(x**2 + y*x**2, x) == (x**2)*(1 + y) assert collect(x**2 + y*x, x) == x*y + x**2 assert collect(2*x**2 + y*x**2 + 3*x*y, [x]) == x**2*(2 + y) + 3*x*y assert collect(2*x**2 + y*x**2 + 3*x*y, [y]) == 2*x**2 + y*(x**2 + 3*x) assert collect(((1 + y + x)**4).expand(), x) == ((1 + y)**4).expand() + \ x*(4*(1 + y)**3).expand() + x**2*(6*(1 + y)**2).expand() + \ x**3*(4*(1 + y)).expand() + x**4 # symbols can be given as any iterable expr = x + y assert collect(expr, expr.free_symbols) == expr
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 test_collect_3(): """Collect with respect to a product""" f = Function('f') assert collect(-x/8 + x*y, -x) == x*(y - Rational(1, 8)) assert collect( 1 + x*(y**2), x*y ) == 1 + x*(y**2) assert collect( x*y + a*x*y, x*y) == x*y*(1 + a) assert collect( 1 + x*y + a*x*y, x*y) == 1 + x*y*(1 + a) assert collect(a*x*f(x) + b*(x*f(x)), x*f(x)) == x*(a + b)*f(x) assert collect(a*x*log(x) + b*(x*log(x)), x*log(x)) == x*(a + b)*log(x) assert collect(a*x**2*log(x)**2 + b*(x*log(x))**2, x*log(x)) == \ x**2*log(x)**2*(a + b) # with respect to a product of three symbols assert collect(y*x*z + a*x*y*z, x*y*z) == (1 + a)*x*y*z
def test_collect_3(): # Collect with respect to a product. f = Function('f') assert collect(-x/8 + x*y, -x) == x*(y - Rational(1, 8)) assert collect(1 + x*(y**2), x*y) == 1 + x*(y**2) assert collect(x*y + a*x*y, x*y) == x*y*(1 + a) assert collect(1 + x*y + a*x*y, x*y) == 1 + x*y*(1 + a) assert collect(a*x*f(x) + b*(x*f(x)), x*f(x)) == x*(a + b)*f(x) assert collect(a*x*log(x) + b*(x*log(x)), x*log(x)) == x*(a + b)*log(x) assert collect(a*x**2*log(x)**2 + b*(x*log(x))**2, x*log(x)) == \ x**2*log(x)**2*(a + b) # with respect to a product of three symbols assert collect(y*x*z + a*x*y*z, x*y*z) == (1 + a)*x*y*z
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 test_collect_D(): D = Derivative f = Function('f') fx = D(f(x), x) fxx = D(f(x), x, x) assert collect(a * fx + b * fx, fx) == (a + b) * fx assert collect(a * D(fx, x) + b * D(fx, x), fx) == (a + b) * D(fx, x) assert collect(a * fxx + b * fxx, fx) == (a + b) * D(fx, x) # issue sympy/sympy#4784 assert collect(5 * f(x) + 3 * fx, fx) == 5 * f(x) + 3 * fx assert collect(f(x) + f(x)*diff(f(x), x) + x*diff(f(x), x)*f(x), f(x).diff(x), exact=True) == \ (x*f(x) + f(x))*D(f(x), x) + f(x) assert collect(1/f(x) + 1/f(x)*diff(f(x), x) + x*diff(f(x), x)/f(x), f(x).diff(x), exact=True) == \ (1/f(x) + x/f(x))*D(f(x), x) + 1/f(x)
def test_collect_D(): D = Derivative f = Function('f') fx = D(f(x), x) fxx = D(f(x), x, x) assert collect(a*fx + b*fx, fx) == (a + b)*fx assert collect(a*D(fx, x) + b*D(fx, x), fx) == (a + b)*D(fx, x) assert collect(a*fxx + b*fxx, fx) == (a + b)*D(fx, x) # issue sympy/sympy#4784 assert collect(5*f(x) + 3*fx, fx) == 5*f(x) + 3*fx assert collect(f(x) + f(x)*diff(f(x), x) + x*diff(f(x), x)*f(x), f(x).diff(x), exact=True) == \ (x*f(x) + f(x))*D(f(x), x) + f(x) assert collect(1/f(x) + 1/f(x)*diff(f(x), x) + x*diff(f(x), x)/f(x), f(x).diff(x), exact=True) == \ (1/f(x) + x/f(x))*D(f(x), x) + 1/f(x)
def test_collect_order(): assert collect(t + t*x + t*x**2 + O(x**3), t) == t*(1 + x + x**2 + O(x**3)) assert collect(t + t*x + x**2 + O(x**3), t) == \ t*(1 + x + O(x**3)) + x**2 + O(x**3) f = a*x + b*x + c*x**2 + d*x**2 + O(x**3) g = x*(a + b) + x**2*(c + d) + O(x**3) assert collect(f, x) == g assert collect(f, x, distribute_order_term=False) == g f = sin(a + b).series(b, 0, 10) assert collect(f, [sin(a), cos(a)]) == \ sin(a)*cos(b).series(b, 0, 10) + cos(a)*sin(b).series(b, 0, 10) assert collect(f, [sin(a), cos(a)], distribute_order_term=False) == \ sin(a)*cos(b).series(b, 0, 10).removeO() + \ cos(a)*sin(b).series(b, 0, 10).removeO() + O(b**10)
def log_to_real(h, q, x, t): """ Convert complex logarithms to real functions. Given real field K and polynomials h in K[t,x] and q in K[t], returns real function f such that: ___ df d \ ` -- = -- ) a log(h(a, x)) dx dx /__, a | q(a) = 0 Examples ======== >>> from diofant.integrals.rationaltools import log_to_real >>> from diofant.abc import x, y >>> from diofant import Poly, sqrt, Rational >>> log_to_real(Poly(x + 3*y/2 + Rational(1, 2), x, domain='QQ[y]'), ... Poly(3*y**2 + 1, y, domain='ZZ'), x, y) 2*sqrt(3)*atan(2*sqrt(3)*x/3 + sqrt(3)/3)/3 >>> log_to_real(Poly(x**2 - 1, x, domain='ZZ'), ... Poly(-2*y + 1, y, domain='ZZ'), x, y) log(x**2 - 1)/2 See Also ======== log_to_atan """ from diofant import collect u, v = symbols('u,v', cls=Dummy) H = h.as_expr().subs({t: u + I * v}).expand() Q = q.as_expr().subs({t: u + I * v}).expand() H_map = collect(H, I, evaluate=False) Q_map = collect(Q, I, evaluate=False) a, b = H_map.get(Integer(1), Integer(0)), H_map.get(I, Integer(0)) c, d = Q_map.get(Integer(1), Integer(0)), Q_map.get(I, Integer(0)) R = Poly(resultant(c, d, v), u) R_u = roots(R, filter='R') if len(R_u) != R.count_roots(): return result = Integer(0) for r_u in R_u.keys(): C = Poly(c.subs({u: r_u}), v) R_v = roots(C, filter='R') if len(R_v) != C.count_roots(): return for r_v in R_v: if not r_v.is_positive: continue D = d.subs({u: r_u, v: r_v}) if D.evalf(chop=True) != 0: continue A = Poly(a.subs({u: r_u, v: r_v}), x) B = Poly(b.subs({u: r_u, v: r_v}), x) AB = (A**2 + B**2).as_expr() result += r_u * log(AB) + r_v * log_to_atan(A, B) R_q = roots(q, filter='R') if len(R_q) != q.count_roots(): return for r in R_q.keys(): result += r * log(h.as_expr().subs(t, r)) return result
def residue(expr, x, x0): """ Finds the residue of ``expr`` at the point ``x=x0``. The residue is defined [1]_ as the coefficient of `1/(x - x_0)` in the power series expansion around `x=x_0`. This notion is essential for the Residue Theorem [2]_ Examples ======== >>> from diofant import residue, sin >>> from diofant.abc import x >>> residue(1/x, x, 0) 1 >>> residue(1/x**2, x, 0) 0 >>> residue(2/sin(x), x, 0) 2 References ========== .. [1] http://en.wikipedia.org/wiki/Residue_%28complex_analysis%29 .. [2] http://en.wikipedia.org/wiki/Residue_theorem """ # The current implementation uses series expansion to # calculate it. A more general implementation is explained in # the section 5.6 of the Bronstein's book {M. Bronstein: # Symbolic Integration I, Springer Verlag (2005)}. For purely # rational functions, the algorithm is much easier. See # sections 2.4, 2.5, and 2.7 (this section actually gives an # algorithm for computing any Laurent series coefficient for # a rational function). The theory in section 2.4 will help to # understand why the resultant works in the general algorithm. # For the definition of a resultant, see section 1.4 (and any # previous sections for more review). from diofant import collect, Mul, Order expr = sympify(expr) if x0 != 0: expr = expr.subs(x, x + x0) for n in [1, 2, 4, 8, 16, 32]: s = expr.nseries(x, n=n) if not s.has(Order) or s.getn() >= 0: break s = collect(s.removeO(), x) if s.is_Add: args = s.args else: args = [s] res = Integer(0) for arg in args: c, m = arg.as_coeff_mul(x) m = Mul(*m) if not (m == 1 or m == x or (m.is_Pow and m.exp.is_Integer)): raise NotImplementedError('term of unexpected form: %s' % m) if m == 1 / x: res += c return res
def test_collect(): assert collect(A*B - B*A, A) == A*B - B*A assert collect(A*B - B*A, B) == A*B - B*A assert collect(A*B - B*A, x) == A*B - B*A
def test_collect(): assert collect(A * B - B * A, A) == A * B - B * A assert collect(A * B - B * A, B) == A * B - B * A assert collect(A * B - B * A, x) == A * B - B * A
def test_sympyissue_5615(): aA, Re, D = symbols('aA Re D') e = ((D**3 * a + b * aA**3) / Re).expand() assert collect(e, [aA**3 / Re, a]) == e
def test_collect_4(): """Collect with respect to a power""" assert collect(a*x**c + b*x**c, x**c) == x**c*(a + b) # issue sympy/sympy#6096: 2 stays with c (unless c is integer or x is positive0 assert collect(a*x**(2*c) + b*x**(2*c), x**c) == x**(2*c)*(a + b)
def test_collect_issues(): D = Derivative f = Function('f') e = (1 + x*D(f(x), x) + D(f(x), x))/f(x) assert collect(e.expand(), f(x).diff(x)) != e
def test_sympyissue_6097(): assert collect(a * y**(2.0 * x) + b * y**(2.0 * x), y**x) == y**(2.0 * x) * (a + b) assert collect(a * 2**(2.0 * x) + b * 2**(2.0 * x), 2**x) == 2**(2.0 * x) * (a + b)
def test_sympyissue_6097(): assert collect(a*y**(2.0*x) + b*y**(2.0*x), y**x) == y**(2.0*x)*(a + b) assert collect(a*2**(2.0*x) + b*2**(2.0*x), 2**x) == 2**(2.0*x)*(a + b)
def test_collect_issues(): D = Derivative f = Function('f') e = (1 + x * D(f(x), x) + D(f(x), x)) / f(x) assert collect(e.expand(), f(x).diff(x)) != e
def test_sympyissue_5615(): aA, Re, D = symbols('aA Re D') e = ((D**3*a + b*aA**3)/Re).expand() assert collect(e, [aA**3/Re, a]) == e
def test_collect_2(): """Collect with respect to a sum""" assert collect(a * (cos(x) + sin(x)) + b * (cos(x) + sin(x)), sin(x) + cos(x)) == (a + b) * (cos(x) + sin(x))
def test_collect_2(): """Collect with respect to a sum""" assert collect(a*(cos(x) + sin(x)) + b*(cos(x) + sin(x)), sin(x) + cos(x)) == (a + b)*(cos(x) + sin(x))