Commit f0342ac2 authored by Mathieu Hemery's avatar Mathieu Hemery
Browse files

This one should be better

parent 6f5f8b5e
......@@ -1300,15 +1300,7 @@ ode_add_expression_to_molecule(NewExpression, Molecule) :-
remove_fraction(ListVar, d( X )/dt = Expr, d( X )/dt = NewExpr) :-
find_common_denominator(Expr, Denominator),
multiply_each_term(Expr, Denominator, Expr2),
simplify(Expr2, Expr3),
(
Expr = Expr2
->
NewExpr = Expr3
;
normalize_expression(Expr3, ListVar, Expr4),
remove_fraction(ListVar, d(X)/dt = Expr4, d(X)/dt = NewExpr)
).
simplify(Expr2, NewExpr).
%! find_common_denominator(+Expr, +ListVariable, -ListDenominator)
......@@ -1397,27 +1389,32 @@ normalize_expr_sr(Term, ListVar, NTerm) :-
normalize_term(Term, ListVar, NTerm).
normalize_term(Term, ListVar, NewTerm) :-
filter_denominator(Term, Numerator, Denominator),
filter_other(Numerator, ListVar, Numbers, Parameters, Variables),
rewrite_term(Numbers, Parameters, Variables, Denominator, NewTerm).
normalize_term(Term, ListVar, Numerator/Denominator) :-
filter_denominator(Term, NumeratorTempo/DenominatorTempo),
filter_other(NumeratorTempo, ListVar, OrdNumerator),
simplify(OrdNumerator, Numerator),
simplify(DenominatorTempo, Denominator).
%! filter_denominator(Term, Numerator, Denominator
%! filter_denominator(Term, Numerator/Denominator)
%
filter_denominator(T1*T2, Num12, Den12) :- !,
filter_denominator(T1, Num1, Den1),
filter_denominator(T2, Num2, Den2),
append(Num1, Num2, Num12),
append(Den1, Den2, Den12).
filter_denominator(T1*T2, (Num1*Num2)/(Den1*Den2)) :- !,
filter_denominator(T1, Num1/Den1),
filter_denominator(T2, Num2/Den2).
filter_denominator(T1/T2, Num12, Den12) :- !,
filter_denominator(T1, Num1, Den1),
filter_denominator(T2, Num2, Den2),
append(Num1, Den2, Num12),
append(Den1, Num2, Den12).
filter_denominator(T1/T2, (Num1*Den2)/(Num2*Den1)) :- !,
filter_denominator(T1, Num1/Den1),
filter_denominator(T2, Num2/Den2).
filter_denominator(X^Nr, Num, Den) :-
filter_denominator(T1+T2, (Num1*Den2 + Num2*Den1)/(Den1*Den2)) :- !,
filter_denominator(T1, Num1/Den1),
filter_denominator(T2, Num2/Den2).
filter_denominator(T1-T2, (Num1*Den2 - Num2*Den1)/(Den1*Den2)) :- !,
filter_denominator(T1, Num1/Den1),
filter_denominator(T2, Num2/Den2).
filter_denominator(X^Nr, Num/Den) :-
(
number(Nr)
->
......@@ -1433,7 +1430,7 @@ filter_denominator(X^Nr, Num, Den) :-
(
Nm =:= 1
->
filter_denominator(X, Den, Num)
filter_denominator(X, Den/Num)
;
Num = [],
Den = [X^Nm]
......@@ -1441,28 +1438,35 @@ filter_denominator(X^Nr, Num, Den) :-
;
N =:= 1
->
filter_denominator(X, Num, Den)
filter_denominator(X, Num/Den)
;
filter_denominator(X, NumT, DenT),
maplist({N}/[X,Y]>>(Y = X^N), NumT, Num),
maplist({N}/[X,Y]>>(Y = X^N), DenT, Den)
filter_denominator(X, NumT/DenT),
Num = NumT^N,
Den = DenT^N
).
filter_denominator(X, [X], []).
filter_denominator(X, X/1).
filter_other(Num, LV, Numbers*Parameters*Variables) :-
filter_other(Num, LV, Numbers, Parameters, Variables).
filter_other([Head|Tail], LV, [Head|Numbers], Parameters, Variables) :-
filter_other(Tail*Head, LV, Numbers*Head, Parameters, Variables) :-
number(Head),!,
filter_other(Tail, LV, Numbers, Parameters, Variables).
filter_other([Head|Tail], LV, Numbers, Parameters, [Head|Variables]) :-
filter_other(Tail*Head, LV, Numbers, Parameters, Variables*Head) :-
member(Head, LV),!,
filter_other(Tail, LV, Numbers, Parameters, Variables).
filter_other([Head|Tail], LV, Numbers, [Head|Parameters], Variables) :- !,
filter_other(Tail*Head, LV, Numbers, Parameters*Head, Variables) :- !,
filter_other(Tail, LV, Numbers, Parameters, Variables).
filter_other([],_LV,[],[],[]).
filter_other(Last,_LV,Last,1,1) :-
number(Last),!.
filter_other(Last,_LV,1,1,Last) :-
member(Last, LV),!.
filter_other(Last,_LV,1,Last,1).
rewrite_term(Numbers, Parameter_list, Variable_list, Denominator, Term) :-
......
......@@ -101,8 +101,8 @@ test(
remove_fraction([a], d(a)/dt = a/b, d(a)/dt = a),
remove_fraction([a], d(a)/dt = a/(b+c) + d, d(a)/dt = a + d*b + d*c),
remove_fraction([a], d(a)/dt = a/(b+c) + -1*d, d(a)/dt = a - d*b - d*c),
remove_fraction([a], d(a)/dt = a/b + c/d + e, d(a)/dt = d*a + c*b + e*b*d),
remove_fraction([a], d(a)/dt = a/b + c/d, d(a)/dt = d*a + c*b).
remove_fraction([a], d(a)/dt = a/b + c/d + e, d(a)/dt = a*d + c*b + e*b*d),
remove_fraction([a], d(a)/dt = a/b + c/d, d(a)/dt = a*d + c*b).
test(
'normalize_ode1', []) :-
......@@ -117,7 +117,7 @@ test(
clear_model,
with_current_ode_system((
add_ode(d(x)/dt = k7+ -1*k8*x + -1*k9*x/(x+k1)),
normalize_ode([d(x)/dt = k7+ - (k8*x)+ - (k9*x)/(x+k1)], [x])
normalize_ode([d(x)/dt=k7/1+ - (k8*x)/1+ - (k9*x)/(x+k1)], [x])
)).
:- end_tests(ode).
Supports Markdown
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