Prolog - Simple Symbolic Differentiator.

home1 home2
 Bib
 Algorithms
 Bioinfo
 FP
 Logic
 MML
 Prog.Lang
and the
 Book

Logic
 Prolog
  Introduction
  Examples

The rules of symbolic differentiation, such as,

d          d       d
- (A+B) =  - A  +  - B
dx         dx      dx

can be easily expressed in Prolog:

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 symbolic differentiator gives the correct answer as the first solution (it is not in simplified form but that could be achieved easily). However other incorrect "solutions" are also output. These are caused by the rule diff(Y,X,0 being too general. It only holds if not(X=Y).


Negative Information.

The problem with the first symbolic differentiator can be fixed by insisting that not(X=Y) for diff(Y,X,0):

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).

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

equal(X, X).

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

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

{\fB Symbolic Differentiator. \fP}

Production Prolog systems have the full set of comparison operators built in but unification (eq(X,X)) is used here.


Just in case you were wondering, the differentiator cannot be run backwards to get an integrator.

Coding Ockham's Razor, L. Allison, Springer

A Practical Introduction to Denotational Semantics, L. Allison, CUP

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite
The GIMP
~ free photoshop
Firefox
web browser

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Friday, 29-Mar-2024 01:12:43 AEDT.