Review of merge and small bug fixes

This commit is contained in:
Hugo Sales 2018-11-22 13:57:46 +00:00
parent 03e8993ec5
commit c6640bf7bb

View File

@ -35,7 +35,7 @@ polynomial_variable(X) :-
% %
power(P^N) :- power(P^N) :-
( (
zcompare((<), 0, N), zcompare((<), 0, N),
polynomial_variable(P) polynomial_variable(P)
; ;
fail fail
@ -45,13 +45,21 @@ power(X) :-
%% Tests: %% Tests:
%% ?- power(x). %% ?- power(x).
%@ true . %@ true .
%% ?- power(a).
%@ false.
%% ?- power(x^1). %% ?- power(x^1).
%@ true . %@ true .
%% ?- power(x^3). %% ?- power(x^3).
%@ true . %@ true .
%% ?- power(x^(-3)). %% ?- power(x^(-3)).
%@ error. %@ false.
%% ?- power(X). %% ?- power(X).
%@ X = x^_7334,
%@ _7334 in 1..sup ;
%@ X = y^_7334,
%@ _7334 in 1..sup ;
%@ X = z^_7334,
%@ _7334 in 1..sup ;
%@ X = x ; %@ X = x ;
%@ X = y ; %@ X = y ;
%@ X = z. %@ X = z.
@ -62,18 +70,28 @@ power(X) :-
% %
term(N) :- term(N) :-
number(N). number(N).
%% N in inf..sup.
term(X) :- term(X) :-
power(X). power(X).
term(L * R) :- term(L * R) :-
term(L), term(L),
term(R). term(R).
%% append_two_atoms_with_star(L, R, T).
%% Tests: %% Tests:
%% ?- term(2*x^3). %% ?- term(2*x^3).
%@ true . %@ true .
%% ?- term(x^(-3)). %% ?- term(x^(-3)).
%@ false. %@ false.
%% ?- term(a).
%@ false.
%% ?- term((-3)*x^2). %% ?- term((-3)*x^2).
%@ true . %@ true .
%% ?- term(3.2*x).
%@ true .
%% ?- term(X).
%% Doesn't give all possible terms, because number(N) is not reversible
%% The ic library seems to be able to help here, but it's not a part of
%% SwiPL by default
%% is_term_valid_in_predicate(+T, +F) is det %% is_term_valid_in_predicate(+T, +F) is det
% %
@ -92,8 +110,9 @@ is_term_valid_in_predicate(T, F) :-
fail fail
). ).
%% Tests: %% Tests:
%% ?- is_term_valid_in_predicate(1, "Chuck Norris"). %% ?- is_term_valid_in_predicate(1, "Test").
%@ true . %@ true .
%% ?- is_term_valid_in_predicate(a, "Test").
%% polynomial(+M:atom) is det %% polynomial(+M:atom) is det
% %
@ -113,6 +132,10 @@ polynomial(L + R) :-
%@ true . %@ true .
%% ?- polynomial(2 + 3*x + 4*x*y^3). %% ?- polynomial(2 + 3*x + 4*x*y^3).
%@ true . %@ true .
%% ?- polynomial(a).
%@ false.
%% ?- polynomial(x^(-3)).
%@ false.
%% power_to_canon(+T:atom, -T^N:atom) is det %% power_to_canon(+T:atom, -T^N:atom) is det
% %
@ -120,12 +143,7 @@ polynomial(L + R) :-
% %
power_to_canon(T^N, T^N) :- power_to_canon(T^N, T^N) :-
polynomial_variable(T), polynomial_variable(T),
%% N \= 1. N #\= 1.
(
zcompare(=, 1, N)
;
true
).
power_to_canon(T, T^1) :- power_to_canon(T, T^1) :-
polynomial_variable(T). polynomial_variable(T).
%% Tests: %% Tests:
@ -133,9 +151,12 @@ power_to_canon(T, T^1) :-
%@ X = x^1 . %@ X = x^1 .
%% ?- power_to_canon(X, x^1). %% ?- power_to_canon(X, x^1).
%@ X = x . %@ X = x .
%@ X = x .
%% ?- power_to_canon(X, x^4). %% ?- power_to_canon(X, x^4).
%@ X = x^4 . %@ X = x^4 .
%% ?- power_to_canon(X, a^1).
%@ false.
%% ?- power_to_canon(X, x^(-3)).
%@ X = x^ -3 .
%% term_to_list(?T, ?List) is det %% term_to_list(?T, ?List) is det
% %
@ -155,13 +176,13 @@ term_to_list(P, [P2]) :-
power(P), power(P),
power_to_canon(P, P2). power_to_canon(P, P2).
%% Tests: %% Tests:
%% ?- term_to_list(1, X).
%@ X = [1] .
%% ?- term_to_list(1*2*y*z*23*x*y*x^3*x, X). %% ?- term_to_list(1*2*y*z*23*x*y*x^3*x, X).
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2, 1] . %@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2, 1] .
%% ?- term_to_list(X, [y^1, x^1]). %% ?- term_to_list(X, [y^1, x^1]).
%@ X = x*y . %@ X = x*y .
%% ?- term_to_list(X, [x^4]). %% ?- term_to_list(X, [x^4]).
%@ false.
%@ false.
%@ X = x^4 . %@ X = x^4 .
%% ?- term_to_list(X, [y^6, z^2, x^4]). %% ?- term_to_list(X, [y^6, z^2, x^4]).
%@ X = x^4*z^2*y^6 . %@ X = x^4*z^2*y^6 .
@ -177,6 +198,10 @@ simplify_term(Term_In, Term_Out) :-
member(0, L2), member(0, L2),
Term_Out = 0 Term_Out = 0
; ;
(
length(L2, 1),
Term_Out = Term_In
);
exclude(==(1), L2, L3), exclude(==(1), L2, L3),
join_like_terms(L3, L4), join_like_terms(L3, L4),
sort(0, @>=, L4, L5), sort(0, @>=, L4, L5),
@ -185,6 +210,10 @@ simplify_term(Term_In, Term_Out) :-
% First result is always the most simplified form. % First result is always the most simplified form.
!. !.
%% Tests: %% Tests:
%% ?- simplify_term(1, X).
%@ X = 1.
%% ?- simplify_term(x, X).
%@ X = x.
%% ?- simplify_term(2*y*z*x^3*x, X). %% ?- simplify_term(2*y*z*x^3*x, X).
%@ X = 2*x^4*y*z. %@ X = 2*x^4*y*z.
%% ?- simplify_term(1*y*z*x^3*x, X). %% ?- simplify_term(1*y*z*x^3*x, X).
@ -193,6 +222,10 @@ simplify_term(Term_In, Term_Out) :-
%@ X = 0. %@ X = 0.
%% ?- simplify_term(6*y*z*7*x*y*x^3*x, X). %% ?- simplify_term(6*y*z*7*x*y*x^3*x, X).
%@ X = 42*x^2*x^3*y^2*z. %@ X = 42*x^2*x^3*y^2*z.
%% ?- simplify_term(a, X).
%@ false.
%% ?- simplify_term(x^(-3), X).
%@ false.
%% join_like_terms(+List, -List) %% join_like_terms(+List, -List)
% %
@ -203,7 +236,6 @@ join_like_terms([P1, P2 | L], [B^N | L2]) :-
power(P2), power(P2),
B^N1 = P1, B^N1 = P1,
B^N2 = P2, B^N2 = P2,
%% B1 == B2, % Wasn't working before..?
N is N1 + N2, N is N1 + N2,
join_like_terms(L, L2). join_like_terms(L, L2).
join_like_terms([N1, N2 | L], [N | L2]) :- join_like_terms([N1, N2 | L], [N | L2]) :-
@ -216,11 +248,9 @@ join_like_terms([X | L], [X | L2]) :-
join_like_terms([], []). join_like_terms([], []).
%% Tests: %% Tests:
%% ?- join_like_terms([2, 3, x^1, x^2], T). %% ?- join_like_terms([2, 3, x^1, x^2], T).
%@ T = [6, x^3]. %@ T = [6, x^3] .
%@ T = [6, x^3].
%% ?- join_like_terms([2, 3, x^1, x^2, y^1, y^6], T). %% ?- join_like_terms([2, 3, x^1, x^2, y^1, y^6], T).
%@ T = [6, x^3, y^7]. %@ T = [6, x^3, y^7] .
%@ T = [6, x^3, y^7].
%% simplify_polynomial(+P:atom, -P2:atom) is det %% simplify_polynomial(+P:atom, -P2:atom) is det
% %
@ -230,7 +260,7 @@ join_like_terms([], []).
simplify_polynomial(M, M2) :- simplify_polynomial(M, M2) :-
%% Are we dealing with a valid term? %% Are we dealing with a valid term?
%is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"), %is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
term(M), %% term(M),
%% If so, simplify it. %% If so, simplify it.
simplify_term(M, M2), simplify_term(M, M2),
!. !.
@ -256,7 +286,9 @@ simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2), simplify_polynomial(P, P2),
simplify_term(M, M2). simplify_term(M, M2).
%% Tests: %% Tests:
%% ?- simplify_polynomial(1, 1). %% ?- simplify_polynomial(1, X).
%@ false.
%@ false.
%@ Invalid term in simplify_polynomial(M, M2): 1 %@ Invalid term in simplify_polynomial(M, M2): 1
%@ false. %@ false.