15 // evaluate 1st arg to get function F
21 // evaluate 2nd arg and then...
23 // example result of 2nd arg what to do
25 // d(f) nil guess X, N = nil
26 // d(f,2) 2 guess X, N = 2
27 // d(f,x) x X = x, N = nil
28 // d(f,x,2) x X = x, N = 2
29 // d(f,x,y) x X = x, N = y
36 if (p2
== symbol(NIL
)) {
39 } else if (isnum(p2
)) {
55 // N might be a symbol instead of a number
60 if (n
== (int) 0x80000000)
61 stop("nth derivative: check n");
68 for (i
= 0; i
< n
; i
++) {
74 for (i
= 0; i
< n
; i
++) {
82 // if N is nil then arglist is exhausted
92 // number number N = arg1, continue
93 // number symbol X = arg1, N = arg2, continue
95 // symbol nil X = N, N = nil, continue
96 // symbol number X = N, N = arg1, continue
97 // symbol symbol X = N, N = arg1, continue
104 if (N
== symbol(NIL
))
105 break; // arglist exhausted
113 N
= pop(); // N = arg2
120 N
= pop(); // N = arg1
124 push(F
); // final result
134 stop("undefined function");
149 d_scalar_scalar(void)
154 // Example: d(sin(cos(x)),cos(x))
155 // Replace cos(x) <- X, find derivative, then do X <- cos(x)
156 push(p1
); // sin(cos(x))
158 push(symbol(SECRETX
)); // X
159 subst(); // sin(cos(x)) -> sin(X)
160 push(symbol(SECRETX
)); // X
162 push(symbol(SECRETX
)); // X
164 subst(); // cos(X) -> cos(cos(x))
169 d_scalar_scalar_1(void)
190 if (car(p1
) == symbol(MULTIPLY
)) {
195 if (car(p1
) == symbol(POWER
)) {
200 if (car(p1
) == symbol(DERIVATIVE
)) {
205 if (car(p1
) == symbol(LOG
)) {
210 if (car(p1
) == symbol(SIN
)) {
215 if (car(p1
) == symbol(COS
)) {
220 if (car(p1
) == symbol(TAN
)) {
225 if (car(p1
) == symbol(ARCSIN
)) {
230 if (car(p1
) == symbol(ARCCOS
)) {
235 if (car(p1
) == symbol(ARCTAN
)) {
240 if (car(p1
) == symbol(SINH
)) {
245 if (car(p1
) == symbol(COSH
)) {
250 if (car(p1
) == symbol(TANH
)) {
255 if (car(p1
) == symbol(ARCSINH
)) {
260 if (car(p1
) == symbol(ARCCOSH
)) {
265 if (car(p1
) == symbol(ARCTANH
)) {
270 if (car(p1
) == symbol(ABS
)) {
275 if (car(p1
) == symbol(SGN
)) {
280 if (car(p1
) == symbol(HERMITE
)) {
285 if (car(p1
) == symbol(ERF
)) {
290 if (car(p1
) == symbol(ERFC
)) {
295 if (car(p1) == symbol(BESSELJ)) {
296 if (iszero(caddr(p1)))
303 if (car(p1) == symbol(BESSELY)) {
304 if (iszero(caddr(p1)))
311 if (car(p1
) == symbol(INTEGRAL
) && caddr(p1
) == p2
) {
312 derivative_of_integral();
338 for (i
= 0; i
< n
; i
++) {
340 for (j
= 0; j
< n
; j
++) {
353 //-----------------------------------------------------------------------------
361 // - -- = - -- + (log u) --
365 // -- = u (- -- + (log u) --)
368 //-----------------------------------------------------------------------------
373 push(caddr(p1
)); // v/u
377 push(cadr(p1
)); // du/dx
383 push(cadr(p1
)); // log u
386 push(caddr(p1
)); // dv/dx
409 // derivative of derivative
411 // example: d(d(f(x,y),y),x)
432 if (car(p3
) == symbol(DERIVATIVE
)) {
436 push_symbol(DERIVATIVE
);
437 push_symbol(DERIVATIVE
);
440 if (lessp(caddr(p3
), caddr(p1
))) {
459 // derivative of a generic function
464 p3
= cdr(p1
); // p3 is the argument list for the function
466 if (p3
== symbol(NIL
) || find(p3
, p2
)) {
467 push_symbol(DERIVATIVE
);
522 push_rational(-1, 2);
538 push_rational(-1, 2);
544 // Without simplify With simplify
546 // d(arctan(y/x),x) -y/(x^2*(y^2/x^2+1)) -y/(x^2+y^2)
548 // d(arctan(y/x),y) 1/(x*(y^2/x^2+1)) x/(x^2+y^2)
612 push_rational(-1, 2);
628 push_rational(-1, 2);
813 derivative_of_integral(void)
849 "d(x^x,x)-(x^x+x^x*log(x))",
852 "d(log(x^2+5),x)-(2*x/(5+x^2))",
867 "d((x*y*z,y,x+z),(x,y,z))",
868 "((y*z,x*z,x*y),(0,1,0),(1,0,1))",
873 "d(cos(theta)^2,cos(theta))",
890 "d(sin(x),x)-cos(x)",
893 "d(cos(x),x)+sin(x)",
896 "d(tan(x),x)-cos(x)^(-2)",
899 "d(arcsin(x),x)-1/sqrt(1-x^2)",
902 "d(arccos(x),x)+1/sqrt(1-x^2)",
905 "d(arctan(x),x)-1/(1+x^2)",
916 "d(sinh(x),x)-cosh(x)",
919 "d(cosh(x),x)-sinh(x)",
922 "d(tanh(x),x)-cosh(x)^(-2)",
925 "d(arcsinh(x),x)-1/sqrt(x^2+1)",
928 "d(arccosh(x),x)-1/sqrt(x^2-1)",
931 "d(arctanh(x),x)-1/(1-x^2)",
934 "d(sin(cos(x)),x)+cos(cos(x))*sin(x)",
937 "d(sin(x)^2,x)-2*sin(x)*cos(x)",
940 "d(sin(cos(x)),cos(x))-cos(cos(x))",
968 "d(erf(x))-2*exp(-x^2)/sqrt(pi)",
1005 test_derivative(void)
1007 test(__FILE__
, s
, sizeof s
/ sizeof (char *));