# procedure mysolve,  Maple V.5, packable

refpkg['mysolve'] := proc(linsys)
   # solve the set of linear equations by row echelon form
   # this function serves as a user interface to the ref function
   # it also performs the back-substitution
   local vars, Ab, B, m,n, i,j,k, sol,rhs,tmprhs;
   option  `Copyright (c) 1997 by Erich Kaltofen`;
   description `optional second argument {vars}`;

   # construct the augmented coefficient matrix
   if nargs > 1 # check if list of variables is input
     then vars := args[2]; # optional second argument
     else vars := indets(linsys); # set of unknowns
   fi;

   Ab := linalg[genmatrix](linsys, vars, flag); # matrix for system
   m := linalg[rowdim](Ab);
   n := linalg[coldim](Ab) - 1;
   if( n <> nops(vars) )
     # check for proper dimensions
     then ERROR(`bad coefficient matrix`, {});
   fi;
   userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
            print(`Augmented coefficient matrix`,Ab));

   # guard against library usage without setting up refpkg properly
   if not(assigned(refpkg['ref']))
      then ERROR(`refpkg[ref] not defined; read in file.`);
   fi;

   B := refpkg['ref'](Ab, n); # row echelon form
   userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
            print(`REF of coeff mat = `, B));

   # must perform back-substitution to construct solution
   # find first pivot element from bottom by scanning rows
   i := m;
   for i from m to 1 by -1 do
       for j from 1 to n do
           if B[i,j] <> 0 then break; # found pivot
           fi;
       od;
       if j <= n then break; # broke from inner loop
       fi;
   od;
   # check for consistency
   for k from i+1 to m do
       if B[k,n+1] <> 0 then
          userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
                   print(`Found inconsistency in row `, k));
          RETURN({}); fi;
   od;
   if i < 1
      then # augmented matrix is the zero matrix
           # return {vars[1] = vars[1],...,vars[n] = vars[n]}
           RETURN(map(proc(t) t=t end, vars));
   fi;
   # system is consistent, B[i,j] contains the last pivot element
   # initialize sol := {vars[j+1] = vars[j+1],...,vars[n]=vars[n]}
   # since vars[j+1],...,vars[n] are free variables
   sol := map(proc(t) t=t end, {op(j+1..n, vars)});
   if j+1<=n then
      userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
               print(`Free variable(s) `, op(j+1..n, vars)));
   fi;
   while(i > 0) do
      # construct right hand side for value of vars[j]
      rhs := B[i,n+1]; # constant term
      for k from j+1 to n do
          # subtract left symbolic values
          rhs := rhs - B[i,k]*vars[k];
      od;
      rhs := rhs/B[i,j]; # divide by pivot
      tmprhs := rhs; # for trace printing
      # substitute current solution into rhs
      rhs := expand(subs(sol,rhs));
      # place equation for vars[j] in front of solution
      sol := {vars[j] = rhs, op(sol)};
      userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
               print(`Solving for `, vars[j], ` = `, tmprhs,` = `, rhs));
      # find next pivot element
      i := i-1;
      if i > 0 then
         # find pivot in new row
         for k from 1 to n do
             if B[i,k] <> 0 then break; fi;
         od;
         # check if intermediate variables skipped over
         if k < j-1 then
            # add vars[k+1] = vars[k+1],...,
            #   vars[j-1] = vars[j-1] to the current solution
            #   since they are also free variables
            # print(k+1, j-1, op(k+1..j-1, vars));
            sol := {op(map(proc(t) t=t end, {op(k+1..j-1, vars)})),
                    op(sol)};
            if k+1 <= j-1 then
               userinfo(3, {'refpkg', 'mysolve'}, `NoName`,
                        print(`Free variable(s) `, op(k+1..j-1, vars)));
            fi;
         fi;
         j := k;
      fi; # i > 0
   od;
   RETURN(sol);
end:
