Added polynomial_to_list, list_to_polynomial and scale_polynomial
This commit is contained in:
parent
1c6d63ed1b
commit
7d78a123ef
82
polimani.pl
82
polimani.pl
@ -1,3 +1,4 @@
|
|||||||
|
%% vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
||||||
%% -*- mode: prolog-*-
|
%% -*- 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
|
||||||
@ -19,7 +20,7 @@ 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]).
|
%% ?- term_to_list(X, [x^4]).
|
||||||
%@ X = x^4 .
|
%@ X = x^4 .
|
||||||
@ -61,15 +62,15 @@ term(L * R) :-
|
|||||||
% predicate the problem ocurred.
|
% predicate the problem ocurred.
|
||||||
%
|
%
|
||||||
is_term_valid_in_predicate(T, F) :-
|
is_term_valid_in_predicate(T, F) :-
|
||||||
(
|
(
|
||||||
term(T)
|
term(T)
|
||||||
;
|
;
|
||||||
write("Invalid term in "),
|
write("Invalid term in "),
|
||||||
write(F),
|
write(F),
|
||||||
write(": "),
|
write(": "),
|
||||||
write(T),
|
write(T),
|
||||||
fail
|
fail
|
||||||
).
|
).
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- is_term_valid_in_predicate().
|
%% ?- is_term_valid_in_predicate().
|
||||||
|
|
||||||
@ -139,8 +140,8 @@ simplify_term(T, T2) :-
|
|||||||
sort(0, @=<, L, L2),
|
sort(0, @=<, L, L2),
|
||||||
join_like_terms(L2, L3),
|
join_like_terms(L2, L3),
|
||||||
list_to_term(L3, T2). % Responsible for parenthesis
|
list_to_term(L3, T2). % Responsible for parenthesis
|
||||||
%% sort(0, @>=, L3, L4),
|
%% sort(0, @>=, L3, L4),
|
||||||
%% term_to_list(T2, L4).
|
%% term_to_list(T2, L4).
|
||||||
%% 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)).
|
||||||
@ -182,20 +183,24 @@ join_like_terms([], []).
|
|||||||
%% simplify_polynomial(+P:atom, -P2:atom) is det
|
%% simplify_polynomial(+P:atom, -P2:atom) is det
|
||||||
%
|
%
|
||||||
% Simplifies a polynomial.
|
% Simplifies a polynomial.
|
||||||
|
% TODO: not everything is a +, there are -
|
||||||
%
|
%
|
||||||
simplify_polynomial(M, M2) :-
|
simplify_polynomial(M, M2) :-
|
||||||
%% Are we dealing with a valid term?
|
%% Are we dealing with a valid term?
|
||||||
is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
|
%is_term_valid_in_predicate(M, "simplify_polynomial(M, M2)"),
|
||||||
|
term(M),
|
||||||
%% If so, simplify it.
|
%% If so, simplify it.
|
||||||
simplify_term(M, M2),
|
simplify_term(M, M2),
|
||||||
!.
|
!.
|
||||||
simplify_polynomial(P + 0, P) :-
|
simplify_polynomial(P + 0, P) :-
|
||||||
%% Ensure valid term
|
%% Ensure valid term
|
||||||
is_term_valid_in_predicate(P, "simplify_polynomial(P + 0, P)"),
|
%is_term_valid_in_predicate(P, "simplify_polynomial(P + 0, P)"),
|
||||||
|
term(P),
|
||||||
!.
|
!.
|
||||||
simplify_polynomial(0 + P, P) :-
|
simplify_polynomial(0 + P, P) :-
|
||||||
%% Ensure valid term
|
%% Ensure valid term
|
||||||
is_term_valid_in_predicate(P, "simplify_polynomial(0 + P, P)"),
|
%is_term_valid_in_predicate(P, "simplify_polynomial(0 + P, P)"),
|
||||||
|
term(P),
|
||||||
!.
|
!.
|
||||||
simplify_polynomial(P + M, P2 + M2) :-
|
simplify_polynomial(P + M, P2 + M2) :-
|
||||||
simplify_polynomial(P, P2),
|
simplify_polynomial(P, P2),
|
||||||
@ -215,7 +220,6 @@ simplify_polynomial(P + M, P2 + M2) :-
|
|||||||
%
|
%
|
||||||
% Simplifies a list of polynomials
|
% Simplifies a list of polynomials
|
||||||
%
|
%
|
||||||
|
|
||||||
simplify_polynomial_list([L1], L3) :-
|
simplify_polynomial_list([L1], L3) :-
|
||||||
simplify_polynomial(L1, L2),
|
simplify_polynomial(L1, L2),
|
||||||
L3 = [L2].
|
L3 = [L2].
|
||||||
@ -226,6 +230,64 @@ simplify_polynomial_list([L1|L2],L3) :-
|
|||||||
% There is nothing further to compute at this point
|
% There is nothing further to compute at this point
|
||||||
!.
|
!.
|
||||||
|
|
||||||
|
%% polynomial_to_list(+P:polynomial, -L:List)
|
||||||
|
%
|
||||||
|
% Converts a polynomial in a list.
|
||||||
|
% TODO: not everything is a +, there are -
|
||||||
|
%
|
||||||
|
polynomial_to_list(T1 + T2, L) :-
|
||||||
|
polynomial_to_list(T1, L1),
|
||||||
|
L = [T2|L1],
|
||||||
|
% The others computations are semantically meaningless
|
||||||
|
!.
|
||||||
|
polynomial_to_list(P, L) :-
|
||||||
|
L = [P].
|
||||||
|
%% Tests:
|
||||||
|
%%?- polynomial_to_list(2*x^2+5+y*2, S).
|
||||||
|
%@S = [y*2, 5, 2*x^2].
|
||||||
|
|
||||||
|
%% list_to_polynomial(+P:polynomial, -L:List)
|
||||||
|
%
|
||||||
|
% Converts a list in a polynomial.
|
||||||
|
% TODO: not everything is a +, there are -
|
||||||
|
%
|
||||||
|
list_to_polynomial([T1|T2], P) :-
|
||||||
|
list_to_polynomial(T2, L1),
|
||||||
|
(
|
||||||
|
not(L1 = []),
|
||||||
|
P = L1+T1
|
||||||
|
;
|
||||||
|
P = T1
|
||||||
|
),
|
||||||
|
% The others computations are semantically meaningless
|
||||||
|
!.
|
||||||
|
list_to_polynomial(T, P) :-
|
||||||
|
P = T.
|
||||||
|
%% Tests:
|
||||||
|
%% TODO
|
||||||
|
|
||||||
|
%% append_two_atoms_with_star(+V1, +V2, -R) is det
|
||||||
|
%
|
||||||
|
% Returns R = V1 * V2
|
||||||
|
%
|
||||||
|
append_two_atoms_with_star(V1, V2, R) :-
|
||||||
|
term_string(V2, V3),
|
||||||
|
atom_concat(V1, *, V4),
|
||||||
|
atom_concat(V4, V3, S),
|
||||||
|
term_string(R, S).
|
||||||
|
%% Tests:
|
||||||
|
% TODO
|
||||||
|
|
||||||
|
%% scale_polynomial(+P:polynomial,+C:constant,-S:polynomial) is det
|
||||||
|
%
|
||||||
|
% Scales a polynomial with a constant
|
||||||
|
%
|
||||||
|
scale_polynomial(P, C, S) :-
|
||||||
|
polynomial_to_list(P, L),
|
||||||
|
maplist(append_two_atoms_with_star(C), L, L2),
|
||||||
|
list_to_polynomial(L2, S).
|
||||||
|
%simplify_polynomial(S1, S).
|
||||||
|
|
||||||
%% monomial_parts(X, Y, Z)
|
%% monomial_parts(X, Y, Z)
|
||||||
%
|
%
|
||||||
% TODO Maybe remove
|
% TODO Maybe remove
|
||||||
|
Reference in New Issue
Block a user