Added README, some docs and fixed a bug

This commit is contained in:
Hugo Sales 2018-11-23 16:21:16 +00:00
parent 899313cf19
commit ef57605b7a
2 changed files with 61 additions and 8 deletions

44
README.md Normal file
View File

@ -0,0 +1,44 @@
# How to run
Run
$ swipl
Inside the REPl, load the file
?- ["polimani.pl"].
> Note: Don't forget the dot at the end of the line.
The user available funtions are:
1) poly2list/2
2) simpoly_list/2
3) simpoly/2
4) scalepoly/3
5) addpoly/3
> Note: In the docs, `foo/N` means the funciton `foo` has `N` parameters.
These names are the ones requested in the assignment.
# Tests
```
:- ["polimani.pl"].
%@ true.
?- poly2list(2*x^2+3*x+5*x^17-7*x^21+3*x^3+25*x^5-4.3, S).
%@ S = [-4.3, 25*x^5, 3*x^3, -7*x^21, 5*x^17, 3*x, 2*x^2].
?- simpoly_list([x*x*x, x^3, 5*x^3, 4.2*z, 2, 42.6, 42*y, 5*z, z^7, z*y^1337, 0], L).
%@ L = [44.6, 7*x^3, 9.2*z, 42*y, y^1337*z, z^7].
?- simpoly(1+x+1+x+1+x+1+x^3+5*x^3+42*x^1337+0, S).
%@ S = 42*x^1337+6*x^3+3*x+4.
?- scalepoly(2*x^2+3*x+5*x^17-7*x^21+3*x^3-23*x^4+25*x^5-4.3, 42, S).
%@ S = 1050*x^5+210*x^17+126*x^3+126*x+84*x^2-294*x^21-966*x^4-180.6.
?- addpoly(2*x^2+3*x+5*x^17-x^4+25*x^5-4.3, 42*x^1337+0-5, S).
%@ S = 42*x^1337+25*x^5+5*x^17+3*x+2*x^2-1*x^4-9.3.
```

View File

@ -26,7 +26,6 @@
* reversing of a predicate.
*/
:- use_module(library(clpfd)).
%% :- use_module(library(clpr)).
/*******************************
@ -141,6 +140,7 @@ power(X) :-
% Returns true if N is a term, false otherwise.
%
term(N) :-
%% number(N).
(nonvar(N),
number(N));
(not(compound(N)),
@ -380,7 +380,7 @@ simplify_polynomial(0, 0) :-
simplify_polynomial(P, P2) :-
polynomial_to_list(P, L),
simplify_polynomial_as_list(L, L2),
polynomial_to_list(P2, L2),
list_to_polynomial(L2, P2),
!.
%% Tests:
%% ?- simplify_polynomial(1, X).
@ -406,7 +406,7 @@ simplify_polynomial(P, P2) :-
%% ?- simplify_polynomial(x + 1 + x, X).
%@ X = 2*x+1.
%% ?- simplify_polynomial(x + 1 + x + 1 + x + 1 + x, X).
%@ X = 4*x+3*1.
%@ X = 4*x+3.
%% join_similar_terms(+P:ListList, -P2:ListList) is det
%
@ -598,6 +598,10 @@ scale_polynomial(P, C, S) :-
%% ?- scale_polynomial(3*x^2, 2, S).
%@ S = 6*x^2.
%% cons(+C:atom, +L:List, -L2:List) is det
%
% 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
@ -607,9 +611,14 @@ cons(C, L, [C | L]).
add_polynomial(P1, P2, S) :-
polynomial_to_list(P1, L1),
polynomial_to_list(P2, L2),
append(L1, L2, LA),
join_similar_parts_of_term(LA,LJ),
list_to_polynomial(LJ, P),
simplify_polynomial(P, S).
append(L1, L2, L3),
simplify_polynomial_as_list(L3, L4),
list_to_polynomial(L4, S),
!.
%% Tests:
%
%% ?- add_polynomial(2, 2, S).
%@ S = 4.
%% ?- add_polynomial(x, x, S).
%@ S = 2*x.
%% ?- add_polynomial(2*x+5*z, 2*z+6*x, S).
%@ S = 8*x+7*z.