Bug fix and added support for scaling a polynomial

This commit is contained in:
Hugo Sales 2018-12-19 21:04:17 +00:00
parent 05fcfcbcb2
commit c057493d86

View File

@ -53,7 +53,7 @@ polyplay :-
( (
LA == [bye], LA == [bye],
write("See ya"), write("See ya"),
nl, nl,
! !
; ;
( (
@ -63,20 +63,20 @@ polyplay :-
writeln("I didn't understand what you want."), writeln("I didn't understand what you want."),
writeln(NC) writeln(NC)
; ;
( (
debug_print(true), debug_print(true),
write(LA), write(LA),
nl, nl,
write(TIn), write(TIn),
nl nl
; ;
process_input(TIn) process_input(TIn)
) )
) )
; ;
writeln("I didn't understand what you want.") writeln("I didn't understand what you want.")
), ),
polyplay polyplay
), ),
!. !.
@ -117,6 +117,8 @@ do_process_input(show(void, T)) :-
print_polynomial_tree(T), print_polynomial_tree(T),
nl. nl.
do_process_input(store(P, T)) :- do_process_input(store(P, T)) :-
assertz(polynomial_store(P, T)).
do_process_input(show(store(P, T))) :-
assertz(polynomial_store(P, T)), assertz(polynomial_store(P, T)),
write(P), write(P),
write(" = "), write(" = "),
@ -124,14 +126,18 @@ do_process_input(store(P, T)) :-
nl. nl.
do_process_input(forget(P)) :- do_process_input(forget(P)) :-
retract(polynomial_store(P,_)). retract(polynomial_store(P,_)).
do_process_input(simplify(P)) :- do_process_input(simplify(PT)) :-
polynomial_tree_to_polynomial(P, PP), polynomial_tree_to_polynomial(PT, P),
simpoly(PP, SP), simpoly(P, SP),
write(SP),
nl.
do_process_input(multiply(TN, PT)) :-
polynomial_tree_to_polynomial(TN, N),
polynomial_tree_to_polynomial(PT, P),
scalepoly(N, P, P2),
simpoly(P2, SP),
write(SP), write(SP),
nl. nl.
%% do_process_input(T) :-
%% %% Empty command, assume simplfiy
%% do_process_input(simplify(T)).
print_polynomial_tree(op(Op, TL, TR)) :- print_polynomial_tree(op(Op, TL, TR)) :-
!, !,
@ -142,19 +148,19 @@ print_polynomial_tree(op(Op, TL, TR)) :-
print_polynomial_tree(T) :- print_polynomial_tree(T) :-
write(T). write(T).
polynomial_tree_to_polynomial(A,B) :- polynomial_tree_to_polynomial(op(Op, TL, TR), P) :-
polynomial_tree_to_atomic_polynomial(A,Pa), !,
name(B, Pa). polynomial_tree_to_polynomial(TL, PL),
polynomial_tree_to_polynomial(TR, PR),
polynomial_tree_to_atomic_polynomial(load(L), P) :- term_to_atom(PL, TermL),
polynomial_store(L, P). term_to_atom(PR, TermR),
polynomial_tree_to_atomic_polynomial(op(Op, TL, TR), P) :- atom_concat(TermL, Op, Temp),
polynomial_tree_to_atomic_polynomial(TL,A), atom_concat(Temp, TermR, PA),
polynomial_tree_to_atomic_polynomial(TR,B), term_to_atom(P, PA).
atom_concat(A, Op, AOp), polynomial_tree_to_polynomial(T, T).
atom_concat(AOp, B, P), %% Tests:
!. %% ?- polynomial_tree_to_polynomial(op(+, 2, op(+, 2, op(*, 1, y))), S).
polynomial_tree_to_atomic_polynomial(A,A). %@ S = 2+2+1*y.
%% nlp_number(?W:Atom, ?D:Int) is det %% nlp_number(?W:Atom, ?D:Int) is det
% %
@ -313,10 +319,10 @@ parse_polynomial_operand(T) --> parse_power(T).
parse_stored_variable(P) --> parse_stored_variable(P) -->
[P], [P],
{ {
atom_codes(P, L), atom_codes(P, L),
cons(F, R, L), cons(F, R, L),
code_type(F, prolog_var_start), code_type(F, prolog_var_start),
maplist(code_type_swap(prolog_identifier_continue), R) maplist(code_type_swap(prolog_identifier_continue), R)
}. }.
%% Tests: %% Tests:
%% ?- parse_stored_variable(P, ['P1'], _). %% ?- parse_stored_variable(P, ['P1'], _).
@ -420,7 +426,7 @@ parse_command(store(P, T)) -->
parse_command(simplify(T)) --> parse_command(simplify(T)) -->
[simplify], [simplify],
parse_polynomial(T). parse_polynomial(T).
parse_command(op(*, TN, TP)) --> parse_command(multiply(TN, TP)) -->
[multiply], [multiply],
parse_number(TN), parse_number(TN),
[by], [by],
@ -723,7 +729,7 @@ polynomial(L - In) :-
%@ true . %@ true .
%% ?- polynomial(3*x^7). %% ?- polynomial(3*x^7).
%@ true . %@ true .
%% ?- polynomial(2 + 3*x + 4*x*y^3). %% ?- polynomial(2+3*x+4*x*y^3).
%@ true . %@ true .
%% ?- polynomial(2 - 3*x + 4*x*y^3). %% ?- polynomial(2 - 3*x + 4*x*y^3).
%@ true . %@ true .