Symbolic Computation - Differential Calculus using Prolog


Note: It seemed that there was some kind of publishing Error in this post. Few symbols were missing. Now things are rectified and post is clear.

I write this after I got impressed with the Prolog’s powerful pattern matching stuff. There should be a alternative to Prolog in the pattern matching stuff, RegEx sounds convincing, but not as much like prolog.

Symbolic Computation is one of the interesting fields of mathematical computation that is implemented in computer for performing the symbolic mathematical operations such as Algebra, Differentiation, Integration. Lot of computer software packages like MatLab, Mathematics supports symbolic computations.

So what is symbolic computation?

Symbolic Computation is just performing the mathematical computation on symbols like , “x”, “y”, “dx” etc. in computer using some computer programming languages.

All the mathematical expression, including variable (with numeric sometime) based expression which are computed under certain mathematical rules are known as symbolic computation. Integrations are computed by integral rules, differentiation is differential calculus rules and so on.

The above are some examples of symbolic computing. But in manual work this seems to be easier, because humans know how to perform a simple simplification, derivative and integral.

But for computers, it is not possible. It need to be teached by writing programs to perform these operations. But now the question is, which programming language to use. Let’s have a brief discussion.

Which Programming Language?

Prolog was one of popular language used for Some of the AI stuffs . Since my problem is some what related to applying intelligence for a machine to perform symbolic computation, I choose Prolog. Since prolog language works on pattern matching, My symbolic computation also works in the same way i.e. pattern matching. if I have two “x” ( say, “x + x”) then I would add these two and write it as “x”. The same applies to y, x, a and any variable.

Prolog is easy to write and understand. I wrote all the things as Facts, as what we write in Java or C++ known as Methods encapsulated inside the Objects. All the function in prolog are named as Facts.

Getting Prolog :

Prolog is freely dustributed programming language, available for both windows and Linux platforms.

  • For Linux, gnu-prolog is available
  • For windows Amzi Prolog - Students Version is one of the freely available prolog.

Here in this article I take an example of Differential Calculus (derivative) i.e. how to differentiate a given expression with respect to a variable and produce the result.

First I need to know the basic rules for differentiation:
  • d ( C , x ) = 0
  • d ( x , x ) = 1
  • d ( x^n , x ) = x * x ^ ( n - 1 )
  • d ( u ± v , x ) = du/dx ± dv/dx
  • d ( u * v , x ) = u * dv/dx + v * du/dx
  • d ( sin(x) , x ) = cos (x)

The above are some rules for basic differentiation. So the notation here I used are easily understandable, any way I explain them.

Notation

Meaning

d

Differentiation Function

1st argument

Function to be differentiated

2nd argument

With respect to variable

^

Raised to the poIr of

*

Multiplication

+

Addition

-

Subtraction

/

division

Now let us define the above rules in the prolog as facts. I first describe a structure for the differentiation function.

d ( f(x) , x , R ).

f(x) => function to be differentiated

x => with respect to variable

R => Result Now let us define the facts in prolog.

Symbolic Computation - Differential Calculus using Prolog - Part 1

Rule 1#
d ( C, X, 0 ):- atomic(C).
The above rule states that, it can match any value passed as function, and checks whether it is atomic, i.e. a number or constant. It Yes then it will return 0 , else it will return nothing and the interpreter fails. Eg: d ( 5 , x , R ) => R = 0
Rule 2#
d ( X , X , 1 ).
If both function and "the with respect" to variable are equal then the derivative is 1. i.e if d( x ) / dx = 1 Eg: d ( x , x , R ) => R = 1
Rule 3#
d ( U + V , X , A + B ):-d ( U , X , A ), % du / dxd ( V , X , B ). % dv / dx
Note, the same rule of subtraction, but instead of plus, use minus sign Eg: d ( x + 1, x , R ) => R = 1 + 0
Rule 4#
d ( U * V , X , U * B + V * A ):-d( U, X, A ), % A = d(U)/dXd( V, X, B ). % B = d (V)/dX
Eg: d ( x * sin(x), x , R ) => R = x*(1*cos(x)) + sin(x)*1
Rule 5#
d ( U^N , X , R ):-atomic( N ),N \= X,d( U, X, A ), R = N * A * U ^ (N - 1).
  • atomic( N ) states that N should be a number
  • N \= X, states that N should not equal to X i.e. not in the form sin(x) ^ x
  • d( U, X, A ), differentiates the function and then finally frame the result of the form N * X ^ ( N – 1 )
Eg: d ( x^3 , x , R ) => R = 3*1*x ^ (3 - 1)
Rule 6#
d ( sin(W) , X , Z * cos ( W ) ):-d ( W , X , Z ).
Rule defines the sine function. The format is in general form as it can differentiate sin(x), sin(x^2), sin(x^2 + x) etc. So similar function declaration for cos, tan, log etc. Eg: d ( sin(x) , x , R ) => R = 1*cos(x)

Here I give you the full code for the differential calculus which covers the basic rules of differentiation.

Basic Rules for differential calculus ( full sMyce code )
d( X, X, 1 ):- !. /* d(X) w.r.t. X is 1 */d( C, X, 0 ):- atomic(C). /* If C is a constant then *//* d(C)/dX is 0 */d( U+V, X, R ):- /* d(U+V)/dX = A+B where */ d( U, X, A ), /* A = d(U)/dX and */ d( V, X, B ), R = A + B. d( U-V, X, R ):- d( U, X, A), d( V, X, B), R = A - B. d( C*U, X, R ):- atomic(C), C \= X, d( U, X, A ), R = C * A, !. d( U*V, X, U*B+V*A ):- /* d(U*V)/dX = B*U+A*V where */ d( U, X, A ), /* A = d(U)/dX and */ d( V, X, B ). /* B = d(V)/dX */ d( U/V, X, (A*V-B*U)/(V^2) ):- /* d(U/V)/dX = (A*V-B*U)/(V*V) */ d( U, X, A), /* where A = d(U)/dX and */ d( V, X, B). /* B = d(V)/dX */ d( U^C, X, R ):- /* d(U^C)/dX = C*A*U^(C-1) */ atomic(C), /* where C is a number or */ C\=X, d( U, X, A ), R = C * A * U ^ ( C - 1 ). d( sin(W), X, Z*cos(W) ):- /* d(sin(W))/dX = Z*cos(W) */ d( W, X, Z). /* where Z = d(W)/dX */ d( exp(W), X, Z*exp(W) ):- /* d(exp(W))/dX = Z*exp(W) */ d( W, X, Z). /* where Z = d(W)/dX */ d( log(W), X, Z/W ):- /* d(log(W))/dX = Z/W */ d( W, X, Z). /* where Z = d(W)/dX */ d( cos(W), X, -(Z*sin(W)) ):- /* d(cos(W))/dX = Z*sin(W) */ d( W, X, Z). /* where Z = d(W)/dX */ d( tan(W), X, (Z*sec(W)^2) ):- /* d(tan(W))/dX = Z*sec(W)^2 */ d( W, X, Z). /* where Z = d(W)/dX */

To run the above program , save the file as “diff.pl”, start prolog interpreter and type “ [‘diff.pl’]. “( don’t miss the “.” , it says the end of function ) and press enter.

The above info would have given a breif idea to develope parsers that can be used in their programs. The prolog is more generalized language and the prolog code can be transformed into C,C++ or Java. Tools for converting prolog to C, C++ or Java are available in the internet for free of cost. My favorite is Prolog Café, which converts prolog programs into Java sMyce code and gnuprolog jar file which is used to use the direct prolog program in a java code without conversion.

Published on: