This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
polynomialmani.pl/polimani.pl

316 lines
6.7 KiB
Perl
Raw Normal View History

2018-11-17 23:53:49 +00:00
%% -*- mode: prolog-*-
2018-11-17 16:14:13 +00:00
%% Follows 'Coding guidelines for Prolog' - Theory and Practice of Logic Programming
%% https://doi.org/10.1017/S1471068411000391
2018-11-19 16:59:53 +00:00
%% polynomial_variable_list(-List:atom) is det
2018-11-18 16:33:09 +00:00
%
% List of possible polynomial variables
%
2018-11-19 16:59:53 +00:00
polynomial_variable_list([x, y, z]).
2018-11-17 16:14:13 +00:00
2018-11-18 16:33:09 +00:00
%% polynomial_variable(?X:atom) is det
%
% Returns true if X is a polynomial variable, false otherwise.
%
polynomial_variable(X) :-
2018-11-19 16:59:53 +00:00
polynomial_variable_list(V),
2018-11-19 15:56:48 +00:00
member(X, V).
2018-11-19 16:59:53 +00:00
polynomial_variable(P) :-
polynomial_variable_list(V),
2018-11-19 15:56:48 +00:00
member(X, V),
2018-11-20 16:14:53 +00:00
P = X^_.
2018-11-19 16:59:53 +00:00
%% Tests:
2018-11-20 16:14:53 +00:00
%% ?- polynomial_variable(x).
%@ true .
2018-11-17 16:14:13 +00:00
2018-11-18 16:33:09 +00:00
%% power(+X:atom) is det
%
2018-11-19 16:59:53 +00:00
% Returns true if X is a power term, false otherwise.
2018-11-18 16:33:09 +00:00
%
2018-11-20 16:14:53 +00:00
power(X ^ N) :-
%% X ^ N = P,
integer(N),
!,
%% write(N),
N >= 1,
polynomial_variable(X),
!.
2018-11-18 16:33:09 +00:00
power(X) :-
polynomial_variable(X),
2018-11-20 16:14:53 +00:00
!.
2018-11-19 16:59:53 +00:00
%% Tests:
%% ?- power(x^1).
2018-11-19 16:07:13 +00:00
%@ true .
2018-11-20 16:14:53 +00:00
%% ?- power(x^(-3)).
%@ false.
%@ true.
%@ -3
%@ true .
2018-11-18 16:33:09 +00:00
%% term(+N:atom) is det
%
2018-11-19 16:59:53 +00:00
% Returns true if N is a term, false otherwise.
2018-11-18 16:33:09 +00:00
%
term(N) :-
number(N).
2018-11-18 16:33:09 +00:00
term(X) :-
power(X).
term(L * R) :-
term(L),
2018-11-20 16:14:53 +00:00
term(R).
2018-11-19 16:59:53 +00:00
%% Tests:
2018-11-20 16:14:53 +00:00
%% ?- term(2*x^3).
%@ true .
%% ?- term(x^(-3)).
%@ true .
2018-11-19 16:59:53 +00:00
%% is_term_valid_in_predicate(+T, +F) is det
2018-11-18 16:33:09 +00:00
%
% Returns true if valid Term, fails with UI message otherwise.
% The fail message reports which Term is invalid and in which
2018-11-19 16:59:53 +00:00
% predicate the problem ocurred.
2018-11-18 16:33:09 +00:00
%
2018-11-19 16:59:53 +00:00
is_term_valid_in_predicate(T, F) :-
2018-11-18 16:33:09 +00:00
(
term(T)
;
write("Invalid term in "),
write(F),
write(": "),
write(T),
fail
).
2018-11-19 16:59:53 +00:00
%% Tests:
2018-11-20 16:14:53 +00:00
%% ?- is_term_valid_in_predicate(1, "Foo").
%@ true .
2018-11-18 16:33:09 +00:00
%% polynomial(+M:atom) is det
%
% Returns true if polynomial, false otherwise.
%
polynomial(M) :-
term(M).
polynomial(L + R) :-
polynomial(L),
2018-11-19 16:59:53 +00:00
term(R).
%% Tests:
%% TODO
2018-11-18 16:33:09 +00:00
%% power_to_canon(+T:atom, -T^N:atom) is det
%
% Returns a canon power term.
%
2018-11-19 16:59:53 +00:00
power_to_canon(T^N, T^N) :-
polynomial_variable(T).
power_to_canon(T, T^1) :-
polynomial_variable(T).
%% Tests:
%% ?- power_to_canon(x, X).
%@ X = x^1.
%% ?- power_to_canon(X, X^1).
%@ X = x .
%@ X = x.
%% term_to_list(?T, ?List) is det
2018-11-18 16:33:09 +00:00
%
2018-11-19 16:59:53 +00:00
% Converts a term to a list and vice versa.
% Can verify if term and list are compatible.
2018-11-18 16:33:09 +00:00
%
term_to_list(L * N, [N | TS]) :-
number(N),
2018-11-19 16:59:53 +00:00
term_to_list(L, TS).
term_to_list(L * P, [P2 | TS]) :-
power(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),
power_to_canon(P, P2).
%% Tests:
2018-11-20 16:14:53 +00:00
%% ?- 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] .
2018-11-19 16:59:53 +00:00
%@ 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 .
%% ?- term_to_list(X, [x^4]).
%@ X = x^4 .
%@ false.
2018-11-19 16:59:53 +00:00
%% ?- term_to_list(X, [y^6, z^2, x^4]).
%@ X = x^4*z^2*y^6 .
2018-11-20 16:14:53 +00:00
%% simplify_term(+Term_In:term, ?Term_Out:term) is det
2018-11-18 16:33:09 +00:00
%
2018-11-19 16:59:53 +00:00
% Simplifies a term.
2018-11-18 16:33:09 +00:00
%
2018-11-20 16:14:53 +00:00
simplify_term(Term_In, Term_Out) :-
term_to_list(Term_In, L),
2018-11-17 23:53:49 +00:00
sort(0, @=<, L, L2),
2018-11-20 16:14:53 +00:00
(
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.
!.
2018-11-19 16:59:53 +00:00
%% Tests:
%% ?- simplify_term(2*y*z*x^3*x, X).
2018-11-20 16:14:53 +00:00
%@ 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.
2018-11-17 23:53:49 +00:00
2018-11-19 16:59:53 +00:00
%% join_like_terms(+List, -List)
%
% Combine powers of the same variable in the given list
%
2018-11-19 15:56:48 +00:00
join_like_terms([P1, P2 | L], [B^N | L2]) :-
2018-11-19 16:59:53 +00:00
power(P1),
power(P2),
2018-11-19 15:56:48 +00:00
B^N1 = P1,
B^N2 = P2,
%% B1 == B2, % Wasn't working before..?
N is N1 + N2,
2018-11-19 16:59:53 +00:00
join_like_terms(L, L2).
2018-11-17 23:53:49 +00:00
join_like_terms([N1, N2 | L], [N | L2]) :-
number(N1),
number(N2),
N is N1 * N2,
2018-11-19 16:59:53 +00:00
join_like_terms(L, L2).
2018-11-17 23:53:49 +00:00
join_like_terms([X | L], [X | L2]) :-
join_like_terms(L, L2).
join_like_terms([], []).
2018-11-19 16:59:53 +00:00
%% Tests:
%% ?- join_like_terms([2, 3, x^1, x^2], T).
2018-11-17 23:53:49 +00:00
%@ 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].
2018-11-19 15:56:48 +00:00
%@ T = [6, x^3, y^7].
2018-11-19 16:59:53 +00:00
%% simplify_polynomial(+P:atom, -P2:atom) is det
2018-11-18 16:33:09 +00:00
%
2018-11-19 16:59:53 +00:00
% Simplifies a polynomial.
2018-11-18 16:33:09 +00:00
%
2018-11-17 16:14:13 +00:00
simplify_polynomial(M, M2) :-
2018-11-19 16:59:53 +00:00
%% Are we dealing with a valid term?
is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
%% If so, simplify it.
simplify_term(M, M2),
!.
2018-11-17 16:14:13 +00:00
simplify_polynomial(P + 0, P) :-
2018-11-19 16:59:53 +00:00
%% Ensure valid term
is_term_valid_in_predicate(P, "simplify_polynomial(P + 0, P)"),
!.
2018-11-17 16:14:13 +00:00
simplify_polynomial(0 + P, P) :-
2018-11-19 16:59:53 +00:00
%% Ensure valid term
is_term_valid_in_predicate(P, "simplify_polynomial(0 + P, P)"),
!.
2018-11-17 16:14:13 +00:00
simplify_polynomial(P + M, P2 + M2) :-
2018-11-19 16:59:53 +00:00
simplify_polynomial(P, P2),
simplify_term(M, M2).
2018-11-17 16:14:13 +00:00
simplify_polynomial(P + M, P2 + M3) :-
monomial_parts(M, _, XExp),
2018-11-19 16:59:53 +00:00
delete_monomial(P, XExp, M2, P2),
!,
2018-11-17 16:14:13 +00:00
add_monomial(M, M2, M3).
simplify_polynomial(P + M, P2 + M2) :-
2018-11-19 16:59:53 +00:00
simplify_polynomial(P, P2),
simplify_term(M, M2).
%% Tests:
2018-11-20 16:14:53 +00:00
%% ?- simplify_polynomial(1, 1).
%@ Invalid term in simplify_polynomial(M, M2): 1
%@ false.
2018-11-19 16:59:53 +00:00
%% simplify_polynomial_list(+L1,-L3) is det
%
% Simplifies a list of polynomials
%
2018-11-17 16:14:13 +00:00
2018-11-19 16:59:53 +00:00
simplify_polynomial_list([L1], L3) :-
simplify_polynomial(L1, L2),
L3 = [L2].
simplify_polynomial_list([L1|L2],L3) :-
simplify_polynomial(L1, P1),
simplify_polynomial_list(L2, P2),
L3 = [P1|P2],
% There is nothing further to compute at this point
!.
2018-11-17 16:14:13 +00:00
2018-11-18 16:33:09 +00:00
%% monomial_parts(X, Y, Z)
%
2018-11-19 16:59:53 +00:00
% TODO Maybe remove
% Separate monomial into it's parts. Given K*X^N, gives K and N
2018-11-18 16:33:09 +00:00
%
2018-11-17 16:14:13 +00:00
monomial_parts(X, 1, X) :-
2018-11-18 16:33:09 +00:00
power(X),
!.
2018-11-17 16:14:13 +00:00
monomial_parts(X^N, 1, X^N) :-
2018-11-18 16:33:09 +00:00
power(X^N),
!.
2018-11-17 16:14:13 +00:00
monomial_parts(K * M, K, M) :-
2018-11-18 16:33:09 +00:00
number(K),
!.
2018-11-17 16:14:13 +00:00
monomial_parts(K, K, indep) :-
2018-11-18 16:33:09 +00:00
number(K),
!.
2018-11-17 16:14:13 +00:00
delete_monomial(M, X, M, 0) :-
2018-11-18 23:18:28 +00:00
term(M),
2018-11-18 16:33:09 +00:00
monomial_parts(M, _, X),
!.
2018-11-17 16:14:13 +00:00
delete_monomial(M + M2, X, M, M2) :-
2018-11-18 23:18:28 +00:00
term(M2),
term(M),
2018-11-18 16:33:09 +00:00
monomial_parts(M, _, X),
!.
2018-11-17 16:14:13 +00:00
delete_monomial(P + M, X, M, P) :-
2018-11-18 23:18:28 +00:00
term(M),
2018-11-18 16:33:09 +00:00
monomial_parts(M, _, X),
!.
2018-11-17 16:14:13 +00:00
delete_monomial(P + M2, X, M, P2 + M2) :-
delete_monomial(P, X, M, P2).
add_monomial(K1, K2, K3) :-
2018-11-18 16:33:09 +00:00
number(K1),
number(K2), !,
2018-11-17 16:14:13 +00:00
K3 is K1 + K2.
add_monomial(M1, M2, M3) :-
monomial_parts(M1, K1, XExp),
monomial_parts(M2, K2, XExp),
K3 is K1 + K2,
p_aux_add_monomial(K3, XExp, M3).
p_aux_add_monomial(K, indep, K) :-
!.
p_aux_add_monomial(0, _, 0) :-
!.
p_aux_add_monomial(1, XExp, XExp) :-
!.
p_aux_add_monomial(K, XExp, K * XExp).
closure_simplify_polynomial(P, P) :-
simplify_polynomial(P, P2),
2018-11-18 16:33:09 +00:00
P==P2,
!.
2018-11-17 16:14:13 +00:00
closure_simplify_polynomial(P, P3) :-
simplify_polynomial(P, P2),
2018-11-18 16:33:09 +00:00
closure_simplify_polynomial(P2, P3),
!.
list_to_term([N | NS], N * L) :-
number(N),
term_to_list(L, NS).