Some improvements
This commit is contained in:
parent
1c6d63ed1b
commit
f6d562a34e
81
polimani.pl
81
polimani.pl
@ -19,25 +19,34 @@ polynomial_variable(X) :-
|
|||||||
polynomial_variable(P) :-
|
polynomial_variable(P) :-
|
||||||
polynomial_variable_list(V),
|
polynomial_variable_list(V),
|
||||||
member(X, V),
|
member(X, V),
|
||||||
P = X^N.
|
P = X^_.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- term_to_list(X, [x^4]).
|
%% ?- polynomial_variable(x).
|
||||||
%@ X = x^4 .
|
%@ true .
|
||||||
|
|
||||||
%% power(+X:atom) is det
|
%% power(+X:atom) is det
|
||||||
%
|
%
|
||||||
% Returns true if X is a power term, false otherwise.
|
% Returns true if X is a power term, false otherwise.
|
||||||
%
|
%
|
||||||
power(X) :-
|
|
||||||
polynomial_variable(X).
|
|
||||||
power(X ^ N) :-
|
power(X ^ N) :-
|
||||||
polynomial_variable(X),
|
%% X ^ N = P,
|
||||||
integer(N),
|
integer(N),
|
||||||
N >= 1.
|
!,
|
||||||
|
%% write(N),
|
||||||
|
N >= 1,
|
||||||
|
polynomial_variable(X),
|
||||||
|
!.
|
||||||
|
power(X) :-
|
||||||
|
polynomial_variable(X),
|
||||||
|
!.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- power(x^1).
|
%% ?- power(x^1).
|
||||||
%@ true .
|
%@ true .
|
||||||
|
%% ?- power(x^(-3)).
|
||||||
|
%@ false.
|
||||||
|
%@ true.
|
||||||
|
%@ -3
|
||||||
|
%@ true .
|
||||||
|
|
||||||
%% term(+N:atom) is det
|
%% term(+N:atom) is det
|
||||||
%
|
%
|
||||||
@ -49,10 +58,12 @@ term(X) :-
|
|||||||
power(X).
|
power(X).
|
||||||
term(L * R) :-
|
term(L * R) :-
|
||||||
term(L),
|
term(L),
|
||||||
term(R),
|
term(R).
|
||||||
!.
|
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% TODO
|
%% ?- term(2*x^3).
|
||||||
|
%@ true .
|
||||||
|
%% ?- term(x^(-3)).
|
||||||
|
%@ true .
|
||||||
|
|
||||||
%% is_term_valid_in_predicate(+T, +F) is det
|
%% is_term_valid_in_predicate(+T, +F) is det
|
||||||
%
|
%
|
||||||
@ -71,7 +82,8 @@ is_term_valid_in_predicate(T, F) :-
|
|||||||
fail
|
fail
|
||||||
).
|
).
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- is_term_valid_in_predicate().
|
%% ?- is_term_valid_in_predicate(1, "Foo").
|
||||||
|
%@ true .
|
||||||
|
|
||||||
%% polynomial(+M:atom) is det
|
%% polynomial(+M:atom) is det
|
||||||
%
|
%
|
||||||
@ -118,7 +130,8 @@ term_to_list(P, [P2]) :-
|
|||||||
power(P),
|
power(P),
|
||||||
power_to_canon(P, P2).
|
power_to_canon(P, P2).
|
||||||
%% Tests:
|
%% 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] .
|
%@ 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]).
|
||||||
%@ X = x*y .
|
%@ X = x*y .
|
||||||
@ -128,28 +141,33 @@ 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 .
|
||||||
|
|
||||||
%% simplify_term(+T:atom, -P) is det
|
%% simplify_term(+Term_In:term, ?Term_Out:term) is det
|
||||||
%
|
%
|
||||||
% Simplifies a term.
|
% Simplifies a term.
|
||||||
%
|
%
|
||||||
simplify_term(1 * P, P).
|
simplify_term(Term_In, Term_Out) :-
|
||||||
simplify_term(0 * _, 0).
|
term_to_list(Term_In, L),
|
||||||
simplify_term(T, T2) :-
|
|
||||||
term_to_list(T, L),
|
|
||||||
sort(0, @=<, L, L2),
|
sort(0, @=<, L, L2),
|
||||||
join_like_terms(L2, L3),
|
(
|
||||||
list_to_term(L3, T2). % Responsible for parenthesis
|
member(0, L2),
|
||||||
%% sort(0, @>=, L3, L4),
|
Term_Out = 0
|
||||||
%% term_to_list(T2, L4).
|
;
|
||||||
|
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:
|
%% Tests:
|
||||||
%% ?- simplify_term(2*y*z*x^3*x, X).
|
%% ?- simplify_term(2*y*z*x^3*x, X).
|
||||||
%@ X = 2*(x^4*(y*z)).
|
%@ X = 2*x^4*y*z.
|
||||||
%@ X = z*(y*(x^4*2)).
|
%% ?- simplify_term(1*y*z*x^3*x, X).
|
||||||
%% ?- simplify_term(2*y*z*23*x*y*x^3*x, X).
|
%@ X = x^4*y*z.
|
||||||
%@ X = 46*(x^2*(x^3*(y^2*z))).
|
%% ?- simplify_term(0*y*z*x^3*x, X).
|
||||||
%@ X = z*(y^2*(x^3*(x^2*46))).
|
%@ X = 0.
|
||||||
%@ X = [2, 23, x^1, x^3, y^1, z^1].
|
%% ?- simplify_term(6*y*z*7*x*y*x^3*x, X).
|
||||||
%@ X = [46, x^4, y^1, z^1].
|
%@ X = 42*x^2*x^3*y^2*z.
|
||||||
|
|
||||||
%% join_like_terms(+List, -List)
|
%% join_like_terms(+List, -List)
|
||||||
%
|
%
|
||||||
@ -209,7 +227,10 @@ simplify_polynomial(P + M, P2 + M2) :-
|
|||||||
simplify_polynomial(P, P2),
|
simplify_polynomial(P, P2),
|
||||||
simplify_term(M, M2).
|
simplify_term(M, M2).
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% TODO
|
%% ?- simplify_polynomial(1, 1).
|
||||||
|
%@ Invalid term in simplify_polynomial(M, M2): 1
|
||||||
|
%@ false.
|
||||||
|
|
||||||
|
|
||||||
%% simplify_polynomial_list(+L1,-L3) is det
|
%% simplify_polynomial_list(+L1,-L3) is det
|
||||||
%
|
%
|
||||||
|
Reference in New Issue
Block a user