Skip to content

Instantly share code, notes, and snippets.

@mymindleaks
Created December 25, 2013 19:48
Show Gist options
  • Save mymindleaks/8126305 to your computer and use it in GitHub Desktop.
Save mymindleaks/8126305 to your computer and use it in GitHub Desktop.
prolog rules for integral calculus symbolic computation
/****************** Integral Calculus ***************/
int( 0 , X , 0).
int( C , X , C * X ):-
atomic(C),
C \= X, !.
int( X , X , 0.5 * X^2 ):- !.
int( 1 / X , X , ln(X) ):- !.
int( C * X , X , C * R):-
atomic(C),
int(X , X , R).
int( X * C , X , R):-
atomic(C),
int(C * X , X , R).
int( U + V , X , A + B ):-
int( U , X , A ),
int( V , X , B ).
int( U - V , X , A - B):-
int( U , X , A ),
int( V , X , B ).
int( U * V , X , U * R):-
atomic(U),
U \= X,
int(V , X , R).
int( A^X , X , A^X / ln(A) ):-
atomic(A),
A \= X,
!.
int( X^N , X , W1 * (X ^ W ) ):-
numeric(N),
W is N + 1,
W1 is 1 / W,
!.
int( X^N , X , (1 / (N + 1) ) * X ^ (N + 1) ):-
atomic(N),
!.
int( Z / X , X , Z * ln(X) ):-
atomic(Z),
Z \= X, !.
int( ( (A*X+B)^(-1) ) * X , X , inf ).
int( ( (A*X+B)^N ) * X , X , R):-
A1 = ( A*X + B) ^ ( N + 1),
A2 = ( A * ( N + 1 ) ),
R = A1 / A2.
int( sin(A*X+B) , X , -(1/A) * cos(A*X+B) ):- !.
int( cos(A*X+B) , X , (1/A) * sin(A*X+B) ):- !.
% integration of functions
int( sin(X) , X , -cos(X) ):- !.
int( cos(X) , X , sin(X) ):- !.
int( tan(X) , X , ln(cos(X)) ):- !.
int( sec(X) , X , ln( sec(X) + tan(X) ) ):- !.
int( cosec(X) , X , ln( cosec(X) - cot(X) ) ):- !.
int( cot(X) , X , ln(sin(X)) ):- !.
int( exp(X) , X , exp(X) ):- !.
int( sec(X)^2 , X , tan(X) ):- !.
int( cosec(X)^2 , X , -cot(X) ):- !.
int( sec(X) * tan(X) , X , sec(X) ):- !.
int( cosec(X) * cot(X) , X , -cosec(X) ):- !.
int( 1/(A*X+B) , X , (1/A) * ln(A*X+B) ):- !.
int( exp(A*X+B) , X , (1/A) * exp(A*X+B) ):- !.
int( 1/sqrt(1-X^2) , X , arcsin(X) ):- !.
int( 1/sqrt(1+X^2) , X , arctan(X) ):- !.
int( 1/(X*sqrt(X^2-1)) , X , arcsec(X) ):- !.
int( 1/(X^2-A^2) , X , (1/2*A) * ln( (X-A) / (X+A) ) ):- !.
int( 1/(A^2-X^2) , X , (1/2*A) * ln( (A+X) / (A-X) ) ):- !.
int( 1/(X^2+A^2) , X , (1/A)* arctan(X/A) ):- !.
int( 1/sqrt(X^2-A^2) , X , ln( X+sqrt(X^2-A^2) ) ):- !.
int( 1/sqrt(A^2-X^2) , X , arcsin(X/A) ):- !.
int( 1/sqrt(A^2+X^2) , X , ln(X+sqrt(X^2+A^2)) ):- !.
% integration of functions
% using substitutions
int( sin(X)*cos(X), X , (1/2)*R):-
int(sin(2*X),X,R).
%if all fails
int(W,X,W).
/****************** End Integral Calculus ***************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment