From 01dbcfb0352ad6ff9d46c723c2aa59352a57b251 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Sun, 25 Nov 2018 17:50:49 +0000 Subject: [PATCH] Let scale polynomial support negatives --- polymani.pl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/polymani.pl b/polymani.pl index 42c4699..04d1a0c 100644 --- a/polymani.pl +++ b/polymani.pl @@ -662,7 +662,6 @@ term_to_canon(L, [N | L2]) :- % term_to_canon_with_coefficient(N, [N2 | TS], TS2) :- number(N2), - %% {N2 >= 0; N2 < 0}, term_to_canon_with_coefficient(N3, TS, TS2), N is N2 * N3, !. @@ -732,7 +731,7 @@ add_terms([NL | TL], [NR | TR], [N2 | TL2]) :- %% have a number in front, so it can be added term_to_canon([NL | TL], [NL2 | TL2]), term_to_canon([NR | TR], [NR2 | TR2]), - %% If they rest of the term is the same + %% If the rest of the term is the same TL2 == TR2, %% Add the coeficients N2 is NL2 + NR2. @@ -856,13 +855,16 @@ scale_polynomial(P, C, S) :- polynomial_to_list(P, L), %% Convert each term to a list maplist(term_to_list, L, L2), + %% Canonize terms + maplist(term_to_canon, L2, L3), %% Append C to the start of each sublist - maplist(cons(C), L2, L3), - %% Convert back - maplist(term_to_list, L4, L3), + maplist(cons(C), L3, L4), + %% Convert to a list of terms + maplist(term_to_list, L5, L4), %% Simplify the resulting polynomial - simplify_polynomial_as_list(L4, L5), - list_to_polynomial(L5, S), + simplify_polynomial_as_list(L5, L6), + %% Return as a simplified polynomial + list_to_polynomial(L6, S), !. %% Tests: %% ?- scale_polynomial(3*x^2, 2, S). @@ -874,7 +876,7 @@ scale_polynomial(P, C, S) :- % cons(C, L, [C | L]). %% Tests: -%% It just trivially works. +%% No need for tests as it is trivially correct. %% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det %