Symbolic Computation – Differential Calculus using Prolog – Part 2

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.

3 thoughts on “Symbolic Computation – Differential Calculus using Prolog – Part 2

  1. Pingback: Symbolic Computation - Differential Calculus using Prolog - Part 1 « My Mind…Leaks

  2. hey….

    i am making this using c++ i did nxn-1 trignometric etc but how do i do logarithmic and chain rule …….. please reply soon

  3. Pingback: Symbolic Computation – Differential Calculus using Prolog – Part 1 | My Mind Leaks - Software, Programming, Architecture & More

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s