1 /* general differentiation simplification: diff(e,x,n), for
3 gendiff(e,x,n):= if numberp(n)then if integerp(n) then diff(e,x,n) else (print ("fractional derivatives not supported"),'diff(e,x,n))
4 else if freeof(x,e) then 0
5 else if atom(e) then 'diff(e,x,n)
6 else if inpart(e,0)="+" then map(lambda([u],gendiff(u,x,n)),e)
7 else if inpart(e,0)="*" then gendiffprod(inpart(e,1),e/inpart(e,1),x,n)
8 else if inpart(e,0)="^" then gendiffpow(inpart(e,1),inpart(e,2),x,n)
11 /* diff of x1^x2 wrt x n times */
12 gendiffpow(x1,x2,x,n):=
13 if x1=x then genfact(x2,n,1)*x^(x2-n)
15 if (x1=%e)and freeof(x,diff(x2,x)) then diff(x2,x)^n*%e^x2
17 if integerp(x2) and x2>1 then gendiff(ratexpand(x2),x,n) else
20 /* replaced by 3-arg version
21 GENDIFF1(Q):=GENDIFF(Q,X,N)$
24 /* a first approach */
25 gendiffprod(a,b,x,n):=if freeof(x,a)then a*gendiff(b,x,n) else
26 if freeof(x,b) then b*gendiff(a,x,n) else
30 return(sum(binomial(n,i)*q*r,i,0,n))))$