From 3089daf6456a810e2b39251b8019b08eb330c185 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 17:49:55 +0000 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] 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/9] 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). - - From 8c722af8e9ad512a4dbf29b36309dbb4f713faaf Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 20:00:44 +0000 Subject: [PATCH 6/9] Re-add list_to_polynomial TODO: implement - --- polimani.pl | 20 ++++++++++++++++++++ teste.pl | 0 2 files changed, 20 insertions(+) create mode 100644 teste.pl diff --git a/polimani.pl b/polimani.pl index 24b7506..06e7e9f 100644 --- a/polimani.pl +++ b/polimani.pl @@ -432,6 +432,26 @@ 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. +% 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 + %% negate_term(T, T2) is det % % Negate the coeficient of a term and return the negated term diff --git a/teste.pl b/teste.pl new file mode 100644 index 0000000..e69de29 From a4c231f1eb6a7c53ef503b82d0de642d671eeca0 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 21:10:20 +0000 Subject: [PATCH 7/9] discreetly fixes the code --- polimani.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polimani.pl b/polimani.pl index 06e7e9f..cdd7054 100644 --- a/polimani.pl +++ b/polimani.pl @@ -288,7 +288,7 @@ simplify_polynomial(P, P2) :- maplist(term_to_list, L9, L8), delete(L9, 0, L10), sort(0, @=<, L10, L11), - polynomial_to_list(P2, L11), + list_to_polynomial(L11, P2), !. %% Tests: %% ?- simplify_polynomial(1, X). From ad892c971150d8b9a7fbe24d41f4f0b9138583c7 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 21:11:10 +0000 Subject: [PATCH 8/9] discreetly fixes the code --- teste.pl | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 teste.pl diff --git a/teste.pl b/teste.pl deleted file mode 100644 index e69de29..0000000 From b00566901740c297d9d6088caa18ce89b258fb7b Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 22 Nov 2018 23:13:32 +0000 Subject: [PATCH 9/9] Add support for - in list_to_polynomial --- polimani.pl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/polimani.pl b/polimani.pl index cdd7054..f63c06e 100644 --- a/polimani.pl +++ b/polimani.pl @@ -272,7 +272,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) :- !. @@ -399,7 +398,6 @@ simplify_polynomial_list(L, L2) :- %% 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), @@ -435,13 +433,21 @@ polynomial_to_list(T, [T]) :- %% 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 + ( + 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 ),