Merged documentation with latest code

This commit is contained in:
Hugo Sales 2018-11-19 16:59:53 +00:00
commit 1c6d63ed1b

View File

@ -2,116 +2,122 @@
%% 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([x, y, z]). %% polynomial_variable_list(-List:atom) is det
polynomial_variable_p(X) :- %
polynomial_variables(V), % List of possible polynomial variables
member(X, V). %
polynomial_variable_p(P) :-
polynomial_variables(V),
member(X, V),
%% number(N),
P = X^N.
polynomial_variable_list([x, y, z]).
%% polynomial_variable(?X:atom) is det
%
% Returns true if X is a polynomial variable, false otherwise.
%
polynomial_variable(X) :-
polynomial_variable_list(V),
member(X, V).
polynomial_variable(P) :-
polynomial_variable_list(V),
member(X, V),
P = X^N.
%% Tests:
%% ?- term_to_list(X, [x^4]). %% ?- term_to_list(X, [x^4]).
%@ X = x^4 . %@ X = x^4 .
power_p(X) :- %% power(+X:atom) is det
polynomial_variable_p(X). %
power_p(X^N) :- % Returns true if X is a power term, false otherwise.
polynomial_variable_p(X), integer(N), N >= 1. %
power(X) :-
%% ?- power_p(x^1). polynomial_variable(X).
power(X^N) :-
polynomial_variable(X),
integer(N),
N >= 1.
%% Tests:
%% ?- power(x^1).
%@ true . %@ true .
%@ true.
term_p(N) :-
%% term(+N:atom) is det
%
% Returns true if N is a term, false otherwise.
%
term(N) :-
number(N). number(N).
term_p(X) :- term(X) :-
power_p(X). power(X).
term_p(L * R) :- term(L * R) :-
term_p(L), term(L),
term_p(R), !. term(R),
polynomial_p(M) :-
term_p(M).
polynomial_p(L + R) :- % Left greedy
polynomial_p(L), % Why?
term_p(R), !.
%% ?- polynomial_p(3*x^2+y*z).
%@ true.
%% ?- polynomial_p(x^100*y*z).
%@ true.
%% ?- polynomial_p(x+y+z).
%@ true.
%@ false.
%@ 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) :- %% Tests:
term_to_list(T, L), %% TODO
sort(0, @=<, L, L2),
join_like_terms(L2, L3),
list_to_term(L3, T2). % Responsible for parenthesis
%% sort(0, @>=, L3, L4),
%% term_to_list(T2, L4).
%% ?- simplify_term(2*y*z*x^3*x, X). %% is_term_valid_in_predicate(+T, +F) is det
%@ X = 2*(x^4*(y*z)). %
%@ X = z*(y*(x^4*2)). % Returns true if valid Term, fails with UI message otherwise.
%% ?- simplify_term(2*y*z*23*x*y*x^3*x, X). % The fail message reports which Term is invalid and in which
%@ X = 46*(x^2*(x^3*(y^2*z))). % predicate the problem ocurred.
%@ X = z*(y^2*(x^3*(x^2*46))). %
%@ X = [2, 23, x^1, x^3, y^1, z^1]. is_term_valid_in_predicate(T, F) :-
%@ X = [46, x^4, y^1, z^1]. (
term(T)
;
write("Invalid term in "),
write(F),
write(": "),
write(T),
fail
).
%% Tests:
%% ?- is_term_valid_in_predicate().
join_like_terms([P1, P2 | L], [B^N | L2]) :- %% polynomial(+M:atom) is det
power_p(P1), %
power_p(P2), % Returns true if polynomial, false otherwise.
B^N1 = P1, %
B^N2 = P2, polynomial(M) :-
%% B1 == B2, % Wasn't working before..? term(M).
N is N1 + N2, polynomial(L + R) :-
join_like_terms(L, L2), polynomial(L),
!. term(R).
join_like_terms([N1, N2 | L], [N | L2]) :- %% Tests:
number(N1), %% TODO
number(N2),
N is N1 * N2,
join_like_terms(L, L2),
!.
join_like_terms([X | L], [X | L2]) :-
join_like_terms(L, L2).
join_like_terms([], []).
%% ?- join_like_terms([2, 3, x^1, x^2], T).
%@ T = [6, x^3].
%@ T = [6, x^3].
%% ?- 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].
%% power_to_canon(+T:atom, -T^N:atom) is det
%
% Returns a canon power term.
%
power_to_canon(T^N, T^N) :-
polynomial_variable(T).
power_to_canon(T, T^1) :-
polynomial_variable(T).
%% Tests:
%% ?- power_to_canon(x, X).
%@ X = x^1.
%% ?- power_to_canon(X, X^1).
%@ X = x .
%@ X = x.
%% term_to_list(?T, ?List) is det
%
% Converts a term to a list and vice versa.
% Can verify if term and list are compatible.
%
term_to_list(L * N, [N | TS]) :- term_to_list(L * N, [N | TS]) :-
number(N), number(N),
term_to_list(L, TS). term_to_list(L, TS).
term_to_list(L * P, [P2 | TS]) :- term_to_list(L * P, [P2 | TS]) :-
power_p(P), power(P),
power_to_canon(P, P2), power_to_canon(P, P2),
term_to_list(L, TS). term_to_list(L, TS).
term_to_list(N, [N]) :- term_to_list(N, [N]) :-
number(N). number(N).
term_to_list(P, [P2]) :- term_to_list(P, [P2]) :-
power_p(P), power(P),
power_to_canon(P, P2). power_to_canon(P, P2).
%% Tests:
%% ?- term_to_list(2*y*z*23*x*y*x^3*x, X). %% ?- 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] . %@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2] .
%% ?- term_to_list(X, [y^1, x^1]). %% ?- term_to_list(X, [y^1, x^1]).
@ -122,84 +128,142 @@ term_to_list(P, [P2]) :-
%% ?- 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 .
%% list_to_term([], 1). %% simplify_term(+T:atom, -P) is det
list_to_term([N], N) :- %
number(N), % Simplifies a term.
!. %
list_to_term([P], P2) :- simplify_term(1 * P, P).
power_p(P), simplify_term(0 * _, 0).
power_to_canon(P2, P), simplify_term(T, T2) :-
!. term_to_list(T, L),
list_to_term([N | LS], N * R) :- sort(0, @=<, L, L2),
number(N), join_like_terms(L2, L3),
list_to_term(LS, R), list_to_term(L3, T2). % Responsible for parenthesis
!. %% sort(0, @>=, L3, L4),
list_to_term([P | LS], P2 * R) :- %% term_to_list(T2, L4).
power_p(P), %% Tests:
power_to_canon(P2, P), %% ?- simplify_term(2*y*z*x^3*x, X).
list_to_term(LS, R), %@ X = 2*(x^4*(y*z)).
!. %@ X = z*(y*(x^4*2)).
%% ?- simplify_term(2*y*z*23*x*y*x^3*x, X).
%@ X = 46*(x^2*(x^3*(y^2*z))).
%@ X = z*(y^2*(x^3*(x^2*46))).
%@ X = [2, 23, x^1, x^3, y^1, z^1].
%@ X = [46, x^4, y^1, z^1].
%% ?- list_to_term([x^1], X). %% join_like_terms(+List, -List)
%@ X = x. %
%% ?- list_to_term([x^1, y^2, z^3], X). % Combine powers of the same variable in the given list
%@ X = x*(y^2*z^3). %
%@ X = x*y^2. join_like_terms([P1, P2 | L], [B^N | L2]) :-
%% ?- list_to_term([x^1, y^3, 5], X). power(P1),
%@ X = x*(y^3*5). power(P2),
%@ X = x*(y^3*(5*1)) . B^N1 = P1,
B^N2 = P2,
power_to_canon(T^N, T^N) :- %% B1 == B2, % Wasn't working before..?
polynomial_variable_p(T). N is N1 + N2,
power_to_canon(T, T^1) :- join_like_terms(L, L2).
polynomial_variable_p(T). join_like_terms([N1, N2 | L], [N | L2]) :-
number(N1),
%% ?- power_to_canon(x, X). number(N2),
%@ X = x^1. N is N1 * N2,
%% ?- power_to_canon(X, X^1). join_like_terms(L, L2).
%@ X = x . join_like_terms([X | L], [X | L2]) :-
%@ X = x. join_like_terms(L, L2).
join_like_terms([], []).
%% Tests:
%% ?- join_like_terms([2, 3, x^1, x^2], T).
%@ T = [6, x^3].
%@ T = [6, x^3].
%% ?- 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].
%% simplify_polynomial(+P:atom, -P2:atom) is det
%
% Simplifies a polynomial.
%
simplify_polynomial(M, M2) :- simplify_polynomial(M, M2) :-
term_p(M), simplify_term(M, M2), !. %% Are we dealing with a valid term?
is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
%% If so, simplify it.
simplify_term(M, M2),
!.
simplify_polynomial(P + 0, P) :- simplify_polynomial(P + 0, P) :-
term_p(P), !. %% Ensure valid term
is_term_valid_in_predicate(P, "simplify_polynomial(P + 0, P)"),
!.
simplify_polynomial(0 + P, P) :- simplify_polynomial(0 + P, P) :-
term_p(P), !. %% Ensure valid term
is_term_valid_in_predicate(P, "simplify_polynomial(0 + P, P)"),
!.
simplify_polynomial(P + M, P2 + M2) :- simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2), simplify_term(M, M2). simplify_polynomial(P, P2),
simplify_term(M, M2).
simplify_polynomial(P + M, P2 + M3) :- simplify_polynomial(P + M, P2 + M3) :-
monomial_parts(M, _, XExp), monomial_parts(M, _, XExp),
delete_monomial(P, XExp, M2, P2), !, delete_monomial(P, XExp, M2, P2),
!,
add_monomial(M, M2, M3). add_monomial(M, M2, M3).
simplify_polynomial(P + M, P2 + M2) :- simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2), simplify_term(M, M2). simplify_polynomial(P, P2),
simplify_term(M, M2).
%% Tests:
%% TODO
%% ?- simplify_polynomial(1*x+(-1)*x, P). %% simplify_polynomial_list(+L1,-L3) is det
%
% Simplifies a list of polynomials
%
simplify_polynomial_list([L1], L3) :-
simplify_polynomial(L1, L2),
L3 = [L2].
simplify_polynomial_list([L1|L2],L3) :-
simplify_polynomial(L1, P1),
simplify_polynomial_list(L2, P2),
L3 = [P1|P2],
% There is nothing further to compute at this point
!.
%% monomial_parts(X, Y, Z)
%
% TODO Maybe remove
% Separate monomial into it's parts. Given K*X^N, gives K and N
%
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(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(M2),
monomial_parts(M, _, X), !. term(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(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),
@ -217,15 +281,14 @@ p_aux_add_monomial(K, XExp, K * XExp).
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? .