home *** CD-ROM | disk | FTP | other *** search
- # lsolve() solve linear equations
- # e.g. solve equations x+y-3=0 and x-y-1=0 for x and y
- # lsolve(x+y-3, x-y-1, x,y) gives [x=2,y=1]
-
- lsolve(eq1_, eq2_, x_, y_) := if(order(eq1,x)<=1 and order(eq1,
- y)<=1 and order(eq2,x)<=1 and order(eq2,y)<=1,
- block( dd:=coef(eq1,x,1)*coef(eq2,y,1)-coef(eq1,y,1)*coef(eq2,x,1),
- [ x=(coef(eq1,y,1)*coef(coef(eq2,x,0),y,0)-coef(coef(eq1,x,0),y,0)*coef(eq2,y,1))/dd,
- y=-(coef(eq1,x,1)*coef(coef(eq2,x,0),y,0)-coef(coef(eq1,x,0),y,0)*coef(eq2,x,1))/dd ],
- local(dd) ))
- lsolve(eq1_, eq2_, eq3_, x_,y_,z_) := if(order(eq1,x)<=1 and order(eq1,
- y)<=1 and order(eq1,z)<=1 and order(eq2,x)<=1 and order(eq2,
- y)<=1 and order(eq2,z)<=1 and order(eq3,x)<=1 and order(eq3,
- y)<=1 and order(eq3,z)<=1,
- block( dd:=coef(eq1,x,1)*coef(eq2,y,1)*coef(eq3,z,1)+coef(eq2,x,
- 1)*coef(eq3,y,1)*coef(eq1,z,1)+coef(eq3,x,1)*coef(eq1,y,1)*coef(eq2,z,
- 1)-coef(eq1,x,1)*coef(eq3,y,1)*coef(eq2,z,1)-coef(eq2,x,1)*coef(eq1,y,
- 1)*coef(eq3,z,1)-coef(eq3,x,1)*coef(eq2,y,1)*coef(eq1,z,1),
- const1:=coef(coef(coef(eq1,x,0),y,0),z,0),
- const2:=coef(coef(coef(eq2,x,0),y,0),z,0),
- const3:=coef(coef(coef(eq3,x,0),y,0),z,0),
- [ x=-(const1*coef(eq2,y,1)*coef(eq3,z,1)+const2*coef(eq3,
- y,1)*coef(eq1,z,1)+const3*coef(eq1,y,1)*coef(eq2,z,
- 1)-const1*coef(eq3,y,1)*coef(eq2,z,1)-const2*coef(eq1,y,
- 1)*coef(eq3,z,1)-const3*coef(eq2,y,1)*coef(eq1,z,1))/dd,
- y=-(coef(eq1,x,1)*const2*coef(eq3,z,1)+coef(eq2,x,
- 1)*const3*coef(eq1,z,1)+coef(eq3,x,1)*const1*coef(eq2,z,
- 1)-coef(eq1,x,1)*const3*coef(eq2,z,1)-coef(eq2,x,1)*const1*coef(eq3,
- z,1)-coef(eq3,x,1)*const2*coef(eq1,z,1))/dd,
- z=-(coef(eq1,x,1)*coef(eq2,y,1)*const3+coef(eq2,x,
- 1)*coef(eq3,y,1)*const1+coef(eq3,x,1)*coef(eq1,y,1)*const2-coef(eq1,
- x,1)*coef(eq3,y,1)*const2-coef(eq2,x,1)*coef(eq1,y,
- 1)*const3-coef(eq3,x,1)*coef(eq2,y,1)*const1)/dd ],
- local(dd,const1,const2,const3) ))
-