doc in progress

This commit is contained in:
Diogo Cordeiro 2018-11-18 16:33:09 +00:00
parent 93a43cde52
commit ff8971c141

View File

@ -2,61 +2,119 @@
%% Follows 'Coding guidelines for Prolog' - Theory and Practice of Logic Programming %% Follows 'Coding guidelines for Prolog' - Theory and Practice of Logic Programming
%% https://doi.org/10.1017/S1471068411000391 %% https://doi.org/10.1017/S1471068411000391
%% polynomial_variables(-List:atom) is det
%
% List of possible polynomial variables
%
polynomial_variables([x, y, z]). polynomial_variables([x, y, z]).
polynomial_variable_p(X) :-
polynomial_variables(V), member(X, V).
power_p(X) :- %% polynomial_variable(?X:atom) is det
polynomial_variable_p(X), !. %
power_p(X^N) :- % Returns true if X is a polynomial variable, false otherwise.
polynomial_variable_p(X), integer(N), N > 1, !. %
term_p(N) :- polynomial_variable(X) :-
number(N). polynomial_variables(V),
term_p(X) :- member(X, V).
power_p(X).
term_p(L * R) :-
term_p(L),
term_p(R), !.
polynomial_p(M) :- %% power(+X:atom) is det
term_p(M). %
polynomial_p(L + R) :- % Left greedy % Returns true if X is a power monomial, false otherwise.
polynomial_p(L), % Why? %
term_p(R), !.
%% ?- polynomial_p(3*x^2+y*z). power(X) :-
%@ true. polynomial_variable(X),
%% ?- polynomial_p(x^100*y*z). !.
%@ true. power(X^N) :-
%% ?- polynomial_p(x+y+z). polynomial_variable(X),
%@ true. integer(N),
%@ false. N > 1,
%@ false.
%% ?- polynomial_p(3*x^2+y*z+x^100*y*z).
%@ true.
%@ true.
%% @ false. WIP
simplify_term(1 * P, P) :-
term_p(P), !.
simplify_term(0 * _, 0) :-
!. !.
simplify_term(T, T2) :-
term_to_list(T, L),
sort(0, @=<, L, L2),
join_like_terms(L2, L3),
sort(0, @>=, L3, L4),
term_to_list(T2, L4).
%% ?- simplify_term(2*y*z*x^3*x, X). %% term(+N:atom) is det
%@ X = [z^1, y^1, x^4, 2]. %
% Returns true if N is a monomial, false otherwise.
%
term(N) :-
number(N).
term(X) :-
power(X).
term(L * R) :-
term(L),
term(R), !.
%% is_term_valid_in_function(+T, +F) is det
%
% Returns true if valid Term, fails with UI message otherwise.
% The fail message reports which Term is invalid and in which
% function the problem ocurred.
%
is_term_valid_in_function(T, F) :-
(
term(T)
;
write("Invalid term in "),
write(F),
write(": "),
write(T),
fail
).
%% polynomial(+M:atom) is det
%
% Returns true if polynomial, false otherwise.
%
polynomial(M) :-
term(M).
polynomial(L + R) :-
polynomial(L),
term(R), !.
%% power_to_canon(+T:atom, -T^N:atom) is det
%
% Returns a canon power term.
%
power_to_canon(T, T^1).
power_to_canon(T^N, T^N).
%% term_to_list(+T, -List) is det
%
%
%
term_to_list(L * N, [N | TS]) :-
number(N),
term_to_list(L, TS),
!.
term_to_list(L * T, [T2 | TS]) :-
power(T),
power_to_canon(T, T2),
term_to_list(L, TS),
!.
term_to_list(T, [T]).
%% ?- term_to_list(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].
%% ?- term_to_list(X, [2, x^1, y^1]).
%@ X = y^1*x*2.
%@ X = y^1*x*2.
%@ X = y^1*x*2.
%@ X = y*x*2.
%@ X = x*2.
%@ X = 2.
%@ false. %@ false.
%@ X = [2, x^4, y^1, z^1]. %@ false.
%@ X = [2, x^1, x^3, y^1, z^1]. %@ X = x*2.
%% ?- simplify_term(2*y*z*23*x*y*x^3*x, X).
%@ X = [2, 23, x^1, x^3, y^1, z^1]. %% join_like_terms(List, List)
%@ X = [46, x^4, y^1, z^1]. %
%
%
join_like_terms([T1, T2 | L], [B1^N | L2]) :- join_like_terms([T1, T2 | L], [B1^N | L2]) :-
not(number(T1)), not(number(T1)),
@ -83,81 +141,67 @@ join_like_terms([], []).
%@ T = [6, x^3, y^7]. %@ T = [6, x^3, y^7].
%@ T = [6, x^3, y]. %@ T = [6, x^3, y].
term_to_list(L * N, [N | TS]) :- %% simplify_term(+T:atom, -P) is det
number(N), %
term_to_list(L, TS), % Simplifies a term.
!. %
term_to_list(L * T, [T2 | TS]) :-
power_p(T),
power_to_canon(T, T2),
term_to_list(L, TS),
!.
term_to_list(T, [T]).
list_to_term([N | NS], N * L) :- simplify_term(1 * P, P).
number(N), simplify_term(0 * _, 0).
term_to_list(L, NS). simplify_term(T, T2) :-
term_to_list(T, L),
sort(0, @=<, L, L2),
join_like_terms(L2, L3),
sort(0, @>=, L3, L4),
term_to_list(T2, L4).
%% ?- term_to_list(2*y*z*23*x*y*x^3*x, X). %% ?- simplify_term(2*y*z*x^3*x, X).
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2]. %@ X = [z^1, y^1, x^4, 2].
%% ?- term_to_list(X, [2, x^1, y^1]).
%@ X = y^1*x*2.
%@ X = y^1*x*2.
%@ X = y^1*x*2.
%@ X = y*x*2.
%@ X = x*2.
%@ X = 2.
%@ false. %@ false.
%@ false. %@ X = [2, x^4, y^1, z^1].
%@ X = x*2. %@ X = [2, x^1, x^3, y^1, z^1].
%% ?- simplify_term(2*y*z*23*x*y*x^3*x, X).
%@ X = [2, 23, x^1, x^3, y^1, z^1].
%@ X = [46, x^4, y^1, z^1].
power_to_canon(T, T^1) :- %% monomial_parts(X, Y, Z)
polynomial_variable_p(T), %
!. %
power_to_canon(T^N, T^N) :- %
polynomial_variable_p(T),
!.
simplify_polynomial(M, M2) :-
term_p(M), simplify_term(M, M2), !.
simplify_polynomial(P + 0, P) :-
term_p(P), !.
simplify_polynomial(0 + P, P) :-
term_p(P), !.
simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2), simplify_term(M, M2).
simplify_polynomial(P + M, P2 + M3) :-
monomial_parts(M, _, XExp),
delete_monomial(P, XExp, M2, P2), !,
add_monomial(M, M2, M3).
simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2), simplify_term(M, M2).
%% ?- simplify_polynomial(1*x+(-1)*x, P).
monomial_parts(X, 1, X) :- monomial_parts(X, 1, X) :-
power_p(X), !. power(X),
!.
monomial_parts(X^N, 1, X^N) :- monomial_parts(X^N, 1, X^N) :-
power_p(X^N), !. power(X^N),
!.
monomial_parts(K * M, K, M) :- monomial_parts(K * M, K, M) :-
coeficient_p(K), !. number(K),
!.
monomial_parts(K, K, indep) :- monomial_parts(K, K, indep) :-
coeficient_p(K), !. number(K),
!.
delete_monomial(M, X, M, 0) :- delete_monomial(M, X, M, 0) :-
term_p(M), term_p(M),
monomial_parts(M, _, X), !. monomial_parts(M, _, X),
!.
delete_monomial(M + M2, X, M, M2) :- delete_monomial(M + M2, X, M, M2) :-
term_p(M2), term_p(M), term_p(M2),
monomial_parts(M, _, X), !. term_p(M),
monomial_parts(M, _, X),
!.
delete_monomial(P + M, X, M, P) :- delete_monomial(P + M, X, M, P) :-
term_p(M), monomial_parts(M, _, X), !. term_p(M),
monomial_parts(M, _, X),
!.
delete_monomial(P + M2, X, M, P2 + M2) :- delete_monomial(P + M2, X, M, P2 + M2) :-
delete_monomial(P, X, M, P2). delete_monomial(P, X, M, P2).
add_monomial(K1, K2, K3) :- add_monomial(K1, K2, K3) :-
number(K1), number(K2), !, number(K1),
number(K2), !,
K3 is K1 + K2. K3 is K1 + K2.
add_monomial(M1, M2, M3) :- add_monomial(M1, M2, M3) :-
monomial_parts(M1, K1, XExp), monomial_parts(M1, K1, XExp),
@ -173,17 +217,48 @@ p_aux_add_monomial(1, XExp, XExp) :-
!. !.
p_aux_add_monomial(K, XExp, K * XExp). p_aux_add_monomial(K, XExp, K * XExp).
%% simplify_polynomial(+P:atom, -P2:atom) is det
%
% Simplifies a polynomial.
%
simplify_polynomial(M, M2) :-
%% Are we dealing with a valid term?
is_term_valid_in_function(M, "simplify_polynomial(M, M2)"),
%% If so, simplify it.
simplify_term(M, M2),
!.
simplify_polynomial(P + 0, P) :-
%% Ensure valid term
is_term_valid_in_function(P, "simplify_polynomial(P + 0, P)"),
!.
simplify_polynomial(0 + P, P) :-
%% Ensure valid term
is_term_valid_in_function(P, "simplify_polynomial(0 + P, P)"),
!.
simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2),
simplify_term(M, M2).
simplify_polynomial(P + M, P2 + M3) :-
monomial_parts(M, _, XExp),
delete_monomial(P, XExp, M2, P2),
!,
add_monomial(M, M2, M3).
simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2),
simplify_term(M, M2).
closure_simplify_polynomial(P, P) :- closure_simplify_polynomial(P, P) :-
simplify_polynomial(P, P2), simplify_polynomial(P, P2),
P==P2, !. P==P2,
!.
closure_simplify_polynomial(P, P3) :- closure_simplify_polynomial(P, P3) :-
simplify_polynomial(P, P2), simplify_polynomial(P, P2),
closure_simplify_polynomial(P2, P3), !. closure_simplify_polynomial(P2, P3),
!.
list_to_term([N | NS], N * L) :-
number(N),
term_to_list(L, NS).
%% ?- simplify_polynomial(1*x+(-1)*x, P).
%@ P = x+ -1*x .
%@ P = x+ -1*x
%@ Unknown action: q (h for help)
%@ Action?
%@ Unknown action: q (h for help)
%@ Action? .