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],
write("See ya"),
nl,
nl,
!
;
(
@ -63,20 +63,20 @@ polyplay :-
writeln("I didn't understand what you want."),
writeln(NC)
;
(
debug_print(true),
write(LA),
nl,
write(TIn),
nl
;
process_input(TIn)
)
(
debug_print(true),
write(LA),
nl,
write(TIn),
nl
;
process_input(TIn)
)
)
;
writeln("I didn't understand what you want.")
),
polyplay
polyplay
),
!.
@ -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
%
@ -313,10 +319,10 @@ parse_polynomial_operand(T) --> parse_power(T).
parse_stored_variable(P) -->
[P],
{
atom_codes(P, L),
cons(F, R, L),
code_type(F, prolog_var_start),
maplist(code_type_swap(prolog_identifier_continue), R)
atom_codes(P, L),
cons(F, R, L),
code_type(F, prolog_var_start),
maplist(code_type_swap(prolog_identifier_continue), R)
}.
%% Tests:
%% ?- parse_stored_variable(P, ['P1'], _).
@ -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],
@ -723,7 +729,7 @@ polynomial(L - In) :-
%@ true .
%% ?- polynomial(3*x^7).
%@ true .
%% ?- polynomial(2 + 3*x + 4*x*y^3).
%% ?- polynomial(2+3*x+4*x*y^3).
%@ true .
%% ?- polynomial(2 - 3*x + 4*x*y^3).
%@ true .