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

Better handling of exponant when normalizing the ODEs

parent 03313d24
......@@ -1360,27 +1360,30 @@ normalize_ode(NormalForm) :-
d( X )/dt = NewExpr,
(
ode(Id, X, Expr),
distribute(Expr, ExprT),
normalize_expression(ExprT, ListVar, NewExpr)
normalize_expression(Expr, ListVar, NewExpr)
),
NormalForm
)
)).
normalize_expression(Expr1 + Expr2, ListVar, NExpr1 + NExpr2) :- !,
normalize_expression(Expr1, ListVar, NExpr1),
normalize_expression(Expr2, ListVar, NExpr2).
normalize_expression(Expr, ListVar, NewExpr) :-
distribute(Expr, ExprTempo),
normalize_expr_sr(ExprTempo, ListVar, NewExpr).
normalize_expression(Expr1 - Expr2, ListVar, NExpr1 + NExpr2) :- !,
normalize_expression(Expr1, ListVar, NExpr1),
normalize_expr_sr(Expr1 + Expr2, ListVar, NExpr1 + NExpr2) :- !,
normalize_expr_sr(Expr1, ListVar, NExpr1),
normalize_expr_sr(Expr2, ListVar, NExpr2).
normalize_expr_sr(Expr1 - Expr2, ListVar, NExpr1 + NExpr2) :- !,
normalize_expr_sr(Expr1, ListVar, NExpr1),
distribute(Expr2*(-1), TExpr2),
normalize_expression(TExpr2, ListVar, NExpr2).
normalize_expr_sr(TExpr2, ListVar, NExpr2).
normalize_expression(-Expr, ListVar, NExpr) :- !,
normalize_expr_sr(-Expr, ListVar, NExpr) :- !,
distribute(Expr*(-1), TExpr),
normalize_expression(TExpr, ListVar, NExpr).
normalize_expr_sr(TExpr, ListVar, NExpr).
normalize_expression(Term, ListVar, NTerm) :-
normalize_expr_sr(Term, ListVar, NTerm) :-
normalize_term(Term, ListVar, NTerm).
......@@ -1389,22 +1392,8 @@ normalize_term(Term, ListVar, NewTerm) :-
filter_other(Numerator, ListVar, Numbers, Parameters, Variables),
rewrite_term(Numbers, Parameters, Variables, Denominator, NewTerm).
%! filter_denominator(Term, Numerator, Denominator)
filter_denominator(Term*X^N, Num, [NTerm|Den]) :-
number(N), N<0, !,
Nm is -N,
(
Nm = 1
->
NTerm = X
;
NTerm = X^Nm
),
filter_denominator(Term, Num, Den).
filter_denominator(Term*X^N, [X^N|Num], Den) :- !,
number(N), N>0, !,
filter_denominator(Term, Num, Den).
%! filter_denominator(Term, Numerator, Denominator
%
filter_denominator(T1*T2, Num12, Den12) :- !,
filter_denominator(T1, Num1, Den1),
......@@ -1412,20 +1401,44 @@ filter_denominator(T1*T2, Num12, Den12) :- !,
append(Num1, Num2, Num12),
append(Den1, Den2, Den12).
filter_denominator(Term/X, Num, [X|Den]) :- !,
filter_denominator(Term, Num, Den).
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(X^N, [], [NTerm]) :- !,
number(N), N<0, !,
Nm is -N,
filter_denominator(X^Nr, Num, Den) :-
(
Nm = 1
number(Nr)
->
NTerm = X
N = Nr
;
NTerm = X^Nm
parameter_value(Nr, N)
),!,
(
N<0
->
Nm is -N,
(
Nm =:= 1
->
filter_denominator(X, Den, Num)
;
Num = [],
Den = [X^Nm]
)
;
N =:= 1
->
filter_denominator(X, Num, Den)
;
filter_denominator(X, NumT, DenT),
assert(powerN(X,X^N)),
maplist(powerN,NumT,Num),
maplist(powerN,DenT,Den),
retract(powerN(X,X^N))
).
filter_denominator(X, [X], []).
......
......@@ -109,7 +109,7 @@ test(
clear_model,
with_current_ode_system((
add_ode(d(x)/dt = (1*(k9*k1*k2) + (-1)*(k9*k3*x1) + (-1)*(k9*k8*x*x/(x+k7)))/k9),
normalize_ode([d(x)/dt = k9*k1*k2/k9+ - (k9*k3*x1)/k9+ - (k9*k8*x^2)/((x+k7)*k9)])
normalize_ode([d(x)/dt = k9*k1*k2/k9+ - (k9*k3*x1)/k9+ - (k9*k8*x^2)/(k9*(x+k7))])
)).
test(
......
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