Commit a63c2699 authored by FAGES Francois's avatar FAGES Francois
Browse files

Prologisback

parent 70d53534
......@@ -44,7 +44,6 @@ commands = [
"compile_transfer_function",
"compile_wgpac",
"continue",
"delete",
"delete_alias",
"delete_attribute",
"delete_column",
......@@ -52,6 +51,7 @@ commands = [
"delete_conservations",
"delete_ctl",
"delete_edge",
"delete_events",
"delete_function",
"delete_graph",
"delete_ltl_pattern",
......
% SWI-Prolog program
% Partial derivatives
% F. Fages, Inria, March 2020
% Derivative of Expression with respect to an Atom (Prolog variables are excluded)
derivative(Expression, Atom, Derivative) :-
deriv(Expression, Atom, Expr),
simplify(Expr, Derivative).
% Simple pattern matching since Prolog variables are excluded from the expression
deriv(X, X, 1) :-
!.
deriv(E, _X, 0) :-
atomic(E), !.
deriv(E1 + E2, X, DE1+DE2) :-
deriv(E1, X, DE1),
deriv(E2, X, DE2).
deriv(E1 - E2, X, DE1-DE2) :-
deriv(E1, X, DE1),
deriv(E2, X, DE2).
deriv(E1 * E2, X, DE1*E2+DE2*E1) :-
deriv(E1, X, DE1),
deriv(E2, X, DE2).
deriv(exp(E1), X, DE1*exp(E1)) :-
deriv(E1, X, DE1).
deriv(log(E1), X, DE1/E1) :-
deriv(E1, X, DE1).
%! simplify(+Expression, -SimpleExpression)
%
% Simplify an arithmetic expression
simplify(A, A):-
atomic(A), !.
simplify(A*B, R) :-
simplify(A, As),
simplify(B, Bs),
(
(As=0; Bs=0)
->
R=0
;
As=1
->
R=Bs
;
Bs=1
->
R=As
;
R=(As*Bs)
).
simplify(A+B, R) :-
simplify(A, As),
simplify(B, Bs),
(
As=0
->
R=Bs
;
Bs=0
->
R=As
;
R=(As+Bs)
).
simplify(A-B, R) :-
simplify(A, As),
simplify(B, Bs),
(
As=0
->
R = -Bs
;
Bs=0
->
R=As
;
R=(As-Bs)
).
simplify(0/_B, 0) :-
!.
simplify(A/1, A) :-
!.
simplify(A/B, R) :-
simplify(A, As),
simplify(B, Bs),
(
As=0
->
R=0
;
Bs=1
->
R=As
;
R=As/Bs
).
simplify(exp(A), R) :-
simplify(A, As),
(
As=0
->
R=1
;
R = exp(As)
).
%== Root finder ==%
%! root(+X:Function, +Precision, -X0)
%
% Determine a zero of a given function using newton method
% Precision is a positive float such that |Function(X0)| < Precision
root(X:Func, Precision, X0) :-
derivative(Func, X, Diff),
root(X:Func, Diff, Precision, 1, X0).
root(X:Func, Diff, Precision, Xcur, X0) :-
evaluate(X:Func, Xcur, Ycur),
(
abs(Ycur) < Precision
->
X0 = Xcur
;
evaluate(X:Diff, Xcur, Tcur),
{(Xcur-Xnext)*Tcur = Ycur},
root(X:Func, Diff, Precision, Xnext, X0)
).
%! evaluate(+Var:Function, X, Eval)
%
% evaluate the value of Function on point X
% use copy_term to avoid the instantiation of the symbolic representation
evaluate(Var:Function, X, Eval) :-
copy_term(Var:Function, Var_loc:Function_loc),
Var_loc is X,
Eval is Function_loc.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment