Let scale polynomial support negatives

This commit is contained in:
Diogo Cordeiro 2018-11-25 17:50:49 +00:00
parent 0f861a810b
commit 01dbcfb035

View File

@ -662,7 +662,6 @@ term_to_canon(L, [N | L2]) :-
% %
term_to_canon_with_coefficient(N, [N2 | TS], TS2) :- term_to_canon_with_coefficient(N, [N2 | TS], TS2) :-
number(N2), number(N2),
%% {N2 >= 0; N2 < 0},
term_to_canon_with_coefficient(N3, TS, TS2), term_to_canon_with_coefficient(N3, TS, TS2),
N is N2 * N3, 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 %% have a number in front, so it can be added
term_to_canon([NL | TL], [NL2 | TL2]), term_to_canon([NL | TL], [NL2 | TL2]),
term_to_canon([NR | TR], [NR2 | TR2]), 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, TL2 == TR2,
%% Add the coeficients %% Add the coeficients
N2 is NL2 + NR2. N2 is NL2 + NR2.
@ -856,13 +855,16 @@ scale_polynomial(P, C, S) :-
polynomial_to_list(P, L), polynomial_to_list(P, L),
%% Convert each term to a list %% Convert each term to a list
maplist(term_to_list, L, L2), maplist(term_to_list, L, L2),
%% Canonize terms
maplist(term_to_canon, L2, L3),
%% Append C to the start of each sublist %% Append C to the start of each sublist
maplist(cons(C), L2, L3), maplist(cons(C), L3, L4),
%% Convert back %% Convert to a list of terms
maplist(term_to_list, L4, L3), maplist(term_to_list, L5, L4),
%% Simplify the resulting polynomial %% Simplify the resulting polynomial
simplify_polynomial_as_list(L4, L5), simplify_polynomial_as_list(L5, L6),
list_to_polynomial(L5, S), %% Return as a simplified polynomial
list_to_polynomial(L6, S),
!. !.
%% Tests: %% Tests:
%% ?- scale_polynomial(3*x^2, 2, S). %% ?- scale_polynomial(3*x^2, 2, S).
@ -874,7 +876,7 @@ scale_polynomial(P, C, S) :-
% %
cons(C, L, [C | L]). cons(C, L, [C | L]).
%% Tests: %% Tests:
%% It just trivially works. %% No need for tests as it is trivially correct.
%% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det %% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det
% %