Cleaned up some documentation and tests

This commit is contained in:
Hugo Sales 2018-11-25 19:28:46 +00:00
parent e75e67559e
commit d2a59155f3

View File

@ -80,6 +80,7 @@ scalepoly(P1, C, S) :-
%% ?- scalepoly(3*x*z+2*z, 4, S). %% ?- scalepoly(3*x*z+2*z, 4, S).
%@ S = 12*x*z+8*z. %@ S = 12*x*z+8*z.
%% ?- scalepoly(3*x*z+2*z, 2, S). %% ?- scalepoly(3*x*z+2*z, 2, S).
%@ S = 6*x*z+4*z.
/* /*
addpoly/3 adds two polynomials as expressions resulting in a addpoly/3 adds two polynomials as expressions resulting in a
@ -113,7 +114,7 @@ is_polynomial_valid_in_predicate(P, F) :-
write(P), write(P),
fail. fail.
%% Tests: %% Tests:
%% ?- is_polynomial_valid_in_predicate(1, "Test"). %% ?- is_polynomial_valid_in_predicate(1-x, "Test").
%@ true. %@ true.
%% ?- is_polynomial_valid_in_predicate(a*4-0*x, "Test"). %% ?- is_polynomial_valid_in_predicate(a*4-0*x, "Test").
%@ Invalid polynomial in Test: a*4-0*x %@ Invalid polynomial in Test: a*4-0*x
@ -180,6 +181,7 @@ polynomial_variable(X) :-
% Returns true if X is a power term, false otherwise. % Returns true if X is a power term, false otherwise.
% %
power(P^N) :- power(P^N) :-
%% CLPFD comparassion. Reversible
N #>= 1, N #>= 1,
polynomial_variable(P). polynomial_variable(P).
power(X) :- power(X) :-
@ -213,22 +215,19 @@ power(X) :-
% Returns true if N is a term, false otherwise. % Returns true if N is a term, false otherwise.
% %
term(N) :- term(N) :-
( % If N is not a free variable
% If N is not a free variable nonvar(N),
nonvar(N), % Assert it as a number
% Assert it as a number number(N).
number(N) term(N) :-
; % If N is a free variable and not compound
% If N is a free variable not(compound(N)),
%% not(compound(N)), var(N),
var(N), % Assert it must be between negative and positive infinity
% Assert it must be between negative and positive infinity % This uses the CLPR library, which makes this reversible,
% This uses the CLP(FD) library, which makes this reversible, % whereas `number(N)` is always false, since it only succeeds
% whereas `number(N)` is always false, since it only succeeds % if the argument is bound (to a integer or float)
% if the argument is bound to a intger or float {N >= 0; N < 0}.
%% N in inf..sup
{N >= 0; N < 0}
).
term(X) :- term(X) :-
power(X). power(X).
term(-X) :- term(-X) :-
@ -321,8 +320,6 @@ power_to_canon(T^N, T^N) :-
N #\= 1. N #\= 1.
power_to_canon(T, T^1) :- power_to_canon(T, T^1) :-
polynomial_variable(T). polynomial_variable(T).
%% power_to_canon(-P, -P2) :-
%% power_to_canon(P, P2).
%% Tests: %% Tests:
%% ?- power_to_canon(x, X). %% ?- power_to_canon(x, X).
%@ X = x^1 . %@ X = x^1 .
@ -397,6 +394,8 @@ term_to_list(-P, [-P2]) :-
%@ X = -2*x^4*z^2*y^6 . %@ X = -2*x^4*z^2*y^6 .
%% ?- term_to_list(X, [x^1, 0]). %% ?- term_to_list(X, [x^1, 0]).
%@ X = 0*x . %@ X = 0*x .
%% ?- term_to_list(X, [y^1, -2]).
%@ X = -2*y .
%% simplify_term(+Term_In:term, ?Term_Out:term) is det %% simplify_term(+Term_In:term, ?Term_Out:term) is det
% %
@ -457,6 +456,7 @@ simplify_term(Term_In, Term_Out) :-
%% join_similar_parts_of_term(+List, -List) is det %% 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.
% Requires that the list be sorted
% %
join_similar_parts_of_term([P1, P2 | L], L2) :- join_similar_parts_of_term([P1, P2 | L], L2) :-
%% If both symbols are powers %% If both symbols are powers
@ -480,6 +480,7 @@ join_similar_parts_of_term([N1, N2 | L], L2) :-
% First result is always the most simplified form. % First result is always the most simplified form.
!. !.
join_similar_parts_of_term([X | L], [X | L2]) :- join_similar_parts_of_term([X | L], [X | L2]) :-
%% Otherwise consume one element and recurse
join_similar_parts_of_term(L, L2), join_similar_parts_of_term(L, L2),
% First result is always the most simplified form. % First result is always the most simplified form.
!. !.
@ -503,7 +504,7 @@ join_similar_parts_of_term([], []).
% Simplifies a polynomial. % Simplifies a polynomial.
% %
simplify_polynomial(0, 0) :- simplify_polynomial(0, 0) :-
% 0 is already fully simplified and is an % 0 is already fully simplified. This is an
% exception to the following algorithm % exception to the following algorithm
!. !.
simplify_polynomial(P, P2) :- simplify_polynomial(P, P2) :-
@ -616,6 +617,7 @@ join_similar_terms([], []).
% %
% Adds the coefficient of the term as the first element of the list % Adds the coefficient of the term as the first element of the list
% %
%% Special cases to make this predicate reversible
term_to_canon([1], [1]) :- term_to_canon([1], [1]) :-
!. !.
term_to_canon(L2, [1 | L]) :- term_to_canon(L2, [1 | L]) :-
@ -632,9 +634,8 @@ term_to_canon([N2 | L], [N | L]) :-
number(N), number(N),
N2 = N, N2 = N,
!. !.
%% Normal case
term_to_canon(L, [N | L2]) :- term_to_canon(L, [N | L2]) :-
%% N == 1 -> L = L2
%% ;
term_to_canon_with_coefficient(N, L, L2), term_to_canon_with_coefficient(N, L, L2),
!. !.
%% Tests: %% Tests:
@ -742,6 +743,7 @@ sign_of_power(-P, -1*P).
% %
% Adds two terms represented as list by adding % Adds two terms represented as list by adding
% the coeficients if the power is the same. % the coeficients if the power is the same.
% Returns false if they can't be added
% Requires the list of terms to be simplified. % Requires the list of terms to be simplified.
% %
add_terms([NL | TL], [NR | TR], [N2 | TL2]) :- add_terms([NL | TL], [NR | TR], [N2 | TL2]) :-
@ -762,6 +764,8 @@ add_terms([NL | TL], [NR | TR], [N2 | TL2]) :-
%@ R = [3, x^3]. %@ R = [3, x^3].
%% ?- 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].
%% ?- add_terms([2, x^3], [3, x^2], R).
%@ false.
%% polynomial_to_list(+P:polynomial, -L:List) is det %% polynomial_to_list(+P:polynomial, -L:List) is det
% %
@ -792,8 +796,6 @@ polynomial_to_list(T, [T]) :-
%@ S = [-2*y, 5, 2*x^2]. %@ S = [-2*y, 5, 2*x^2].
%% ?- polynomial_to_list(2*x^2-5-y*2, S). %% ?- polynomial_to_list(2*x^2-5-y*2, S).
%@ S = [-2*y, -5, 2*x^2]. %@ S = [-2*y, -5, 2*x^2].
%% ?- polynomial_to_list(P, [-2*y, -5, 2*x^2]).
%@ ERROR: Unhandled exception: type_error(_527068=_527074*_527076,2,'a real number',_527074*_527076)
%% list_to_polynomial(+L:List, -P:Polynomial) is det %% list_to_polynomial(+L:List, -P:Polynomial) is det
% %
@ -849,9 +851,10 @@ negate_term(T, T2) :-
%% (-)/1 is an operator, needs to be evaluated, otherwise %% (-)/1 is an operator, needs to be evaluated, otherwise
%% it gives a symbolic result, which messes with further processing %% it gives a symbolic result, which messes with further processing
N2 is -N, N2 is -N,
%% Convert the term back from canonic form
term_to_canon(L3, [N2 | R]),
%% Reverse the order of the list, because converting %% Reverse the order of the list, because converting
%% implicitly reverses it %% implicitly reverses it
term_to_canon(L3, [N2 | R]),
reverse(L3, L4), reverse(L3, L4),
term_to_list(T2, L4), term_to_list(T2, L4),
!. !.
@ -896,7 +899,8 @@ scale_polynomial(P, C, S) :-
% %
cons(C, L, [C | L]). cons(C, L, [C | L]).
%% Tests: %% Tests:
%% No need for tests as it is trivially correct. %% ?- cons(C, L, L2).
%@ L2 = [C|L].
%% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det %% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det
% %