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
1 changed files with 30 additions and 26 deletions

View File

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