Remove '+ 0' when simplifying polynomials

This commit is contained in:
Hugo Sales 2018-11-22 16:32:21 +00:00
parent e9536674be
commit 5b4b38e0f9

View File

@ -263,30 +263,30 @@ join_similar_parts_of_term([], []).
% Simplifies a polynomial.
% TODO: not everything is a +, there are -
%
simplify_polynomial(M, M2) :-
%% Are we dealing with a valid term?
%is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
term(M),
%% If so, simplify it.
simplify_term(M, M2),
simplify_polynomial(0, 0) :-
!.
simplify_polynomial(P, P2) :-
polynomial_to_list(P, L),
transform_list(term_to_list, L, L2),
transform_list(join_similar_parts_of_term, L2, L3),
transform_list(sort(0, @=<), L3, L4),
maplist(term_to_list, L, L2),
maplist(join_similar_parts_of_term, L2, L3),
maplist(sort(0, @=<), L3, L4),
join_similar_terms(L4, L5),
transform_list(sort(0, @>=), L5, L6),
transform_list(term_to_list, L7, L6),
polynomial_to_list(P2, L7),
delete(L7, 0, L8),
polynomial_to_list(P2, L8),
!.
%% Tests:
%% ?- simplify_polynomial(1, X).
%@ X = 1.
%% ?- simplify_polynomial(0, X).
%@ X = 0.
%% ?- simplify_polynomial(x, X).
%@ X = x.
%% ?- simplify_polynomial(x*x, X).
%@ X = x^2.
%% ?- simplify_polynomial(0 + x*x, X).
%@ X = x^2.
%% ?- simplify_polynomial(x^2*x + 3*x^3, X).
%@ X = 4*x^3.
%% ?- simplify_polynomial(x^2*x + 3*x^3 + x^3 + x*x*x, X).