> # Excerpt from a MUG posting > # > # Steve Newby wrote: > # > # In my work, I often have to solve a series of equations and then refer > # back to those solutions later in the worksheet (ie those solutions > # become inputs into other equations). The procedure works just fine. > # > # However, when the worksheet is saved and then reevaluated a few days > # later, there is no way to guarantee that Maple outputs those solutions > # in the same ORDER. I am then left with no alternative but to sift > # through through the worksheet, editing as necessary, to make sure that > # the references still work. > # > #From: Ivan Huerta > #Subject: Constant Irritant > #To: maple-list@daisy.uwaterloo.ca > #Date: Tue, 14 May 96 13:20:16 INVCL > # > #Hi: > # > #Some alternatives .. > # > #Define a lexorder comparison of expressions of the type: var = value > #according to var > # > compare_lhs:= proc(a,b) ; > lexorder( lhs(a), lhs(b) ); > end; > # > #then use instead > # > sort_solve:= proc(set_eqs,set_vars) ; > sort( [op(solve(set_eqs,set_vars))], compare_lhs); > end; > # > #The function sort_solve gives the solutions in a list sorted by variable name, > #which is always the same. > # > #Another possibility is to index solutions by variable name > #by means of a table. This solution allows to easily > #"loop" over all values of a variable var for different solution sets. > # > table_solve:= proc(set_eqs,set_vars) > local sol,i,t; > sol:= solve(set_eqs,set_vars); > for i from 1 to nops(sol) do t[lhs(sol[i])]:=rhs( sol[i]) od; > eval(t); > end; > # > #If > # > sol:=table_solve(set_eqs,set_vars); > # > #then > # > sol[var]; > # > #gives the value of var in the solution set sol > # > #Of course one needs to do some argument checking to make these routines more > #robust. > # > #Examples: > # > eq1:= xx+ y + Zz = 1; > eq2 :=xx -y + Zz = 1; > eq3 :=xx +y + 2*Zz = 0; > set_eqs := {eq1,eq2,eq3}; > set_vars := {xx,y,Zz}; > solve(set_eqs,set_vars); > # > # {y = 0, Zz = -1, xx = 2} > # > sort_solve(set_eqs,set_vars); #result is independent of run > # > # [Zz = -1, xx = 2, y = 0] > # > sol:=table_solve( set_eqs,set_vars); > # > # sol := table([ > # xx = 2 > # Zz = -1 > # y = 0 > # ]) > # > sol[y]; > # > # 0 > # > sol[xx]; > # > # 2 > # > #Hope this helps, > # > #Saludos, > # > #Ivan Huerta > #Facultad de Matematicas > #Pontificia Universidad Catolica de Chile > # > #=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=- > # > #Date: Tue, 14 May 1996 12:05:50 -0700 (PDT) > #From: Jim Gunson > #To: maple-list@daisy.uwaterloo.ca, newby@voicenet.com > #Subject: Constant Irritant > # > #I have shared your frustration. In addition I wish that solve would > #accept lists of equations and variables, instead of insisting on sets. > # > #In working with system of equations in linear algebra, I have students > #use "genmatrix", "gausselim" and "backsub". These give a solution in > #vector form so that if the variables are specified as [x,y], a solution > #[1,2] means x=1, y =2. If your equations are always linear. I commend > #this approach. > # > #An alternative is the code below, which will reorder the solutions into a > #LIST in the order of the variables. This works only for single solutions. > #If the solution may be a set, then the use of "map" will allow the > #reordering procedure to be applied to each solution. > # > #getinorder takes a solution set sols and list of variables and sort > #the solutions in the order given by that of vars. > getinorder:= proc(sols,vars) > local novars,newsol,var,sol; > novars:=nops(vars); > newsol:=op({}); > for var in vars do > for sol in sols do > if op(1,sol)=var > then newsol:=newsol,var=op(2,sol):fi; > od; > od; > RETURN([newsol]): > end: > # > #solinoder solves a system sys for a list of variables vars, and > #returns a list of solutions. > # > solinorder:=proc(sys,vars)local sols; > sols:=getinorder(solve(sys,{op(vars)}),vars); > end; > # > solinorder := proc(sys, vars) > local sols; > sols := getinorder(solve(sys, {op(vars)}), vars) > end > # > eq1:=2*x+3*y=2;eq2:=x-y=3; > solinorder({eq1,eq2},[x,y]); > # > # eq1 := 2 x + 3 y = 2 > # > # eq2 := x - y = 3 > # > # [x = 11/5, y = -4/5] > # > #----------------------------------------------------------------------------- > #Name: Jim Gunson | email: jimg@kwantlen.bc.ca > #Title: Mathematics Instructor | phone: (604) 599-2201 > #Kwantlen University College, BC, Canada | fax: (604) 599-2279