From adf2dc7f322b0be2ef16ba13f411dd1cac34515e Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Sat, 24 Nov 2018 16:10:44 +0000 Subject: [PATCH] Improved list_to_polynomial and finished documentation. --- polymani.pl | 59 ++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/polymani.pl b/polymani.pl index ab93cf6..858bb24 100644 --- a/polymani.pl +++ b/polymani.pl @@ -289,7 +289,7 @@ power_to_canon(T, T^1) :- % % Converts a term to a list and vice versa. % A term is multiplication of a number or a power -% and another term +% and another term. % Can verify if term and list are compatible. % term_to_list(L * N, [N | TS]) :- @@ -330,7 +330,9 @@ term_to_list(P, [P2]) :- %% simplify_term(+Term_In:term, ?Term_Out:term) is det % -% Simplifies a term. +% Simplifies a given term. +% This function can also be be used to verify if +% a term is simplified. % simplify_term(Term_In, Term_Out) :- term_to_list(Term_In, L), @@ -380,7 +382,7 @@ simplify_term(Term_In, Term_Out) :- %% join_similar_parts_of_term(+List, -List) is det % -% Combine powers of the same variable in the given list +% Combine powers of the same variable in the given list. % join_similar_parts_of_term([P1, P2 | L], L2) :- %% If both symbols are powers @@ -464,7 +466,7 @@ simplify_polynomial(P, P2) :- %% simplify_polynomial_as_list(+L1:List,-L3:List) is det % -% Simplifies a polynomial represented as a list +% Simplifies a polynomial represented as a list. % simplify_polynomial_as_list(L, L11) :- %% Convert each term to a list @@ -502,7 +504,7 @@ simplify_polynomial_as_list(L, L11) :- % `maplist(sort(0, @>=), L, L2), % sort(0, @>=, L2, L3)` % and that the sublists to be sorted with -% `sort(0, @=<)` since that is inherited from `add_terms` +% `sort(0, @=<)` since that is inherited from `add_terms`. % join_similar_terms([TL, TR | L], L2) :- %% Check if terms can be added and add them @@ -524,10 +526,10 @@ join_similar_terms([], []). %% term_to_canon(+T:List, -T2:List) is det % -% Adds a 1 if there's no number in the list +% Adds a 1 if there's no number in the list. % Requires the list to be sorted such that the -% numbers come first. For instance with -% `sort(0, @=<)` +% numbers come first. +% For instance with `sort(0, @=<)`. % term_to_canon([T | TS], [1, T | TS]) :- %% Since the list is sorted, if the first element @@ -610,38 +612,39 @@ polynomial_to_list(T, [T]) :- %% ?- polynomial_to_list(2*x^2+3*x+5*x^17-7*x^21+3*x^3-23*x^4+25*x^5-4.3, S). %@ S = [-4.3, 25*x^5, -23*x^4, 3*x^3, -7*x^21, 5*x^17, 3*x, 2* ... ^ ...]. -%% list_to_polynomial(+P:polynomial, -L:List) is det +%% list_to_polynomial(+L:List, -P:Polynomial) is det % % Converts a list in a polynomial. +% An empty list will return false. % list_to_polynomial([T1|T2], P) :- + % Start recursive calls until we are in the + % end of the list. We know that the `-` will + % always be at the left of a term. 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 - ) + % If this is a negative term + term_string(T1, S1), + string_chars(S1, [First|_]), + First = -, + % Concat them + term_string(L1, S2), + string_concat(S2,S1,S3), + term_string(P, S3) ; - P = T1 + % Otherwise sum them + P = L1+T1 ), % The others computations are semantically meaningless !. -list_to_polynomial(T, P) :- - P = T. +list_to_polynomial([T], T). %% Tests: %% ?- list_to_polynomial([1, x, x^2], P). %@ P = x^2+x+1. %% negate_term(T, T2) is det % -% Negate the coeficient of a term and return the negated term +% Negate the coeficient of a term and return the negated term. % negate_term(T, T2) :- term_to_list(T, L), @@ -668,9 +671,9 @@ negate_term(T, T2) :- %% ?- negate_term(3*x*y^2, R). %@ R = -3*x*y^2. -%% scale_polynomial(+P:polynomial,+C:constant,-S:polynomial) is det +%% scale_polynomial(+P:Polynomial,+C:Constant,-S:Polynomial) is det % -% Multiplies a polynomial by a scalar +% Multiplies a polynomial by a scalar. % scale_polynomial(P, C, S) :- polynomial_to_list(P, L), @@ -690,13 +693,13 @@ scale_polynomial(P, C, S) :- %% cons(+C:atom, +L:List, -L2:List) is det % -% Add an atom C to the head of a list L +% Add an atom C to the head of a list L. % cons(C, L, [C | L]). %% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det % -% S = P1 + P2 +% S = P1 + P2. % add_polynomial(P1, P2, S) :- %% Convert both polynomials to lists