Created
July 12, 2017 06:05
-
-
Save jarble/210140099f86d5ade47772fb35b8c671 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :- initialization(main). | |
| main :- recursive_simplify_expr(((x+y+1)^2 = (x-y)^2),Output),writeln(["Output: ", Output]). | |
| %mul([add([mul([add([mul([x,x]),mul([x,y2])])]),mul([add([mul([x,y1]),mul([y1,y2])])])])])=0 | |
| simplify_expr(mul([A|Rest]),mul([1|[A|Rest]])) :- | |
| atom(A). | |
| simplify_expr(mul([A]),A). | |
| simplify_expr(A/B,A1/B1) :- | |
| simplify_expr(A,A1),simplify_expr(B,B1). | |
| %simplify_expr(add(A),add(B)) :- | |
| % replace_multiple(A,[add([A1|Rest]),add([A2|Rest])],[add([A1+A2|Rest])],B),integer(A1),integer(A2). | |
| simplify_expr(A/B,A1/B1) :- | |
| integer(A),integer(B),gcd(A,B,C),A1 is A/C, B1 is B/C. | |
| simplify_expr(mul([A|Rest])/mul([B|Rest1]),mul([A1|Rest])/mul([B1|Rest1])) :- | |
| integer(A),integer(B),gcd(A,B,C),A1 is A/C, B1 is B/C. | |
| simplify_expr(A/1,A). | |
| simplify_expr(A/(B/C),(A*C)/B). | |
| simplify_expr(mul(A),mul(B)) :- | |
| replace_multiple(A,[A1,-A2],[A3,A2],B),number(A1),A3 is -A1,atom(A2). | |
| simplify_expr(mul(A),mul(B)) :- | |
| replace_multiple(A,[add(A1),B1],[add(B2)],B),nth0(Index1,A,B1),nth0(Index2,A,add(A1)),dif(Index1,Index2),multiply_all(A1,B2,B1). | |
| simplify_expr(add([X]),X) :- | |
| integer(X). | |
| simplify_expr((A^Exp),A*(A^Exp2)) :- | |
| integer(Exp),Exp > 1, Exp2 is Exp - 1. | |
| simplify_expr(A^1,A). | |
| simplify_expr(A^0,0). | |
| simplify_expr(add(A),add(B)) :- | |
| replace_multiple(A,[mul([sin(A1),sin(A1)]),mul([cos(A1),cos(A1)])],[1],B). | |
| simplify_expr(add(A),add(B)) :- | |
| replace_multiple(A,[mul([Num,sin(A1),sin(A1)]),mul([Num,cos(A1),cos(A1)])],[1],B). | |
| simplify_expr(add(A),add(B)) :- | |
| replace_multiple(A,[add(A1)],[],B1),append(B1,A1,B). | |
| simplify_expr(add(A),add(B)) :- | |
| replace_multiple(A,[A1,mul([Num,A1])],[mul([A3,A1])],B),integer(Num),atom(A1),A3 is Num+1. | |
| simplify_expr(-add(A),(-1)*add(A)). | |
| simplify_expr(add([-add(A)]),-add(A)). | |
| simplify_expr(add([0|X]),add(X)). | |
| simplify_expr(X<Y,Y>X). | |
| simplify_expr(0=0,true). | |
| simplify_expr(X=0,false) :- | |
| number(X),dif(X,0). | |
| simplify_expr(X=0,Y=0) :- | |
| simplify_expr(X,Y). | |
| simplify_expr(X=Y,X-Y = 0) :- | |
| dif(Y,0). | |
| simplify_expr(mul(A),0) :- | |
| member(0,A). | |
| simplify_expr(-mul([A|B]),mul([-A|B])) :- | |
| integer(A). | |
| simplify_expr(-A,B) :- | |
| integer(A), B is -A. | |
| simplify_expr(A - B, A+(-B)). | |
| simplify_expr(-A,-B) :- | |
| simplify_expr(A,B). | |
| simplify_expr(mul(A),mul(B)) :- | |
| replace_multiple(A,[mul(A1)],A1,B). | |
| simplify_expr(mul([A1|[A2|Rest]]),mul([A3|Rest])) :- | |
| integer(A1),integer(A2),A3 is A1*A2. | |
| simplify_expr(add(A),add(B)) :- | |
| nth0(A1_index,A,mul([A1|Rest])),nth0(A2_index,A,mul([A2|Rest])),dif(A1_index,A2_index),replace_multiple(A,[mul([A1|Rest]),mul([A2|Rest])],[mul([A3|Rest])],B), integer(A1), integer(A2), A3 is A1 + A2. | |
| simplify_expr(add([mul(X)]),mul(X)). | |
| simplify_expr(X+Y,add([X,Y])). | |
| simplify_expr(X*Y,mul([X,Y])). | |
| simplify_expr(add([A,B]),C) :- integer(A),integer(B),C is A+B. | |
| simplify_expr(add(A),add(B)) :- | |
| simplify_all(A,B). | |
| simplify_expr(add(A),add(B)) :- | |
| msort(A,B). | |
| simplify_expr(mul(A),mul(B)) :- | |
| msort(A,B). | |
| simplify_expr(mul(A),mul(B)) :- | |
| simplify_all(A,B). | |
| simplify_all([],[]). | |
| simplify_all([A|Rest],[A1|Rest1]) :- | |
| recursive_simplify_expr(A,A1),simplify_all(Rest,Rest1). | |
| recursive_simplify_expr(A,Output) :- | |
| simplify_expr(A,B),dif(A,B),recursive_simplify_expr(B,Output);A=Output. | |
| replace_multiple(List,To_replace,Replacements,Output) :- | |
| members(To_replace,List),remove_all(List,To_replace,Output1),append(Output1,Replacements,Output). | |
| remove_all(List,[],List). | |
| remove_all(List,[To_remove|Rest],Result) :- | |
| members([To_remove|Rest],List),delete_one(To_remove,List,Result1),remove_all(Result1,Rest,Result). | |
| delete_one(_, [], []). | |
| delete_one(Term, [Term|Tail], Tail). | |
| delete_one(Term, [Head|Tail], [Head|Result]) :- | |
| delete_one(Term, Tail, Result). | |
| members([],_). | |
| members([A|A1],B) :- member(A,B),members(A1,B). | |
| multiply_all([],[],_). | |
| multiply_all([First|Rest],[First1|Rest1],B) :- | |
| First1 = B*First, multiply_all(Rest,Rest1,B). | |
| %from https://stackoverflow.com/a/22452766/975097 | |
| gcd(X, Y, Z) :- | |
| X < 0, !, | |
| gcd(-X, Y, Z). | |
| gcd(X, Y, Z) :- | |
| Y < 0, !, | |
| gcd(X, -Y, Z). | |
| gcd(X, 0, X) :- X > 0. | |
| gcd(0, Y, Y) :- Y > 0. | |
| gcd(X, Y, Z) :- | |
| X > Y, Y > 0, | |
| X1 is X - Y, | |
| gcd(Y, X1, Z). | |
| gcd(X, Y, Z) :- | |
| X =< Y, X > 0, | |
| Y1 is Y - X, | |
| gcd(X, Y1, Z). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment