Catch-Alls.

A symbolic differentiator can easily be written in Prolog. Most systems allow the use of infix operators but prefix operators plus, times and so on are sufficient and are used here.

diff(plus(A,B), X, plus(DA, DB))
   <= diff(A, X, DA) and diff(B, X, DB).

diff(times(A,B), X, plus(times(A, DB), times(DA, B)))
   <= diff(A, X, DA) and diff(B, X, DB).

equal(X, X).
diff(X, X, 1).
diff(Y, X, 0) <= not equal(Y, X).

? diff( plus(times(x, x), times(3,x)), x, Dx).

{\fB Symbolic Differentiator. \fP}

The output includes the correct answer.


diff(plus(times(x, x), times(3, x)), x,
     plus(plus(times(x, 1), times(1, x)), plus(times(3, 1), times(0, x)))) yes

equivalent infix: diff(x*x+3*x, x, x*1+1*x+3*1+0*3)

 --- d/dx  of  x*x+3*x --- 

This is correct although it could be usefully simplified to 2*x+3; it is straightforward to write a simplifier for polynomials in Prolog if the ability to do arithmetic is provided. The output also includes many incorrect answers not shown. The reason is that the rule diff(Y,X,0) catches too many cases. It should be more correctly stated as

diff(Y, X, 0) <= atomic(Y) and not equal(Y,X).

 --- Correct dy/dx Rule. --- 

This requires negation and also the predicate atomic.

atomic(x).  atomic(y).  atomic(3).

 --- Some Atomic Terms. --- 

Atomic cannot in general be defined by the user but is usually part of a Prolog system. Integers and other constants are atomic. Note the conflict of terminology between an atom and an atomic term.

[*Exercise: Make the changes described above to the differentiator.]


[Previous Page] [Next Page] [Index] © L. Allison, Dept. Computer Science, Monash University