Home > Archive > Prolog > May 2007 > Help with prolog derivative program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help with prolog derivative program
|
|
| Jean-Guillaume Pyraksos 2007-04-05, 7:09 pm |
| SWI-Prolog/ MacOS-X if it matters...
I just have written a prolog program to compute a derivative of a
polynomial :
?- deriv(3*x**5+5*x-2,x,D).
D = 15*x**4+5
Yes
Little problem to get rid of the trailing 0 but I found a hack.
Problems :
1) What if i ask :
?- deriv(3*X**5+5*X-2,X,D).
I get something not understandable.
2) How can I get the value of a polynomial ?
?- value(3*x**5+5*x-2,x,1,V). % for x=1
It's ok with X :
?- P = 3*X**5+5*X-2, X=1, V is P.
Thanks a lot,
JG
| |
|
| On Thu, 05 Apr 2007 15:19:01 +0200, Jean-Guillaume Pyraksos
<wissme@hotmail.com> wrote:
>SWI-Prolog/ MacOS-X if it matters...
>I just have written a prolog program to compute a derivative of a
>polynomial :
>
>?- deriv(3*x**5+5*x-2,x,D).
>D = 15*x**4+5
>Yes
>
>Little problem to get rid of the trailing 0 but I found a hack.
>
>Problems :
>
>1) What if i ask :
> ?- deriv(3*X**5+5*X-2,X,D).
>
> I get something not understandable.
>
>2) How can I get the value of a polynomial ?
> ?- value(3*x**5+5*x-2,x,1,V). % for x=1
>
> It's ok with X :
> ?- P = 3*X**5+5*X-2, X=1, V is P.
>
Crystak ball and black cat is needed to debug program not seeing its
code.
A.L.
| |
| Jean-Guillaume Pyraksos 2007-04-06, 8:03 am |
| SWI-Prolog/ MacOS-X if it matters... The PROLOG CODE is at the end.
I just have written a prolog program to compute a derivative of a
polynomial :
?- deriv(3*x**5+5*x-2,x,D).
D = 15*x**4+5
Yes
Little problem to get rid of the trailing 0 but I found a hack.
Problems :
1) What if i ask :
?- deriv(3*X**5+5*X-2,X,D).
I get something not understandable.
2) How can I get the value of a polynomial ?
?- value(3*x**5+5*x-2,x,1,V). % for x=1
It's ok with X :
?- P = 3*X**5+5*X-2, X=1, V is P.
Thanks a lot,
JG
% derive.pl
derive1(X,X,1).
derive1(N,X,0) :- number(N).
derive1(P1+P2,X,D1+D2) :- derive(P1,X,D1), derive(P2,X,D2).
derive1(P1-P2,X,D1-D2) :- derive(P1,X,D1), derive(P2,X,D2).
derive1(X**2,X,2*X).
derive1(X**N,X,N*X**N1) :- number(N), N =\= 2, N1 is N-1.
derive1(-X**2,X,-2*X).
derive1(-X**N,X,N1*X**N2) :- number(N), N =\= 2, N1 is -N, N2 is N-1.
derive1(K*X**2,X,K1*X) :- number(K), K1 is 2*K.
derive1(K*X**N,X,K1*X**N1) :- number(K), number(N), N =\= 2, K1 is K*N,
N1 is N-1.
derive1(K*X,X,K) :- number(K).
derive(P,X,DP) :- derive1(P,X,D), D = DP+0.
derive(P,X,DP) :- derive1(P,X,D), D = DP-0.
derive(P,X,DP) :- derive1(P,X,DP).
test1 :- test(-x**5+4*x**3-x**2+5*x-8,x).
test(Expr,X) :-
write('Processing : '), write(Expr), nl,
derive(Expr,X,D),
write('Derivative is : '), write(D), nl.
value(P,X,A,V) :- number(A), X=A, V is P.
test2 :- value(6*X**8-5*X**3+3*X**2-27,X,-15,V),
write('Value of 6*X**8-5*X**3+3*X**2-27 with X=-15 : '),
write(V), nl.
| |
| Marco Gavanelli 2007-04-06, 7:06 pm |
| Jean-Guillaume Pyraksos wrote:
> SWI-Prolog/ MacOS-X if it matters... The PROLOG CODE is at the end.
>
> I just have written a prolog program to compute a derivative of a
> polynomial :
>
> ?- deriv(3*x**5+5*x-2,x,D).
> D = 15*x**4+5
> Yes
>
> Little problem to get rid of the trailing 0 but I found a hack.
>
> Problems :
>
> 1) What if i ask :
> ?- deriv(3*X**5+5*X-2,X,D).
>
> I get something not understandable.
>
> 2) How can I get the value of a polynomial ?
> ?- value(3*x**5+5*x-2,x,1,V). % for x=1
>
> It's ok with X :
> ?- P = 3*X**5+5*X-2, X=1, V is P.
The problem is that if the second argument is a constant (e.g.,
lowercase 'x'), you cannot assign it a numerical value later on: it
already has a value.
If the second argument is a variable, Prolog could try and unify it with
something you do not expect. So, if you call
?- deriv(3*X**5+5*X-2,X,D).
Prolog will unify with the first clause, giving X=3*X**5+5*X-2. This
result is clearly wrong, and its due to the fact that by default, Prolog
does not perform the occur-check. So, the first idea is: turn the
occur-check on (see in the manual how this is done in your system).
A second idea could be: forbid the unification of the second argument.
You could have two different variables in the first and second argument
of your clauses, and then try and check if, after unification, they are
the very same variable. You can do it with predicate ==
Cheers,
Marco
--
http://www.ing.unife.it/docenti/MarcoGavanelli/
| |
|
|
| Nick Wedd 2007-05-09, 7:06 pm |
| In message <wissme-969CD3.15190105042007@malibu.unice.fr>,
Jean-Guillaume Pyraksos <wissme@hotmail.com> writes
>SWI-Prolog/ MacOS-X if it matters...
>I just have written a prolog program to compute a derivative of a
>polynomial :
>
>?- deriv(3*x**5+5*x-2,x,D).
>D = 15*x**4+5
>Yes
>
>Little problem to get rid of the trailing 0 but I found a hack.
>
>Problems :
>
>1) What if i ask :
> ?- deriv(3*X**5+5*X-2,X,D).
>
> I get something not understandable.
I'm not surprised.
>2) How can I get the value of a polynomial ?
> ?- value(3*x**5+5*x-2,x,1,V). % for x=1
You could write some code to do this, it would be tedious, but not
difficult.
1.) Write a predicate, using univ (spelled =..), to convert your
expression to a list structure.
2.) Walk this list structure building another one that is the same but
with all the xs replaced by 1s.
3.) Reverse (1.) - if you wrote the predicate nicely, you just call it
again with a different argument instantiated.
4.) Evaluate the result.
Nick
>
> It's ok with X :
> ?- P = 3*X**5+5*X-2, X=1, V is P.
>
>Thanks a lot,
>
> JG
--
Nick Wedd nick@maproom.co.uk
|
|
|
|
|