d(y_, x_,a_, n_) := If(isnumber(n),block(p:=d(y,x,n), replace(p,x,a))); d(y_, xx_=a_, n_) := d(y, xx,a, n); d(a_+b_,x_,n_):=d(a,x,n)+d(b,x,n); d(a_=b_,x_,n_):=d(a,x,n)=d(b,x,n); d(-y_,x_,n_):= -d(y,x,n); d(d(y_,x_,n_),x_,m_) := d(y,x,n+m); d(ds(y_,x_,n_),x_,m_) := ds(y,x,n+m); d(a_*x_^m_,x_,n_):= If(isfree(a,x), a*d(x^m,x,n), If(n>0 and isinteger(n), sum(binomial(n,r)*fallingfactorial(m,r)*d(a,x,n-r)*x^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), sum(binomial(n,r)*fallingfactorial(m,r)*d(a,x,n-r)*x^(m-r), r,0,ceil(abs(m))) ))); d(x_^m_*y_,x_,n_):= If(isfree(y,x), y*d(x^m,x,n), If(n>0 and isinteger(n), sum(binomial(n,r)*fallingfactorial(m,r)*d(y,x,n-r)*x^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), sum(binomial(n,r)*fallingfactorial(m,r)*d(y,x,n-r)*x^(m-r), r,0,ceil(abs(m))) ))); d(a_*(c_+x_)^m_,x_,n_):= If(isfree(a,c,x), a*d((c+x)^m,x,n), If(n>0 and isinteger(n), sum(binomial(n,r)*fallingfactorial(m,r)*d(a,x,n-r)*(c+x)^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), sum(binomial(n,r)*fallingfactorial(m,r)*d(a,x,n-r)*(c+x)^(m-r), r,0,ceil(abs(m))) ))); d((c_+x_)^m_*y_,x_,n_):= If(isfree(y,c,x), y*d((c+x)^m,x,n), If(n>0 and isinteger(n), sum(binomial(n,r)*fallingfactorial(m,r)*d(y,x,n-r)*(c+x)^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), sum(binomial(n,r)*fallingfactorial(m,r)*d(y,x,n-r)*(c+x)^(m-r), r,0,ceil(abs(m))) ))); d(x_*y_,x_,n_):=If(isfree(y,x),If(n>1,0), If(n> -1,x*d(y,x,n)+n*d(y,x,n-1) )); d(a_*x_,x_,n_):=If(isfree(a,x),If(n>1,0), x*d(a,x,n)+n*d(a,x,n-1)); d(e^(x_^m_)*x_,x_,n_):=If(n>= -1 and n<0 and m*n==(n-1), exp(x^m)*m^n); d(e^(-x_^m_)*x_,x_,n_):=If(n>= -1 and n<0 and m*n==(n-1), exp(-x^m)*(-m)^n); d(a_*b_*c_,x_,n_):= If(isfree(a,x), d(b*c,x,n)*a); d(a_^x_,x_,n_):= If(isfree(a,x), log(a)^n*a^x); d(c_^(a_*x_),x_,n_) := If(isfree(c,a,x),(a*log(c))^n*c^(a*x)); d(c_^(x_+b_),x_,n_) := If(isfree(c,b,x), log(c)^n*c^(x+b)); d(c_^(b_+a_*x_),x_,n_) := If(isfree(c,b,a,x), (a*log(c))^n*c^(a*x+b)); d(e^f_,x_,p_):= If(p>=0 and p<=1, d(f,x)^p*e^f); d(e^x_,x_,n_) := e^x; d(e^(a_*x_),x_,n_) := If(isfree(a,x), a^n*e^(a*x)); d(e^(x_+b_),x_,n_) := If(isfree(b,x), e^(x+b)); d(e^(b_+a_*x_),x_,n_) := If(isfree(b,a,x), a^n*e^(a*x+b)); d(x_^m_,x_,n_) := If(isfree(m,x), If(m<0 and isinteger(m) and (m>=n or n1, (-1)^n*(n-2)!*x^(1-n), (psi(2)-psi(2-n)+log(x))/gamma(2-n)*x^(1-n)); d(x_^m_*log(x_),x_,n_) := If(isinteger(n-m) and n>m, -(-1)^(n-m)*(n-m-1)!*gamma(1+m)*x^(m-n), If(m> -1, fallingfactorial(m,n)*(psi(1+m)-psi(1+m-n)+log(x))*x^(m-n), If(m== -1 and n>= -1 and n<=0, x^(-1-n)*log(x)^(1-n)/gamma(2-n), If(m< -1, fallingfactorial(m,n)*(psi(abs(m))-psi(abs(m-n))+log(x))*x^(m-n) )))); d(log(x_)^m_,x_,p_):= If(p>=0 and p<=1, fallingfactorial(m,p)*x^(-p)*log(x)^(m-p) ); d(log(x_+c_)^m_,x_,p_):= If(p>=0 and p<=1 and isfree(c,x), fallingfactorial(m,p)*(x+c)^(-p)*log(x+c)^(m-p) ); d(x_^n_*log(x_)^m_,x_,p_):= If(p>=0 and p<=1 and n==p-1, fallingfactorial(m,p)*log(x)^(m+p-1)); d(x_^p_*log(x_)^m_,x_,p_):= If(p>= -1 and p<=0, fallingfactorial(m,p)*log(x)^(m-p) ); d(log(x_)^m_/x_,x_,p_):= If(p>= -1 and p<=0, fallingfactorial(m,p)*x^(-1-p)*log(x)^(m-p) ); d(e^x_*x_^m_,x_,n_):= If(n>0 and isinteger(n), exp(x)*sum(binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), exp(x)*sum(binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,ceil(abs(m))) )); d(e^(a_*x_)*x_^m_,x_,n_):= If(isfree(m,a,x), If(n>0 and isinteger(n), exp(a*x)*sum(a^(n-r)*binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), exp(a*x)*sum(a^(n-r)*binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,ceil(abs(m))) ))); d(e^(c_+a_*x_)*x_^m_,x_,n_):= If(isfree(m,c,a,x), If(n>0 and isinteger(n), exp(c+a*x)*sum(a^(n-r)*binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,n), If(n>0 and (m>0 and isinteger(n)), exp(c+a*x)*sum(a^(n-r)*binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,ceil(abs(m))) ))); d(e^(c_+x_)*x_^m_,x_,n_):= If(isfree(m,c,x), If(n>0 and isinteger(n), exp(c+x)*sum(binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), exp(c+x)*sum(binomial(n,r)*fallingfactorial(m,r)*x^(m-r), r,0,ceil(abs(m))) ))); d(e^x_*(c_+x_)^m_,x_,n_):= If(isfree(c,m,x), If(n>0 and isinteger(n), exp(x)*sum(binomial(n,r)*fallingfactorial(m,r)*(c+x)^(m-r), r,0,n), If(n>0 or (m>0 and isinteger(m)), exp(x)*sum(binomial(n,r)*fallingfactorial(m,r)*(c+x)^(m-r), r,0,ceil(abs(m))) ))); d(x_^m_*sin(x_),x_,p_):= sum(binomial(p,r)*fallingfactorial(m,r)*x^(m-r)*sin(x+(p-r)*pi/2), r,0,abs(m),1); d(x_^m_*cos(x_),x_,p_):= sum(binomial(p,r)*fallingfactorial(m,r)*x^(m-r)*cos(x+(p-r)*pi/2), r,0,abs(m),1); d(x_^m_*sinh(x_),x_,p_):= sum(binomial(p,r)*fallingfactorial(m,r)*x^(m-r)*(exp(x)-(-1)^(p-r)*exp(-x))/2, r,0,abs(m),1); d(x_^m_*cosh(x_),x_,p_):= sum(binomial(p,r)*fallingfactorial(m,r)*x^(m-r)*(exp(x)+(-1)^(p-r)*exp(-x))/2, r,0,abs(m),1); d(e^(x_^m_),x_,p_):= If(p>= -1 and p<=0, (-1)^(p/m-p)*m^p*gamma(1+p-p/m, -x^m), If(p>=0 and p<=1, (m*x^(m-1))^p*e^(x^m) )); d(e^(a_*x_^m_),x_,p_):= If(p>= -1 and p<=0, (-a)^(p/m-p)*(a*m)^p*gamma(1+p-p/m, -a*x^m), If(p>=0 and p<=1, (a*m*x^(m-1))^p*e^(a*x^m) )); d(e^(x_^m_)*x_^n_,x_,p_):= If(p>= -1 and p<0, If(n==p*(1-m), exp(x^m)*m^p, -(-1)^((n-p)/(p*m))*m^p*gamma((n-p)/(-p*m),-x^m) )); d(e^(a_*x_^m_)*x_^n_,x_,p_):= If(p>= -1 and p<0 and n==p*(1-m), exp(a*x^m)*(a*m)^p); d(x_^d_*mittag(a_,b_,x_^a_),x_,a2_) := If(d==b-1, If(a2==a,x^(b-a-1)/gamma(b-a)+x^d*mittag(a,b,x^a),If(a2== -a,x^(a+b-1)*mittag(a,a+b,x^a),x^(d-n)*mittag(a,b-n,x^a) ))); d(x_^d_*mittag(a_,b_,c_*x_^a_),x_,a2_) := If(isfree(c,x) and d==b-1, If(a2==a,x^(b-a-1)/gamma(b-a)+c*x^d*mittag(a,b,c*x^a), If(a2== -a,x^(a+b-1)*mittag(a,a+b,c*x^a),c*x^(d-n)*mittag(a,b-n,c*x^a) ))); d(mittag(a_,a_,c_*x_^a2_),x_,a_) := If(isfree(c,x) and a2== -a, c*x^(-2a)*mittag(a,a,c*x^a2) ); d(mittag(a_,a_,x_^a2_),x_,a_) := If( a2== -a, x^(-2a)*mittag(a,a,x^a2) ); d(mittag(a_,b_,x_^a_),x_,a_) := 1/x^a/gamma(b-a)+mittag(a,b,x^a); d(mittag(a_,b_,c_*x_^a_),x_,a_) := If(isfree(c,x), 1/x^a/gamma(b-a)+c*mittag(a,b,c*x^a) ); d(mittag(p_,f_),x_,p_):= d(f,x,p)*mittag(p,f) ; d(gamma(n_,a_*log(x_)),x_,p_) := If(p>= -1 and p<=0, d(log(x)^n/n+x^(-p)*gamma(n, a*log(x)),x,1+p), If(p>=0 and p<=1, fallingfactorial(n,p)*(-a^n)*x^(-p-a)*log(x)^(n-p)/n )); d(gamma(n_,log(x_)),x_,p_) := If(p>= -1 and p<=0, d(log(x)^n/n+x^(-p)*gamma(n, log(x)),x,1+p), If(p>=0 and p<=1, (-1)^p*x^(-2p)*log(x)^(n-p) )); d(gamma(n_,x_^m_),x_,p_):= If(p>= -1 and p<=0, (-m)^p*gamma(n+p-p/m,x^m) ); d(gamma(n_,a_*x_^m_),x_,p_):= If(p>0 and p<=1 and isfree(a,x) and n+p-p/m==1, (a)^(p/m-p)*(-a*m)^p*e^(-a*x^m) ); d(gamma(m_, x_), x_,p_) := If(p== -1, x*gamma(m,x)-gamma(m+1,x), If( p>0 and p<1, sum((-1)^(p+r)*binomial(p,r)*fallingfactorial(m-1,r)*gamma(m-r,x), r,0,ceil(abs(m-1))) )); d(zeta(x_),x_,p_):= gamma(p)*zeta(p,x); d(zeta(m_,x_),x_,p_):= risingfactorial(m,p)*zeta(m+p,x); d(ei(x_),x_,p_):=If(p>=0, -(-1)^p*gamma(p,-x)/x^p); d(ei(m_, x_), x_,p_) := (-1)^p*ei(m-p,x); d(erf(x_),x_,p_):= If(abs(p)<= 1, -(-2)^p*gamma((p+1)/2,x^2)/sqrt(pi) ); d(erfc(m_, x_), x_,p_) := erfc(m-p,x); d(erfi(x_),x_,p_):= If(abs(p)<= 1, (-2)^p*gamma((p+1)/2,-x^2)/sqrt(pi) ); d(psi(m_,x_), x_,p_) := psi(m+p,x); d(psi(x_),x_,p_) := psi(p,x); d(loggamma(x_),x_,p_) := psi(p-1,x); d(gamma(x_),x_,p_) := If(p>=0 and p<=1, gamma(x)*(psi(p-1,x)*p+1-p)); d(sin(x_),x_,n_) := sin(x+n*pi/2); d(cos(x_),x_,n_) := cos(x+n*pi/2); d(sinh(x_),x_,n_) := If(iseven(n), sinh(x), If(isodd(n), cosh(x), exp(x)/2-exp(-x)*(-1)^n/2)); d(cosh(x_),x_,n_) := If(iseven(n), cosh(x), If(isodd(n), sinh(x), exp(x)/2+(-1)^n*exp(-x)/2)); #d(sinh(x_),x_,n_) := If(iseven(n), sinh(x), If(isodd(n), cosh(x), sinh(x-n*pi/2*i)*(-1)^(n/2) )); #d(cosh(x_),x_,n_) := If(iseven(n), cosh(x), If(isodd(n), sinh(x), cosh(x-n*pi/2*i)*(-1)^(n/2) )); d(abs(y_),x_,n_) := d(y,x,n)/sgn(y); d(abs(x_),x_,n_) := If(n>1,0, If(n<1, 1/sgn(x)/(1-n)!*x^(1-n) )); d(sgn(y_),x_,n_) := If(n>0,0, If(n<0, sgn(y)*x^(-n)/(-n)! )); d(csgn(y_),x_,n_) := If(n>0,0, If(n<0, csgn(y)*x^(-n)/(-n)! )); d(delta(y_),x_,n_):=If(n>0,0, If(n<0, theta(y)*x^(-n)/(-n)! )); d(log(x_),x_,n_):= If(n>0 and isinteger(n), -(-1)^n*(n-1)!/x^n,If(abs(n)<=1,(log(x)+psi(1)-psi(1-n))/gamma(1-n)/x^n )); d(atanh(x_),x_,n_):= (psi(1)-psi(-n)+log(x+1))*(x+1)^(-n)/(-n)!/2+(psi(1)-psi(-n)+log(1-x))*(1-x)^(-n)/(-n)!/2; d(acoth(x_),x_,n_):= (psi(1)-psi(-n)+log(x+1))*(x+1)^(-n)/(-n)!/2+(psi(1)-psi(-n)+log(1-x))*(1-x)^(-n)/(-n)!/2; d(atan(x_),x_,n_):= (psi(1)-psi(-n)+log(x-i))*(x-i)^(-n)/(-n)!/2-(psi(1)-psi(-n)+log(i+x))*(i+x)^(-n)/(-n)!/2; d(acot(x_),x_,n_):= (psi(1)-psi(-n)+log(x+i))*(x+i)^(-n)/(-n)!/2-(psi(1)-psi(-n)+log(x-i))*(x-i)^(-n)/(-n)!/2; d(sqrt(x_),x_, n_):= If(n==0.5, i/(-1.5)!*log(x), (1/2)!/(1/2-n)!*x^(1/2-n)); d(exp(x_),x_,n_) := exp(x); d(1/sqrt(x_),x_,n_):= If(n==-0.5, sqrt(pi)/2*log(x), (-1)^(-n)*gamma(n+1/2)/gamma(1/2)*x^(-1/2-n)); d(x_,x_,n_) := If(n>1, 0, x^(1-n)/(1-n)!); d(y_,x_,1) := d(y,x); d(y_,x_,0) := y; d(y_,xx_=a_) := block(p:=d(y,xx), replace(p,xx,a)); d(sum(f(x_),x_,a_, b_)) := sum(d(f(x)),x,a,b); d(infsums(x_^k_/k_!,k_),x_,n_) := infsums(x^k/k!,k); d(infsums(x_^k_/k_!,k_),x_) := infsums(x^k/k!,k); d(infsums(x_^(2k_)/(2k_)!,k_),x_) := infsums(x^(2k+1)/(2k+1)!,k); d(infsums(x_^(1+2k_)*1/(1+2k_)!,k_),x_) := infsums(x^(2k)/(2k)!,k); d(infsums((-1)^k_*x_^(2k_)*1/(2k_)!,k_),x_) := -infsums((-1)^k*x^(2k+1)/(2k+1)!,k); d(infsums((-1)^k_*x_^(1+2k_)*1/(1+2k_)!,k_),x_) := infsums((-1)^k*x^(2k)/(2k)!,k); d(mittag(a_,b_, x_), x_) := (mittag(a,b-1,x)-(b-1)*mittag(a,b,x))/(a*x); d(mittag(a_,x_), x_) := mittag(a,0,x)/a/x; d(gamma(n_, x_), x_) := -x^(n-1)*exp(-x); d(zeta(n_,x_), x_) := n*zeta(1+n,x); d(psi(n_,x_), x_) := psi(1+n,x); d(ei(n_, x_), x_) := -ei(n-1,x); d(erf(n_, x_), x_) := n!/sqrt(pi)*exp(-x^n); d(erfc(n_, x_), x_) := erfc(n-1,x); d(li(n_,x_), x_) := li(n-1,x)/x; d(log(n_,x_), x_) := 1/x/log(n) ; d(d(y_,x_,n_),x_) := d(y,x,n+1); d(d(y_,x_),x_) := d(y,x,2); d(integrate(y_,x_),x_) := y; d(integrate(y_,t_,a_,b_),x_) := If(not(isfree(a,x)), -replace(y,t,a)*d(a,x), If(not(isfree(b,x)), replace(y,t,b)*d(b,x) )); d(integrates(y_,t_,a_,b_),x_) := If(not(isfree(a,x)), -replace(y,t,a)*d(a,x), If(not(isfree(b,x)), replace(y,t,b)*d(b,x) )); d(sgn(x_),x_) := 0; d(csgn(x_),x_) := 0; d(theta(x_),x_) := 0; d(delta(x_),x_) := 0; d(log10(x_),x_) := 1/x/log(10); d(atan(x_,y_),y_) := y/(x^2+y^2); d(atan(x_,y_),x_) := -y/(x^2+y^2); d(acot(x_),x_) := -1/(x^2+1); d(asec(x_),x_) := 1/(x*sqrt(1-x^2)); d(acsc(x_),x_) := -1/(x*sqrt(1+x^2)); d(tanh(x_),x_) := sech(x)^2; d(coth(x_),x_) := -csch(x)^2; d(sech(x_),x_) := -tanh(x)*sech(x); d(csch(x_),x_) := -coth(x)*csch(x); d(asin(x_),x_) := 1/(1-x^2)^(1/2); d(acos(x_),x_) := -1/sqrt(1-x^2); d(asinh(x_),x_) := 1/sqrt(x^2+1); d(acosh(x_),x_) := 1/sqrt(x^2-1); d(atanh(x_),x_) := 1/(1-x^2); d(acoth(x_),x_) := 1/(1-x^2); d(asech(x_),x_) := -1/(x*sqrt(1-x^2)); d(acsch(x_),x_) := -1/(x*sqrt(1+x^2)); d(gauss(x_), x_) := -e^(-x^2/2)/sqrt(2pi)*x; d(erf(x_), x_) := 2/sqrt(pi)*exp(-x^2); d(erfi(x_), x_) := 2/sqrt(pi)*exp(x^2); d(gamma(x_), x_) := gamma(x)*psi(x); d(loggamma(x_), x_) := psi(x); d(zeta(x_), x_) := zeta(1,x); d(psi(x_), x_) := psi(1,x); d(factorial(x_), x_) := gamma(x+1)*psi(x+1); d(li(x_),x_) := 1/log(x); d(ei(x_),x_) := exp(x)/x; d(si(x_),x_) := sin(x)/x; d(ci(x_),x_) := cos(x)/x; d(shi(x_),x_) := sinh(x)/x; d(chi(x_),x_) := cosh(x)/x; d(sqrt(x_),x_):= 1/2*x^(-1/2); d(cbrt(x_),x_):= 1/3*x^(-2/3); d(y_):=d(y,x);