Progess...
This commit is contained in:
parent
43340345d6
commit
93a43cde52
95
polimani.pl
95
polimani.pl
@ -1,3 +1,4 @@
|
|||||||
|
%% -*- mode: prolog-*-
|
||||||
%% 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
|
||||||
|
|
||||||
@ -10,12 +11,6 @@ power_p(X) :-
|
|||||||
power_p(X^N) :-
|
power_p(X^N) :-
|
||||||
polynomial_variable_p(X), integer(N), N > 1, !.
|
polynomial_variable_p(X), integer(N), N > 1, !.
|
||||||
|
|
||||||
%% coefficient_p(K) :-
|
|
||||||
%% number(K).
|
|
||||||
|
|
||||||
%% term_p(X) :-
|
|
||||||
%% polynomial_variable_p(X), !.
|
|
||||||
|
|
||||||
term_p(N) :-
|
term_p(N) :-
|
||||||
number(N).
|
number(N).
|
||||||
term_p(X) :-
|
term_p(X) :-
|
||||||
@ -27,7 +22,7 @@ term_p(L * R) :-
|
|||||||
polynomial_p(M) :-
|
polynomial_p(M) :-
|
||||||
term_p(M).
|
term_p(M).
|
||||||
polynomial_p(L + R) :- % Left greedy
|
polynomial_p(L + R) :- % Left greedy
|
||||||
polynomial_p(L), !, % Why?
|
polynomial_p(L), % Why?
|
||||||
term_p(R), !.
|
term_p(R), !.
|
||||||
|
|
||||||
%% ?- polynomial_p(3*x^2+y*z).
|
%% ?- polynomial_p(3*x^2+y*z).
|
||||||
@ -40,60 +35,88 @@ polynomial_p(L + R) :- % Left greedy
|
|||||||
%@ false.
|
%@ false.
|
||||||
%% ?- polynomial_p(3*x^2+y*z+x^100*y*z).
|
%% ?- polynomial_p(3*x^2+y*z+x^100*y*z).
|
||||||
%@ true.
|
%@ true.
|
||||||
%@ true .
|
%@ true.
|
||||||
%@ false.
|
%% @ false. WIP
|
||||||
%@ false.
|
|
||||||
%@ false.
|
|
||||||
%@ false.
|
|
||||||
|
|
||||||
simplify_term(1 * P, P) :-
|
simplify_term(1 * P, P) :-
|
||||||
term_p(P), !.
|
term_p(P), !.
|
||||||
simplify_term(0 * _, 0) :-
|
simplify_term(0 * _, 0) :-
|
||||||
!.
|
!.
|
||||||
simplify_term(T, S) :-
|
simplify_term(T, T2) :-
|
||||||
term_to_list(T, L),
|
term_to_list(T, L),
|
||||||
sort(L, L2),
|
sort(0, @=<, L, L2),
|
||||||
join_like_terms(L2, S).
|
join_like_terms(L2, L3),
|
||||||
|
sort(0, @>=, L3, L4),
|
||||||
|
term_to_list(T2, L4).
|
||||||
|
|
||||||
%% ?- simplify_term(2*y*z*x^3*x, X).
|
%% ?- 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].
|
%@ 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]) :-
|
join_like_terms([T1, T2 | L], [B1^N | L2]) :-
|
||||||
P^N1 is T,
|
not(number(T1)),
|
||||||
P^N2 is T2,
|
not(number(T2)),
|
||||||
|
B1^N1 = T1,
|
||||||
|
B2^N2 = T2,
|
||||||
|
B1 == B2,
|
||||||
|
join_like_terms(L, L2),
|
||||||
N is N1 + N2,
|
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,
|
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).
|
%% ?- join_like_terms([2, 3, x^1, x^2], T).
|
||||||
%@ ERROR: Arguments are not sufficiently instantiated
|
%@ T = [6, x^3].
|
||||||
%@ ERROR: In:
|
%@ T = [6, x^3].
|
||||||
%@ ERROR: [9] _3138^_3140 is _3134
|
%% ?- join_like_terms([2, 3, x^1, x^2, y^1, y^6], T).
|
||||||
%@ ERROR: [8] join_like_terms([2,3|...],[_3188^_3190,...|...]) at /tmp/ediprologODLYxF:62
|
%@ T = [6, x^3, y^7].
|
||||||
%@ ERROR: [7] <user>
|
%@ T = [6, x^3, y].
|
||||||
%@ 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].
|
|
||||||
|
|
||||||
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 * T, [T2 | TS]) :-
|
term_to_list(L * T, [T2 | TS]) :-
|
||||||
power_p(T),
|
power_p(T),
|
||||||
power_to_canon(T, T2),
|
power_to_canon(T, T2),
|
||||||
term_to_list(L, TS), !.
|
term_to_list(L, TS),
|
||||||
|
!.
|
||||||
term_to_list(T, [T]).
|
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) :-
|
power_to_canon(T, T^1) :-
|
||||||
polynomial_variable_p(T).
|
polynomial_variable_p(T),
|
||||||
|
!.
|
||||||
power_to_canon(T^N, T^N) :-
|
power_to_canon(T^N, T^N) :-
|
||||||
polynomial_variable_p(T).
|
polynomial_variable_p(T),
|
||||||
|
!.
|
||||||
|
|
||||||
simplify_polynomial(M, M2) :-
|
simplify_polynomial(M, M2) :-
|
||||||
term_p(M), simplify_term(M, M2), !.
|
term_p(M), simplify_term(M, M2), !.
|
||||||
|
Reference in New Issue
Block a user