Merge branch 'master' into negatives_in_polynomials

This commit is contained in:
Diogo Cordeiro 2018-11-22 23:25:38 +00:00 committed by GitHub
commit c05ba00425
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,81 @@
%% -*- mode: prolog-*- %% -*- mode: prolog-*-
%% vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: %% 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. * polimani.pl
%% For instance, number(N) is always false, which prevents the *
%% reversing of a predicate. * 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(clpfd)).
/*******************************
* 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 %% polynomial_variable_list(-List) is det
% %
% List of possible polynomial variables % List of possible polynomial variables
@ -388,12 +454,14 @@ add_terms([NL | TL], [NR | TR], [N2 | TL2]) :-
%% ?- add_terms([2, x^3], [3, x^3], R). %% ?- add_terms([2, x^3], [3, x^3], R).
%@ R = [5, x^3]. %@ R = [5, x^3].
%% simplify_polynomial_list(+L1,-L3) is det %% simplify_polynomial_list(+L:list, -S:list) is det
% %
% Simplifies a list of polynomials % Simplifies a polynomial represented as a list
% %
simplify_polynomial_list(L, L2) :- simplify_polynomial_list(L, S) :-
maplist(simplify_polynomial, L, L2). polynomial_to_list(P1, L),
simplify_polynomial(P1, P2),
polynomial_to_list(P2, S).
%% polynomial_to_list(+P:polynomial, -L:List) %% polynomial_to_list(+P:polynomial, -L:List)
% %
@ -509,76 +577,9 @@ append_two_atoms_with_star(V1, V2, R) :-
scale_polynomial(P, C, S) :- scale_polynomial(P, C, S) :-
polynomial_to_list(P, L), polynomial_to_list(P, L),
maplist(append_two_atoms_with_star(C), L, L2), maplist(append_two_atoms_with_star(C), L, L2),
list_to_polynomial(L2, S). polynomial_to_list(S, L2),
%simplify_polynomial(S1, S). simplify_polynomial(S, S1),
!.
%% Tests: %% Tests:
%% ?- scale_polynomial(3*x^2, 2, S). %% ?- scale_polynomial(3*x^2, 2, S).
%@ S = 2*3*x^2. %@ S = 2*3*x^2.
%@ S = 2*(3*x^2).
%% 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).