3

I am new to Prolog, having to write a symbolic differentiation tool for Calculus class. I have it working in such a way that:

?- diff(ln(x^3),D).
D = 1*3*x^(3-1) / x^3 ;

and:

?- diff(x*x*x*x,D).
D = x*x*x*1+x*(x*x*1+x*(x*1+x*1)).

Works, however, I would prefer to have it as:

D = 3x^(-2) / x^3;

and:

D = 4x^(3);

Is there a simple way to simplify these mathematical expressions. Thanks!

lurker
  • 56,987
  • 9
  • 69
  • 103
Justin Sanders
  • 313
  • 2
  • 12
  • You need to write some simplifying predicates. For example, `simp(1*X, X)`. You can use `number(X)` which succeeds if `X` is a number. So `simp(X*Y, Z) :- number(X), number(Y), Z is X*Y.` etc. – lurker Apr 28 '17 at 20:19
  • If you want, I have some working code (SIMPSV.PRO, by Sergio Vaghi, 1987). I think you also lookup it on Internet. – CapelliC Apr 28 '17 at 21:47
  • Of interest: [Prolog - simplify derivative](http://stackoverflow.com/q/30224582/1243762) – Guy Coder Apr 28 '17 at 21:58
  • Of interest: GitHub - [wjur/sym-diff-prolog](https://github.com/wjur/sym-diff-prolog/blob/master/sym-diff.pl) – Guy Coder Apr 28 '17 at 22:00
  • Of interest: ["The Art of Prolog"](https://books.google.com/books?id=w-XjuvpOrjMC&pg=PA81&lpg=PA81&dq=prolog+derivative&source=bl&ots=4YG_YGF_Qr&sig=VoVLbYnfCSL0TgOv6x96257Eavk&hl=en&sa=X&ved=0ahUKEwiw5sH9ksjTAhWJWSYKHfv6BgoQ6AEIRTAF#v=onepage&q=prolog%20derivative&f=false) by Leon Sterling and Ehud Shapiro – Guy Coder Apr 28 '17 at 22:01
  • Of interest: [SYMDIFF](http://www.j-paine.org/prolog/mathnotes/symdiff.pl) Most of this is from "The Art of Prolog" but the person did not note it in the code. Use with caution as it has major problems. – Guy Coder Apr 28 '17 at 22:04
  • Of interest: [PRESS: PRolog Equation Solving System](https://github.com/maths/PRESS) More code from some of the experts in Prolog. – Guy Coder Apr 28 '17 at 22:05
  • While all of the links can be helpful, be sure to thoroughly understand the code and test it before using. I myself have not tested them all, but I would not use any in a production setting without a full vetting. – Guy Coder Apr 28 '17 at 22:10
  • I too am working on similar code, what I did no see in your question, but that should be there are words like, [term rewriting](https://en.wikipedia.org/wiki/Rewriting), [Normal form](https://en.wikipedia.org/wiki/Normal_form_(abstract_rewriting)) and/or [Canonical form](https://en.wikipedia.org/wiki/Canonical_form) – Guy Coder Apr 28 '17 at 22:12
  • `having to write a symbolic differentiation tool for Calculus class` why? Did your high school teacher give this to you? If so do they have a working version, because if they don't then how do you know if they are capable of helping you. Not trying to be rude, but avert you from a task that may be harder than you know. If you don't know about the words I noted in the earlier comment, then you have a lot to learn and work to do. – Guy Coder Apr 28 '17 at 22:15
  • `D = 1*3*x^(3-1) / x^3` is different from `D = 3x^(-2) / x^3`. Should be `D = 3x^(2) / x^3` – Rafalon Apr 29 '17 at 10:07

1 Answers1

0

you can use goal_expansion/2 for that (on compile time):

goal_expansion(+, -). 

follow Prolog - simplify derivative

example:

goal_expansion(X,Y):-simplify(X,Y).
Anton Danilov
  • 1,246
  • 11
  • 26