Define a Prolog predicate that calculates the derivative of a mathematical expression

ChatpGPT (Jan 30 2023 Version)
Prompt

Define a Prolog predicate that calculates the derivative of a mathematical expression using the symbolic differentiation rules. Includes rules for trigonometric functions and inverse trigonometric functions, sum rule, product rule, quotient rule, and chain rule. For example

derive(A+B, X, DA+DB) :- derive(A, X, DA), derive(B, X, DB).

Completion

Here is a possible implementation of the derive/3 predicate in Prolog:

% base case: derivative of a variable is 1 if it matches the input variable, 0 otherwise
derive(X, X, 1).
derive(_, _, 0).

% derivative of a constant is 0
derive(C, _, 0) :- number(C).

% sum rule: derivative of a sum is the sum of the derivatives
derive(A+B, X, DA+DB) :- derive(A, X, DA), derive(B, X, DB).

% product rule: derivative of a product is the sum of the products of the factors and their derivatives
derive(AB, X, DAB+A*DB) :- derive(A, X, DA), derive(B, X, DB).

% quotient rule: derivative of a quotient is the difference of the products of the numerator and denominator derivatives
derive(A/B, X, (DAB-ADB)/B2) :- derive(A, X, DA), derive(B, X, DB), B2 = B*B.

% power rule: derivative of a power is the product of the power and the derivative of the base, times the base to the power minus one
derive(A^N, X, N*A^(N1)*DA) :- number(N), N1 is N-1, derive(A, X, DA).

% chain rule: derivative of a composition is the product of the derivative of the outer function and the derivative of the inner function, chain-wise
derive(F(G), X, DF*DG) :- derive(F, Y, DF), derive(G, X, DG), Y = G.

% derivative of a trigonometric function
derive(sin(A), X, cos(A)*DA) :- derive(A, X, DA).
derive(cos(A), X, -sin(A)*DA) :- derive(A, X, DA).
derive(tan(A), X, DA/cos(A)^2) :- derive(A, X, DA).

% derivative of an inverse trigonometric function
derive(asin(A), X, DA/sqrt(1-A^2)) :- derive(A, X, DA).
derive(acos(A), X, -DA/sqrt(1-A^2)) :- derive(A, X, DA).
derive(atan(A), X, DA/(1+A^2)) :- derive(A, X, DA).

% simplify/normalize the output by applying some algebraic rules
simplify(D, D) :- number(D). % constants don't need to be simplified
simplify(A+A, 2A).
simplify(-A+A, 0).
simplify(A-A, 0).
simplify(A1, A).
simplify(1A, A).
simplify(A0, 0).
simplify(0A, 0).
simplify(0+D, D).
simplify(D+0, D).
simplify(D-D, 0).
simplify(A/A, 1).
simplify(A^1, A).
simplify(A^0, 1).
simplify((AB)/B, A).
simplify((A+B)/A, 1+B/A).
simplify((A-B)/A, 1-B/A).

% derive/2 is a wrapper that calls derive/3 and simplifies the result
derive(E, X, D) :- derive(E, X, D0), simplify(D0, D).

It has some rough edges but the basics are there and sound.

3 Likes