def test_issue_21195(): t = symbols('t') x = Function('x')(t) dx = x.diff(t) exp1 = cos(x) + cos(x) * dx exp2 = sin(x) + tan(x) * (dx.diff(t)) exp3 = sin(x) * sin(t) * (dx.diff(t)).diff(t) A = Matrix([[exp1], [exp2], [exp3]]) B = Matrix([[exp1.diff(x)], [exp2.diff(x)], [exp3.diff(x)]]) assert A.diff(x) == B
def apart_list_full_decomposition(P, Q, dummygen): """ Bronstein's full partial fraction decomposition algorithm. Given a univariate rational function ``f``, performing only GCD operations over the algebraic closure of the initial ground domain of definition, compute full partial fraction decomposition with fractions having linear denominators. Note that no factorization of the initial denominator of ``f`` is performed. The final decomposition is formed in terms of a sum of :class:`RootSum` instances. References ========== .. [1] [Bronstein93]_ """ f, x, U = P/Q, P.gen, [] u = Function('u')(x) a = Dummy('a') partial = [] for d, n in Q.sqf_list_include(all=True): b = d.as_expr() U += [ u.diff(x, n - 1) ] h = cancel(f*b**n) / u**n H, subs = [h], [] for j in range(1, n): H += [ H[-1].diff(x) / j ] for j in range(1, n + 1): subs += [ (U[j - 1], b.diff(x, j) / j) ] for j in range(0, n): P, Q = cancel(H[j]).as_numer_denom() for i in range(0, j + 1): P = P.subs(*subs[j - i]) Q = Q.subs(*subs[0]) P = Poly(P, x) Q = Poly(Q, x) G = P.gcd(d) D = d.quo(G) B, g = Q.half_gcdex(D) b = (P * B.quo(g)).rem(D) Dw = D.subs(x, next(dummygen)) numer = Lambda(a, b.as_expr().subs(x, a)) denom = Lambda(a, (x - a)) exponent = n-j partial.append((Dw, numer, denom, exponent)) return partial
def apart_list_full_decomposition(P, Q, dummygen): """ Bronstein's full partial fraction decomposition algorithm. Given a univariate rational function ``f``, performing only GCD operations over the algebraic closure of the initial ground domain of definition, compute full partial fraction decomposition with fractions having linear denominators. Note that no factorization of the initial denominator of ``f`` is performed. The final decomposition is formed in terms of a sum of :class:`RootSum` instances. References ========== 1. [Bronstein93]_ """ f, x, U = P/Q, P.gen, [] u = Function('u')(x) a = Dummy('a') partial = [] for d, n in Q.sqf_list_include(all=True): b = d.as_expr() U += [ u.diff(x, n - 1) ] h = cancel(f*b**n) / u**n H, subs = [h], [] for j in range(1, n): H += [ H[-1].diff(x) / j ] for j in range(1, n + 1): subs += [ (U[j - 1], b.diff(x, j) / j) ] for j in range(0, n): P, Q = cancel(H[j]).as_numer_denom() for i in range(0, j + 1): P = P.subs(*subs[j - i]) Q = Q.subs(*subs[0]) P = Poly(P, x) Q = Poly(Q, x) G = P.gcd(d) D = d.quo(G) B, g = Q.half_gcdex(D) b = (P * B.quo(g)).rem(D) Dw = D.subs(x, dummygen.next()) numer = Lambda(a, b.as_expr().subs(x, a)) denom = Lambda(a, (x - a)) exponent = n-j partial.append((Dw, numer, denom, exponent)) return partial
def apart(f, z=None, **args): """Computes partial fraction decomposition of a rational function. Given a rational function 'f', performing only gcd operations over the algebraic closure of the initial field of definition, compute full partial fraction decomposition with fractions having linear denominators. For all other kinds of expressions the input is returned in an unchanged form. Note however, that `apart` function can thread over sums and relational operators. Note that no factorization of the initial denominator of `f` is needed. The final decomposition is formed in terms of a sum of RootSum instances. By default RootSum tries to compute all its roots to simplify itself. This behavior can be however avoided by setting the keyword flag evaluate=False, which will make this function return a formal decomposition. >>> from sympy import apart >>> from sympy.abc import x, y >>> apart(y/(x+2)/(x+1), x) y/(1 + x) - y/(2 + x) >>> apart(1/(1+x**5), x, evaluate=False) RootSum(Lambda(_a, -_a/(5*(x - _a))), x**5 + 1, x, domain='ZZ') References ========== .. [Bronstein93] M. Bronstein, B. Salvy, Full partial fraction decomposition of rational functions, Proceedings ISSAC '93, ACM Press, Kiev, Ukraine, 1993, pp. 157-160. """ f = cancel(f) if z is None: symbols = f.atoms(Symbol) if not symbols: return f if len(symbols) == 1: z = list(symbols)[0] else: raise ValueError("multivariate partial fractions are not supported") P, Q = f.as_numer_denom() if not Q.has(z): return f partial, r = div(P, Q, z) f, q, U = r / Q, Q, [] u = Function('u')(z) a = Dummy('a') q = Poly(q, z) for d, n in q.sqf_list(all=True, include=True): b = d.as_basic() U += [ u.diff(z, n-1) ] h = cancel(f*b**n) / u**n H, subs = [h], [] for j in range(1, n): H += [ H[-1].diff(z) / j ] for j in range(1, n+1): subs += [ (U[j-1], b.diff(z, j) / j) ] for j in range(0, n): P, Q = cancel(H[j]).as_numer_denom() for i in range(0, j+1): P = P.subs(*subs[j-i]) Q = Q.subs(*subs[0]) P = Poly(P, z) Q = Poly(Q, z) G = P.gcd(d) D = d.exquo(G) B, g = Q.half_gcdex(D) b = (P * B.exquo(g)).rem(D) numer = b.as_basic() denom = (z-a)**(n-j) expr = numer.subs(z, a) / denom partial += RootSum(Lambda(a, expr), D, **args) return partial
kamke1_1 = [ 0, #/* 1 */ y.diff(x)-(a4*x**4 + a3*x**3 + a2*x**2 + a1*x + a0)**(-1/2), #/* 2 */ y.diff(x)+a*y-c*exp(b*x), #/* 3 */ y.diff(x)+a*y-b*sin(c*x), #/* 4 */ y.diff(x)+2*x*y-x*exp(-x**2), #/* 5 */ y.diff(x)+y*cos(x)-exp(2*x), #/* 6 */ y.diff(x)+y*cos(x)-sin(2*x)/2, #/* 7 */ y.diff(x)+y*cos(x)-exp(-sin(x)), #/* 8 */ y.diff(x) + y*tan(x) - sin(2*x), #/* 9 */ y.diff(x)-(sin(log(x))+cos(log(x))+a)*y, #/* 10 */ y.diff(x) + f(x).diff(x)*y - f(x)*f(x).diff(x), #/* 11 */ y.diff(x) + f(x)*y - g(x), #/* 12 */ y.diff(x) + y**2 - 1, #/* 13 */ y.diff(x) + y**2 - a*x - b, #/* 14 */ y.diff(x) + y**2 + a*x**m, #/* 15 */ y.diff(x) + y**2 - 2*x**2*y + x**4 -2*x-1, #/* 16 */ y.diff(x) + y**2 +(x*y-1)*f(x), #/* 17 */ y.diff(x) - y**2 -3*y + 4, #/* 18 */ y.diff(x)-y**2-x*y-x+1, #/* 19 */ y.diff(x) - (y + x)**2, #/* 20 */ y.diff(x)-y**2+(x**2+1)*y-2*x, #/* 21 */ y.diff(x)-y**2+y*sin(x)-cos(x), #/* 22 */ y.diff(x)-y**2-y*sin(2*x)-cos(2*x), #/* 23 */ y.diff(x) + a*y**2 - b, #/* 24 */ y.diff(x) + a*y**2 - b*x**nu, #/* 25 */ y.diff(x)+a*y**2-b*x**(2*nu)-c*x**(nu-1), #/* 26 */ y.diff(x)-(A*y- a)*(B*y-b), #/* 27 */ y.diff(x) + a*y*(y-x) - 1, #/* 28 */ y.diff(x)+x*y**2-x**3*y-2*x, #/* 29 */ y.diff(x) - x*y**2 - 3*x*y, #/* 30 */ y.diff(x)+x**(-a-1)*y**2-x**a, #/* 31 */ #/* only if n # -1 */ y.diff(x) - a*x**n*(y**2+1), #/* 32 */ y.diff(x) + y**2*sin(x) - 2*sin(x)/cos(x)**2, #/* 33 */ y.diff(x)-y**2*f(x).diff(x)/g(x)+g(x).diff(x)/f(x), #/* 34 */ y.diff(x)+f(x)*y**2+g(x)*y, #/* 35 */ y.diff(x)+f(x)*(y**2+2*a*y+b), #/* 36 */ y.diff(x) + y**3 + a*x*y**2, #/* 37 */ y.diff(x)-y**3-a*exp(x)*y**2, #/* 38 */ y.diff(x) - a*y**3 - b*x**(-3/2), #/* 39 */ y.diff(x)-a3*y**3-a2*y**2-a1*y-a0, #/* 40 */ y.diff(x)+3*a*y**3+6*a*x*y**2, #/* 41 */ y.diff(x)+a*x*y**3+b*y**2, #/* 42 */ y.diff(x)-x*(x+2)*y**3-(x+3)*y**2, #/* 43 */ y.diff(x)+(3*a*x**2+4*a**2*x+b)*y**3+3*x*y**2, #/* 44 */ y.diff(x)+2*a*x**3*y**3+2*x*y, #/* 45 */ y.diff(x)+2*(a**2*x**3-b**2*x)*y**3+3*b*y**2, #/* 46 */ y.diff(x)- x**a*y**3+3*y**2-x**(-a)*y-x**(-2*a)+ a*x**(-a-1), #/* 47 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x) - a*(x**n - x)*y**3 - y**2,*/ 0, #/* 48 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x) - (a*x**n+b*x)*y**3 - c*y**2, */ 0, #/* 49 this is actually a Weierstrass equation */ y.diff(x) - a*f(x).diff(x)*y**3 - 6*a*f(x)*y**2 - (2*a+1)*(f(x).diff(x,2)/f(x).diff(x))*y - 2*(a+1), #/* 50 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x) = f3(x)*y**3 + f2(x)*y**2 + f1(x)*y+f0(x),*/ 0, #/* 51 */ y.diff(x) = (y-f(x))*(y-g(x))*(y-(a*f(x)+b*g(x))/(a+b))*h(x) + (y-g(x))/(f(x)-g(x))*diff(f(x),x)+(y-f(x))/(g(x)-f(x))*diff(g(x),x), #/* 52 n is integer */ y.diff(x)-a*y**n-b*x**(n/(1-n)), #/* 53 */ y.diff(x)-f(x)**(1-n)*g(x).diff(x)*y**n/(a*g(x)+b)**n-f(x).diff(x)*y/f(x)-f(x)*g(x).diff(g(x),x), #/* 54 */ y.diff(x)-a**n*f(x)**(1-n)*g(x).diff(x)*y**n-f(x).diff(x)*y/f(x)-f(x)*g(x).diff(x), #/* 55 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x) = f(x)*y**n + g(x)*y + h(x),*/ 0, #/* 56 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x)+f(x)*y**a + g(x)*y**b,*/ 0, #/* 57 */ y.diff(x)-sqrt(abs(y)), #/* 58 */ y.diff(x)-a*sqrt(y)-b*x, #/* 59 */ y.diff(x)-a*sqrt(y**2+1)-b, #/* 60 */ y.diff(x)-sqrt(y**2-1)/sqrt(x**2-1), #/* 61 */ y.diff(x)-sqrt(x**2-1)/sqrt(y**2-1), #/* 62 */ y.diff(x)=(y-x**2*sqrt(x**2-y**2))/(x*y*sqrt(x**2-y**2)+x), #/* 63 NOTE: no abs-sign here! */ y.diff(x)-(1+ y**2)/((y+sqrt(1+y))*sqrt(1+x)**3), #/* 64 */ y.diff(x)-sqrt((a*y**2+b*y+c)/(a*x**2+b*x+c)), #/* 65 */ y.diff(x)-sqrt(y**3+1)/sqrt(x**3+1), #/* 66 NOTE: no abs sign here!*/ #/*y.diff(x)-sqrt(abs(y*(1-y)*(1-a*y)))/sqrt(abs(x*(1-x)*(1-a*x))),*/ y.diff(x)-(sqrt(y*(1-y)*(1-a*y)))/(sqrt(x*(1-x)*(1-a*x))), #/* 67 */ y.diff(x)-sqrt(1-y**4)/sqrt(1-x**4), #/* 68 */ y.diff(x)-sqrt((a*y**4+b*y**2+1)/(a*x**4+b*x**2+1)), #/* 69 */ /* nijso bug: missing a0,b0 */ y.diff(x)=sqrt((a0 + a1*x**1 + a2*x**2 + a3*x**3 + a4*x**4)*(b0 +b1*y**1+b2*y**2+b3*y**3+b4*y**4)), #/* 70 */ /* nijso bug: missing a0,b0 */ y.diff(x)=sqrt((a0 + a1*x**1 + a2*x**2 + a3*x**3 + a4*x**4)/(b0 + b1*y**1+b2*y**2+b3*y**3+b4*y**4)), #/* 71 */ /* *nijso BUG: missing b0,a0 */ y.diff(x)=sqrt((b0 + b1*y**1 + b2*y**2 + b3*y**3 + b4*y**4)/(a0 + a1*x**1+b2*x**2+b3*x**3+b4*x**4)), #/* 72 y'=R1(x,sqrt(X))*R2(y,sqrt(Y)) with R1,R2 rational functions, here an example */ y.diff(x)=(y/sqrt(b1*y**1 + b2*y**2 + b3*y**3 + b4*y**4))*(x/sqrt(a1*x**1+b2*x**2+b3*x**3+b4*x**4)), #/* 73 */ /* nijso bug added a0,b0, removed b4,a4*/ y.diff(x)=(b0 + b1*y**1+b2*y**2+b3*y**3)**(2/3)/(a0 + a1*x**1 + a2*x**2 + a3*x**3)**(2/3), #/* 74 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x)=f(x)*(y-g(x))*sqrt((y-a)*(y-b)),*/ 0, #/* 75 */ y.diff(x)-exp(x-y)+exp(x), #/* 76 */ y.diff(x)-a*cos(y)+b, #/* 77 */ y.diff(x)=cos(a*y+b*x), #/* 78 */ y.diff(x)+a*sin(a1*y+b1*x)+b, #/* 79 - Too general - E S Cheb-Terrab and T Kolokolnikov */ #/*y.diff(x)+f(x)*cos(a*y)+g(x)*sin(a*y)+h(x),*/ 0, #/* 80 */ y.diff(x)+f(x)*sin(y)+(1-f(x).diff(x))*cos(y)-f(x).diff(x)-1, #/* 81 */ y.diff(x)+2*tan(y)*tan(x),
def apart(f, z, domain=None, index=None): """Computes full partial fraction decomposition of a univariate rational function over the algebraic closure of its field of definition. Although only gcd operations over the initial field are required, the expansion is returned in a formal form with linear denominators. However it is possible to force expansion of the resulting formal summations, and so factorization over a specified domain is performed. To specify the desired behavior of the algorithm use the 'domain' keyword. Setting it to None, which is done be default, will result in no factorization at all. Otherwise it can be assigned with one of Z, Q, R, C domain specifiers and the formal partial fraction expansion will be rewritten using all possible roots over this domain. If the resulting expansion contains formal summations, then for all those a single dummy index variable named 'a' will be generated. To change this default behavior issue new name via 'index' keyword. For more information on the implemented algorithm refer to: [1] M. Bronstein, B. Salvy, Full partial fraction decomposition of rational functions, in: M. Bronstein, ed., Proceedings ISSAC '93, ACM Press, Kiev, Ukraine, 1993, pp. 157-160. """ f = Basic.sympify(f) if isinstance(f, Basic.Add): return Add(*[ apart(g) for g in f ]) else: if f.is_fraction(z): f = normal(f, z) else: return f P, Q = f.as_numer_denom() if not Q.has(z): return f u = Function('u')(z) if index is None: A = Symbol('a', dummy=True) else: A = Symbol(index) partial, r = div(P, Q, z) f, q, U = r / Q, Q, [] for k, d in enumerate(sqf(q, z)): n, d = k + 1, d.as_basic() U += [ u.diff(z, k) ] h = normal(f * d**n, z) / u**n H, subs = [h], [] for j in range(1, n): H += [ H[-1].diff(z) / j ] for j in range(1, n+1): subs += [ (U[j-1], d.diff(z, j) / j) ] for j in range(0, n): P, Q = together(H[j]).as_numer_denom() for i in range(0, j+1): P = P.subs(*subs[j-i]) Q = Q.subs(*subs[0]) G = gcd(P, d, z) D = quo(d, G, z) g, B, _ = ext_gcd(Q, D, z) b = rem(P * B / g, D, z) term = b.subs(z, A) / (z - A)**(n-j) if domain is None: a = D.diff(z) if not a.has(z): partial += term.subs(A, -D.subs(z, 0) / a) else: partial += Basic.Sum(term, (A, Basic.RootOf(D, z))) else: raise NotImplementedError return partial