Merged upstream
This commit is contained in:
commit
bcc918a560
188
polimani.pl
188
polimani.pl
@ -1,16 +1,82 @@
|
||||
%% -*- mode: prolog-*-
|
||||
%% vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
||||
%% Follows 'Coding guidelines for Prolog' - Theory and Practice of Logic Programming
|
||||
%% https://doi.org/10.1017/S1471068411000391
|
||||
|
||||
%% Import the Constraint Logic Programming over Finite Domains lybrary
|
||||
%% Essentially, this library improves the way Prolog deals with integers,
|
||||
%% allowing more predicates to be reversible.
|
||||
%% For instance, integer(N) is always false, which prevents the
|
||||
%% reversing of a predicate.
|
||||
/**
|
||||
*
|
||||
* polimani.pl
|
||||
*
|
||||
* Assignment 1 - Polynomial Manipulator
|
||||
* Programming in Logic - DCC-FCUP
|
||||
*
|
||||
* Diogo Peralta Cordeiro
|
||||
* up201705417@fc.up.pt
|
||||
*
|
||||
* Hugo David Cordeiro Sales
|
||||
* up201704178@fc.up.pt
|
||||
*
|
||||
|
||||
*********************************************
|
||||
* Follows 'Coding guidelines for Prolog' *
|
||||
* https://doi.org/10.1017/S1471068411000391 *
|
||||
*********************************************
|
||||
*/
|
||||
|
||||
/* Import the Constraint Logic Programming over Finite Domains lybrary
|
||||
* Essentially, this library improves the way Prolog deals with integers,
|
||||
* allowing more predicates to be reversible.
|
||||
* For instance, number(N) is always false, which prevents the
|
||||
* reversing of a predicate.
|
||||
*/
|
||||
:- use_module(library(clpfd)).
|
||||
%% :- use_module(library(clpr)).
|
||||
|
||||
|
||||
/*******************************
|
||||
* USER INTERFACE *
|
||||
*******************************/
|
||||
/*
|
||||
poly2list/2 transforms a list representing a polynomial (second
|
||||
argument) into a polynomial represented as an expression (first argu-
|
||||
ment) and vice-versa.
|
||||
*/
|
||||
poly2list(P, L) :-
|
||||
polynomial_to_list(P, L).
|
||||
|
||||
/*
|
||||
simpolylist/2 simplifies a polynomial represented as a list into
|
||||
another polynomial as a list.
|
||||
*/
|
||||
simpoly_list(L, S) :-
|
||||
simplify_polynomial_list(L, S).
|
||||
|
||||
/*
|
||||
simpoly/2 simplifies a polynomial represented as an expression
|
||||
as another polynomial as an expression.
|
||||
*/
|
||||
simpoly(P, S) :-
|
||||
simplify_polynomial(P, S).
|
||||
|
||||
/*
|
||||
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
|
||||
resulting in a second polynomial. The two first arguments are assumed to
|
||||
be ground. The polynomial resulting from the sum is in simplified form.
|
||||
*/
|
||||
scalepoly(P1, P2, S) :-
|
||||
scale_polynomial(P1, P2, S).
|
||||
|
||||
/*
|
||||
addpoly/3 adds two polynomials as expressions resulting in a
|
||||
third one. The two first arguments are assumed to be ground.
|
||||
The polynomial resulting from the sum is in simplified form.
|
||||
*/
|
||||
addpoly(P1, P2, S) :-
|
||||
add_polynomial(P1, P2, S).
|
||||
|
||||
|
||||
/*******************************
|
||||
* BACKEND *
|
||||
*******************************/
|
||||
|
||||
%% polynomial_variable_list(-List) is det
|
||||
%
|
||||
% List of possible polynomial variables
|
||||
@ -302,7 +368,6 @@ join_similar_parts_of_term([], []).
|
||||
%% simplify_polynomial(+P:atom, -P2:atom) is det
|
||||
%
|
||||
% Simplifies a polynomial.
|
||||
% TODO: not everything is a +, there are -
|
||||
%
|
||||
simplify_polynomial(0, 0) :-
|
||||
!.
|
||||
@ -410,9 +475,9 @@ add_terms([NL | TL], [NR | TR], [N2 | TL2]) :-
|
||||
%% ?- add_terms([2, x^3], [3, x^3], R).
|
||||
%@ R = [5, x^3].
|
||||
|
||||
%% simplify_polynomial_as_list(+L1,-L3) is det
|
||||
%% simplify_polynomial_as_list(+L1:List,-L3:List) is det
|
||||
%
|
||||
% Simplifies a list of polynomials
|
||||
% Simplifies a polynomial represented as a list
|
||||
%
|
||||
simplify_polynomial_as_list(L, L11) :-
|
||||
maplist(term_to_list, L, L2),
|
||||
@ -429,7 +494,6 @@ simplify_polynomial_as_list(L, L11) :-
|
||||
%% polynomial_to_list(+P:polynomial, -L:List)
|
||||
%
|
||||
% Converts a polynomial in a list.
|
||||
% TODO: not everything is a +, there are -
|
||||
%
|
||||
polynomial_to_list(L - T, [T2 | LS]) :-
|
||||
term(T),
|
||||
@ -478,6 +542,34 @@ polynomial_to_list(T, [T]) :-
|
||||
%@ % Execution Aborted
|
||||
%@ P = -2.3+x+x^2 .
|
||||
|
||||
%% list_to_polynomial(+P:polynomial, -L:List)
|
||||
%
|
||||
% Converts a list in a polynomial.
|
||||
%
|
||||
list_to_polynomial([T1|T2], P) :-
|
||||
list_to_polynomial(T2, L1),
|
||||
(
|
||||
not(L1 = []),
|
||||
(
|
||||
term_string(T1, S1),
|
||||
string_chars(S1, [First|_]),
|
||||
First = -,
|
||||
term_string(L1, S2),
|
||||
string_concat(S2,S1,S3),
|
||||
term_string(P, S3)
|
||||
;
|
||||
P = L1+T1
|
||||
)
|
||||
;
|
||||
P = T1
|
||||
),
|
||||
% The others computations are semantically meaningless
|
||||
!.
|
||||
list_to_polynomial(T, P) :-
|
||||
P = T.
|
||||
%% Tests:
|
||||
%% TODO
|
||||
|
||||
%% negate_term(T, T2) is det
|
||||
%
|
||||
% Negate the coeficient of a term and return the negated term
|
||||
@ -531,9 +623,9 @@ scale_polynomial(P, C, S) :-
|
||||
maplist(term_to_list, L, L2),
|
||||
maplist(cons(C), L2, L3),
|
||||
maplist(term_to_list, L4, L3),
|
||||
%% maplist(append_two_atoms_with_star(C), L, L2),
|
||||
simplify_polynomial_as_list(L4, L5),
|
||||
list_to_polynomial(L5, S).
|
||||
list_to_polynomial(L5, S),
|
||||
!.
|
||||
%simplify_polynomial(S1, S).
|
||||
%% Tests:
|
||||
%% ?- scale_polynomial(3*x^2, 2, S).
|
||||
@ -550,73 +642,3 @@ scale_polynomial(P, C, S) :-
|
||||
%@ S = [[_21808, x^2, 3]] ;
|
||||
%@ false.
|
||||
%@ S = 2*3*x^2.
|
||||
%@ S = 2*(3*x^2).
|
||||
|
||||
cons(C, L, [C | L]).
|
||||
|
||||
%% monomial_parts(X, Y, Z)
|
||||
%
|
||||
% TODO Maybe remove
|
||||
% Separate monomial into it's parts. Given K*X^N, gives K and N
|
||||
%
|
||||
monomial_parts(X, 1, X) :-
|
||||
power(X),
|
||||
!.
|
||||
monomial_parts(X^N, 1, X^N) :-
|
||||
power(X^N),
|
||||
!.
|
||||
monomial_parts(K * M, K, M) :-
|
||||
number(K),
|
||||
!.
|
||||
monomial_parts(K, K, indep) :-
|
||||
number(K),
|
||||
!.
|
||||
|
||||
|
||||
delete_monomial(M, X, M, 0) :-
|
||||
term(M),
|
||||
monomial_parts(M, _, X),
|
||||
!.
|
||||
delete_monomial(M + M2, X, M, M2) :-
|
||||
term(M2),
|
||||
term(M),
|
||||
monomial_parts(M, _, X),
|
||||
!.
|
||||
delete_monomial(P + M, X, M, P) :-
|
||||
term(M),
|
||||
monomial_parts(M, _, X),
|
||||
!.
|
||||
delete_monomial(P + M2, X, M, P2 + M2) :-
|
||||
delete_monomial(P, X, M, P2).
|
||||
|
||||
add_monomial(K1, K2, K3) :-
|
||||
number(K1),
|
||||
number(K2), !,
|
||||
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),
|
||||
P==P2,
|
||||
!.
|
||||
closure_simplify_polynomial(P, P3) :-
|
||||
simplify_polynomial(P, P2),
|
||||
closure_simplify_polynomial(P2, P3),
|
||||
!.
|
||||
|
||||
list_to_term([N | NS], N * L) :-
|
||||
number(N),
|
||||
term_to_list(L, NS).
|
||||
|
||||
|
Reference in New Issue
Block a user