From 3089daf6456a810e2b39251b8019b08eb330c185 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 17:49:55 +0000 Subject: [PATCH 1/5] Add formal header --- polimani.pl | 66 ++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/polimani.pl b/polimani.pl index 448bfb9..cb4fea0 100644 --- a/polimani.pl +++ b/polimani.pl @@ -1,13 +1,32 @@ %% -*- 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, number(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)). %% polynomial_variable_list(-List) is det @@ -388,26 +407,6 @@ polynomial_to_list(T, [T]) :- %%?- polynomial_to_list(P, [x^2, x, -2.3]). %@ P = -2.3+x+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 @@ -441,6 +440,15 @@ scale_polynomial(P, C, S) :- %@ S = 2*3*x^2. %@ S = 2*(3*x^2). + + + + +/* CENAS DO PROF: */ + + + + %% monomial_parts(X, Y, Z) % % TODO Maybe remove @@ -503,7 +511,3 @@ closure_simplify_polynomial(P, P3) :- closure_simplify_polynomial(P2, P3), !. -list_to_term([N | NS], N * L) :- - number(N), - term_to_list(L, NS). - From d217a20c70f4d67c0f2c8f2f65ddb6460a0a7a56 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 18:04:45 +0000 Subject: [PATCH 2/5] Add user interface --- polimani.pl | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/polimani.pl b/polimani.pl index cb4fea0..2f21b4a 100644 --- a/polimani.pl +++ b/polimani.pl @@ -29,6 +29,52 @@ */ :- 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 that simplifies a polynomial represented as an expression + as another polynomial as an expression. +*/ +simpoly(P, S) :- + simplify_polynomial(P, S). + +/* + scalepoly/3 that multiplies one polynomial as 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 that 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 From 0a65fd0a7f7d8d8f280d3e1444a49a3be0e6828a Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 18:12:03 +0000 Subject: [PATCH 3/5] Implement professor's simpoly_list and remove previous one --- polimani.pl | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/polimani.pl b/polimani.pl index 2f21b4a..f71796f 100644 --- a/polimani.pl +++ b/polimani.pl @@ -411,19 +411,14 @@ transform_list(Pred, [L | LS], [R | RS]) :- %% ?- transform_list(term_to_list, L, [[x^1], [x^1], [2]]). %@ L = [x, x, 2]. -%% 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([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 - !. +simplify_polynomial_list(L, S) :- + polynomial_to_list(P1, L), + simplify_polynomial(P1, P2), + polynomial_to_list(P2, S). %% polynomial_to_list(+P:polynomial, -L:List) % From 3471f6a328b3eb559cddcf76151553608bc7dab1 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 18:34:23 +0000 Subject: [PATCH 4/5] Small typos fixed --- polimani.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/polimani.pl b/polimani.pl index f71796f..8dfaea0 100644 --- a/polimani.pl +++ b/polimani.pl @@ -49,14 +49,14 @@ simpoly_list(L, S) :- simplify_polynomial_list(L, S). /* - simpoly/2 that simplifies a polynomial represented as an expression + simpoly/2 simplifies a polynomial represented as an expression as another polynomial as an expression. */ simpoly(P, S) :- simplify_polynomial(P, S). /* - scalepoly/3 that multiplies one polynomial as expression by a scalar + scalepoly/3 multiplies one polynomial as 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. */ @@ -64,7 +64,7 @@ scalepoly(P1, P2, S) :- scale_polynomial(P1, P2, S). /* - addpoly/3 that adds two polynomials as expressions resulting in a + 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. */ From 202f57ef9d2f754a694535c2acd81f9961a11eee Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 22 Nov 2018 18:55:37 +0000 Subject: [PATCH 5/5] Minor changes to comments and to scale_polynomial --- polimani.pl | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/polimani.pl b/polimani.pl index 8dfaea0..d636d4f 100644 --- a/polimani.pl +++ b/polimani.pl @@ -13,33 +13,33 @@ * * 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. -*/ +/* 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)). + /******************************* * 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). + polynomial_to_list(P, L). /* simpolylist/2 simplifies a polynomial represented as a list into @@ -56,7 +56,7 @@ simpoly(P, S) :- simplify_polynomial(P, S). /* - scalepoly/3 multiplies one polynomial as expression by a scalar + 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. */ @@ -71,6 +71,7 @@ scalepoly(P1, P2, S) :- addpoly(P1, P2, S) :- add_polynomial(P1, P2, S). + /******************************* * BACKEND * *******************************/ @@ -474,14 +475,12 @@ append_two_atoms_with_star(V1, V2, R) :- 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). + polynomial_to_list(S, L2), + simplify_polynomial(S, S1), + !. %% Tests: %% ?- scale_polynomial(3*x^2, 2, S). %@ S = 2*3*x^2. -%@ S = 2*(3*x^2). - -