#factor(a_*b_) := If(isatom(a) or isnumber(a), a*factor(b)); factor(a_*b_+a_*c_):=a*(b+c); factor(a_*b_-a_*c_):=a*(b-c); factor(a_*c_+b_*c_):=c*(b+a); factor(a_*c_-b_*c_):=c*(a-b); factor(a_+a_*c_):=a*(1+c); factor(a_^2+a_*c_):=a*(a+c); factor(a_^2-b_):=(a+sqrt(b))*(a-sqrt(b)); factor(a_-b_^2):=(sqrt(a)+b)*(sqrt(a)-b); factor(a_^2-b_^2):=(a+b)*(a-b); factor(c_+x_^2) := If(isfree(c,x), (x-sqrt(-c))*(x+sqrt(-c))); factor(c_+x_+x_^2):=factor(solve(c_+x_+x_^2,x_)); factor(c_+b_*x_+x_^2):=factor(solve(c_+b_*x_+x_^2,x_)); factor(c_+b_*x_+a_*x_^2):=factor(solve(c_+b_*x_+a_*x_^2,x_)); factor(1+3*x_+3*x_^2+x_^3):=(1+x)^3; factor(-1+3*x_-3*x_^2+x_^3):=(x-1)^3; factor(1+2*a_+a_^2):=(a+1)^2; factor(1-2*a_+a_^2):=(a-1)^2; factor(2*a_*b_+a_^2+b_^2):=(a_+b_)^2; factor((-2)*a_*b_+a_^2+b_^2):=(a_-b_)^2; factor(x_^n_-y_^n_):=(x-y)*sum(x^(n-k-1)*y^k,k,0,n-1,1); factor(x_^n_+1):=if(iseven(n),(x^2+x+1)*sum(x^(n-k-1),k,0,n-1,2)); factor(x_^n_-1):=(x-1)*sum(x^k,k,0,n-1,1); factor(x_^3+1):=(x+1)*(x^2-x+1); factor(a_^3+b_^3):=(a+b)*(a^2-a*b+b^2); factor(cos(x_)*sin(x_)) := sin(2x)/2; factor(2*cos(x_)^2) := 1+cos(2x); factor(sin(a_)*cos(b_)+cos(a_)*sin(b_)):=sin(a_+b_); factor(sin(a_)*cos(b_)-cos(a_)*sin(b_)):=sin(a_-b_); factor(cos(a_)*cos(b_)- sin(a_)*sin(b_)):=cos(a_+b_); factor(cos(a_)*cos(b_)+ sin(a_)*sin(b_)):=cos(a_-b_); factor(2*sin(x_)^2) := 1-cos(2x); factor(sqrt(a_)*sqrt(b_)):=sqrt(a_*b_); factor(exp(a_)*exp(b_)):=exp(a_*b_); factor(log(a_)+log(b_)):=log(a_*b_); factor(a_^n_*b_^n_):=(a_*b_)^n; factor(x_=a_,y_=b_):=(x_-a_)*(y_-b_); factor(x_=a_,y_=b_,z_=c_):=(x_-a_)*(y_-b_)*(z_-c_); factor(x_=(a_,b_)):=(x_-a_)*(x_-b_); factor(x_=(a_,b_,c_)):=(x_-a_)*(x_-b_)*(x_-c_); factor(x_=(a_,b_,c_,d_)):=(x_-a_)*(x_-b_)*(x_-c_)*(x_-d_); factor(x_=(a_ or b_)):=(x_-a_)*(x_-b_); factor(a_ and b_):=(x-a)*(x-b); factor(a_ and b_ and c_):=(x-a)*(x-b)*(x-c); factor(x_=a_ and x_=b_)):=(x_-a_)*(x_-b_);