Small bug fixes and testing

This commit is contained in:
Hugo Sales 2018-11-23 15:32:15 +00:00
parent bcc918a560
commit d5fdb9e044

View File

@ -40,21 +40,24 @@
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
another polynomial as a list.
*/
simpoly_list(L, S) :-
simplify_polynomial_list(L, S).
simplify_polynomial_as_list(L, S),
!.
/*
simpoly/2 simplifies a polynomial represented as an expression
as another polynomial as an expression.
*/
simpoly(P, S) :-
simplify_polynomial(P, S).
simplify_polynomial(P, S),
!.
/*
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
@ -62,7 +65,8 @@ simpoly(P, S) :-
be ground. The polynomial resulting from the sum is in simplified form.
*/
scalepoly(P1, P2, S) :-
scale_polynomial(P1, P2, S).
scale_polynomial(P1, P2, S),
!.
/*
addpoly/3 adds two polynomials as expressions resulting in a
@ -70,7 +74,8 @@ scalepoly(P1, P2, S) :-
The polynomial resulting from the sum is in simplified form.
*/
addpoly(P1, P2, S) :-
add_polynomial(P1, P2, S).
add_polynomial(P1, P2, S),
!.
/*******************************
@ -102,7 +107,8 @@ polynomial_variable(X) :-
%
power(P^N) :-
(
zcompare((<), 0, N),
N #>= 1,
%% zcompare((<), -1, N),
polynomial_variable(P)
;
fail
@ -121,12 +127,12 @@ power(X) :-
%% ?- power(x^(-3)).
%@ false.
%% ?- power(X).
%@ X = x^_7334,
%@ _7334 in 1..sup ;
%@ X = y^_7334,
%@ _7334 in 1..sup ;
%@ X = z^_7334,
%@ _7334 in 1..sup ;
%@ X = x^_2420,
%@ _2420 in 0..sup ;
%@ X = y^_2420,
%@ _2420 in 0..sup ;
%@ X = z^_2420,
%@ _2420 in 0..sup ;
%@ X = x ;
%@ X = y ;
%@ X = z.
@ -138,7 +144,8 @@ power(X) :-
term(N) :-
(nonvar(N),
number(N));
(not(compound(N)), var(N),
(not(compound(N)),
var(N),
N in inf..sup).
%% {N >= -1000, N =< 1000}.
%% N ::= inf..sup.
@ -148,7 +155,6 @@ term(X) :-
term(L * R) :-
term(L),
term(R).
%% append_two_atoms_with_star(L, R, T).
%% Tests:
%% ?- term(2*x^3).
%@ true .
@ -188,8 +194,6 @@ term(L * R) :-
%@ X = _1188*x,
%@ _1188 in inf..sup .
%% Doesn't give all possible terms, because number(N) is not reversible
%% The ic library seems to be able to help here, but it's not a part of
%% SwiPL by default
%% is_term_valid_in_predicate(+T, +F) is det
%
@ -278,6 +282,8 @@ term_to_list(P, [P2]) :-
%@ X = [1] .
%% ?- term_to_list(-1, X).
%@ X = [-1] .
%% ?- term_to_list(2 * 3, X).
%@ X = [3, 2] .
%% ?- term_to_list(1*2*y*z*23*x*y*x^3*x, X).
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2, 1] .
%% ?- term_to_list(X, [-1]).
@ -491,56 +497,37 @@ simplify_polynomial_as_list(L, L11) :-
delete(L9, 0, L10),
sort(0, @=<, L10, L11).
%% polynomial_to_list(+P:polynomial, -L:List)
%% polynomial_to_list(+P:polynomial, -L:List) is det
%
% Converts a polynomial in a list.
%
polynomial_to_list(L - T, [T2 | LS]) :-
term(T),
negate_term(T, T2),
polynomial_to_list(L, LS).
polynomial_to_list(L, LS),
!.
polynomial_to_list(L + T, [T | LS]) :-
term(T),
polynomial_to_list(L, LS).
polynomial_to_list(L, LS),
!.
polynomial_to_list(T, [T]) :-
term(T).
term(T),
!.
%% Tests:
%% ?- polynomial_to_list(2, S).
%@ S = [2] .
%@ S = [2].
%% ?- polynomial_to_list(x^2, S).
%@ S = [x^2] .
%@ S = [x^2].
%% ?- polynomial_to_list(x^2 + x^2, S).
%@ S = [x^2, x^2] .
%@ S = [x^2, x^2].
%% ?- polynomial_to_list(2*x^2+5+y*2, S).
%@ S = [y*2, 5, 2*x^2] .
%@ S = [y*2, 5, 2*x^2].
%% ?- 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(2*x^2-5-y*2, S).
%@ S = [-2*y, -5, 2*x^2] .
%% ?- polynomial_to_list(P, [2]).
%@ P = 2 .
%% ?- polynomial_to_list(P, [x]).
%@ P = x .
%% ?- polynomial_to_list(P, [x^2, x, 2.3]).
%@ ERROR: Type error: `integer' expected, found `_10776*_10778' (a compound)
%@ ERROR: In:
%@ ERROR: [16] throw(error(type_error(integer,...),_10828))
%@ ERROR: [14] clpfd:attr_unify_hook(clpfd_attr(no,no,no,from_to(inf,sup),fd_props([],[],[])),_10896*_10898) at /usr/lib/swipl-7.6.4/library/clp/clpfd.pl:7127
%@ ERROR: [13] '$attvar':uhook(clpfd,clpfd_attr(no,no,no,from_to(inf,sup),fd_props([],[],[])),_10962*_10964) at /usr/lib/swipl-7.6.4/boot/attvar.pl:86
%@ ERROR: [12] '$attvar':call_all_attr_uhooks(att(clpfd,clpfd_attr(no,no,no,...,...),[]),_11020*_11022) at /usr/lib/swipl-7.6.4/boot/attvar.pl:63
%@ ERROR: [11] '$attvar':'$wakeup'(wakeup(att(clpfd,...,[]),_11072*_11074,[])) at /usr/lib/swipl-7.6.4/boot/attvar.pl:58
%@ ERROR: [10] term_to_list(_11104*_11106,[_11110|_11112]) at /tmp/ediprologEjek6K:198
%@ ERROR: [9] negate_term(_11142*_11144,x^2) at /tmp/ediprologEjek6K:470
%@ ERROR: [8] polynomial_to_list(_11180-_11186*_11188,[x^2,x|...]) at /tmp/ediprologEjek6K:436
%@ ERROR: [7] <user>
%@ ERROR:
%@ ERROR: Note: some frames are missing due to last-call optimization.
%@ ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
%@ Exception: (10) term_to_list(_9692{clpfd = ...}, _11268) ? abort
%@ % Execution Aborted
%@ Action (h for help) ? abort
%@ % Execution Aborted
%@ P = -2.3+x+x^2 .
%@ S = [-2*y, -5, 2*x^2].
%% ?- 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)
%
@ -595,25 +582,6 @@ negate_term(T, T2) :-
%% ?- negate_term(3*x*y^2, R).
%@ R = -3*x*y^2.
%% append_two_atoms_with_star(+V1, +V2, -R) is det
%
% Returns R = V1 * V2
%
append_two_atoms_with_star(V1, V2, R) :-
% Convert term V2 into a string V3
term_string(V2, V3),
% Concat atom V1 with * into a compound V4
atom_concat(V1, *, V4),
% Concat atom V4 with V3 into a compound S
atom_concat(V4, V3, S),
% Convert compound S into a term R
term_string(R, S).
%% Tests:
% ?- append_two_atoms_with_star(2, x^2, R).
%@ R = 2*x^2.
%@ R = 2*x^2.
%@ R = 2*3.
%% scale_polynomial(+P:polynomial,+C:constant,-S:polynomial) is det
%
% Scales a polynomial with a constant
@ -629,16 +597,6 @@ scale_polynomial(P, C, S) :-
%simplify_polynomial(S1, S).
%% Tests:
%% ?- scale_polynomial(3*x^2, 2, S).
%@ ERROR: Undefined procedure: list_to_polynomial/2
%@ ERROR: In:
%@ ERROR: [9] list_to_polynomial([6* ...],_32496)
%@ ERROR: [8] scale_polynomial(3*x^2,2,_32536) at /tmp/ediprologEjek6K:536
%@ ERROR: [7] <user>
%@ Exception: (9) list_to_polynomial([6*x^2], _30888) ? abort
%@ % Execution Aborted
%@ S = [[2, x^2, 3]] .
%@ S = [[3, x^2, 3]] .
%@ false.
%@ S = [[_21808, x^2, 3]] ;
%@ false.
%@ S = 2*3*x^2.
%@ S = 6*x^2.
cons(C, L, [C | L]).