Progess...

This commit is contained in:
Hugo Sales 2018-11-17 23:53:49 +00:00
parent 43340345d6
commit 93a43cde52

View File

@ -1,3 +1,4 @@
%% -*- mode: prolog-*-
%% Follows 'Coding guidelines for Prolog' - Theory and Practice of Logic Programming
%% https://doi.org/10.1017/S1471068411000391
@ -10,12 +11,6 @@ power_p(X) :-
power_p(X^N) :-
polynomial_variable_p(X), integer(N), N > 1, !.
%% coefficient_p(K) :-
%% number(K).
%% term_p(X) :-
%% polynomial_variable_p(X), !.
term_p(N) :-
number(N).
term_p(X) :-
@ -27,7 +22,7 @@ term_p(L * R) :-
polynomial_p(M) :-
term_p(M).
polynomial_p(L + R) :- % Left greedy
polynomial_p(L), !, % Why?
polynomial_p(L), % Why?
term_p(R), !.
%% ?- polynomial_p(3*x^2+y*z).
@ -40,60 +35,88 @@ polynomial_p(L + R) :- % Left greedy
%@ false.
%% ?- polynomial_p(3*x^2+y*z+x^100*y*z).
%@ true.
%@ true .
%@ false.
%@ false.
%@ false.
%@ false.
%@ true.
%% @ false. WIP
simplify_term(1 * P, P) :-
term_p(P), !.
simplify_term(0 * _, 0) :-
!.
simplify_term(T, S) :-
simplify_term(T, T2) :-
term_to_list(T, L),
sort(L, L2),
join_like_terms(L2, S).
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).
%@ X = [z^1, y^1, x^4, 2].
%@ false.
%@ X = [2, x^4, y^1, z^1].
%@ 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].
join_like_terms([T1, T2 | L], [P^N | L]) :-
P^N1 is T,
P^N2 is T2,
join_like_terms([T1, T2 | L], [B1^N | L2]) :-
not(number(T1)),
not(number(T2)),
B1^N1 = T1,
B2^N2 = T2,
B1 == B2,
join_like_terms(L, L2),
N is N1 + N2,
!.
join_like_terms([N1, N2 | L], [N | L]) :-
join_like_terms([N1, N2 | L], [N | L2]) :-
number(N1),
number(N2),
N is N1 * N2,
join_like_terms(L, L2),
!.
join_like_terms(T, T).
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).
%@ ERROR: Arguments are not sufficiently instantiated
%@ ERROR: In:
%@ ERROR: [9] _3138^_3140 is _3134
%@ ERROR: [8] join_like_terms([2,3|...],[_3188^_3190,...|...]) at /tmp/ediprologODLYxF:62
%@ ERROR: [7] <user>
%@ Exception: (8) join_like_terms([2, 3, x^1, x^2], _2428) ? abort
%@ % Execution Aborted
%@ T = [6, x^1, x^2].
%@ T = [6, x^1].
%@ T = [6].
%@ T = [2].
%@ 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].
term_to_list(L * N, [N | TS]) :-
number(N),
term_to_list(L, TS), !.
term_to_list(L, TS),
!.
term_to_list(L * T, [T2 | TS]) :-
power_p(T),
power_to_canon(T, T2),
term_to_list(L, TS), !.
term_to_list(L, TS),
!.
term_to_list(T, [T]).
list_to_term([N | NS], N * L) :-
number(N),
term_to_list(L, NS).
%% ?- 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 = x*2.
power_to_canon(T, T^1) :-
polynomial_variable_p(T).
polynomial_variable_p(T),
!.
power_to_canon(T^N, T^N) :-
polynomial_variable_p(T).
polynomial_variable_p(T),
!.
simplify_polynomial(M, M2) :-
term_p(M), simplify_term(M, M2), !.