Commit 564c9057 authored by Mathieu Hemery's avatar Mathieu Hemery
Browse files

Improve the is_polynomial recognition

parent 6e8f73be
......@@ -56,7 +56,8 @@
ode_predicate/1,
remove_fraction/3,
normalize_ode/2,
is_polynomial/1
is_polynomial/1,
is_polynomial_sr/1
]).
% Only for separate compilation/linking
......@@ -1535,21 +1536,36 @@ multiply_them([Head|Tail], Term*Head) :-
multiply_them(Tail, Term).
%! is_polynomial(+Poly)
%! is_polynomial(+Expr)
%
% detect if a given term is a polynomial
% Check detect if a given term is a polynomial
is_polynomial(A + B) :- is_polynomial(A), is_polynomial(B).
is_polynomial(A - B) :- is_polynomial(A), is_polynomial(B).
is_polynomial(-(A)) :- is_polynomial(A).
is_polynomial(A*B) :- is_polynomial(A), is_polynomial(B).
is_polynomial(A^N) :- is_polynomial(A), is_numeric(N).
is_polynomial(A/N) :- is_numeric(N), is_polynomial(A).
is_polynomial(A) :- is_numeric(A).
is_polynomial(A) :- is_molecule(A).
is_polynomial(Expr) :-
(
is_polynomial_sr(Expr)
;
format("~w is not recognized as polynomial.~n", [Expr]),
fail
),!.
is_polynomial_sr(A + B) :- is_polynomial_sr(A), is_polynomial_sr(B).
is_polynomial_sr(A - B) :- is_polynomial_sr(A), is_polynomial_sr(B).
is_polynomial_sr(-(A)) :- is_polynomial_sr(A).
is_polynomial_sr(A*B) :- is_polynomial_sr(A), is_polynomial_sr(B).
is_polynomial_sr(A^N) :- is_polynomial_sr(A), is_numeric(N).
is_polynomial_sr(A/N) :- is_numeric(N), is_polynomial_sr(A).
is_polynomial_sr(A) :- is_numeric(A).
is_polynomial_sr(A) :- is_molecule(A).
is_molecule(A) :-
identifier_kind(_ModelId, A, object).
is_numeric(N) :-
item([kind:parameter, key:N]);
number(N).
is_numeric(A + B) :- is_numeric(A), is_numeric(B).
is_numeric(A - B) :- is_numeric(A), is_numeric(B).
is_numeric(-(A)) :- is_numeric(A).
is_numeric(A*B) :- is_numeric(A), is_numeric(B).
is_numeric(A^N) :- is_numeric(A), is_numeric(N).
is_numeric(A/N) :- is_numeric(N), is_numeric(A).
......@@ -120,13 +120,15 @@ test(
normalize_ode([d(x)/dt=k7/1+ - (k8*x)/1+ - (k9*x)/(x+k1)], [x])
)).
test('is_polynomial', [nondet]) :-
test('is_polynomial_sr', [nondet]) :-
clear_model,
%Ensure that a, b and c are recognized as molecules
add_ode([d(a)/dt = 1, d(b)/dt = 1, d(c)/dt = 1]),
is_polynomial(a*b + c^3 - 2*b),
is_polynomial(a*(c/4)^2),
\+(is_polynomial(exp(2*a))),
\+(is_polynomial(2*a/b)).
command(parameter(x=1, y=1, z=1)),
is_polynomial_sr(a*b + c^3 - 2*b),
is_polynomial_sr(a*(c/4)^2),
is_polynomial_sr(a/(x+y)),
\+(is_polynomial_sr(exp(2*a))),
\+(is_polynomial_sr(2*a/b)).
:- end_tests(ode).
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