Cy137 5 Newbie Poster

I'm trying to write a program in prolog to do symbolic calculus. It's failing on several functions, of which I dont have the *exact* form of the input. These are:

Differentiation: Failed: [y_const, const_exp]

Integration: Failed: [multi_x, distributive, trig2, trig, const, simple_poly]

Any help?

is_var(x).
is_var(y).

is_constant(a).
is_constant(b).
is_constant(c).
is_constant(e).
is_constant(pi).
is_constant(X) :- number(X).

d(X,X,1) :- !.
d(Y,_,0) :- is_constant(Y),!.
d(Y,_,0) :- is_var(Y),!. % up to this clause, X != Y

% free_of(E,X) <=> expression E is free of X
free_of(X,X) :- !,fail.
free_of(Y,X) :- is_var(Y).
free_of(E,_) :- is_constant(E).
free_of(add(A,B),X) :- free_of(A,X), free_of(B,X).
free_of(sub(A,B),X) :- free_of(A,X), free_of(B,X).
free_of(mul(A,B),X) :- free_of(A,X), free_of(B,X).
free_of(div(A,B),X) :- free_of(A,X), free_of(B,X).
free_of(exp(A,B),X) :- free_of(A,X), free_of(B,X).
free_of(ln(A),X) :- free_of(A,X).
free_of(sin(A),X) :- free_of(A,X).
free_of(cos(A),X) :- free_of(A,X).

% canonicalize(E,X,R) <=> R is a canonical representation of E, mainly for terms of polynomials
%   For the canonicalized expression, constants appear first, and powers of x are grouped together,
%   making it appear as K*exp(X,M), where both K and M are free of X, note that K may be 1, and M may be 1 or 0

% assuming two canonicalized expressions, combining them into one canonicalized expression
canonicalize_combine(mul(mul(A,exp(X,M)),mul(B,exp(X,N))), X, mul(mul(A,B),exp(X,add(M,N)))) :- free_of(A,X), free_of(B,X).
canonicalize_combine(div(mul(A,exp(X,M)),nul(B,exp(X,N))), X, mul(div(A,B),exp(X,sub(M,N)))) :- free_of(A,X), free_of(B,X).
canonicalize(X,X,mul(1,exp(X,1))).
canonicalize(K,X,mul(K,exp(X,0))) :- free_of(K,X).
canonicalize(exp(X,M),X, mul(1,exp(X,M))) :- free_of(M,X),!.
canonicalize(mul(U,V),X, R) :- canonicalize(U,X,A), canonicalize(V,X,B), canonicalize_combine(mul(A,B),X,R).
canonicalize(div(U,V),X, R) :- canonicalize(U,X,A), canonicalize(V,X,B), canonicalize_combine(div(A,B),X,R).

%%%%%%DIFFERENTIATION EQUATIONS%%%%%%%%%%%%
d(add(U,V),X,R):-d(U,X,A),d(V,X,B),R=add(A,B).
d(sub(U,V),X,R):-d(U,X,A),d(V,X,B),R=sub(A,B).
d(mul(C,U),X,R):-atomic(C),C\=X,d(U,X,A),R=mul(C,A),!.
d(mul(U,V),X,R):-d(U,X,A),d(V,X,B),R=add(mul(U,B),mul(V,A)).
d(div(U,V),X,R):-d(U,X,A),d(V,X,B),R=div(sub(mul(A,V),mul(B,U)),exp(V,2)).
d(exp(U,C),X,R):-atomic(C),C\=X,d(U,X,A),R=mul(C,mul(A,exp(U,sub(C,1)))).
d(sin(W),X,R):-d(W,X,Z),R=mul(Z,cos(W)).
d(ln(W),X,R):-d(W,X,Z),R=div(Z,W).
d(cos(W),X,R):-d(W,X,Z),R=(mul(Z,-sin(W))).
d(exp(C,X),X,R):-atomic(C),C=E,d(W,X,Z),R=C*exp(W).

%%%%%%INTEGRATION EQUATIONS%%%%%%%%%%%%%%%%%%%%%%
in(A,X,R):-atomic(A),free_of(A,X),R=mul(A,X).
in(X,X,R):-R=div(exp(X,2),2).
in(exp(X,A),X,R):-atomic(A),R=div(exp(X,add(A,1)),add(A,1)).
in(div(C,X),X,R):-R=mul(C,ln(X)).
in(exp(e,X),X,R):-R=exp(e,X).
in(exp(A,X),X,R):-atomic(A),R=div(exp(A,X),ln(A)).
in(ln(X),X,R):-R=sub(mul(X,ln(X)),X).
in(sin(X),X,R):-R=(-cos(X)).
in(cos(X),X,R):-R=sin(X).
in(add(U,V),X,R):-in(U,X,A),in(V,X,B),R=add(A,B).
in(mul(add(U,V),W),X,R):-in(U,X,A),in(V,X,B),R=add(mul(W,A),mul(W,B)).
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.