Merge branch 'negatives_in_polynomials'
This commit is contained in:
commit
899313cf19
152
polimani.pl
152
polimani.pl
@ -26,6 +26,7 @@
|
|||||||
* reversing of a predicate.
|
* reversing of a predicate.
|
||||||
*/
|
*/
|
||||||
:- use_module(library(clpfd)).
|
:- use_module(library(clpfd)).
|
||||||
|
%% :- use_module(library(clpr)).
|
||||||
|
|
||||||
|
|
||||||
/*******************************
|
/*******************************
|
||||||
@ -38,21 +39,24 @@
|
|||||||
argument) and vice-versa.
|
argument) and vice-versa.
|
||||||
*/
|
*/
|
||||||
poly2list(P, L) :-
|
poly2list(P, L) :-
|
||||||
polynomial_to_list(P, L).
|
polynomial_to_list(P, L),
|
||||||
|
!.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
simpolylist/2 simplifies a polynomial represented as a list into
|
simpolylist/2 simplifies a polynomial represented as a list into
|
||||||
another polynomial as a list.
|
another polynomial as a list.
|
||||||
*/
|
*/
|
||||||
simpoly_list(L, S) :-
|
simpoly_list(L, S) :-
|
||||||
simplify_polynomial_list(L, S).
|
simplify_polynomial_as_list(L, S),
|
||||||
|
!.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
simpoly/2 simplifies a polynomial represented as an expression
|
simpoly/2 simplifies a polynomial represented as an expression
|
||||||
as another polynomial as an expression.
|
as another polynomial as an expression.
|
||||||
*/
|
*/
|
||||||
simpoly(P, S) :-
|
simpoly(P, S) :-
|
||||||
simplify_polynomial(P, S).
|
simplify_polynomial(P, S),
|
||||||
|
!.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
|
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
|
||||||
@ -60,7 +64,8 @@ simpoly(P, S) :-
|
|||||||
be ground. The polynomial resulting from the sum is in simplified form.
|
be ground. The polynomial resulting from the sum is in simplified form.
|
||||||
*/
|
*/
|
||||||
scalepoly(P1, P2, S) :-
|
scalepoly(P1, P2, S) :-
|
||||||
scale_polynomial(P1, P2, S).
|
scale_polynomial(P1, P2, S),
|
||||||
|
!.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
addpoly/3 adds two polynomials as expressions resulting in a
|
addpoly/3 adds two polynomials as expressions resulting in a
|
||||||
@ -68,7 +73,8 @@ scalepoly(P1, P2, S) :-
|
|||||||
The polynomial resulting from the sum is in simplified form.
|
The polynomial resulting from the sum is in simplified form.
|
||||||
*/
|
*/
|
||||||
addpoly(P1, P2, S) :-
|
addpoly(P1, P2, S) :-
|
||||||
add_polynomial(P1, P2, S).
|
add_polynomial(P1, P2, S),
|
||||||
|
!.
|
||||||
|
|
||||||
|
|
||||||
/*******************************
|
/*******************************
|
||||||
@ -100,7 +106,8 @@ polynomial_variable(X) :-
|
|||||||
%
|
%
|
||||||
power(P^N) :-
|
power(P^N) :-
|
||||||
(
|
(
|
||||||
zcompare((<), 0, N),
|
N #>= 1,
|
||||||
|
%% zcompare((<), -1, N),
|
||||||
polynomial_variable(P)
|
polynomial_variable(P)
|
||||||
;
|
;
|
||||||
fail
|
fail
|
||||||
@ -119,12 +126,12 @@ power(X) :-
|
|||||||
%% ?- power(x^(-3)).
|
%% ?- power(x^(-3)).
|
||||||
%@ false.
|
%@ false.
|
||||||
%% ?- power(X).
|
%% ?- power(X).
|
||||||
%@ X = x^_7334,
|
%@ X = x^_2420,
|
||||||
%@ _7334 in 1..sup ;
|
%@ _2420 in 0..sup ;
|
||||||
%@ X = y^_7334,
|
%@ X = y^_2420,
|
||||||
%@ _7334 in 1..sup ;
|
%@ _2420 in 0..sup ;
|
||||||
%@ X = z^_7334,
|
%@ X = z^_2420,
|
||||||
%@ _7334 in 1..sup ;
|
%@ _2420 in 0..sup ;
|
||||||
%@ X = x ;
|
%@ X = x ;
|
||||||
%@ X = y ;
|
%@ X = y ;
|
||||||
%@ X = z.
|
%@ X = z.
|
||||||
@ -134,14 +141,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) :-
|
||||||
number(N).
|
(nonvar(N),
|
||||||
%% N in inf..sup.
|
number(N));
|
||||||
|
(not(compound(N)),
|
||||||
|
var(N),
|
||||||
|
N in inf..sup).
|
||||||
|
%% {N >= -1000, N =< 1000}.
|
||||||
|
%% N ::= inf..sup.
|
||||||
|
%% (var(N), N in inf..sup).
|
||||||
term(X) :-
|
term(X) :-
|
||||||
power(X).
|
power(X).
|
||||||
term(L * R) :-
|
term(L * R) :-
|
||||||
term(L),
|
term(L),
|
||||||
term(R).
|
term(R).
|
||||||
%% append_two_atoms_with_star(L, R, T).
|
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- term(2*x^3).
|
%% ?- term(2*x^3).
|
||||||
%@ true .
|
%@ true .
|
||||||
@ -156,9 +168,31 @@ term(L * R) :-
|
|||||||
%% ?- term(3.2*x).
|
%% ?- term(3.2*x).
|
||||||
%@ true .
|
%@ true .
|
||||||
%% ?- term(X).
|
%% ?- term(X).
|
||||||
|
%@ X in inf..sup ;
|
||||||
|
%@ X = x^_1242,
|
||||||
|
%@ _1242 in 1..sup ;
|
||||||
|
%@ X = y^_1242,
|
||||||
|
%@ _1242 in 1..sup ;
|
||||||
|
%@ X = z^_1242,
|
||||||
|
%@ _1242 in 1..sup ;
|
||||||
|
%@ X = x ;
|
||||||
|
%@ X = y ;
|
||||||
|
%@ X = z ;
|
||||||
|
%@ X = _1330*_1332,
|
||||||
|
%@ _1330 in inf..sup,
|
||||||
|
%@ _1332 in inf..sup ;
|
||||||
|
%@ X = _1406*x^_1414,
|
||||||
|
%@ _1406 in inf..sup,
|
||||||
|
%@ _1414 in 1..sup ;
|
||||||
|
%@ X = _1406*y^_1414,
|
||||||
|
%@ _1406 in inf..sup,
|
||||||
|
%@ _1414 in 1..sup ;
|
||||||
|
%@ X = _1406*z^_1414,
|
||||||
|
%@ _1406 in inf..sup,
|
||||||
|
%@ _1414 in 1..sup ;
|
||||||
|
%@ X = _1188*x,
|
||||||
|
%@ _1188 in inf..sup .
|
||||||
%% Doesn't give all possible terms, because number(N) is not reversible
|
%% 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
|
%% is_term_valid_in_predicate(+T, +F) is det
|
||||||
%
|
%
|
||||||
@ -247,6 +281,8 @@ term_to_list(P, [P2]) :-
|
|||||||
%@ X = [1] .
|
%@ X = [1] .
|
||||||
%% ?- term_to_list(-1, X).
|
%% ?- term_to_list(-1, X).
|
||||||
%@ X = [-1] .
|
%@ 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).
|
%% ?- 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] .
|
%@ X = [x^1, x^3, y^1, x^1, 23, z^1, y^1, 2, 1] .
|
||||||
%% ?- term_to_list(X, [-1]).
|
%% ?- term_to_list(X, [-1]).
|
||||||
@ -343,17 +379,8 @@ simplify_polynomial(0, 0) :-
|
|||||||
!.
|
!.
|
||||||
simplify_polynomial(P, P2) :-
|
simplify_polynomial(P, P2) :-
|
||||||
polynomial_to_list(P, L),
|
polynomial_to_list(P, L),
|
||||||
maplist(term_to_list, L, L2),
|
simplify_polynomial_as_list(L, L2),
|
||||||
maplist(sort(0, @>=), L2, L3),
|
polynomial_to_list(P2, L2),
|
||||||
sort(0, @>=, L3, L4),
|
|
||||||
maplist(join_similar_parts_of_term, L4, L5),
|
|
||||||
maplist(sort(0, @=<), L5, L6),
|
|
||||||
join_similar_terms(L6, L7),
|
|
||||||
maplist(reverse, L7, L8),
|
|
||||||
maplist(term_to_list, L9, L8),
|
|
||||||
delete(L9, 0, L10),
|
|
||||||
sort(0, @=<, L10, L11),
|
|
||||||
list_to_polynomial(L11, P2),
|
|
||||||
!.
|
!.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- simplify_polynomial(1, X).
|
%% ?- simplify_polynomial(1, X).
|
||||||
@ -454,28 +481,38 @@ add_terms([NL | TL], [NR | TR], [N2 | TL2]) :-
|
|||||||
%% ?- 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].
|
||||||
|
|
||||||
%% simplify_polynomial_list(+L:list, -S:list) is det
|
%% 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_list(L, S) :-
|
simplify_polynomial_as_list(L, L11) :-
|
||||||
polynomial_to_list(P1, L),
|
maplist(term_to_list, L, L2),
|
||||||
simplify_polynomial(P1, P2),
|
maplist(sort(0, @>=), L2, L3),
|
||||||
polynomial_to_list(P2, S).
|
sort(0, @>=, L3, L4),
|
||||||
|
maplist(join_similar_parts_of_term, L4, L5),
|
||||||
|
maplist(sort(0, @=<), L5, L6),
|
||||||
|
join_similar_terms(L6, L7),
|
||||||
|
maplist(reverse, L7, L8),
|
||||||
|
maplist(term_to_list, L9, L8),
|
||||||
|
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.
|
% Converts a polynomial in a list.
|
||||||
%
|
%
|
||||||
polynomial_to_list(L - T, [T2 | LS]) :-
|
polynomial_to_list(L - T, [T2 | LS]) :-
|
||||||
term(T),
|
term(T),
|
||||||
negate_term(T, T2),
|
negate_term(T, T2),
|
||||||
polynomial_to_list(L, LS).
|
polynomial_to_list(L, LS),
|
||||||
|
!.
|
||||||
polynomial_to_list(L + T, [T | LS]) :-
|
polynomial_to_list(L + T, [T | LS]) :-
|
||||||
term(T),
|
term(T),
|
||||||
polynomial_to_list(L, LS).
|
polynomial_to_list(L, LS),
|
||||||
|
!.
|
||||||
polynomial_to_list(T, [T]) :-
|
polynomial_to_list(T, [T]) :-
|
||||||
term(T).
|
term(T),
|
||||||
|
!.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- polynomial_to_list(2, S).
|
%% ?- polynomial_to_list(2, S).
|
||||||
%@ S = [2].
|
%@ S = [2].
|
||||||
@ -489,14 +526,8 @@ 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]).
|
%% ?- 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).
|
||||||
%@ P = 2 .
|
%@ S = [-4.3, 25*x^5, -23*x^4, 3*x^3, -7*x^21, 5*x^17, 3*x, 2* ... ^ ...].
|
||||||
%% ?- polynomial_to_list(P, [x]).
|
|
||||||
%@ P = x .
|
|
||||||
%% ?- polynomial_to_list(P, [x^2, x, 2.3]).
|
|
||||||
%@ Action (h for help) ? abort
|
|
||||||
%@ % Execution Aborted
|
|
||||||
%@ P = -2.3+x+x^2 .
|
|
||||||
|
|
||||||
%% list_to_polynomial(+P:polynomial, -L:List)
|
%% list_to_polynomial(+P:polynomial, -L:List)
|
||||||
%
|
%
|
||||||
@ -551,36 +582,23 @@ negate_term(T, T2) :-
|
|||||||
%% ?- negate_term(3*x*y^2, R).
|
%% ?- negate_term(3*x*y^2, R).
|
||||||
%@ R = -3*x*y^2.
|
%@ 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
|
%% scale_polynomial(+P:polynomial,+C:constant,-S:polynomial) is det
|
||||||
%
|
%
|
||||||
% Scales a polynomial with a constant
|
% Scales a polynomial with a constant
|
||||||
%
|
%
|
||||||
scale_polynomial(P, C, S) :-
|
scale_polynomial(P, C, S) :-
|
||||||
polynomial_to_list(P, L),
|
polynomial_to_list(P, L),
|
||||||
maplist(append_two_atoms_with_star(C), L, L2),
|
maplist(term_to_list, L, L2),
|
||||||
list_to_polynomial(L2, S).
|
maplist(cons(C), L2, L3),
|
||||||
|
maplist(term_to_list, L4, L3),
|
||||||
|
simplify_polynomial_as_list(L4, L5),
|
||||||
|
list_to_polynomial(L5, S),
|
||||||
|
!.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- scale_polynomial(3*x^2, 2, S).
|
%% ?- scale_polynomial(3*x^2, 2, S).
|
||||||
%@ S = 2*3*x^2.
|
%@ S = 6*x^2.
|
||||||
|
|
||||||
|
cons(C, L, [C | L]).
|
||||||
|
|
||||||
%% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det
|
%% add_polynomial(+P1:polynomial,+P2:polynomial,-S:polynomial) is det
|
||||||
%
|
%
|
||||||
|
Reference in New Issue
Block a user