diff --git a/polymani.pl b/polymani.pl index faa596c..c0bd94f 100644 --- a/polymani.pl +++ b/polymani.pl @@ -351,7 +351,6 @@ term_to_list(-P, [-P2]) :- %% Tests: %% ?- term_to_list(1, X). %@ X = [1] . -%@ X = [1] . %% ?- term_to_list(-1, X). %@ X = [-1] . %% ?- term_to_list(x, X). @@ -368,7 +367,6 @@ term_to_list(-P, [-P2]) :- %@ X = -1 . %% ?- term_to_list(X, [x^1, -1]). %@ X = -1*x . -%@ X = -1*x . %% ?- term_to_list(X, [-x^1]). %@ X = -x . %% ?- term_to_list(X, [y^1, x^1]). @@ -379,6 +377,8 @@ term_to_list(-P, [-P2]) :- %@ X = x^4*z^2*y^6 . %% ?- term_to_list(X, [y^6, z^2, x^4, -2]). %@ X = -2*x^4*z^2*y^6 . +%% ?- term_to_list(X, [x^1, 0]). +%@ X = 0*x . %% simplify_term(+Term_In:term, ?Term_Out:term) is det % @@ -526,7 +526,7 @@ simplify_polynomial(P, P2) :- % % Simplifies a polynomial represented as a list. % -simplify_polynomial_as_list(L, L11) :- +simplify_polynomial_as_list(L, L13) :- %% Convert each term to a list maplist(term_to_list, L, L2), %% Sort each sublist; done so the next @@ -540,23 +540,31 @@ simplify_polynomial_as_list(L, L11) :- %% Done so the next call simplifies has less work maplist(sort(0, @=<), L5, L6), join_similar_terms(L6, L7), + %% Exclude any sublist that includes a 0 (such as the + %% equivalent to the term 0*x) + exclude(member(0), L7, L8), %% Reverse each sublist, because the next call %% reverses the result - maplist(reverse, L7, L8), - maplist(term_to_list, L9, L8), + maplist(reverse, L8, L9), + maplist(term_to_list, L10, L9), %% Delete any 0 from the list - delete(L9, 0, L10), + delete(L10, 0, L11), %% Sort list converting back gives the result in the correct order - sort(0, @=<, L10, L11). + sort(0, @=<, L11, L12), + ( + %% If the list is empty, the result is a list with 0 + L12 = [], L13 = [0] + ; + %% Otherwise, this is the result + L13 = L12 + ). %% Tests: %% ?- simplify_polynomial_as_list([x, 1, x^2, x*y, 3*x^2, 4*x], L). %@ L = [1, 4*x^2, 5*x, x*y] . -%@ L = [1, 4*x^2, 5*x, x*y] . %% ?- simplify_polynomial_as_list([1, x^2, x*y, 3*x^2, -4, -1*x], L). %@ L = [-3, -1*x, 4*x^2, x*y] . -%@ L = [-3, -1*x, 4*x^2, x*y] . -%% ?- simplify_polynomial_as_list([0*x], L). -%@ L = [0*x] . +%% ?- simplify_polynomial_as_list([0*x, 0], L). +%@ L = [0] . %% join_similar_terms(+P:ListList, -P2:ListList) is det %