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

@ -117,6 +117,8 @@ do_process_input(show(void, T)) :-
print_polynomial_tree(T),
nl.
do_process_input(store(P, T)) :-
assertz(polynomial_store(P, T)).
do_process_input(show(store(P, T))) :-
assertz(polynomial_store(P, T)),
write(P),
write(" = "),
@ -124,14 +126,18 @@ do_process_input(store(P, T)) :-
nl.
do_process_input(forget(P)) :-
retract(polynomial_store(P,_)).
do_process_input(simplify(P)) :-
polynomial_tree_to_polynomial(P, PP),
simpoly(PP, SP),
do_process_input(simplify(PT)) :-
polynomial_tree_to_polynomial(PT, P),
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),
nl.
%% do_process_input(T) :-
%% %% Empty command, assume simplfiy
%% do_process_input(simplify(T)).
print_polynomial_tree(op(Op, TL, TR)) :-
!,
@ -142,19 +148,19 @@ print_polynomial_tree(op(Op, TL, TR)) :-
print_polynomial_tree(T) :-
write(T).
polynomial_tree_to_polynomial(A,B) :-
polynomial_tree_to_atomic_polynomial(A,Pa),
name(B, Pa).
polynomial_tree_to_atomic_polynomial(load(L), P) :-
polynomial_store(L, P).
polynomial_tree_to_atomic_polynomial(op(Op, TL, TR), P) :-
polynomial_tree_to_atomic_polynomial(TL,A),
polynomial_tree_to_atomic_polynomial(TR,B),
atom_concat(A, Op, AOp),
atom_concat(AOp, B, P),
!.
polynomial_tree_to_atomic_polynomial(A,A).
polynomial_tree_to_polynomial(op(Op, TL, TR), P) :-
!,
polynomial_tree_to_polynomial(TL, PL),
polynomial_tree_to_polynomial(TR, PR),
term_to_atom(PL, TermL),
term_to_atom(PR, TermR),
atom_concat(TermL, Op, Temp),
atom_concat(Temp, TermR, PA),
term_to_atom(P, PA).
polynomial_tree_to_polynomial(T, T).
%% Tests:
%% ?- polynomial_tree_to_polynomial(op(+, 2, op(+, 2, op(*, 1, y))), S).
%@ S = 2+2+1*y.
%% nlp_number(?W:Atom, ?D:Int) is det
%
@ -420,7 +426,7 @@ parse_command(store(P, T)) -->
parse_command(simplify(T)) -->
[simplify],
parse_polynomial(T).
parse_command(op(*, TN, TP)) -->
parse_command(multiply(TN, TP)) -->
[multiply],
parse_number(TN),
[by],