WIP
This commit is contained in:
parent
93a43cde52
commit
2c98f9471f
137
polimani.pl
137
polimani.pl
@ -4,12 +4,21 @@
|
||||
|
||||
polynomial_variables([x, y, z]).
|
||||
polynomial_variable_p(X) :-
|
||||
polynomial_variables(V), member(X, V).
|
||||
polynomial_variables(V),
|
||||
member(X, V).
|
||||
polynomial_variable_p(P) :-
|
||||
polynomial_variables(V),
|
||||
member(X, V),
|
||||
%% number(N),
|
||||
P is X^N.
|
||||
|
||||
power_p(X) :-
|
||||
polynomial_variable_p(X), !.
|
||||
polynomial_variable_p(X).
|
||||
power_p(X^N) :-
|
||||
polynomial_variable_p(X), integer(N), N > 1, !.
|
||||
polynomial_variable_p(X), integer(N), N >= 1.
|
||||
|
||||
%% ?- power_p(x^1).
|
||||
%@ true..
|
||||
|
||||
term_p(N) :-
|
||||
number(N).
|
||||
@ -46,26 +55,27 @@ 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).
|
||||
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).
|
||||
%@ 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^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].
|
||||
|
||||
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),
|
||||
join_like_terms([P1, P2 | L], [B^N | L2]) :-
|
||||
power_p(P1),
|
||||
power_p(P2),
|
||||
B^N1 = P1,
|
||||
B^N2 = P2,
|
||||
%% B1 == B2, % Wasn't working before..?
|
||||
N is N1 + N2,
|
||||
join_like_terms(L, L2),
|
||||
!.
|
||||
join_like_terms([N1, N2 | L], [N | L2]) :-
|
||||
number(N1),
|
||||
@ -76,47 +86,92 @@ join_like_terms([N1, N2 | L], [N | 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].
|
||||
%@ T = [6, x^3, y^7].
|
||||
|
||||
|
||||
term_to_list(L * N, [N | TS]) :-
|
||||
number(N),
|
||||
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(T, [T]).
|
||||
|
||||
list_to_term([N | NS], N * L) :-
|
||||
number(N),
|
||||
term_to_list(L, NS).
|
||||
term_to_list(L, TS).
|
||||
term_to_list(L * P, [P2 | TS]) :-
|
||||
power_p(P),
|
||||
power_to_canon(P, P2),
|
||||
term_to_list(L, TS).
|
||||
term_to_list(N, [N]) :-
|
||||
number(N).
|
||||
term_to_list(P, [P2]) :-
|
||||
power_p(P),
|
||||
power_to_canon(P, P2).
|
||||
|
||||
%% ?- 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]
|
||||
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2] .
|
||||
%@ X = [x, x^3, y, x, 23, z, y, 2] .
|
||||
%@ X = [x, x^3, y, x, 23, z, y, 2] .
|
||||
%@ 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, [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.
|
||||
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2].
|
||||
%% ?- term_to_list(X, [y^1, x^1]).
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ X = x*y .
|
||||
%@ false.
|
||||
%% ?- term_to_list(X, [x^4]).
|
||||
%@ false.
|
||||
%@ false.
|
||||
%@ false.
|
||||
%@ false.
|
||||
%@ false.
|
||||
%@ X = x*2.
|
||||
|
||||
power_to_canon(T, T^1) :-
|
||||
polynomial_variable_p(T),
|
||||
%@ false.
|
||||
|
||||
%% list_to_term([], 1).
|
||||
list_to_term([N], N) :-
|
||||
number(N),
|
||||
!.
|
||||
list_to_term([P], P2) :-
|
||||
power_p(P),
|
||||
power_to_canon(P2, P),
|
||||
!.
|
||||
list_to_term([N | LS], N * R) :-
|
||||
number(N),
|
||||
list_to_term(LS, R),
|
||||
!.
|
||||
list_to_term([P | LS], P2 * R) :-
|
||||
power_p(P),
|
||||
power_to_canon(P2, P),
|
||||
list_to_term(LS, R),
|
||||
!.
|
||||
|
||||
%% ?- list_to_term([x^1], X).
|
||||
%@ X = x.
|
||||
%% ?- list_to_term([x^1, y^2, z^3], X).
|
||||
%@ X = x*(y^2*z^3).
|
||||
%@ X = x*y^2.
|
||||
%% ?- list_to_term([x^1, y^3, 5], X).
|
||||
%@ X = x*(y^3*5).
|
||||
%@ X = x*(y^3*(5*1)) .
|
||||
|
||||
power_to_canon(T^N, T^N) :-
|
||||
polynomial_variable_p(T),
|
||||
!.
|
||||
polynomial_variable_p(T).
|
||||
power_to_canon(T, T^1) :-
|
||||
polynomial_variable_p(T).
|
||||
|
||||
%% ?- power_to_canon(x, X).
|
||||
%@ X = x^1.
|
||||
%% ?- power_to_canon(X, X^1).
|
||||
%@ X = x .
|
||||
%@ X = x.
|
||||
|
||||
simplify_polynomial(M, M2) :-
|
||||
term_p(M), simplify_term(M, M2), !.
|
||||
|
Reference in New Issue
Block a user