Some improvements

This commit is contained in:
Hugo Sales 2018-11-20 16:14:53 +00:00
parent 1c6d63ed1b
commit f6d562a34e
1 changed files with 52 additions and 31 deletions

View File

@ -19,25 +19,34 @@ polynomial_variable(X) :-
polynomial_variable(P) :-
polynomial_variable_list(V),
member(X, V),
P = X^N.
P = X^_.
%% Tests:
%% ?- term_to_list(X, [x^4]).
%@ X = x^4 .
%% ?- polynomial_variable(x).
%@ true .
%% power(+X:atom) is det
%
% Returns true if X is a power term, false otherwise.
%
power(X) :-
polynomial_variable(X).
power(X^N) :-
polynomial_variable(X),
power(X ^ N) :-
%% X ^ N = P,
integer(N),
N >= 1.
!,
%% write(N),
N >= 1,
polynomial_variable(X),
!.
power(X) :-
polynomial_variable(X),
!.
%% Tests:
%% ?- power(x^1).
%@ true .
%% ?- power(x^(-3)).
%@ false.
%@ true.
%@ -3
%@ true .
%% term(+N:atom) is det
%
@ -49,10 +58,12 @@ term(X) :-
power(X).
term(L * R) :-
term(L),
term(R),
!.
term(R).
%% Tests:
%% TODO
%% ?- term(2*x^3).
%@ true .
%% ?- term(x^(-3)).
%@ true .
%% is_term_valid_in_predicate(+T, +F) is det
%
@ -71,7 +82,8 @@ is_term_valid_in_predicate(T, F) :-
fail
).
%% Tests:
%% ?- is_term_valid_in_predicate().
%% ?- is_term_valid_in_predicate(1, "Foo").
%@ true .
%% polynomial(+M:atom) is det
%
@ -118,7 +130,8 @@ term_to_list(P, [P2]) :-
power(P),
power_to_canon(P, P2).
%% Tests:
%% ?- term_to_list(2*y*z*23*x*y*x^3*x, X).
%% ?- term_to_list(1*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, 1] .
%@ 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 .
@ -128,28 +141,33 @@ term_to_list(P, [P2]) :-
%% ?- term_to_list(X, [y^6, z^2, x^4]).
%@ X = x^4*z^2*y^6 .
%% simplify_term(+T:atom, -P) is det
%% simplify_term(+Term_In:term, ?Term_Out:term) is det
%
% Simplifies a term.
%
simplify_term(1 * P, P).
simplify_term(0 * _, 0).
simplify_term(T, T2) :-
term_to_list(T, L),
simplify_term(Term_In, Term_Out) :-
term_to_list(Term_In, L),
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).
(
member(0, L2),
Term_Out = 0
;
exclude(==(1), L2, L3),
join_like_terms(L3, L4),
sort(0, @>=, L4, L5),
term_to_list(Term_Out, L5)
),
% First result is always the most simplified form.
!.
%% Tests:
%% ?- simplify_term(2*y*z*x^3*x, X).
%@ 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].
%@ X = 2*x^4*y*z.
%% ?- simplify_term(1*y*z*x^3*x, X).
%@ X = x^4*y*z.
%% ?- simplify_term(0*y*z*x^3*x, X).
%@ X = 0.
%% ?- simplify_term(6*y*z*7*x*y*x^3*x, X).
%@ X = 42*x^2*x^3*y^2*z.
%% join_like_terms(+List, -List)
%
@ -209,7 +227,10 @@ simplify_polynomial(P + M, P2 + M2) :-
simplify_polynomial(P, P2),
simplify_term(M, M2).
%% Tests:
%% TODO
%% ?- simplify_polynomial(1, 1).
%@ Invalid term in simplify_polynomial(M, M2): 1
%@ false.
%% simplify_polynomial_list(+L1,-L3) is det
%